package org.jeecg.modules.stm.survey.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 
 
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;

import org.jeecg.common.system.vo.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.jeecg.modules.stm.survey.entity.SurveyUser;
import org.jeecg.modules.stm.survey.service.ISurveyUserService;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;

/**
 * @Description: 调查用户信息
 * @Author: jeecg-boot
 * @Date:   2023-11-14
 * @Version: V1.0
 */
  
@RestController
@RequestMapping("/survey/surveyUser")
@Slf4j
public class SurveyUserController extends JeecgController<SurveyUser, ISurveyUserService> {
	@Autowired
	private ISurveyUserService surveyUserService;
	
	/**
	 * 分页列表查询
	 *
	 * @param surveyUser
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "调查用户信息-分页列表查询")
	  
	@GetMapping(value = "/list")
	public Result<IPage<SurveyUser>> queryPageList(SurveyUser surveyUser,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {

		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		QueryWrapper<SurveyUser> queryWrapper = QueryGenerator.initQueryWrapper(surveyUser, req.getParameterMap());
		Page<SurveyUser> page = new Page<SurveyUser>(pageNo, pageSize);

		IPage<SurveyUser> pageList = surveyUserService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param surveyUser
	 * @return
	 */
	@AutoLog(value = "调查用户信息-添加")
	  
	@RequiresPermissions("survey:survey_user:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody SurveyUser surveyUser) {
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		surveyUser.setCreatedTime(new Date());
		surveyUser.setCreatedUser(sysUser.getId());
		surveyUserService.save(surveyUser);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param surveyUser
	 * @return
	 */
	@AutoLog(value = "调查用户信息-编辑")
	  
	@RequiresPermissions("survey:survey_user:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody SurveyUser surveyUser) {
		surveyUserService.updateById(surveyUser);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "调查用户信息-通过id删除")
	  
	@RequiresPermissions("survey:survey_user:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		surveyUserService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "调查用户信息-批量删除")
	  
	@RequiresPermissions("survey:survey_user:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.surveyUserService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "调查用户信息-通过id查询")
	  
	@GetMapping(value = "/queryById")
	public Result<SurveyUser> queryById(@RequestParam(name="id",required=true) String id) {
		SurveyUser surveyUser = surveyUserService.getById(id);
		if(surveyUser==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(surveyUser);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param surveyUser
    */
    @RequiresPermissions("survey:survey_user:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, SurveyUser surveyUser) {
        return super.exportXls(request, surveyUser, SurveyUser.class, "调查用户信息");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequiresPermissions("survey:survey_user:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, SurveyUser.class);
    }

}
