package com.ruoyi.postCheck.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.annotation.TableField;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.constant.PostPlanEnum;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.enums.DeptCode;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.postCheck.domain.PostPlanCheckUser;
import com.ruoyi.postCheck.domain.PostPlanInfo;
import com.ruoyi.postCheck.domain.PostProgramme;
import com.ruoyi.system.domain.SysPost;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysPostService;
import io.swagger.annotations.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.postCheck.domain.PostPlan;
import com.ruoyi.postCheck.service.IPostPlanService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 岗检计划Controller
 *
 * @author csg
 * @date 2023-12-19
 */
@Api(tags = "2.3.1-岗检计划接口")
@RestController
@RequestMapping("/postCheck/postPlan")
public class PostPlanController extends BaseController {

    @Autowired
    private IPostPlanService postPlanService;

    @Autowired
    private ISysDeptService sysDeptService;

    @Autowired
    private ISysPostService sysPostService;

    /**
     * 查询岗检计划列表
     */
    @ApiOperation("查询岗检计划列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "name", value = "name", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "content", value = "content", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "startTime", value = "start_time", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "endTime", value = "end_time", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "status", value = "status", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "delFlag", value = "del_flag", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "createUserId", value = "create_user_id", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "checkWorkgroupId", value = "check_workgroup_id", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "checkGroupId", value = "check_group_id", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "statusIcfa", value = "1-内控法务部审批-状态：1-待审核；3-通过；4-驳回；", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "dateIcfa", value = "1-内控法务部审批-通过日期", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "reasonIcfa", value = "1-内控法务部审批-未通过理由", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "statusHr", value = "2-人力资源审批-状态：1-待审核；3-通过；4-驳回；", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "dateHr", value = "2-人力资源审批-通过日期", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "reasonHr", value = "2-人力资源审批-未通过理由", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "statusTime", value = "审核时间", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "statusUserId", value = "审核时人ID", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "statusIcfaUserId", value = "1-内控法务部审批人ID", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "statusHrUserId", value = "2-人力资源审批人ID", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "pageNum", value = "页码从1开始", dataType = "Integer", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", dataType = "Integer", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "orderBy", value = "排序字段", dataType = "String", dataTypeClass = String.class),

