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

import java.util.ArrayList;
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.SurveyTplMain;
import org.jeecg.modules.stm.survey.service.ISurveyTplMainService;

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: 模板表
 * @Author: jeecg-boot
 * @Date:   2023-11-15
 * @Version: V1.0
 */
  
@RestController
@RequestMapping("/survey/surveyTplMain")
@Slf4j
public class SurveyTplMainController extends JeecgController<SurveyTplMain, ISurveyTplMainService> {
	@Autowired
	private ISurveyTplMainService surveyTplMainService;
	
	/**
	 * 分页列表查询
	 *
	 * @param surveyTplMain
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "模板表-分页列表查询")
	  
	@GetMapping(value = "/list")
	public Result<IPage<SurveyTplMain>> queryPageList(SurveyTplMain surveyTplMain,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<SurveyTplMain> queryWrapper = QueryGenerator.initQueryWrapper(surveyTplMain, req.getParameterMap());
		Page<SurveyTplMain> page = new Page<SurveyTplMain>(pageNo, pageSize);
		IPage<SurveyTplMain> pageList = surveyTplMainService.page(page, queryWrapper);
		return Result.OK(pageList);
	}


	   
	 @GetMapping(value = "/listTree")
	 public Result<List<SurveyTplMain>> listTree(SurveyTplMain surveyTplMain,
													   HttpServletRequest req) {
		 QueryWrapper<SurveyTplMain> queryWrapper = QueryGenerator.initQueryWrapper(surveyTplMain, req.getParameterMap());
		 List<SurveyTplMain> list = surveyTplMainService.list(queryWrapper);
		 List<SurveyTplMain>  retList = new ArrayList<>();
		 getTreeList(retList,list,null);
		 return Result.OK(retList);
	 }
	
	/**
	 *   添加
	 *
	 * @param surveyTplMain
	 * @return
	 */
	@AutoLog(value = "模板表-添加")
	  
	//@RequiresPermissions("survey:survey_tpl_main:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody SurveyTplMain surveyTplMain) {
		surveyTplMainService.save(surveyTplMain);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param surveyTplMain
	 * @return
	 */
	@AutoLog(value = "模板表-编辑")
	  
	//@RequiresPermissions("survey:survey_tpl_main:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody SurveyTplMain surveyTplMain) {
		surveyTplMainService.updateById(surveyTplMain);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "模板表-通过id删除")
	  
	@RequiresPermissions("survey:survey_tpl_main:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		surveyTplMainService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "模板表-批量删除")
	  
	@RequiresPermissions("survey:survey_tpl_main:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.surveyTplMainService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "模板表-通过id查询")
	  
	@GetMapping(value = "/queryById")
	public Result<SurveyTplMain> queryById(@RequestParam(name="id",required=true) String id) {
		SurveyTplMain surveyTplMain = surveyTplMainService.getById(id);
		if(surveyTplMain==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(surveyTplMain);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param surveyTplMain
    */
    @RequiresPermissions("survey:survey_tpl_main:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, SurveyTplMain surveyTplMain) {
        return super.exportXls(request, surveyTplMain, SurveyTplMain.class, "模板表");
    }

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

	 private void getTreeList(List<SurveyTplMain> treeList, List<SurveyTplMain> metaList, SurveyTplMain temp) {
		 for (SurveyTplMain item : metaList) {
			 Integer tempPid = item.getPid();
			 if (temp == null && tempPid==0) {
				 treeList.add(item);
				 getTreeList(treeList, metaList, item);
			 } else if (temp != null && tempPid != null && tempPid.equals(temp.getId())) {
				 if(temp.getChildren()==null){
					 temp.setChildren(new ArrayList<SurveyTplMain>());
				 }
				 temp.getChildren().add(item);
				 getTreeList(treeList, metaList, item);
			 }

		 }
	 }

}
