package com.ruoyi.postCheck.controller;

import com.fasterxml.classmate.AnnotationOverrides;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.PostListEnum;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.DeptCode;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.postCheck.domain.PostCheckedItems;
import com.ruoyi.postCheck.domain.PostList;
import com.ruoyi.postCheck.domain.PostListInfo;
import com.ruoyi.postCheck.domain.tools.PostCheckListType;
import com.ruoyi.postCheck.domain.tools.PostListApproveTypeUser;
import com.ruoyi.postCheck.service.IPostListService;
import com.ruoyi.system.service.ISysPostService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 岗位责任制清单Controller
 *
 * @author csg
 * @date 2023-12-20
 */
@Api(tags = "1.1.1-岗位责任制清单接口")
@RestController
@RequestMapping("/postCheck/postCheckList")
public class PostListController extends BaseController {
    @Autowired
    private IPostListService postListService;

    @Autowired
    private ISysPostService sysPostService;

    /**
     * 查询岗位责任制清单列表
     */
    @ApiOperation("查询岗位责任制清单列表")
    @PreAuthorize("@ss.hasPermi('postCheck:postCheckList:list')")
    @GetMapping("/list")
    public TableDataInfo list(PostList postList) {
//        startPage();
        startPage("desc", "t1.create_time");
        List<PostList> list = postListService.selectPostListList(postList);
        return getDataTable(list);
    }

    /**
     * 导出岗位责任制清单列表
     @ApiOperation("导出岗位责任制清单列表")
     @PreAuthorize("@ss.hasPermi('postCheck:postCheckList:export')")
     @Log(title = "岗位责任制清单", businessType = BusinessType.EXPORT)
     @PostMapping("/export") public void export(HttpServletResponse response, PostList postList) {
     List<PostList> list = postListService.selectPostListList(postList);
     ExcelUtil<PostList> util = new ExcelUtil<PostList>(PostList.class);
     util.exportExcel(response, list, "岗位责任制清单数据");
     }
     */

    /**
     * 获取岗位责任制清单详细信息
     */
    @ApiOperation("获取岗位责任制清单详细信息")
    @PreAuthorize("@ss.hasPermi('postCheck:postCheckList:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(
            @ApiParam(name = "id", value = "清单id", required = true)
            @PathVariable("id") Long id) {
        return success(postListService.selectPostListById(id));
    }


