package org.jeecg.modules.stm.project.exec.controller;

import java.util.Arrays;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.stm.project.execpro.entity.StProjectExecPro;
import org.jeecg.modules.stm.project.execpro.service.IStProjectExecProService;
import org.jeecg.modules.stm.project.execrisk.entity.StProjectExecRisk;
import org.jeecg.modules.stm.project.execrisk.service.IStProjectExecRiskService;
import org.jeecg.modules.stm.project.exec.entity.StProjectExec;
import org.jeecg.modules.stm.project.exec.service.IStProjectExecService;
import org.jeecg.modules.stm.project.exec.vo.StProjectExecExVo;
import org.jeecg.modules.stm.project.exec.vo.StProjectExecVo;
import org.jeecg.modules.stm.utils.UserUtil;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

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-08-17
 * @Version: V1.0
 */
  
@RestController
@RequestMapping("/project.exec/stProjectExec")
public class StProjectExecController extends JeecgController<StProjectExec, IStProjectExecService> {
	@Autowired
	private IStProjectExecService stProjectExecService;
	@Autowired
	private IStProjectExecProService stProjectExecProService;
	@Autowired
	private IStProjectExecRiskService stProjectExecRiskService;

	
	/**
	 * 分页列表查询
	 * 
	 * @param stProjectExec
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	// @AutoLog(value = "项目实施-分页列表查询")
	  
	@GetMapping(value = "/list")
	public Result<IPage<StProjectExecVo>> queryPageList(StProjectExecVo stProjectExecVo,
			@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
			@RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
		// QueryWrapper<StProjectExec> queryWrapper =
		// QueryGenerator.initQueryWrapper(stProjectExecVo,
		// req.getParameterMap());
		Page<StProjectExecVo> page = new Page<StProjectExecVo>(pageNo, pageSize);
		IPage<StProjectExecVo> pageList = stProjectExecService.queryStProjectExecVoByPage(page, stProjectExecVo);
		return Result.OK(pageList);
	}
	


	// @AutoLog(value = "项目实施-分页列表查询")
	  
	@GetMapping(value = "/stProjectExecExVoList")
	public Result<IPage<StProjectExecExVo>> queryPageList(StProjectExecExVo stProjectExecExVo,
			@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
			@RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
		// QueryWrapper<StProjectExec> queryWrapper =
		// QueryGenerator.initQueryWrapper(stProjectExecVo,
		// req.getParameterMap());

		String userCode = UserUtil.getUserCode();
		stProjectExecExVo.setConfirmUser(userCode);
		Page<StProjectExecExVo> page = new Page<StProjectExecExVo>(pageNo, pageSize);
		IPage<StProjectExecExVo> pageList = stProjectExecService.queryStProjectExecExVoByPage(page, stProjectExecExVo);
		return Result.OK(pageList);
	}
	// @AutoLog(value = "项目实施-分页列表查询")
	  
	@GetMapping(value = "/queryProPageList")
	public Result<IPage<StProjectExecExVo>> queryProPageList(StProjectExecExVo stProjectExecExVo,
			@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
			@RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
		// QueryWrapper<StProjectExec> queryWrapper =
		// QueryGenerator.initQueryWrapper(stProjectExecVo,
		// req.getParameterMap());
		Page<StProjectExecExVo> page = new Page<StProjectExecExVo>(pageNo, pageSize);
		IPage<StProjectExecExVo> pageList = stProjectExecService.queryStProjectExecPro(page, stProjectExecExVo);
		return Result.OK(pageList);
	}

	/**
	 * 添加
	 *
	 * @param stProjectExec
	 * @return
	 */
	@AutoLog(value = "项目实施-添加")
	  