            @ApiImplicitParam(name = "checkGroupName", value = "检查工作组名称", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "checkTeamName", value = "检查小组名称", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "checkedUserNames", value = "检查人员中文名称逗号拼接", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "checkedDeptNames", value = "被检查部门名称", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "checkedPostNames", value = "被检查岗位名称（多个拼接）", dataType = "String", dataTypeClass = String.class),



    })
    @PreAuthorize("@ss.hasPermi('postCheck:postPlan:list')")
    @GetMapping("/list")
    public TableDataInfo list(PostPlan postPlan) {
        startPage();
        List<PostPlan> list = postPlanService.selectPostPlanList(postPlan);
        return getDataTable(list);
    }

    /**
     * 导出岗检计划列表
     */
    @ApiOperation("导出岗检计划列表")
    @PreAuthorize("@ss.hasPermi('postCheck:postPlan:export')")
    @Log(title = "岗检计划", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PostPlan postPlan) {
        List<PostPlan> list = postPlanService.selectPostPlanList(postPlan);
        ExcelUtil<PostPlan> util = new ExcelUtil<PostPlan>(PostPlan.class);
        util.exportExcel(response, list, "岗检计划数据");
    }

    /**
     * 获取岗检计划详细信息
     */
    @ApiOperation("获取岗检计划详细信息")
    @PreAuthorize("@ss.hasPermi('postCheck:postPlan:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(postPlanService.selectPostPlanById(id));
    }

    /**
     * 新增岗检计划
     * TO-郭浩：检查人员未设置，将检查人员设置到数组中-> "checkUserChildren" : [{"checkedUserId" : 1, "type" : 1}, {"checkedUserId" : 2, "type" : 2}]
     * checkedUserId：用户ID
     * type：1-组长、2-组员
     */
    @ApiOperation("新增岗检计划")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "name", value = "name", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "content", value = "content", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "startTime", value = "start_time", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "endTime", value = "end_time", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "status", value = "status", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "delFlag", value = "del_flag", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "createUserId", value = "create_user_id", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "checkWorkgroupId", value = "check_workgroup_id", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "checkGroupId", value = "check_group_id", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "statusIcfa", value = "1-内控法务部审批-状态：1-待审核；3-通过；4-驳回；", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "dateIcfa", value = "1-内控法务部审批-通过日期", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "reasonIcfa", value = "1-内控法务部审批-未通过理由", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "statusHr", value = "2-人力资源审批-状态：1-待审核；3-通过；4-驳回；", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "dateHr", value = "2-人力资源审批-通过日期", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "reasonHr", value = "2-人力资源审批-未通过理由", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "statusTime", value = "审核时间", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "statusUserId", value = "审核时人ID", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "statusIcfaUserId", value = "1-内控法务部审批人ID", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "statusHrUserId", value = "2-人力资源审批人ID", dataType = "Long", dataTypeClass = Long.class),

            @ApiImplicitParam(name = "checkUserChildren", value = "检查人员数组", dataType = "List", dataTypeClass = PostPlanCheckUser.class),
            @ApiImplicitParam(name = "children", value = "检查小组数组", dataType = "List", dataTypeClass = PostPlanInfo.class),
    })
    @PreAuthorize("@ss.hasPermi('postCheck:postPlan:add')")
    @Log(title = "岗检计划", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PostPlan postPlan) {
        return toAjax(postPlanService.insertPostPlan(postPlan));
    }

    /**
     * 修改岗检计划
     */
    @ApiOperation("修改岗检计划")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "name", value = "name", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "content", value = "content", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "startTime", value = "start_time", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "endTime", value = "end_time", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "status", value = "status", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "delFlag", value = "del_flag", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "createUserId", value = "create_user_id", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "checkWorkgroupId", value = "check_workgroup_id", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "checkGroupId", value = "check_group_id", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "statusIcfa", value = "1-内控法务部审批-状态：1-待审核；3-通过；4-驳回；", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "dateIcfa", value = "1-内控法务部审批-通过日期", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "reasonIcfa", value = "1-内控法务部审批-未通过理由", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "statusHr", value = "2-人力资源审批-状态：1-待审核；3-通过；4-驳回；", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "dateHr", value = "2-人力资源审批-通过日期", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "reasonHr", value = "2-人力资源审批-未通过理由", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "statusTime", value = "审核时间", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "statusUserId", value = "审核时人ID", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "statusIcfaUserId", value = "1-内控法务部审批人ID", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "statusHrUserId", value = "2-人力资源审批人ID", dataType = "Long", dataTypeClass = Long.class),
    })
    @PreAuthorize("@ss.hasPermi('postCheck:postPlan:edit')")
    @Log(title = "岗检计划", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PostPlan postPlan) {
        return toAjax(postPlanService.updatePostPlan(postPlan));
    }

    /**
     * 删除岗检计划
     */
    @ApiOperation("删除岗检计划")
    @ApiImplicitParam(name = "ids", value = "Long类型的主键数组", dataType = "Long[]", dataTypeClass = Long[].class)
    @PreAuthorize("@ss.hasPermi('postCheck:postPlan:remove')")
    @Log(title = "岗检计划", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(postPlanService.deletePostPlanByIds(ids));
    }


    @ApiOperation("审核1：岗检计划审核-Icfa")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键ID", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "statusIcfa", value = "2-内控法务部审批-状态：1-待审核；3-通过；4-驳回；", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "reasonIcfa", value = "2-内控法务部审批-未通过理由", dataType = "String", dataTypeClass = String.class),
    })
    @Log(title = "岗检计划审核-Icfa", businessType = BusinessType.UPDATE)
    @PutMapping("/approveIcfa")
    public AjaxResult approveIcfa(Long id, Integer statusIcfa, String reasonIcfa) {
        // 判断当前状态
        PostPlan postPlan = postPlanService.selectPostPlanById(id);
        // 审核过得不在审核
        if (postPlan.getStatusIcfa() == PostPlanEnum.STATUS_ICFA_PASS.getValue()) {
            return AjaxResult.error("审核失败，请勿重复审核！");
        }
        // 判断审核的权限
        LoginUser loginUser = SecurityUtils.getLoginUser();
        // 当前登录用户的部门id 查询岗位id是否在内
        SysDept sysDept = sysDeptService.selectDeptById(loginUser.getDeptId());
        if (!sysDept.getDeptCode().equals(DeptCode.DEPT_ICFA.getCode())) {
            return AjaxResult.error(HttpStatus.FORBIDDEN, "没有权限，请联系管理员授权");
        }
        // 判断岗位是否在权限范围内
        SysPost sysPost = sysPostService.selectPostByDeptId(loginUser.getDeptId());
        if (sysPost.getPostLevel() != 1) {
            return AjaxResult.error(HttpStatus.FORBIDDEN, "没有权限，请联系管理员授权");
        }

        return toAjax(postPlanService.approveIcfa(id, statusIcfa, reasonIcfa));
    }


    @ApiOperation("审核2：岗检计划审核-HR")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键ID", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "statusHr", value = "1-人力资源审批-状态：1-待审核；3-通过；4-驳回；", dataType = "Long", dataTypeClass = Long.class),
//            @ApiImplicitParam(name = "dateHr", value = "date_hr", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "reasonHr", value = "1-人力资源审批-未通过理由", dataType = "String", dataTypeClass = String.class),

    })
    @Log(title = "岗检计划审核-HR", businessType = BusinessType.UPDATE)
    @PutMapping("/approveHr")
    public AjaxResult approveHr(Long id, Integer statusHr, String reasonHr) {
        // 判断当前状态
        PostPlan postPlan = postPlanService.selectPostPlanById(id);
        // 审核过得不在审核
        if (postPlan.getStatusHr() == PostPlanEnum.STATUS_HR_PASS.getValue()) {
            return AjaxResult.error( "审核失败，请勿重复审核！");
        }
        // 判断顺序
        if (postPlan.getStatusIcfa() != PostPlanEnum.STATUS_ICFA_PASS.getValue()
                && postPlan.getStatus() != PostPlanEnum.STATUS_ING.getValue()) {
            return AjaxResult.error(HttpStatus.WARN,"审核失败，审核流程有误！！");
        }
        // 判断审核的权限
        LoginUser loginUser = SecurityUtils.getLoginUser();
        // 当前登录用户的部门id 查询岗位id是否在内
        SysDept sysDept = sysDeptService.selectDeptById(loginUser.getDeptId());
        if (!sysDept.getDeptCode().equals(DeptCode.DEPT_HR.getCode())) {
            return AjaxResult.error(HttpStatus.FORBIDDEN, "没有权限，请联系管理员授权");
        }

        // 判断岗位是否在权限范围内
        SysPost sysPost = sysPostService.selectPostByDeptId(loginUser.getDeptId());
        if (sysPost.getPostLevel() != 1) {
            return AjaxResult.error(HttpStatus.FORBIDDEN, "没有权限，请联系管理员授权");
        }

        return toAjax(postPlanService.approveHr(id, statusHr, reasonHr));
    }


}
