package com.ruoyi.postCheck.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.PostAssessmentRatingOtherEnum;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.DeptCode;
import com.ruoyi.postCheck.domain.PostAssessmentRatingOther;
import com.ruoyi.postCheck.service.IPostAssessmentRatingOtherService;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 打分表(职能互评生产日常)Controller
 *
 * @author csg
 * @date 2024-01-01
 */
@Api(tags = "4.2.1-打分表(职能互评生产日常)接口")
@RestController
@RequestMapping("/postCheck/postAssessmentRatingOther")
public class PostAssessmentRatingOtherController extends BaseController {
    @Autowired
    private IPostAssessmentRatingOtherService postAssessmentRatingOtherService;

    //    @Autowired
//    private ISysPostService sysPostService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISysDeptService sysDeptService;


    /**
     * 查询打分表(职能互评生产日常)列表
     */
    @ApiOperation("查询打分表(职能互评生产日常)列表")
    @ApiImplicitParams({
//            @ApiImplicitParam(name = "id", value = "主键", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "type", value = "类型：1-职能互评；2-生产日常；", dataType = "Integer", dataTypeClass = Integer.class),
//                    @ApiImplicitParam(name = "batchId", value = "批次号（管理员创建的）", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "startTime", value = "评分开始时间（管理员创建的）", dataType = "Date", dataTypeClass = Date.class),
//            @ApiImplicitParam(name = "status", value = "状态：1-进行中；2-已完成", dataType = "Integer", dataTypeClass = Integer.class),
//            @ApiImplicitParam(name = "deptId", value = "评分部门ID：打分部门，自己给其他12个部门打分；", dataType = "Long", dataTypeClass = Long.class),
//                    @ApiImplicitParam(name = "completeTime", value = "评分完成时间", dataType = "Date", dataTypeClass = Date.class),
//                    @ApiImplicitParam(name = "scoreUserId", value = "评分人ID", dataType = "Long", dataTypeClass = Long.class),
//                    @ApiImplicitParam(name = "scoreUserName", value = "评分人姓名", dataType = "String", dataTypeClass = String.class),
//                    @ApiImplicitParam(name = "createUserId", value = "创建者用户ID", dataType = "Long", dataTypeClass = Long.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:postAssessmentRatingOther:list')")
    @GetMapping("/list")
    public TableDataInfo list(PostAssessmentRatingOther postAssessmentRatingOther) {
//        startPage();
        startPage("desc", "t1.create_time");

        //仅可以查看自己部门的打分任务
        if (postAssessmentRatingOther.getType() == PostAssessmentRatingOtherEnum.TYPE_ZN_HP.getValue()) {
            postAssessmentRatingOther.setDeptId(getDeptId());
        }
        //生产部门是三大部门给生产部门评分
        else if (postAssessmentRatingOther.getType() == PostAssessmentRatingOtherEnum.TYPE_SC_RC.getValue()) {
            postAssessmentRatingOther.setDeptId(null);
        }

//        postAssessmentRatingOther.setDeptId(30l);//职能
//        postAssessmentRatingOther.setDeptId(19l);//生产

        List<PostAssessmentRatingOther> list = postAssessmentRatingOtherService.selectPostAssessmentRatingOtherList(postAssessmentRatingOther);
        return getDataTable(list);
    }

    /**
     * 导出打分表(职能互评生产日常)列表
     @ApiOperation("导出打分表(职能互评生产日常)列表")
     @PreAuthorize("@ss.hasPermi('postCheck:postAssessmentRatingOther:export')")
     @Log(title = "打分表(职能互评生产日常)", businessType = BusinessType.EXPORT)
     @PostMapping("/export") public void export(HttpServletResponse response, PostAssessmentRatingOther postAssessmentRatingOther)
     {
     List<PostAssessmentRatingOther> list = postAssessmentRatingOtherService.selectPostAssessmentRatingOtherList(postAssessmentRatingOther);
     ExcelUtil<PostAssessmentRatingOther> util = new ExcelUtil<PostAssessmentRatingOther>(PostAssessmentRatingOther.class);
     util.exportExcel(response, list, "打分表(职能互评生产日常)数据");
     }
     */

    /**
     * 获取打分表(职能互评生产日常)详细信息
     */
    @ApiOperation("获取打分表(职能互评生产日常)详细信息")
//    @PreAuthorize("@ss.hasPermi('postCheck:postAssessmentRatingOther:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(
            @ApiParam(name = "id", value = "打分表id", required = true)
            @PathVariable("id") Long id) {
        return success(postAssessmentRatingOtherService.selectPostAssessmentRatingOtherById(id));
    }

    /**
     * 新增打分表(职能互评生产日常)
     @ApiOperation("新增打分表(职能互评生产日常)")
     @ApiImplicitParams({
     @ApiImplicitParam(name = "id", value = "主键", dataType = "Long", dataTypeClass = Long.class),
     @ApiImplicitParam(name = "type", value = "类型：1-职能互评；2-生产日常；", dataType = "Integer", dataTypeClass = Integer.class),
     @ApiImplicitParam(name = "batchId", value = "批次号（管理员创建的）", dataType = "String", dataTypeClass = String.class),
     @ApiImplicitParam(name = "startTime", value = "评分开始时间（管理员创建的）", dataType = "Date", dataTypeClass = Date.class),
     @ApiImplicitParam(name = "status", value = "状态：1-进行中；2-已完成", dataType = "Integer", dataTypeClass = Integer.class),
     @ApiImplicitParam(name = "deptId", value = "评分部门ID：打分部门，自己给其他12个部门打分；", dataType = "Long", dataTypeClass = Long.class),
     //        @ApiImplicitParam(name = "completeTime", value = "评分完成时间", dataType = "Date", dataTypeClass = Date.class),
     //        @ApiImplicitParam(name = "scoreUserId", value = "评分人ID", dataType = "Long", dataTypeClass = Long.class),
     //        @ApiImplicitParam(name = "scoreUserName", value = "评分人姓名", dataType = "String", dataTypeClass = String.class),
     //        @ApiImplicitParam(name = "createUserId", value = "创建者用户ID", dataType = "Long", dataTypeClass = Long.class),
     //        @ApiImplicitParam(name = "delFlag", value = "删除标志（0代表存在 2代表删除）", dataType = "Integer", dataTypeClass = Integer.class),
     })
     @PreAuthorize("@ss.hasPermi('postCheck:postAssessmentRatingOther:add')")
     @Log(title = "打分表(职能互评生产日常)", businessType = BusinessType.INSERT)
     @PostMapping public AjaxResult add(@RequestBody PostAssessmentRatingOther postAssessmentRatingOther)
     {
     return toAjax(postAssessmentRatingOtherService.insertPostAssessmentRatingOther(postAssessmentRatingOther));
     }
     */

    /**
     * 修改打分表(职能互评生产日常)
     @ApiOperation("修改打分表(职能互评生产日常)")
     @ApiImplicitParams({
     @ApiImplicitParam(name = "id", value = "主键", dataType = "Long", dataTypeClass = Long.class),
     @ApiImplicitParam(name = "type", value = "类型：1-职能互评；2-生产日常；", dataType = "Integer", dataTypeClass = Integer.class),
     @ApiImplicitParam(name = "batchId", value = "批次号（管理员创建的）", dataType = "String", dataTypeClass = String.class),
     @ApiImplicitParam(name = "startTime", value = "评分开始时间（管理员创建的）", dataType = "Date", dataTypeClass = Date.class),
     @ApiImplicitParam(name = "status", value = "状态：1-进行中；2-已完成", dataType = "Integer", dataTypeClass = Integer.class),
     @ApiImplicitParam(name = "deptId", value = "评分部门ID：打分部门，自己给其他12个部门打分；", dataType = "Long", dataTypeClass = Long.class),
     @ApiImplicitParam(name = "completeTime", value = "评分完成时间", dataType = "Date", dataTypeClass = Date.class),
     @ApiImplicitParam(name = "scoreUserId", value = "评分人ID", dataType = "Long", dataTypeClass = Long.class),
     @ApiImplicitParam(name = "scoreUserName", value = "评分人姓名", dataType = "String", dataTypeClass = String.class),
     @ApiImplicitParam(name = "createUserId", value = "创建者用户ID", dataType = "Long", dataTypeClass = Long.class),
     @ApiImplicitParam(name = "delFlag", value = "删除标志（0代表存在 2代表删除）", dataType = "Integer", dataTypeClass = Integer.class),
     })
     @PreAuthorize("@ss.hasPermi('postCheck:postAssessmentRatingOther:edit')")
     @Log(title = "打分表(职能互评生产日常)", businessType = BusinessType.UPDATE)
     @PutMapping public AjaxResult edit(@RequestBody PostAssessmentRatingOther postAssessmentRatingOther)
     {
     return toAjax(postAssessmentRatingOtherService.updatePostAssessmentRatingOther(postAssessmentRatingOther));
     }
     */

    /**
     * 删除打分表(职能互评生产日常)
     *
     * @ApiOperation("删除打分表(职能互评生产日常)")
     * @ApiImplicitParam(name = "ids", value = "Long类型的主键数组", dataType = "Long[]", dataTypeClass = Long[].class)
     * @PreAuthorize("@ss.hasPermi('postCheck:postAssessmentRatingOther:remove')")
     * @Log(title = "打分表(职能互评生产日常)", businessType = BusinessType.DELETE)
     * @DeleteMapping("/{ids}") public AjaxResult remove(@PathVariable Long[] ids)
     * {
     * return toAjax(postAssessmentRatingOtherService.deletePostAssessmentRatingOtherByIds(ids));
     * }
     */


    @ApiOperation("有权限创建打分任务的用户列表（职能部门互评/生产部门日常评分页面需要先校验权限才能展示按钮）")
    @GetMapping("/approveTypeUserList")
    public AjaxResult approveTypeUserList() {
        //版本1：查询内控法务部的经理
//        List<Long> userHaveAuthorityList = sysPostService.userHaveAuthorityList(DeptCode.DEPT_ICFA.getCode());
        //版本2：查询内控法务部的全员 2024年5月11日18:45:59
        SysDept sysDept = new SysDept();
        sysDept.setDeptCode(DeptCode.DEPT_ICFA.getCode());
        List<SysDept> deptList = sysDeptService.selectDeptList(sysDept);
//        deptList = null;
        if (CollectionUtils.isEmpty(deptList)) {
            return error("系统未发现部门编码等于【" + DeptCode.DEPT_ICFA.getCode() + "】的内控法务部部门，请联系管理员！");
        }
        if (deptList.size() != 1) {
            return error("系统发现部门编码等于【" + DeptCode.DEPT_ICFA.getCode() + "】的部门不唯一，请联系管理员！");
        }
        List<SysUser> userList = sysUserService.postUserList(deptList.get(0).getDeptId(), null, null, null, null, null, null);
        List<Long> userHaveAuthorityList = userList.stream().map(t -> t.getUserId()).collect(Collectors.toList());
        return success(userHaveAuthorityList);
    }


    /**
     * 管理员创建互评计划：(职能部门互评、生产部门日常)
     * 互评批次：管理员启动互评任务，初始化13个部门互相打分的记录
     */
    @ApiOperation("新增打分表(职能互评/生产日常)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "类型：1-职能互评；2-生产日常；", dataType = "Integer", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "startTime", value = "评分开始时间（管理员创建的）", dataType = "Date", dataTypeClass = Date.class),
    })
    @Log(title = "新增打分表(职能互评/生产日常)", businessType = BusinessType.INSERT)
    @PostMapping("/batchAddRatingOther")
    public AjaxResult batchAddRatingOther(@RequestBody PostAssessmentRatingOther postAssessmentRatingOther) {

        //版本1：查询内控法务部的经理
//        List<Long> userHaveAuthorityList = sysPostService.userHaveAuthorityList(DeptCode.DEPT_ICFA.getCode());
        //版本2：查询内控法务部的全员 2024年5月11日18:45:59
        SysDept sysDept = new SysDept();
        sysDept.setDeptCode(DeptCode.DEPT_ICFA.getCode());
        List<SysDept> deptList = sysDeptService.selectDeptList(sysDept);
        if (CollectionUtils.isEmpty(deptList)) {
            return error("系统未发现部门编码等于【" + DeptCode.DEPT_ICFA.getCode() + "】的内控法务部部门，请联系管理员！");
        }
        if (deptList.size() != 1) {
            return error("系统发现部门编码等于【" + DeptCode.DEPT_ICFA.getCode() + "】的部门不唯一，请联系管理员！");
        }
        List<SysUser> userList = sysUserService.postUserList(deptList.get(0).getDeptId(), null, null, null, null, null, null);
        List<Long> userHaveAuthorityList = userList.stream().map(t -> t.getUserId()).collect(Collectors.toList());

        if (!userHaveAuthorityList.contains(getUserId())) {
            return error("无权限，请切换内控法务部人员账号操作！");
        }

        postAssessmentRatingOther.setDeptId(getDeptId());
        Integer type = postAssessmentRatingOther.getType();
        if (type == PostAssessmentRatingOtherEnum.TYPE_ZN_HP.getValue()) {
            return toAjax(postAssessmentRatingOtherService.batchAddRatingOther(postAssessmentRatingOther));
        } else if (type == PostAssessmentRatingOtherEnum.TYPE_SC_RC.getValue()) {
            return toAjax(postAssessmentRatingOtherService.addRatingOtherSC(postAssessmentRatingOther));
        } else {
            return AjaxResult.error("类型错误，请检查！");
        }
    }


    @ApiOperation("职能部门互评打分提交")
    @Log(title = "职能部门互评打分提交", businessType = BusinessType.UPDATE)
    @PostMapping("/functionalDepartmentSub")
    public AjaxResult functionalDepartmentSub(@RequestBody PostAssessmentRatingOther postAssessmentRatingOther) {
        //不知道前端咋写的，职能部门打分结果与生产部门打分结果，提交方式居然不一样，人才绝
        return toAjax(postAssessmentRatingOtherService.batchAddRatingOtherUpdate(postAssessmentRatingOther));
    }

    @ApiOperation("生产部门日常评分打分提交")
    @Log(title = "生产部门日常评分打分提交", businessType = BusinessType.UPDATE)
    @PostMapping("/productionDepartmentSub")
    public AjaxResult productionDepartmentSub(@RequestBody List<PostAssessmentRatingOther> postAssessmentRatingOther) {
        //不知道前端咋写的，职能部门打分结果与生产部门打分结果，提交方式居然不一样，人才绝
        return toAjax(postAssessmentRatingOtherService.productionDepartmentSub(postAssessmentRatingOther));
    }
}