	@RequiresPermissions("project.exec:st_project_exec:add")
	@PostMapping(value = "/addPro")
	public Result<String> addPro(@RequestBody StProjectExecExVo stProjectExecExVo) {
		String userCode = UserUtil.getUserCode();
		StProjectExecPro stProjectExecPro = new StProjectExecPro();
		stProjectExecPro.setProjectExecId(stProjectExecExVo.getId());
		stProjectExecPro.setProblemTitle(stProjectExecExVo.getProblemItem());
		stProjectExecPro.setProblemContent(stProjectExecExVo.getProblemExample());
		stProjectExecPro.setCreatedUser(userCode);
		stProjectExecPro.setUpdatedUser(userCode);
		stProjectExecProService.save(stProjectExecPro);
		return Result.OK("添加成功！");
	}
	/**
	 * 添加
	 *
	 * @param stProjectExec
	 * @return
	 */
	@AutoLog(value = "项目实施-添加")
	  
	@RequiresPermissions("project.exec:st_project_exec:add")
	@PostMapping(value = "/updatePro")
	public Result<String> updatePro(@RequestBody StProjectExecExVo stProjectExecExVo) {
		String userCode = UserUtil.getUserCode();
		StProjectExecPro stProjectExecPro = new StProjectExecPro();
		stProjectExecPro.setId(stProjectExecExVo.getId());
		stProjectExecPro.setSeverity(stProjectExecExVo.getSeverity());
		stProjectExecPro.setAdviceLevel(stProjectExecExVo.getAdviceLevel());
		stProjectExecPro.setAdviceAdvice(stProjectExecExVo.getAdviceAdvice());
		stProjectExecPro.setUpdatedUser(userCode);
		stProjectExecProService.updateById(stProjectExecPro);
		return Result.OK("添加成功！");
	}
	

	/**
	 * 添加
	 *
	 * @param stProjectExec
	 * @return
	 */
	@AutoLog(value = "项目实施-添加")
	  
	@RequiresPermissions("project.exec:st_project_exec:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody StProjectExecExVo stProjectExecExVo) {
		// stProjectExecService.save(stProjectExec);
		return Result.OK("添加成功！");
	}

	/**
	 * 添加
	 *
	 * @param stProjectExec
	 * @return
	 */
	@AutoLog(value = "项目实施-添加")
	  
	@RequiresPermissions("project.exec:st_project_exec:add")
	@PostMapping(value = "/addRisk")
	public Result<String> addRisk(@RequestBody StProjectExecExVo stProjectExecExVo) {
		String userCode = UserUtil.getUserCode();
		StProjectExecRisk stProjectExecRisk = new StProjectExecRisk();
		stProjectExecRisk.setProjectExecId(stProjectExecExVo.getId());
		stProjectExecRisk.setRiskTitle(stProjectExecExVo.getEventClassifyName());
		stProjectExecRisk.setRiskContent(stProjectExecExVo.getScenarioName());
		stProjectExecRisk.setCreatedUser(userCode);
		stProjectExecRisk.setUpdatedUser(userCode);
		stProjectExecRiskService.save(stProjectExecRisk);
		return Result.OK("添加成功！");
	}

	/**
	 * 编辑
	 *
	 * @param stProjectExec
	 * @return
	 */
	@AutoLog(value = "项目实施-编辑")
	  
	@RequiresPermissions("project.exec:st_project_exec:edit")
	@RequestMapping(value = "/edit", method = { RequestMethod.PUT, RequestMethod.POST })
	public Result<String> edit(@RequestBody StProjectExecVo stProjectExecVo) {
		if (stProjectExecVo.getExecLevel() == null || stProjectExecVo.getExecLevel().equals("0000")) {
			stProjectExecVo.setExecLevel("0001");
		} else if (stProjectExecVo.getExecLevel().equals("0001")) {
			stProjectExecVo.setExecLevel("0002");
		} else if (stProjectExecVo.getExecLevel().equals("0002")) {
			stProjectExecVo.setExecLevel("0003");
		} else if (stProjectExecVo.getExecLevel().equals("0003")) {
			stProjectExecVo.setExecLevel("0004");
		} else if (stProjectExecVo.getExecLevel().equals("0004")) {
			stProjectExecVo.setExecLevel("0005");
		} else if (stProjectExecVo.getExecLevel().equals("0005")) {
			stProjectExecVo.setExecLevel("0000");
		}
		stProjectExecService.submitExec(stProjectExecVo);
		return Result.OK("编辑成功!");
	}

