package com.ruoyi.postCheck.controller;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.constant.HttpStatus;
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.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.PostAssessmentRules;
import com.ruoyi.postCheck.service.IPostAssessmentRulesService;
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-28
 */
@Api(tags = "4.3.1-考核规则接口")
@RestController
@RequestMapping("/postCheck/postAssessmentRules")
public class PostAssessmentRulesController extends BaseController
{
    @Autowired
    private IPostAssessmentRulesService postAssessmentRulesService;

    @Autowired
    private ISysDeptService sysDeptService;

    @Autowired
    private ISysPostService sysPostService;


    /**
     * 查询考核规则列表
     */
    @ApiOperation("查询考核规则列表")
    @ApiImplicitParams({
                    @ApiImplicitParam(name = "id", value = "主键ID", dataType = "Long", dataTypeClass = Long.class),
                    @ApiImplicitParam(name = "type", value = "评分分类/项目/专业：1-岗检考核规则；2-职能部门互评表；；3-生产部门日常得分；", dataType = "Integer", dataTypeClass = Integer.class),
                    @ApiImplicitParam(name = "dutyType", value = "职责类别：1-业务职责；2-党建及党风廉政；3-安全环保职责；4-合规与保密职责；5-任职资格与能力素质要求；6-岗位责任制文件完整性与适宜性；", dataType = "Integer", dataTypeClass = Integer.class),
                    @ApiImplicitParam(name = "fullScore", value = "满分/权重", dataType = "Integer", dataTypeClass = Integer.class),
                    @ApiImplicitParam(name = "rule", value = "考核规则/评分标准", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "problemLevelOne", value = "type=1-岗检考核规则：一般分数", dataType = "BigDecimal", dataTypeClass = BigDecimal.class),
                    @ApiImplicitParam(name = "problemLevelTwo", value = "type=1-岗检考核规则：重要分数", dataType = "BigDecimal", dataTypeClass = BigDecimal.class),
                    @ApiImplicitParam(name = "status", value = "状态：1-待审核；3-通过；4-驳回；", dataType = "Integer", dataTypeClass = Integer.class),
                    @ApiImplicitParam(name = "statusByUserId", value = "审核人ID", dataType = "Long", dataTypeClass = Long.class),
                    @ApiImplicitParam(name = "statusByUserName", value = "审核人姓名", dataType = "String", dataTypeClass = String.class),
//                    @ApiImplicitParam(name = "delFlag", value = "删除标志（0代表存在 2代表删除）", dataType = "Integer", dataTypeClass = Integer.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:postAssessmentRules:list')")
    @GetMapping("/list")
    public TableDataInfo list(PostAssessmentRules postAssessmentRules)
    {
        startPage();
        List<PostAssessmentRules> list = postAssessmentRulesService.selectPostAssessmentRulesList(postAssessmentRules);
        return getDataTable(list);
    }

    /**
     * 导出考核规则列表
     */
    @ApiOperation("导出考核规则列表")
    @PreAuthorize("@ss.hasPermi('postCheck:postAssessmentRules:export')")
    @Log(title = "考核规则", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PostAssessmentRules postAssessmentRules)
    {
        List<PostAssessmentRules> list = postAssessmentRulesService.selectPostAssessmentRulesList(postAssessmentRules);
        ExcelUtil<PostAssessmentRules> util = new ExcelUtil<PostAssessmentRules>(PostAssessmentRules.class);
        util.exportExcel(response, list, "考核规则数据");
    }

    /**
     * 获取考核规则详细信息
     */
    @ApiOperation("获取考核规则详细信息")
    @PreAuthorize("@ss.hasPermi('postCheck:postAssessmentRules:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(postAssessmentRulesService.selectPostAssessmentRulesById(id));
    }

    /**
     * 新增考核规则
     */
    @ApiOperation("新增考核规则")
    @ApiImplicitParams({
//        @ApiImplicitParam(name = "id", value = "主键ID", dataType = "Long", dataTypeClass = Long.class),
        @ApiImplicitParam(name = "type", value = "评分分类/项目/专业：1-岗检考核规则；2-职能部门互评表；；3-生产部门日常得分；", dataType = "Integer", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "dutyType", value = "职责类别：1-业务职责；2-党建及党风廉政；3-安全环保职责；4-合规与保密职责；5-任职资格与能力素质要求；6-岗位责任制文件完整性与适宜性；", dataType = "Integer", dataTypeClass = Integer.class),
        @ApiImplicitParam(name = "fullScore", value = "满分/权重", dataType = "Integer", dataTypeClass = Integer.class),
        @ApiImplicitParam(name = "rule", value = "考核规则/评分标准", dataType = "String", dataTypeClass = String.class),
        @ApiImplicitParam(name = "problemLevelOne", value = "type=1-岗检考核规则：一般分数", dataType = "BigDecimal", dataTypeClass = BigDecimal.class),
        @ApiImplicitParam(name = "problemLevelTwo", value = "type=1-岗检考核规则：重要分数", dataType = "BigDecimal", dataTypeClass = BigDecimal.class),
//        @ApiImplicitParam(name = "status", value = "状态：1-待审核；3-通过；4-驳回；", dataType = "Integer", dataTypeClass = Integer.class),
//        @ApiImplicitParam(name = "statusByUserId", value = "审核人ID", dataType = "Long", dataTypeClass = Long.class),
//        @ApiImplicitParam(name = "statusByUserName", value = "审核人姓名", dataType = "String", dataTypeClass = String.class),
//        @ApiImplicitParam(name = "delFlag", value = "删除标志（0代表存在 2代表删除）", dataType = "Integer", dataTypeClass = Integer.class),
    })
    @PreAuthorize("@ss.hasPermi('postCheck:postAssessmentRules:add')")
    @Log(title = "考核规则", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PostAssessmentRules postAssessmentRules)
    {
        return toAjax(postAssessmentRulesService.insertPostAssessmentRules(postAssessmentRules));
    }

    /**
     * 修改考核规则
     */
    @ApiOperation("修改考核规则")
    @ApiImplicitParams({
                    @ApiImplicitParam(name = "id", value = "主键ID", dataType = "Long", dataTypeClass = Long.class),
                    @ApiImplicitParam(name = "type", value = "评分分类/项目/专业：1-岗检考核规则；2-职能部门互评表；；3-生产部门日常得分；", dataType = "Integer", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "dutyType", value = "职责类别：1-业务职责；2-党建及党风廉政；3-安全环保职责；4-合规与保密职责；5-任职资格与能力素质要求；6-岗位责任制文件完整性与适宜性；", dataType = "Integer", dataTypeClass = Integer.class),
                    @ApiImplicitParam(name = "fullScore", value = "满分/权重", dataType = "Integer", dataTypeClass = Integer.class),
                    @ApiImplicitParam(name = "rule", value = "考核规则/评分标准", dataType = "String", dataTypeClass = String.class),
                    @ApiImplicitParam(name = "problemLevelOne", value = "type=1-岗检考核规则：一般分数", dataType = "BigDecimal", dataTypeClass = BigDecimal.class),
                    @ApiImplicitParam(name = "problemLevelTwo", value = "type=1-岗检考核规则：重要分数", dataType = "BigDecimal", dataTypeClass = BigDecimal.class),
//                    @ApiImplicitParam(name = "status", value = "状态：1-待审核；3-通过；4-驳回；", dataType = "Integer", dataTypeClass = Integer.class),
//                    @ApiImplicitParam(name = "statusByUserId", value = "审核人ID", dataType = "Long", dataTypeClass = Long.class),
//                    @ApiImplicitParam(name = "statusByUserName", value = "审核人姓名", dataType = "String", dataTypeClass = String.class),
//                    @ApiImplicitParam(name = "delFlag", value = "删除标志（0代表存在 2代表删除）", dataType = "Integer", dataTypeClass = Integer.class),
    })
    @PreAuthorize("@ss.hasPermi('postCheck:postAssessmentRules:edit')")
    @Log(title = "考核规则", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PostAssessmentRules postAssessmentRules)
    {
        return toAjax(postAssessmentRulesService.updatePostAssessmentRules(postAssessmentRules));
    }

    /**
     * 删除考核规则
     */
    @ApiOperation("删除考核规则")
    @ApiImplicitParam(name = "ids", value = "Long类型的主键数组", dataType = "Long[]", dataTypeClass = Long[].class)
    @PreAuthorize("@ss.hasPermi('postCheck:postAssessmentRules:remove')")
    @Log(title = "考核规则", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(postAssessmentRulesService.deletePostAssessmentRulesByIds(ids));
    }

    @ApiOperation("考核规则审核")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "status", value = "状态：3-通过；4-驳回；", dataType = "Integer", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "reason", value = "未通过理由", dataType = "String", dataTypeClass = String.class),
    })
    @Log(title = "考核规则审核", businessType = BusinessType.UPDATE)
    @PutMapping("/statusApprove")
    public AjaxResult statusApprove(Long id, Integer status, String statusReason) {

        // 判断审核的权限
        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(postAssessmentRulesService.statusApprove(id, status,  statusReason));
    }



    @ApiOperation("考核规则审核-职能")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "status", value = "状态：3-通过；4-驳回；", dataType = "Integer", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "reason", value = "未通过理由", dataType = "String", dataTypeClass = String.class),
    })
    @Log(title = "考核规则审核-职能", businessType = BusinessType.UPDATE)
    @PutMapping("/statusFunctions")
    public AjaxResult statusFunctions(Long id, Integer status, String statusReason) {

        // 判断审核的权限
        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(postAssessmentRulesService.statusApprove(id, status,  statusReason));
    }


    @ApiOperation("考核规则审核-生产")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "status", value = "状态：3-通过；4-驳回；", dataType = "Integer", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "reason", value = "未通过理由", dataType = "String", dataTypeClass = String.class),
    })
    @Log(title = "考核规则审核-生产", businessType = BusinessType.UPDATE)
    @PutMapping("/statusProduction")
    public AjaxResult statusProduction(Long id, Integer status, String statusReason) {

        // 判断审核的权限
        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(postAssessmentRulesService.statusApprove(id, status,  statusReason));
    }
}
