package com.ruoyi.postCheck.controller;

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

import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.constant.PostProgrammeEnum;
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.common.utils.StringUtils;
import com.ruoyi.system.domain.SysPost;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysPostService;
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.PostProgramme;
import com.ruoyi.postCheck.service.IPostProgrammeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiImplicitParam;

/**
 * 岗检方案Controller
 *
 * @author csg
 * @date 2023-12-19
 */
@Api(tags = "2.1.1-岗检方案接口")
@RestController
@RequestMapping("/postCheck/programme")
public class PostProgrammeController extends BaseController {

    @Autowired
    private IPostProgrammeService postProgrammeService;

    @Autowired
    private ISysDeptService sysDeptService;

    @Autowired
    private ISysPostService sysPostService;

    /**
     * 查询岗检方案列表
     */
    @ApiOperation("查询岗检方案列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键ID", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "name", value = "名称", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "submitDate", value = "提交日期", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "statusHr", value = "1-人力资源审批-状态：1-待审核；3-通过；4-驳回；", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "dateHr", value = "1-人力资源审批-通过日期", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "reasonHr", value = "1-人力资源审批-未通过理由", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "statusIcfa", value = "2-内控法务部审批-状态：1-待审核；3-通过；4-驳回；", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "dateIcfa", value = "2-内控法务部审批-通过日期", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "reasonIcfa", value = "2-内控法务部审批-未通过理由", dataType = "String", dataTypeClass = String.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 = "diskPath", value = "岗检方案文件地址", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "deptId", value = "提交部门ID", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "deptName", value = "提交部门名称", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "status", value = "状态：1-待审核；2-审核中；3-通过；4-驳回；", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "statusTime", value = "审核时间", dataType = "Date", dataTypeClass = Date.class),
//            @ApiImplicitParam(name = "statusUserId", value = "审核时人ID", dataType = "Long", dataTypeClass = Long.class),
//            @ApiImplicitParam(name = "statusIcfaUserId", value = "2-内控法务部审批人ID", dataType = "Long", dataTypeClass = Long.class),
//            @ApiImplicitParam(name = "statusHrUserId", value = "1-人力资源审批人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)
    })
    @PreAuthorize("@ss.hasPermi('postCheck:programme:list')")
    @GetMapping("/list")
    public TableDataInfo list(PostProgramme postProgramme) {
        startPage();
        List<PostProgramme> list = postProgrammeService.selectPostProgrammeList(postProgramme);
        return getDataTable(list);
    }

    /**
     * 导出岗检方案列表
     */
    @ApiOperation("导出岗检方案列表")
    @PreAuthorize("@ss.hasPermi('postCheck:programme:export')")
    @Log(title = "岗检方案", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PostProgramme postProgramme) {
        List<PostProgramme> list = postProgrammeService.selectPostProgrammeList(postProgramme);
        ExcelUtil<PostProgramme> util = new ExcelUtil<PostProgramme>(PostProgramme.class);
        util.exportExcel(response, list, "岗检方案数据");
    }

    /**
     * 获取岗检方案详细信息
     */
    @ApiOperation("获取岗检方案详细信息")
    @PreAuthorize("@ss.hasPermi('postCheck:programme:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(postProgrammeService.selectPostProgrammeById(id));
    }

    /**
     * 新增岗检方案
     */
    @ApiOperation("新增岗检方案")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键ID", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "name", value = "名称", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "submitDate", value = "提交日期", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "statusHr", value = "1-人力资源审批-状态：1-待审核；3-通过；4-驳回；", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "dateHr", value = "1-人力资源审批-通过日期", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "reasonHr", value = "1-人力资源审批-未通过理由", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "statusIcfa", value = "2-内控法务部审批-状态：1-待审核；3-通过；4-驳回；", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "dateIcfa", value = "2-内控法务部审批-通过日期", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "reasonIcfa", value = "2-内控法务部审批-未通过理由", dataType = "String", dataTypeClass = String.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 = "diskPath", value = "岗检方案文件地址", dataType = "String", dataTypeClass = String.class),