	/**
	 * 更新填报
	 *
	 * @param stProjectExec
	 * @return
	 */
	@AutoLog(value = "项目实施-编辑")
	  
	@RequiresPermissions("project.exec:st_project_exec:edit")
	@RequestMapping(value = "/editTb", method = { RequestMethod.PUT, RequestMethod.POST })
	public Result<String> editTb(@RequestBody StProjectExec stProjectExec) {
		String userCode = UserUtil.getUserCode();
		stProjectExec.setConfirmStatus("1");
		if (stProjectExec.getId() == null) {
			stProjectExec.setUpdatedUser(userCode);
			stProjectExec.setUpdatedUser(userCode);
			stProjectExecService.save(stProjectExec);
		} else {
			stProjectExec.setUpdatedUser(userCode);
			stProjectExecService.updateById(stProjectExec);
		}
		return Result.OK("编辑成功!");
	}
	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "项目实施-批量分配")
	  
	@RequiresPermissions("project.exec:st_project_exec:edit")
	@DeleteMapping(value = "/confirmBatch")
	public Result<String> confirmBatch(@RequestParam(name = "ids", required = true) String ids,@RequestBody StProjectExec stProjectExec) {
		String[] idList=ids.split(",");
		for(String idstr:idList){
			if(idstr==null||idstr.trim().equals("")){
				continue;
			}			
			Integer id=Integer.parseInt(idstr);
			stProjectExec.setConfirmStatus("1");
			stProjectExec.setId(id);			
			String userCode = UserUtil.getUserCode();
			if (stProjectExec.getId() == null) {
				stProjectExec.setUpdatedUser(userCode);
				stProjectExec.setUpdatedUser(userCode);

				stProjectExecService.save(stProjectExec);
			} else {
				stProjectExec.setUpdatedUser(userCode);
				stProjectExecService.updateById(stProjectExec);
			}
		}
		return Result.OK("批量分配成功!");
	}

	
	
	/**
	 * 更新填报
	 *
	 * @param stProjectExec
	 * @return
	 */
	@AutoLog(value = "项目实施-编辑")
	  
	@RequiresPermissions("project.exec:st_project_exec:edit")
	@RequestMapping(value = "/editTemp", method = { RequestMethod.PUT, RequestMethod.POST })
	public Result<String> editTemp(@RequestBody StProjectExec stProjectExec) {
		if (stProjectExec.getId() == null) {
			stProjectExecService.save(stProjectExec);
		} else {
			stProjectExecService.updateById(stProjectExec);
		}
		return Result.OK("编辑成功!");
	}

	/**
	 * 更新填报
	 *
	 * @param stProjectExec
	 * @return
	 */
	@AutoLog(value = "项目实施-编辑")
	  
	@RequiresPermissions("project.exec:st_project_exec:edit")
	@RequestMapping(value = "/editCheck", method = { RequestMethod.PUT, RequestMethod.POST })
	public Result<String> editCheck(@RequestBody StProjectExecExVo stProjectExecExVo) {
		String userCode = UserUtil.getUserCode();
		StProjectExec stProjectExec = new StProjectExec();
		stProjectExec.setId(stProjectExecExVo.getId());
		stProjectExec.setConfirmStatus("1");
		stProjectExec.setExecContent(stProjectExecExVo.getExecContent());
		if (stProjectExecExVo.getStProjectExecPros() == null
				|| stProjectExecExVo.getStProjectExecPros().size() == 0) {
			return Result.error("问题项不能为空!");

		}
		if (stProjectExecExVo.getStProjectExecRisks() == null
				|| stProjectExecExVo.getStProjectExecRisks().size() == 0) {
			return Result.error("风险事件为空!");

		}
		if (stProjectExec.getId() == null) {
			stProjectExec.setCreatedUser(userCode);
			stProjectExec.setUpdatedUser(userCode);
			stProjectExecService.save(stProjectExec);
		} else {
			stProjectExec.setUpdatedUser(userCode);
			stProjectExecService.updateById(stProjectExec);
		}
		for (StProjectExecPro stProjectExecPro : stProjectExecExVo.getStProjectExecPros()) {
			if (stProjectExecPro.getProblemTitle() == null 
					|| stProjectExecPro.getProblemTitle().trim().equals("")) {
				return Result.error("问题项不能为空!");
			}
			if (stProjectExecPro.getProblemContent() == null 
					|| stProjectExecPro.getProblemContent().trim().equals("")) {
				return Result.error("问题项不能为空!");
			}
			if (stProjectExecPro.getId() != null) {
				stProjectExecPro.setCreatedUser(userCode);
				stProjectExecPro.setUpdatedUser(userCode);
				stProjectExecProService.updateById(stProjectExecPro);
			} else {
				stProjectExecPro.setUpdatedUser(userCode);
				stProjectExecProService.save(stProjectExecPro);
			}
		}
		for (StProjectExecRisk stProjectExecRisk : stProjectExecExVo.getStProjectExecRisks()) {
			if (stProjectExecRisk.getRiskTitle() == null 
					|| stProjectExecRisk.getRiskTitle().trim().equals("")) {
				return Result.error("风险事件不能为空!");
			}
			if (stProjectExecRisk.getRiskContent()== null 
					|| stProjectExecRisk.getRiskContent().trim().equals("")) {
				return Result.error("风险事件不能为空!");
			}
			if (stProjectExecRisk.getId() != null) {
				stProjectExecRisk.setCreatedUser(userCode);
				stProjectExecRisk.setUpdatedUser(userCode);
				stProjectExecRiskService.updateById(stProjectExecRisk);
			} else {
				stProjectExecRisk.setUpdatedUser(userCode);
				stProjectExecRiskService.save(stProjectExecRisk);
			}
		}
		return Result.OK("编辑成功!");
	}

	/**
	 * 更新填报
	 *
	 * @param stProjectExec
	 * @return
	 */
	@AutoLog(value = "项目实施-编辑")
	  
	@RequiresPermissions("project.exec:st_project_exec:edit")
	@RequestMapping(value = "/editDamage", method = { RequestMethod.PUT, RequestMethod.POST })
	public Result<String> editDamage(@RequestBody StProjectExecExVo stProjectExecExVo) {
		String userCode = UserUtil.getUserCode();
		if ("1".equals(stProjectExecExVo.getConfirmStatus())) {
			StProjectExec stProjectExec = new StProjectExec();
			stProjectExec.setId(stProjectExecExVo.getId());
			stProjectExec.setExecContent(stProjectExecExVo.getExecContent());
			stProjectExec.setDamageLevel(stProjectExecExVo.getDamageLevel());
			stProjectExec.setDamageScore(stProjectExecExVo.getDamageScore());
			stProjectExec.setTrueStatus(stProjectExecExVo.getTrueStatus());
			stProjectExec.setExecContentDes(stProjectExecExVo.getExecContentDes());
			stProjectExec.setFileUploadPathDes(stProjectExecExVo.getFileUploadPathDes());
			if (stProjectExecExVo.getStProjectExecPros() == null
					|| stProjectExecExVo.getStProjectExecPros().size() == 0) {
				return Result.error("问题项不能为空!");

			}
			if (stProjectExecExVo.getStProjectExecRisks() == null
					|| stProjectExecExVo.getStProjectExecRisks().size() == 0) {
				return Result.error("风险事件为空!");

			}
			if (stProjectExec.getId() == null) {
				stProjectExec.setCreatedUser(userCode);
				stProjectExec.setUpdatedUser(userCode);
				stProjectExecService.save(stProjectExec);
			} else {
				stProjectExec.setUpdatedUser(userCode);
				stProjectExecService.updateById(stProjectExec);
			}
			for (StProjectExecPro stProjectExecPro : stProjectExecExVo.getStProjectExecPros()) {
				if (stProjectExecPro.getProblemTitle() == null 
						|| stProjectExecPro.getProblemTitle().trim().equals("")) {
					return Result.error("问题项不能为空!");
				}
				if (stProjectExecPro.getProblemContent() == null 
						|| stProjectExecPro.getProblemContent().trim().equals("")) {
					return Result.error("问题项不能为空!");
				}
				if (stProjectExecPro.getId() != null) {
					stProjectExecPro.setCreatedUser(userCode);
					stProjectExecPro.setUpdatedUser(userCode);
					stProjectExecProService.updateById(stProjectExecPro);
				} else {
					stProjectExecPro.setUpdatedUser(userCode);
					stProjectExecProService.save(stProjectExecPro);
				}
			}
			for (StProjectExecRisk stProjectExecRisk : stProjectExecExVo.getStProjectExecRisks()) {
				if (stProjectExecRisk.getRiskTitle() == null 
						|| stProjectExecRisk.getRiskTitle().trim().equals("")) {
					return Result.error("风险事件不能为空!");
				}
				if (stProjectExecRisk.getRiskContent()== null 
						|| stProjectExecRisk.getRiskContent().trim().equals("")) {
					return Result.error("风险事件不能为空!");
				}
				if (stProjectExecRisk.getId() != null) {
					stProjectExecRisk.setCreatedUser(userCode);
					stProjectExecRisk.setUpdatedUser(userCode);
					stProjectExecRiskService.updateById(stProjectExecRisk);
				} else {
					stProjectExecRisk.setUpdatedUser(userCode);
					stProjectExecRiskService.save(stProjectExecRisk);
				}
			}
		} else {
			StProjectExec stProjectExec = new StProjectExec();
			stProjectExec.setId(stProjectExecExVo.getId());
			stProjectExec.setConfirmStatus("1");
			stProjectExec.setConfirmUser(stProjectExecExVo.getConfirmUser());
			stProjectExec.setConfirmDepCode(stProjectExecExVo.getConfirmDepCode());
			if (stProjectExec.getId() == null) {
				stProjectExecService.save(stProjectExec);
			} else {
				stProjectExecService.updateById(stProjectExec);
			}
		}

		return Result.OK("编辑成功!");
	}

	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "项目实施-通过id删除")
	  
	@RequiresPermissions("project.exec:st_project_exec:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name = "id", required = true) Integer id) {
		stProjectExecService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "项目实施-通过id删除")
	  
	@RequiresPermissions("project.exec:st_project_exec:delete")
	@DeleteMapping(value = "/deletePro")
	public Result<String> deletePro(@RequestParam(name = "id", required = true) Integer id) {
		stProjectExecProService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "项目实施-通过id删除")
	  
	@RequiresPermissions("project.exec:st_project_exec:delete")
	@DeleteMapping(value = "/deleteRisk")
	public Result<String> deleteRisk(@RequestParam(name = "id", required = true) Integer id) {
		stProjectExecRiskService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "项目实施-批量删除")
	  
	@RequiresPermissions("project.exec:st_project_exec:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
		this.stProjectExecService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	// @AutoLog(value = "项目实施-通过id查询")
	  
	@GetMapping(value = "/queryById")
	public Result<StProjectExec> queryById(@RequestParam(name = "id", required = true) String id) {
		StProjectExec stProjectExec = stProjectExecService.getById(id);
		if (stProjectExec == null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(stProjectExec);
	}

	/**
	 * 导出excel
	 *
	 * @param request
	 * @param stProjectExec
	 */
	@RequiresPermissions("project.exec:st_project_exec:exportXls")
	@RequestMapping(value = "/exportXls")
	public ModelAndView exportXls(HttpServletRequest request, StProjectExecExVo stProjectExecExVo) {
		return stProjectExecService.exportInterviewXls( request,stProjectExecExVo);
	}

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

}
