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

import java.util.Arrays;
import java.util.List;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.stm.survey.entity.SurveyTplItem;
import org.jeecg.modules.stm.survey.service.ISurveyTplItemService;

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.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
 
 
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

 /**
 * @Description: survey_tpl_item
 * @Author: jeecg-boot
 * @Date:   2023-11-16
 * @Version: V1.0
 */

@RestController
@RequestMapping("/survey/surveyTplItem")
@Slf4j
public class SurveyTplItemController extends JeecgController<SurveyTplItem, ISurveyTplItemService> {
	@Autowired
	private ISurveyTplItemService surveyTplItemService;
	
	/**
	 * 分页列表查询
	 *
	 * @param surveyTplItem
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "survey_tpl_item-分页列表查询")

	@GetMapping(value = "/list")
	public Result<IPage<SurveyTplItem>> queryPageList(SurveyTplItem surveyTplItem,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<SurveyTplItem> queryWrapper = QueryGenerator.initQueryWrapper(surveyTplItem, req.getParameterMap());
		Page<SurveyTplItem> page = new Page<SurveyTplItem>(pageNo, pageSize);
		IPage<SurveyTplItem> pageList = surveyTplItemService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	 @GetMapping(value = "/allList")
	 public Result<List<SurveyTplItem>> queryAllList(SurveyTplItem surveyTplItem, HttpServletRequest req) {
		 QueryWrapper<SurveyTplItem> queryWrapper = QueryGenerator.initQueryWrapper(surveyTplItem, req.getParameterMap());
		 List<SurveyTplItem> list = surveyTplItemService.list(queryWrapper);
		 return Result.OK(list);
	 }
	
	/**
	 *   添加
	 *
	 * @param surveyTplItem
	 * @return
	 */
	@AutoLog(value = "survey_tpl_item-添加")

	@RequiresPermissions("survey:survey_tpl_item:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody SurveyTplItem surveyTplItem) {
		surveyTplItemService.save(surveyTplItem);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param surveyTplItem
	 * @return
	 */
	@AutoLog(value = "survey_tpl_item-编辑")

	@RequiresPermissions("survey:survey_tpl_item:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody SurveyTplItem surveyTplItem) {
		surveyTplItemService.updateById(surveyTplItem);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "survey_tpl_item-通过id删除")

	@RequiresPermissions("survey:survey_tpl_item:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		surveyTplItemService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "survey_tpl_item-批量删除")

	@RequiresPermissions("survey:survey_tpl_item:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.surveyTplItemService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "survey_tpl_item-通过id查询")

	@GetMapping(value = "/queryById")
	public Result<SurveyTplItem> queryById(@RequestParam(name="id",required=true) String id) {
		SurveyTplItem surveyTplItem = surveyTplItemService.getById(id);
		if(surveyTplItem==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(surveyTplItem);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param surveyTplItem
    */
    @RequiresPermissions("survey:survey_tpl_item:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, SurveyTplItem surveyTplItem) {
        return super.exportXls(request, surveyTplItem, SurveyTplItem.class, "survey_tpl_item");
    }

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

}
