package org.jeecg.modules.stm.rectify.plan.controller;

import java.util.Arrays;
import java.util.Date;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.stm.rectify.assin.entity.StProblemTaskAssin;
import org.jeecg.modules.stm.rectify.assin.service.IStProblemTaskAssinService;
import org.jeecg.modules.stm.rectify.plan.entity.StRectifyPlan;
import org.jeecg.modules.stm.rectify.plan.service.IStRectifyPlanService;
import org.jeecg.modules.stm.rectify.plan.vo.StRectifyPlanVo;
import org.jeecg.modules.stm.utils.UserUtil;

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-07-21
 * @Version: V1.0
 */

@RestController
@RequestMapping("/rectify.plan/stRectifyPlan")
@Slf4j
public class StRectifyPlanController extends JeecgController<StRectifyPlan, IStRectifyPlanService> {
	@Autowired
	private IStRectifyPlanService stRectifyPlanService;
	@Autowired
	private IStProblemTaskAssinService stProblemTaskAssinService;
	/**
	 * 分页列表查询
	 *
	 * @param stRectifyPlan
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "整改计划-分页列表查询")

	@GetMapping(value = "/list")
	public Result<IPage<StRectifyPlanVo>> queryPageList(StRectifyPlanVo stRectifyPlanVoQuery,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		Page<StRectifyPlanVo> page = new Page<StRectifyPlanVo>(pageNo, pageSize);
		StRectifyPlanVo stRectifyPlanVo=new StRectifyPlanVo();
		String userCode = UserUtil.getUserCode();
		stRectifyPlanVo.setHeadCode(userCode);
		stRectifyPlanVo.setProblemItem(stRectifyPlanVoQuery.getProblemItem());
		stRectifyPlanVo.setStrategy(stRectifyPlanVoQuery.getStrategy());
		stRectifyPlanVo.setEditFlag(stRectifyPlanVoQuery.getEditFlag());
		IPage<StRectifyPlanVo> pageList = stRectifyPlanService.queryStRectifyPlanVoByPage(page, stRectifyPlanVo);
		return Result.OK(pageList);
	}

	@GetMapping(value = "/executeList")
	public Result<IPage<StRectifyPlanVo>> executeList(StRectifyPlanVo stRectifyPlanVoQuery,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		Page<StRectifyPlanVo> page = new Page<StRectifyPlanVo>(pageNo, pageSize);
		StRectifyPlanVo stRectifyPlanVo=new StRectifyPlanVo();
		stRectifyPlanVo.setEditFlag("1");
		String userCode = UserUtil.getUserCode();
		stRectifyPlanVo.setExecUser(userCode);
		stRectifyPlanVo.setApproveFlag(stRectifyPlanVoQuery.getApproveFlag());
		stRectifyPlanVo.setProblemItem(stRectifyPlanVoQuery.getProblemItem());
		stRectifyPlanVo.setStrategy(stRectifyPlanVoQuery.getStrategy());
		IPage<StRectifyPlanVo> pageList = stRectifyPlanService.queryStRectifyPlanVoByPage(page, stRectifyPlanVo);
		return Result.OK(pageList);
	}

	@GetMapping(value = "/approveList")
	public Result<IPage<StRectifyPlanVo>> approveList(StRectifyPlanVo stRectifyPlanVoQuery,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		Page<StRectifyPlanVo> page = new Page<StRectifyPlanVo>(pageNo, pageSize);
		StRectifyPlanVo stRectifyPlanVo=new StRectifyPlanVo();
		stRectifyPlanVo.setEditFlag("1");
		stRectifyPlanVo.setApproveFlag(stRectifyPlanVoQuery.getApproveFlag());
		String userCode = UserUtil.getUserCode();
		stRectifyPlanVo.setApproveUser(userCode);
		stRectifyPlanVo.setProblemItem(stRectifyPlanVoQuery.getProblemItem());
		stRectifyPlanVo.setStrategy(stRectifyPlanVoQuery.getStrategy());
		IPage<StRectifyPlanVo> pageList = stRectifyPlanService.queryStRectifyPlanVoByPage(page, stRectifyPlanVo);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param stRectifyPlan
	 * @return
	 */
	@AutoLog(value = "整改计划-添加")

	@RequiresPermissions("rectify.plan:st_rectify_plan:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody StRectifyPlan stRectifyPlan) {
		stRectifyPlanService.save(stRectifyPlan);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param stRectifyPlan
	 * @return
	 */
	@AutoLog(value = "整改计划-编辑")

	@RequiresPermissions("rectify.plan:st_rectify_plan:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody StRectifyPlan stRectifyPlan) {
		if(stRectifyPlan.getPlanStartDate().after(stRectifyPlan.getPlanEndDate())){
			return Result.error("计划结束日期不能早于计划开始日期");
		}
		StProblemTaskAssin stProblemTaskAssin=stProblemTaskAssinService.getById(stRectifyPlan.getProblemTaskId());
		if(stProblemTaskAssin.getLateFinishTime().before(stRectifyPlan.getPlanEndDate())){
			return Result.error("计划结束日期不能晚于最晚完成时间");
		}
		stRectifyPlan.setEditDate(new Date());
		stRectifyPlan.setEditFlag("1");
		stRectifyPlan.setApproveFlag("0");
		stRectifyPlanService.saveOrUpateStRectifyPlan(stRectifyPlan);
		return Result.OK("编辑成功!");
	}
	@AutoLog(value = "整改计划-编辑")

	@RequiresPermissions("rectify.plan:st_rectify_plan:edit")
	@RequestMapping(value = "/approveEdit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> approveEdit(@RequestBody StRectifyPlan stRectifyPlan) {
		stRectifyPlan.setApproveTime(new Date());
		stRectifyPlanService.saveOrUpateStRectifyApprove(stRectifyPlan);
		return Result.OK("编辑成功!");
	}
	@AutoLog(value = "整改计划-编辑")

	@RequiresPermissions("rectify.plan:st_rectify_plan:edit")
	@RequestMapping(value = "/executeEdit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> executeEdit(@RequestBody StRectifyPlan stRectifyPlan) {
		if(stRectifyPlan.getStartDate().after(stRectifyPlan.getEndDate())){
			return Result.error("实际结束日期不能早于实际开始日期");
		}
		stRectifyPlan.setExecDate(new Date());
		stRectifyPlan.setApproveFlag("1");
		stRectifyPlanService.saveOrUpateStRectifyExecute(stRectifyPlan);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "整改计划-通过id删除")

	@RequiresPermissions("rectify.plan:st_rectify_plan:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		stRectifyPlanService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "整改计划-批量删除")

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

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

    /**
    * 导出excel
    *
    * @param request
    * @param stRectifyPlan
    */
    @RequiresPermissions("rectify.plan:st_rectify_plan:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, StRectifyPlan stRectifyPlan) {
        return super.exportXls(request, stRectifyPlan, StRectifyPlan.class, "整改计划");
    }

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

}