//            @ApiImplicitParam(name = "deptId", value = "提交部门ID", dataType = "Long", dataTypeClass = Long.class),
//            @ApiImplicitParam(name = "deptName", value = "提交部门名称", dataType = "String", dataTypeClass = String.class),
//            @ApiImplicitParam(name = "status", value = "状态：1-待审核；2-审核中；3-通过；4-驳回；", dataType = "Long", dataTypeClass = Long.class),
//            @ApiImplicitParam(name = "statusTime", value = "审核时间", dataType = "Date", dataTypeClass = Date.class),
//            @ApiImplicitParam(name = "statusUserId", value = "审核时人ID", dataType = "Long", dataTypeClass = Long.class),
//            @ApiImplicitParam(name = "statusIcfaUserId", value = "2-内控法务部审批人ID", dataType = "Long", dataTypeClass = Long.class),
//            @ApiImplicitParam(name = "statusHrUserId", value = "1-人力资源审批人ID", dataType = "Long", dataTypeClass = Long.class),
    })
    @PreAuthorize("@ss.hasPermi('postCheck:programme:add')")
    @Log(title = "岗检方案", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PostProgramme postProgramme) {
        return toAjax(postProgrammeService.insertPostProgramme(postProgramme));
    }

    /**
     * 修改岗检方案
     */
    @ApiOperation("修改岗检方案")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键ID", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "name", value = "名称", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "submitDate", value = "提交日期", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "statusHr", value = "1-人力资源审批-状态：1-待审核；3-通过；4-驳回；", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "dateHr", value = "1-人力资源审批-通过日期", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "reasonHr", value = "1-人力资源审批-未通过理由", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "statusIcfa", value = "2-内控法务部审批-状态：1-待审核；3-通过；4-驳回；", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "dateIcfa", value = "2-内控法务部审批-通过日期", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "reasonIcfa", value = "2-内控法务部审批-未通过理由", dataType = "String", dataTypeClass = String.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 = "diskPath", value = "岗检方案文件地址", dataType = "String", dataTypeClass = String.class),
//            @ApiImplicitParam(name = "deptId", value = "提交部门ID", dataType = "Long", dataTypeClass = Long.class),
//            @ApiImplicitParam(name = "deptName", value = "提交部门名称", dataType = "String", dataTypeClass = String.class),
//            @ApiImplicitParam(name = "status", value = "状态：1-待审核；2-审核中；3-通过；4-驳回；", dataType = "Long", dataTypeClass = Long.class),
//            @ApiImplicitParam(name = "statusTime", value = "审核时间", dataType = "Date", dataTypeClass = Date.class),
//            @ApiImplicitParam(name = "statusUserId", value = "审核时人ID", dataType = "Long", dataTypeClass = Long.class),
//            @ApiImplicitParam(name = "statusIcfaUserId", value = "2-内控法务部审批人ID", dataType = "Long", dataTypeClass = Long.class),
//            @ApiImplicitParam(name = "statusHrUserId", value = "1-人力资源审批人ID", dataType = "Long", dataTypeClass = Long.class),
    })

    @PreAuthorize("@ss.hasPermi('postCheck:programme:edit')")
    @Log(title = "岗检方案", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PostProgramme postProgramme) {
        return toAjax(postProgrammeService.updatePostProgramme(postProgramme));
    }

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


    @ApiOperation("审核1：岗检方案审核-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 = "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) {
        // 判断当前状态
        PostProgramme postProgramme = postProgrammeService.selectPostProgrammeById(id);
        // 审核过得不在审核

        if (postProgramme.getStatusHr() == PostProgrammeEnum.STATUS_HR_PASS.getValue()) {
            return AjaxResult.error("审核失败，请勿重复审核！");
        }
        // 判断审核的权限
        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(postProgrammeService.approveHr(id, statusHr, reasonHr));
    }


    @ApiOperation("审核2：岗检方案审核-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) {
        // 判断当前状态
        PostProgramme postProgramme = postProgrammeService.selectPostProgrammeById(id);
        // 审核过得不在审核
        if (postProgramme.getStatusHr() != PostProgrammeEnum.STATUS_PASS.getValue()
                && postProgramme.getStatus() == PostProgrammeEnum.STATUS_ING.getValue()) {
            return AjaxResult.error(HttpStatus.WARN,"审核失败，审核流程有误！！");
        }

        if (postProgramme.getStatusIcfa() == PostProgrammeEnum.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(postProgrammeService.approveIcfa(id, statusIcfa, reasonIcfa));
    }
}