    @ApiOperation("岗位责任制清单批量导入（先调用上传文件接口再将上传文件的相对路径放到设置到参数中）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fileNameList", value = "数组：前端上传文件接口返回的相对路径（含文件名称）,例如：/upload/2024/02/23/办公室主任_2A003.xlsx", dataType = "String[]", dataTypeClass = String[].class),
    })
    @PostMapping(value = "/batchImport")
    public AjaxResult batchImport(@RequestBody String[] fileNameList) {
        try {
            postListService.batchImport(fileNameList);
        } catch (Exception e) {
            e.printStackTrace();
            return error("全部失败，本次操作为批量，发现文件中存在以下问题，请检查！" + e.getMessage());
        }
        return success();
    }

    /**
     * 新增岗位责任制清单
     */
    @ApiOperation("新增岗位责任制清单")
    /*
    @ApiImplicitParams({
//            @ApiImplicitParam(name = "id", value = "主键", dataType = "Long", dataTypeClass = Long.class),
//            @ApiImplicitParam(name = "status", value = "状态：1-待审核；2-审核中；3-通过；4-驳回；", dataType = "Long", dataTypeClass = Long.class),
//            @ApiImplicitParam(name = "statusByUserId", value = "审核人ID", dataType = "Long", dataTypeClass = Long.class),
//            @ApiImplicitParam(name = "statusByUserName", value = "审核人姓名", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "unitId", value = "所在单位", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "postId", value = "岗位ID", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "positionLevel", value = "岗位级别", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "directSuperior", value = "直接上级", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "subordinateCount", value = "辖员人数", dataType = "Integer", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "deptId", value = "所在部门ID", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "positionCode", value = "岗位编码", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "isConfidential", value = "是否涉密", dataType = "Integer", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "directSubordinate", value = "直接下级", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "creationDate", value = "编制日期", dataType = "Date", dataTypeClass = Date.class),
            @ApiImplicitParam(name = "revisionDate", value = "修订日期", dataType = "Date", dataTypeClass = Date.class),
            @ApiImplicitParam(name = "jobSummary", value = "工作摘要", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "remark", value = "所在部门祖级id", dataType = "String", dataTypeClass = String.class),
//            @ApiImplicitParam(name = "createUserId", value = "创建者用户ID", dataType = "Long", dataTypeClass = Long.class),
            *//** 岗位责任制清单明细信息 *//*
            @ApiImplicitParam(name = "children", value = "岗位责任制清单明细", dataType = "PostListInfo", dataTypeClass = PostListInfo.class),
    })*/

    @PreAuthorize("@ss.hasPermi('postCheck:postCheckList:add')")
    @Log(title = "岗位责任制清单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PostList postList) {
        return toAjax(postListService.insertPostList(postList));
    }

    /**
     * 修改岗位责任制清单
     */
    @ApiOperation("修改岗位责任制清单")
    @PreAuthorize("@ss.hasPermi('postCheck:postCheckList:edit')")
    @Log(title = "岗位责任制清单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PostList postList) {
        return toAjax(postListService.updatePostList(postList));
    }

    /*
    @ApiOperation("批量新建岗位责任制清单（废弃）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "createUserId", value = "创建者用户ID", dataType = "Long", dataTypeClass = Long.class),
    })
    @PreAuthorize("@ss.hasPermi('postCheck:postCheckList:add')")
    @Log(title = "批量新建岗位责任制清单", businessType = BusinessType.INSERT)
    @PostMapping("/batchAdd")
    public AjaxResult batchAdd(@RequestBody List<PostList> postList) {
        return toAjax(postListService.batchInsertPostList(postList));
    }
    */

    /**
     * 修改岗位责任制清单
     @ApiOperation("批量修改岗位责任制清单（废弃）")
     @ApiImplicitParams({
     @ApiImplicitParam(name = "createUserId", value = "创建者用户ID", dataType = "Long", dataTypeClass = Long.class),
     })
     @PreAuthorize("@ss.hasPermi('postCheck:postCheckList:edit')")
     @Log(title = "批量修改岗位责任制清单", businessType = BusinessType.UPDATE)
     @PutMapping("/batchUpdate") public AjaxResult batchUpdate(@RequestBody List<PostList> postList) {
     return toAjax(postListService.batchUpdatePostList(postList));
     }
     */


    /**
     * 删除岗位责任制清单
     */
    @ApiOperation("删除岗位责任制清单")
    @ApiImplicitParam(name = "ids", value = "Long类型的主键数组", dataType = "Long[]", dataTypeClass = Long[].class)
    @PreAuthorize("@ss.hasPermi('postCheck:postCheckList:remove')")
    @Log(title = "岗位责任制清单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(postListService.deletePostListByIds(ids));
    }


    @ApiOperation("岗位责任制清单审核权限用户列表")
    @GetMapping("/approveTypeUserList")
    public AjaxResult approveTypeUserList() {
        /*
        [
            {
                "type" ：1 // 1-业务职责；2-党建及党风廉政建设职责；3-安全环保职责；4-合规保密职；5-任职资格与能力素质要求
                "userList" : [111111, 222222, 33333] // 用户权限列表
            },
            {
                "type" ：2 // 1-业务职责；2-党建及党风廉政建设职责；3-安全环保职责；4-合规保密职；5-任职资格与能力素质要求
                "userList" : [111111, 222222, 33333] // 用户权限列表
            },
            {
                "type" ：3 // 1-业务职责；2-党建及党风廉政建设职责；3-安全环保职责；4-合规保密职；5-任职资格与能力素质要求
                "userList" : [111111, 222222, 33333] // 用户权限列表
            },
            {
                "type" ：4 // 1-业务职责；2-党建及党风廉政建设职责；3-安全环保职责；4-合规保密职；5-任职资格与能力素质要求
                "userList" : [111111, 222222, 33333] // 用户权限列表
            }
        ]
         */

//        Map<Integer, List<Long>> typeMap = new HashMap<>();
//        typeMap.put(PostListEnum.TYPE_ONE.getValue(), sysPostService.userHaveAuthorityList(DeptCode.DEPT_HR.getCode()));
//        typeMap.put(PostListEnum.TYPE_TWO.getValue(), sysPostService.userHaveAuthorityList(DeptCode.DEPT_PARTY.getCode()));
//        typeMap.put(PostListEnum.TYPE_THREE.getValue(), sysPostService.userHaveAuthorityList(DeptCode.DEPT_HSE.getCode()));

//        List<Long> userHaveAuthorityList = sysPostService.userHaveAuthorityList(DeptCode.DEPT_ICFA.getCode());
//        List<Long> userHaveAuthorityListV2 = sysPostService.userHaveAuthorityList(DeptCode.DEPT_OFFICE.getCode());
//        userHaveAuthorityList.addAll(userHaveAuthorityListV2);
//        typeMap.put(PostListEnum.TYPE_FOUR.getValue(), userHaveAuthorityList);

        List<PostListApproveTypeUser> typeUserList = new ArrayList<>();
        PostListApproveTypeUser data1 = new PostListApproveTypeUser(PostListEnum.TYPE_ONE.getValue(), sysPostService.userHaveAuthorityList(DeptCode.DEPT_HR.getCode()));
        PostListApproveTypeUser data2 = new PostListApproveTypeUser(PostListEnum.TYPE_TWO.getValue(), sysPostService.userHaveAuthorityList(DeptCode.DEPT_PARTY.getCode()));
        PostListApproveTypeUser data3 = new PostListApproveTypeUser(PostListEnum.TYPE_THREE.getValue(), sysPostService.userHaveAuthorityList(DeptCode.DEPT_HSE.getCode()));
        List<Long> userHaveAuthorityList = sysPostService.userHaveAuthorityList(DeptCode.DEPT_ICFA.getCode());
        List<Long> userHaveAuthorityListV2 = sysPostService.userHaveAuthorityList(DeptCode.DEPT_OFFICE.getCode());
        userHaveAuthorityList.addAll(userHaveAuthorityListV2);
        PostListApproveTypeUser data4 = new PostListApproveTypeUser(PostListEnum.TYPE_FOUR.getValue(), userHaveAuthorityList);
        typeUserList.add(data1);
        typeUserList.add(data2);
        typeUserList.add(data3);
        typeUserList.add(data4);

        return success(typeUserList);
    }


    @ApiOperation("1、岗位责任制清单提交")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键ID", dataType = "String", dataTypeClass = String.class),
    })
    @Log(title = "岗位责任制清单提交审核", businessType = BusinessType.UPDATE)
    @GetMapping("/submitApprove")
    public AjaxResult submitApprove(Long id) {
        return toAjax(postListService.submitApprove(id));
    }


    @ApiOperation("2、岗位责任制清单审核")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键ID", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "type", value = "审核的类型（1-业务职责；2-党建及党风廉政建设职责；3-安全环保职责；4-合规保密职；5-任职资格与能力素质要求）", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "status", value = "状态：3-通过；4-驳回；", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "reason", value = "未通过理由（驳回时必选）", dataType = "String", dataTypeClass = String.class),
    })
    @Log(title = "岗位责任制清单审核", businessType = BusinessType.UPDATE)
    @PutMapping("/approve")
    public AjaxResult approve(Integer type, Long id, Integer status, String reason) {
        return toAjax(postListService.approve(type, id, status, reason));
    }


    @ApiOperation("1、岗位责任制清单检查项目下拉框")
    @GetMapping("/typeList")
    public AjaxResult typeList() {
      /*
        List<Map<Integer, String>> list = new ArrayList<>();
        Map<Integer, String> map = new HashMap<>();
        map.put(6, "岗位责任制文件完整性与适宜性");
        map.put(5, "任职资格与能力素质要求符合性");
        map.put(1, "业务职责有效性");
        map.put(2, "党建及党风廉政建设职责有效性");
        map.put(3, "安全环保职责有效性");
        map.put(4, "合规与保密职责有效性");
        map.forEach(
                (k, v) -> {
                    Map<Integer, String> data = new HashMap<>();
                    data.put(k, v);
                    list.add(data);
                }
        );*/

        List<PostCheckListType> list = new ArrayList<>();
        PostCheckListType data6 = new PostCheckListType(PostListEnum.TYPE_SIX.getValue(), "岗位责任制文件完整性与适宜性");// 6

        PostCheckListType data5 = new PostCheckListType(PostListEnum.TYPE_FIVE.getValue(), "任职资格与能力素质要求符合性");// 5

        PostCheckListType data1 = new PostCheckListType(PostListEnum.TYPE_ONE.getValue(), "业务职责有效性");// 1
        PostCheckListType data2 = new PostCheckListType(PostListEnum.TYPE_TWO.getValue(), "党建及党风廉政建设职责有效性");// 2
        PostCheckListType data3 = new PostCheckListType(PostListEnum.TYPE_THREE.getValue(), "安全环保职责有效性");// 3
        PostCheckListType data4 = new PostCheckListType(PostListEnum.TYPE_FOUR.getValue(), "合规与保密职责有效性");//4
        list.add(data6);
        list.add(data5);
        list.add(data1);
        list.add(data2);
        list.add(data3);
        list.add(data4);
        return success(list);
    }

    /**
     * 检查表生成时，根据“检查项目”查询“检查内容”
     * 岗检表生成页面，根据岗检清单中的“区域划分”（业务职责、党建党风、安全环保……）查询“事项”中最后有值事项（一级事项、……、五级事项）
     * 清单中的区域 = 岗检表的检查“检查项目”
     *
     * @return
     */
    @ApiOperation("2、岗位责任制清单检查内容：参数是检查项目下拉框")
    @GetMapping("/contentListByType")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deptId", value = "参数->部门ID", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "postId", value = "参数->岗位ID", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "type", value = "参数->检查项目（业务职责、党建党风、安全环保……）", dataType = "Integer", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "postCheckedContent", value = "返回->检查内容", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "postCheckedStandard", value = "返回->检查标准", dataType = "String", dataTypeClass = String.class),
    })
    public AjaxResult contentListByType(Long deptId, Long postId, Integer type) {
        List<PostCheckedItems> itemsList = postListService.contentListByType(deptId, postId, type);
        return success(itemsList);
    }
}
