package com.ruoyi.postCheck.controller;

import com.ruoyi.common.annotation.Log;
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.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.SysDeptEnum;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.postCheck.domain.PostCheckedProblem;
import com.ruoyi.postCheck.domain.excel.problem.ProblemExportBean;
import com.ruoyi.postCheck.domain.excel.problem.ProblemExportTaiZhangBean;
import com.ruoyi.postCheck.domain.tools.ProblemCategory;
import com.ruoyi.postCheck.service.IPostCheckedProblemService;
import com.ruoyi.postCheck.util.excel.create.PostProblemCreateExcel;
import com.ruoyi.system.service.ISysDeptService;
import io.swagger.annotations.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 岗检问题表Controller
 *
 * @author csg
 * @date 2023-12-23
 */
@Api(tags = "3.3.1-岗检问题整改")
@RestController
@RequestMapping("/postCheck/postCheckedProblem")
public class PostCheckedProblemController extends BaseController {
    @Autowired
    private IPostCheckedProblemService postCheckedProblemService;
    @Autowired
    private ISysDeptService sysDeptService;

    /**
     * 查询岗检问题表列表
     */
    @ApiOperation("查询岗检问题表列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "status", value = "状态", dataType = "Integer", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "problemNo", value = "问题编号", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "checkWorkgroupId", value = "检查工作组ID", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "checkGroupId", value = "检查小组ID", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "checkUserName", value = "检查人姓名", dataType = "Long", dataTypeClass = Long.class),
//            @ApiImplicitParam(name = "deptApproveUserId", value = "审核1：整改部门经理ID", dataType = "Long", dataTypeClass = Long.class),
//            @ApiImplicitParam(name = "deptApproveTime", value = "审核1：整改部门经理审核时间", dataType = "Date", dataTypeClass = Date.class),
//            @ApiImplicitParam(name = "specialityDepartmentReviewerId", value = "审核2：专业部门的审核人ID（非必选，方案填写人选择）", dataType = "Long", dataTypeClass = Long.class),
//            @ApiImplicitParam(name = "specialityDepartmentReviewTime", value = "审核2：专业部门审核人审核时间", dataType = "Date", dataTypeClass = Date.class),
            @ApiImplicitParam(name = "problemLevel", value = "问题等级（一般、重要）", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "dutyType", value = "职责类别：1-业务职责；2-党建及党风廉政；3-安全环保职责；4-合规保密职；", dataType = "Integer", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "problemAttribute", value = "问题属性（完整问题、有效性问题、适宜性问题）", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "problemCategory", value = "问题类别（职责权限问题、工作内容问题、工作标准问题、考核奖励问题、任职资格问题、其他）", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "description", value = "问题描述", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "problemCause", value = "问题原因", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "isImmediatelyCorrected", value = "是否立行立改(1-Yes；2-No；)", dataType = "Integer", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "rectificationMeasure", value = "整改措施", dataType = "String", dataTypeClass = String.class),

            @ApiImplicitParam(name = "checkedDeptId", value = "整改责任部门ID", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "checkedPostId", value = "整改责任岗位ID", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "checkedDeptName", value = "整改责任部门名称", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "checkedPostName", value = "整改责任岗位名称", dataType = "String", dataTypeClass = String.class),

            @ApiImplicitParam(name = "rectificationResponsibleUserId", value = "整改责任人ID", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "rectificationPeriod", value = "整改期限", dataType = "Date", dataTypeClass = Date.class),
            @ApiImplicitParam(name = "isRectificationCompleted", value = "整改情况-是否完成(1-Yes；2-No；)", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "rectificationCompletionTime", value = "整改完成时间", dataType = "Date", dataTypeClass = Date.class),
//            @ApiImplicitParam(name = "rectificationCompletionImg", value = "整改完成图片磁盘路径", dataType = "String", dataTypeClass = String.class),
//            @ApiImplicitParam(name = "reviewDepartmentReviewerId", value = "审核3：复查部门的审核人ID（措施上传时必选，实施人选择）", dataType = "Long", dataTypeClass = Long.class),
//            @ApiImplicitParam(name = "reviewDepartmentReviewerTime", value = "审核3：复查部门的审核人复查时间", dataType = "Date", dataTypeClass = Date.class),
            @ApiImplicitParam(name = "reviewConclusion", value = "复查结论", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "reason", value = "未通过理由", dataType = "String", dataTypeClass = String.class),
//            @ApiImplicitParam(name = "delFlag", value = "删除标志（0代表存在 2代表删除）", dataType = "Integer", dataTypeClass = Integer.class),
//            @ApiImplicitParam(name = "createUserId", value = "创建者用户ID", dataType = "Long", dataTypeClass = Long.class),
//            @ApiImplicitParam(name = "checkTime", value = "检查时间", dataType = "Date", dataTypeClass = Date.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:postCheckedProblem:list')")
    @GetMapping("/list")
    public TableDataInfo list(PostCheckedProblem postCheckedProblem) {
        startPage("desc", "t1.create_time");
        List<PostCheckedProblem> list = postCheckedProblemService.selectPostCheckedProblemList(postCheckedProblem);
        return getDataTable(list);
    }


    /**
     * 岗检问题整改台账导出Excel文件：此接口返回导出文件地址，页面再调用下载文件接口保存文件
     * @param ids
     * @return
     */
    @ApiOperation("岗检问题整改台账导出")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "岗检问题ids", dataType = "String", dataTypeClass = String.class),
    })
    @GetMapping(value = "/batchExportTaiZhang")
    public AjaxResult batchExportTaiZhang(String ids) {
        if (StringUtils.isBlank(ids)) {
            return error("请先选择要导出问题清单！");
        }

        String[] idSplit = ids.split(",");
        List<Long> idList = new ArrayList<>();
        for (String id : idSplit) {
            idList.add(Long.parseLong(id));
        }

        PostCheckedProblem problemParam = new PostCheckedProblem();
        problemParam.setProblemIdList(idList);
        List<PostCheckedProblem> postCheckedProblemList = postCheckedProblemService.selectPostCheckedProblemList(problemParam);

        if (idList.size() != postCheckedProblemList.size()) {
            return error("页面选择的条数，与数据库记录不匹配，请刷新页面重试！");
        }

        List<ProblemExportTaiZhangBean> problemExportBeanList = new ArrayList<>();
        for (int i = 0; i < postCheckedProblemList.size(); i++) {
            PostCheckedProblem problem = postCheckedProblemList.get(i);
            ProblemExportTaiZhangBean data = new ProblemExportTaiZhangBean();
            BeanUtils.copyProperties(problem, data);

            data.setNo(i + 1);
//            data.setProblemCause(null); //问题原因
//            data.setIsImmediatelyCorrected(null);//是否立行立改问题
//            data.setRectificationMeasure(null);//整改措施/建议
//            data.setRectificationPeriod(null);//整改期限
//            data.setSpecialityDepartment(null);//专业审核部门
//            data.setSpecialityDepartmentReviewerName(null);//专业审核人
//            data.setIsRectificationCompleted(null);//整改情况
//            data.setRectificationCompletionTime(null);//整改完成时间
//            data.setReviewDepartment(null);//复查部门
//            data.setReviewDepartmentReviewerName(null);//复查人
            problemExportBeanList.add(data);
        }


        PostProblemCreateExcel<ProblemExportTaiZhangBean> excelBeanExcelUtil = new PostProblemCreateExcel<>(ProblemExportTaiZhangBean.class);
        AjaxResult ajaxResult = excelBeanExcelUtil.exportExcel(problemExportBeanList, "问题整改台账", "问题整改台账", null);
        String code = ajaxResult.get(AjaxResult.CODE_TAG).toString();
        if ("200".equals(code)) {
            String msg = ajaxResult.get(AjaxResult.MSG_TAG).toString();
            return success(msg);
        }
        return ajaxResult;
    }


    /**
     * 岗检问题导入导出再导入的业务说明：
     * 1、导入岗检问题：用户下载模板后，线下将问题录入到Excel中，将Excel上传到服务器，后台保存新增的问题
     * POST /postCheck/postCheckedProblem/batchImport 参数数数组，放到请求的Body中
     * 2、导出岗检问题：选择选择需要导出的问题，导出成Excel文件
     * GET /postCheck/postCheckedProblem/batchExport 参数Param类型的，参数属性是‘ids’逗号拼接（问题的id唯一主键），直接拼接到URL后面即可，例如：ids?123456,987654
     * 3、整改方案/整改实施：用户先导出文件，再在导出文件的基础上填充问题的方案和整改实施信息后将表格重新导入到系统中
     * POST /postCheck/postCheckedProblem/batchImportRectification 参数数数组，放到请求的Body中
     */

    @ApiOperation("岗检问题批量导入")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fileNameList", value = "数组：前端上传文件接口返回的相对路径（含文件名称）,例如：/upload/2024/02/23/办公室主任_2A003.xlsx", dataType = "String[]", dataTypeClass = String[].class),
    })
    @PostMapping(value = "/batchImport")
    @Log(title = "岗检问题-批量导入", businessType = BusinessType.INSERT)
    public AjaxResult batchImport(@RequestBody String[] fileNameList) throws Exception {
        //1、解析文件
        //2、验证数据的正确性
        //3、调用插入问题的方法
        if (fileNameList == null) {
            return error("参数为空，请检查！");
        }
        if (fileNameList.length == 0) {
            return error("参数中文件数量等于0，请检查！");
        }
        if (fileNameList.length != 1) {
            return error("一次仅支持一个文件，请检查！");
        }
//        String localPath = RuoYiConfig.getProfile();
        //含文件名的相对路径：/upload/2024/03/28/FLAMING MOUNTAIN_20240328183324A002.xlsx
        String resource = fileNameList[0];
        /*
        // 文件绝对磁盘路径（含文件名称）
        String downloadPath = localPath + resource;
        File file = new File(downloadPath);
        InputStream inputStream = new FileInputStream(file);
        ExcelUtil excelUtil = new ExcelUtil(PostCheckedProblemExcelBean.class);
        List<PostCheckedProblemExcelBean> dataList = excelUtil.importExcel(inputStream, 1);
        for (int i = 0; i < dataList.size(); i++) {
            PostCheckedProblemExcelBean data = dataList.get(i);
            Integer no = data.getNo();

            String checkUserName = data.getCheckUserName();
            Date checkTime = data.getCheckTime();

            String checkedDeptName = data.getCheckedDeptName();
            String checkedPostName = data.getCheckedPostName();
            Integer problemLevel = data.getProblemLevel();//问题级别
            Integer dutyType = data.getDutyType();//职责类别
            Integer problemAttribute = data.getProblemAttribute();//问题属性
            Integer problemCategory = data.getProblemCategory();//问题类别

        }
        */

        String resultMsg = postCheckedProblemService.batchImportProblem(resource);
        if ("SUCCESS".equals(resultMsg)) {
            return success("问题清单导入成功！");
        }
        return error(resultMsg);
//        return error("接口待开发！接口待开发！接口待开发！接口待开发！" + resource);
    }


    /**
     * 导出岗检问题：选择选择需要导出的问题，导出成Excel文件：返回的Excel文件路径，前端再去调用下载文件接口保存文件
     * @param ids
     * @return
     */
    @ApiOperation("导出岗检问题")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "岗检问题ids", dataType = "String", dataTypeClass = String.class),
    })
    @GetMapping(value = "/batchExport")
    public AjaxResult batchExport(String ids) {
        if (StringUtils.isBlank(ids)) {
            return error("请先选择要导出问题清单！");
        }

        String[] idSplit = ids.split(",");
        List<Long> idList = new ArrayList<>();
        for (String id : idSplit) {
            idList.add(Long.parseLong(id));
        }

        PostCheckedProblem problemParam = new PostCheckedProblem();
        problemParam.setProblemIdList(idList);
        List<PostCheckedProblem> postCheckedProblemList = postCheckedProblemService.selectPostCheckedProblemList(problemParam);

        if (idList.size() != postCheckedProblemList.size()) {
            return error("页面选择的条数，与数据库记录不匹配，请刷新页面重试！");
        }

        List<ProblemExportBean> problemExportBeanList = new ArrayList<>();
        for (int i = 0; i < postCheckedProblemList.size(); i++) {
            PostCheckedProblem problem = postCheckedProblemList.get(i);
            ProblemExportBean data = new ProblemExportBean();
            BeanUtils.copyProperties(problem, data);

            data.setNo(i + 1);
            data.setProblemCause(null); //问题原因
            data.setIsImmediatelyCorrected(null);//是否立行立改问题
            data.setRectificationMeasure(null);//整改措施/建议
            data.setRectificationPeriod(null);//整改期限
            data.setSpecialityDepartment(null);//专业审核部门
            data.setSpecialityDepartmentReviewerName(null);//专业审核人
            data.setIsRectificationCompleted(null);//整改情况
            data.setRectificationCompletionTime(null);//整改完成时间
            data.setReviewDepartment(null);//复查部门
            data.setReviewDepartmentReviewerName(null);//复查人
            problemExportBeanList.add(data);
        }


        //岗检系统中部门信息
        SysDept sysDeptParam = new SysDept();
        sysDeptParam.setStatus(SysDeptEnum.STATUS_VALID.getValue().toString());
        List<SysDept> sysDeptList = sysDeptService.selectDeptList(sysDeptParam);
        List<String> deptNameList = sysDeptList.stream().map(SysDept::getDeptName).collect(Collectors.toList());


        PostProblemCreateExcel<ProblemExportBean> excelBeanExcelUtil = new PostProblemCreateExcel<>(ProblemExportBean.class);
        AjaxResult ajaxResult = excelBeanExcelUtil.exportExcel(problemExportBeanList, "岗位责任制检查问题整改表", "岗位责任制检查问题整改表", deptNameList);
        String code = ajaxResult.get(AjaxResult.CODE_TAG).toString();
        if ("200".equals(code)) {
            String msg = ajaxResult.get(AjaxResult.MSG_TAG).toString();
            return success(msg);
        }
        return ajaxResult;


/*

        PostChecked param = new PostChecked();
        param.setIdList(idList);
        List<PostChecked> postCheckedList = postCheckedService.selectPostCheckedList(param);
        if (idList.size() != postCheckedList.size()) {
            return error("页面选择的记录数与数据库不匹配，请刷新页面再试！页面选择 " + idList.size() + " 条， 数据库匹配 " + postCheckedList.size() + " 条");
        }


        List<String> fileNameList = new ArrayList<>();
        for (PostChecked postChecked : postCheckedList) {
            String fileName = new PostCheckedCreateExcel().createExcel(postChecked, postChecked.getChildren());
            fileNameList.add(downloadPath + fileName);
        }

        String zipFileName = "批量导出岗位检查表-" + DateUtils.dateTimeNow() + ".zip";//"C:\\gnhz\\uploadPath\\download\\批量导出岗位检查表-" + DateUtils.dateTimeNow() + ".zip";
        new ZipFileUtils().createZipFile(fileNameList, downloadPath + zipFileName);
*/

//        String fileName = "问题清单批量导入（模板）.xls";

//        return success(File.separator + "download" + File.separator + fileName);
        //        /upload/2024/01/15/2024岗检方案_20240115131209A001_20240115155951A001.docx
        //        return success("/upload/2024/01/18/岗位责任制检查表_20240118001.xlsx");
    }


    /**
     * 3、整改方案/整改实施导入：用户通过岗检问题导出，导出文件，在导出文件的基础上填充问题的方案和整改信息后将表格重新导入到系统中
     * @param fileNameList
     * @return
     */
    @ApiOperation("整改方案/整改实施导入")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fileNameList", value = "数组：前端上传文件接口返回的相对路径（含文件名称）,例如：/upload/2024/02/23/办公室主任_2A003.xlsx", dataType = "String[]", dataTypeClass = String[].class),
    })
    @PostMapping(value = "/batchImportRectification")
    public AjaxResult batchImportRectification(@RequestBody String[] fileNameList) {
           //1、解析文件
        //2、验证数据的正确性
        //3、更新问题
        if (fileNameList == null) {
            return error("参数为空，请检查！");
        }
        if (fileNameList.length == 0) {
            return error("参数中文件数量等于0，请检查！");
        }
        if (fileNameList.length != 1) {
            return error("一次仅支持一个文件，请检查！");
        }
        //含文件名的相对路径：/upload/2024/03/28/FLAMING MOUNTAIN_20240328183324A002.xlsx
        String resource = fileNameList[0];


        String resultMsg = postCheckedProblemService.batchImportRectification(resource);
        if ("SUCCESS".equals(resultMsg)) {
            return success("问题清单导入成功！");
        }
        return error(resultMsg);

//        return success("接口待开发！接口待开发！接口待开发！接口待开发！" + fileNameList);
    }


    /**
     * 获取岗检问题表详细信息
     */
    @ApiOperation("获取岗检问题表详细信息")
    /*
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "status", value = "状态：1-待指定整改人（问题录入完成时）；2-待填写方案（部门管理员指定整改人后）；3-方案填写完成；10-部门审核通过；11-部门审核驳回；20-专业部门人审核（非必填）通过；21-专业部门人审核驳回（非必填）；30-整改人待上传实施（整改人上传实施图片）；31-上传实施照片完成；40-复查部门人审核通过；41-复查部门人审核驳回", dataType = "Integer", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "problemNo", value = "问题编号（需要甲方提供规则规则）", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "checkWorkgroupId", value = "检查工作组ID", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "checkGroupId", value = "检查小组ID", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "checkUserName", value = "检查人姓名", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "deptApproveUserId", value = "审核1：整改部门经理ID", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "deptApproveTime", value = "审核1：整改部门经理审核时间", dataType = "Date", dataTypeClass = Date.class),
            @ApiImplicitParam(name = "specialityDepartmentReviewerId", value = "审核2：专业部门的审核人ID（非必选，方案填写人选择）", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "specialityDepartmentReviewerName", value = "审核2：专业部门的审核人名称（非必选，方案填写人选择）", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "specialityDepartmentReviewTime", value = "审核2：专业部门审核人审核时间", dataType = "Date", dataTypeClass = Date.class),
            @ApiImplicitParam(name = "problemLevel", value = "问题等级（一般、重要）", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "dutyType", value = "职责类别：1-业务职责；2-党建及党风廉政；3-安全环保职责；4-合规保密职；", dataType = "Integer", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "problemAttribute", value = "问题属性（完整问题、有效性问题、适宜性问题）", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "problemCategory", value = "问题类别（职责权限问题、工作内容问题、工作标准问题、考核奖励问题、任职资格问题、其他）", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "description", value = "问题描述", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "problemCause", value = "问题原因", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "isImmediatelyCorrected", value = "是否立行立改(1-Yes；2-No；)", dataType = "Integer", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "rectificationMeasure", value = "整改措施", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "checkedDeptId", value = "整改责任部门ID（被检查部门ID）", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "checkedPostId", value = "整改责任岗位ID（被检查岗位ID）", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "rectificationResponsibleUserId", value = "整改责任人ID", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "rectificationPeriod", value = "整改期限", dataType = "Date", dataTypeClass = Date.class),
            @ApiImplicitParam(name = "isRectificationCompleted", value = "整改情况-是否完成(1-Yes；2-No；)", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "rectificationCompletionTime", value = "整改完成时间", dataType = "Date", dataTypeClass = Date.class),
            @ApiImplicitParam(name = "rectificationCompletionImg", value = "整改完成图片磁盘路径", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "reviewDepartmentReviewerId", value = "审核3：复查部门的审核人ID（措施上传时必选，实施人选择）", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "reviewDepartmentReviewerName", value = "审核3：复查部门的审核人名称（措施上传时必选，实施人选择）", dataType = "Long", dataTypeClass = Long.class),

            @ApiImplicitParam(name = "reviewDepartmentReviewerTime", value = "审核3：复查部门的审核人复查时间", dataType = "Date", dataTypeClass = Date.class),
            @ApiImplicitParam(name = "reviewConclusion", value = "复查结论", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "reason", value = "未通过理由", dataType = "String", dataTypeClass = String.class),
//            @ApiImplicitParam(name = "delFlag", value = "删除标志（0代表存在 2代表删除）", dataType = "Integer", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "createUserId", value = "创建者用户ID", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "checkTime", value = "检查时间", dataType = "Date", dataTypeClass = Date.class),
    })
    */
    @PreAuthorize("@ss.hasPermi('postCheck:postCheckedProblem:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(
            @ApiParam(name = "id", value = "岗检问题id", required = true)
            @PathVariable("id") Long id) {
        return success(postCheckedProblemService.selectPostCheckedProblemById(id));
    }

    /**
     * 新增岗检问题表
     */
    @ApiOperation("新增岗检问题表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "planInfoId", value = "岗检计划明细ID（岗检问题录入时列表的唯一标识）", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "checkUserName", value = "检查人姓名", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "problemLevel", value = "问题等级（一般、重要）", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "dutyType", value = "职责类别：1-业务职责；2-党建及党风廉政；3-安全环保职责；4-合规保密职；", dataType = "Integer", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "problemAttribute", value = "问题属性（完整问题、有效性问题、适宜性问题）", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "problemCategory", value = "问题类别（职责权限问题、工作内容问题、工作标准问题、考核奖励问题、任职资格问题、其他）", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "description", value = "问题描述", dataType = "String", dataTypeClass = String.class),
    })
    @PreAuthorize("@ss.hasPermi('postCheck:postCheckedProblem:add')")
    @Log(title = "岗检问题表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody List<PostCheckedProblem> postCheckedProblemList) {
        return toAjax(postCheckedProblemService.insertPostCheckedProblem(postCheckedProblemList));
    }

    /**
     * 修改岗检问题表
     */
    @ApiOperation("修改岗检问题表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键", dataType = "Long", dataTypeClass = Long.class),
//            @ApiImplicitParam(name = "status", value = "状态：1-待指定整改人（问题录入完成时）；2-待填写方案（部门管理员指定整改人后）；3-方案填写完成；10-部门审核通过；11-部门审核驳回；20-专业部门人审核（非必填）通过；21-专业部门人审核驳回（非必填）；30-整改人待上传实施（整改人上传实施图片）；31-上传实施照片完成；40-复查部门人审核通过；41-复查部门人审核驳回", dataType = "Integer", dataTypeClass = Integer.class),
//            @ApiImplicitParam(name = "problemNo", value = "问题编号（需要甲方提供规则规则）", dataType = "String", dataTypeClass = String.class),
//            @ApiImplicitParam(name = "checkWorkgroupId", value = "检查工作组ID", dataType = "Long", dataTypeClass = Long.class),
//            @ApiImplicitParam(name = "checkGroupId", value = "检查小组ID", dataType = "Long", dataTypeClass = Long.class),
//            @ApiImplicitParam(name = "deptApproveUserId", value = "审核1：整改部门经理ID", dataType = "Long", dataTypeClass = Long.class),
//            @ApiImplicitParam(name = "deptApproveTime", value = "审核1：整改部门经理审核时间", dataType = "Date", dataTypeClass = Date.class),
//            @ApiImplicitParam(name = "specialityDepartmentReviewerId", value = "审核2：专业部门的审核人ID（非必选，方案填写人选择）", dataType = "Long", dataTypeClass = Long.class),
//            @ApiImplicitParam(name = "specialityDepartmentReviewTime", value = "审核2：专业部门审核人审核时间", dataType = "Date", dataTypeClass = Date.class),
            @ApiImplicitParam(name = "checkUserName", value = "检查人姓名", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "problemLevel", value = "问题等级（一般、重要）", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "problemAttribute", value = "问题属性（完整问题、有效性问题、适宜性问题）", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "problemCategory", value = "问题类别（职责权限问题、工作内容问题、工作标准问题、考核奖励问题、任职资格问题、其他）", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "description", value = "问题描述", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "problemCause", value = "问题原因", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "isImmediatelyCorrected", value = "是否立行立改(1-Yes；2-No；)", dataType = "Integer", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "rectificationMeasure", value = "整改措施", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "checkedDeptId", value = "整改责任部门ID（被检查部门ID）", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "checkedPostId", value = "整改责任岗位ID（被检查岗位ID）", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "rectificationResponsibleUserId", value = "整改责任人ID", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "rectificationPeriod", value = "整改期限", dataType = "Date", dataTypeClass = Date.class),
            @ApiImplicitParam(name = "isRectificationCompleted", value = "整改情况-是否完成(1-Yes；2-No；)", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "rectificationCompletionTime", value = "整改完成时间", dataType = "Date", dataTypeClass = Date.class),
            @ApiImplicitParam(name = "rectificationCompletionImg", value = "整改完成图片磁盘路径", dataType = "String", dataTypeClass = String.class),
//            @ApiImplicitParam(name = "reviewDepartmentReviewerId", value = "审核3：复查部门的审核人ID（措施上传时必选，实施人选择）", dataType = "Long", dataTypeClass = Long.class),
//            @ApiImplicitParam(name = "reviewDepartmentReviewerTime", value = "审核3：复查部门的审核人复查时间", dataType = "Date", dataTypeClass = Date.class),
//            @ApiImplicitParam(name = "reviewConclusion", value = "复查结论", dataType = "String", dataTypeClass = String.class),
//            @ApiImplicitParam(name = "reason", value = "未通过理由", dataType = "String", dataTypeClass = String.class),
//            @ApiImplicitParam(name = "delFlag", value = "删除标志（0代表存在 2代表删除）", dataType = "Integer", dataTypeClass = Integer.class),
//            @ApiImplicitParam(name = "createUserId", value = "创建者用户ID", dataType = "Long", dataTypeClass = Long.class),
//            @ApiImplicitParam(name = "checkTime", value = "检查时间", dataType = "Date", dataTypeClass = Date.class),
            @ApiImplicitParam(name = "dutyType", value = "职责类别：1-业务职责；2-党建及党风廉政；3-安全环保职责；4-合规保密职；", dataType = "Integer", dataTypeClass = Integer.class),
    })
    @PreAuthorize("@ss.hasPermi('postCheck:postCheckedProblem:edit')")
    @Log(title = "岗检问题表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody List<PostCheckedProblem> postCheckedProblemList) {
        return toAjax(postCheckedProblemService.updatePostCheckedProblem(postCheckedProblemList));
    }

    /**
     * 删除岗检问题表
     */
    @ApiOperation("删除岗检问题表")
//    @ApiImplicitParam(name = "ids", value = "Long类型的主键数组", dataType = "Long[]", dataTypeClass = Long[].class)
    @PreAuthorize("@ss.hasPermi('postCheck:postCheckedProblem:remove')")
    @Log(title = "岗检问题表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(
            @ApiParam(name = "ids", value = "删除岗检问题ids", required = true)
            @PathVariable Long[] ids) {
        return toAjax(postCheckedProblemService.deletePostCheckedProblemByIds(ids));
    }


    /**
     * 步骤1->指定岗检问题整改责任人：填写方案、指定复查人、上传实施、指定验收人
     * @param ids
     * @param rectificationResponsibleUserId
     * @return
     */
    @ApiOperation("指定岗检问题整改责任人")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "多个主键英文逗号拼接", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "rectificationResponsibleUserId", value = "整改责任人ID", dataType = "Long", dataTypeClass = Long.class)
    })
    @Log(title = "指定方案填写人", businessType = BusinessType.UPDATE)
    @PutMapping("/setRectificationResponsibleUserId")
//    public AjaxResult setRectificationResponsibleUserId(Long id, Long rectificationResponsibleUserId) {
    public AjaxResult setRectificationResponsibleUserId(String ids, Long rectificationResponsibleUserId) {
        return toAjax(postCheckedProblemService.setRectificationResponsibleUserId(ids, rectificationResponsibleUserId));
    }

    /**
     * 步骤2->方案整改：整改实施人填写方案
     * @param id
     * @param problemCause
     * @param isImmediatelyCorrected
     * @param rectificationMeasure
     * @param rectificationPeriod
     * @return
     */
    @ApiOperation("岗检问题整改责任人填写整改方案")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "problemCause", value = "问题原因", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "isImmediatelyCorrected", value = "是否立行立改(1-Yes；2-No；)", dataType = "Integer", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "rectificationMeasure", value = "整改措施/建议", dataType = "String", dataTypeClass = String.class),
//            @ApiImplicitParam(name = "rectificationResponsibleUserId", value = "整改责任人ID", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "rectificationPeriod", value = "整改期限", dataType = "Date", dataTypeClass = Date.class),
    })
    @Log(title = "岗检问题整改责任人填写整改方案", businessType = BusinessType.UPDATE)
    @PutMapping("/updateRectificationMeasure")
    public AjaxResult updateRectificationMeasure(Long id, String problemCause, Integer isImmediatelyCorrected, String rectificationMeasure, Date rectificationPeriod) {
        return toAjax(postCheckedProblemService.updateRectificationMeasure(id, problemCause, isImmediatelyCorrected, rectificationMeasure, rectificationPeriod));
    }


    /**
     * 步骤3->指定专业部门的审核人（非必选操作）
     * @param id
     * @param specialityDepartmentReviewerId
     * @return
     */
    @ApiOperation("岗检问题整改责任人指定专业部门的审核人")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "specialityDepartmentReviewerId", value = "专业部门的审核人ID", dataType = "Long", dataTypeClass = Long.class)
    })
    @Log(title = "岗检问题整改责任人指定专业部门的审核人", businessType = BusinessType.UPDATE)
    @PutMapping("/setSpecialityDepartmentReviewerId")
    public AjaxResult setSpecialityDepartmentReviewerId(Long id, Long specialityDepartmentReviewerId) {
        return toAjax(postCheckedProblemService.setSpecialityDepartmentReviewerId(id, specialityDepartmentReviewerId));
    }


    /**
     * 步骤4->审核1：部门经理批量审核
     * @param ids
     * @param status
     * @param reason
     * @return
     */
    @ApiOperation("部门经理批量审核")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "主键，英文逗号拼接", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "status", value = "10-部门审核通过；11-部门审核驳回；", dataType = "Integer", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "reason", value = "未通过理由", dataType = "String", dataTypeClass = String.class),
    })
    @Log(title = "部门经理批量审核", businessType = BusinessType.UPDATE)
    @PutMapping("/setDeptApproveTime")
//    public AjaxResult setDeptApproveTime(Long id, Integer status, String reason) {
    public AjaxResult setDeptApproveTime(String ids, Integer status, String reason) {
        String errorMsg = "";
        int count = 0;
        String[] idArr = ids.split(",");
        for (int i = 0; i < idArr.length; i++) {
            String idStr = idArr[i];
            Long id = Long.parseLong(idStr);
            try {
                count += postCheckedProblemService.setDeptApproveTime(id, status, reason);
            } catch (Exception e) {
                errorMsg += "第" + (i + 1) + "条：" + e.getMessage();
                e.printStackTrace();
                logger.error("部门经理批量审核-失败记录：id = {} 失败提示信息：{}", id, e.getMessage());
            }
        }
        if (count > 0) {
            return toAjax(count);
        }
        return error(errorMsg);
    }


    /**
     * 步骤5->审核2：专业部门审核人批量审核
     * @param ids
     * @param status
     * @param reason
     * @return
     */
    @ApiOperation("专业部门审核人批量审核")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "主键，英文逗号拼接", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "status", value = "20-专业部门人审核（非必填）通过；21-专业部门人审核驳回（非必填）；", dataType = "Integer", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "reason", value = "未通过理由", dataType = "String", dataTypeClass = String.class),
    })
    @Log(title = "审核2：专业部门审核人批量审核", businessType = BusinessType.UPDATE)
    @PutMapping("/setSpecialityDepartmentReviewTime")
    public AjaxResult setSpecialityDepartmentReviewTime(String ids, Integer status, String reason) {
        int count = 0;
        String[] idArr = ids.split(",");
        for (int i = 0; i < idArr.length; i++) {
            String idStr = idArr[i];
            Long id = Long.parseLong(idStr);
            try {
                count += postCheckedProblemService.setSpecialityDepartmentReviewTime(id, status, reason);
            } catch (Exception e) {
                e.printStackTrace();
                logger.error("专业部门审核人批量审核-失败记录：id = {} 失败提示信息：{}", id, e.getMessage());
            }
        }
        return toAjax(count);
    }


    /**
     * 步骤6->实施整改：上传实施照片
     * @param id
     * @param isRectificationCompleted
     * @param rectificationCompletionTime
     * @param rectificationCompletionImg
     * @return
     */
    @ApiOperation("岗检问题上传实施照片")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "isRectificationCompleted", value = "整改情况-是否完成(1-Yes；2-No；)", dataType = "Integer", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "rectificationCompletionTime", value = "整改完成时间", dataType = "Date", dataTypeClass = Date.class),
            @ApiImplicitParam(name = "rectificationCompletionImg", value = "整改完成图片磁盘路径", dataType = "String", dataTypeClass = String.class),
    })
    @Log(title = "实施整改", businessType = BusinessType.UPDATE)
    @PutMapping("/updateRectificationCompletionImg")
    public AjaxResult updateRectificationCompletionImg(Long id, Integer isRectificationCompleted, Date rectificationCompletionTime, String rectificationCompletionImg) {
        return toAjax(postCheckedProblemService.updateRectificationCompletionImg(id, isRectificationCompleted, rectificationCompletionTime, rectificationCompletionImg));
    }


    /**
     * 步骤7->指定复查人
     * @param id
     * @param reviewDepartmentReviewerId
     * @return
     */
    @ApiOperation("岗检问题指定复查人")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "reviewDepartmentReviewerId", value = "复查人ID", dataType = "Long", dataTypeClass = Long.class)
    })
    @Log(title = "岗检问题指定复查人", businessType = BusinessType.UPDATE)
    @PutMapping("/setReviewDepartmentReviewerId")
    public AjaxResult setReviewDepartmentReviewerId(Long id, Long reviewDepartmentReviewerId) {
        return toAjax(postCheckedProblemService.setReviewDepartmentReviewerId(id, reviewDepartmentReviewerId));
    }


    /**
     * 步骤8->审核3：复查部门审核人批量复查
     * @param ids
     * @param status
     * @param reviewConclusion
     * @param reason
     * @return
     */
    @ApiOperation("岗检问题复查部门审核人批量复查")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "主键，英文逗号拼接", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "status", value = "30-复查部门人审核通过；31-复查部门人审核驳回", dataType = "Integer", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "reviewConclusion", value = "复查结论", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "reason", value = "未通过理由", dataType = "String", dataTypeClass = String.class),
    })
    @Log(title = "岗检问题复查部门审核人批量复查", businessType = BusinessType.UPDATE)
    @PutMapping("/setReviewDepartmentReviewerTime")
    public AjaxResult setReviewDepartmentReviewerTime(String ids, Integer status, String reviewConclusion, String reason) {
        int count = 0;
        String[] idArr = ids.split(",");
        for (int i = 0; i < idArr.length; i++) {
            String idStr = idArr[i];
            Long id = Long.parseLong(idStr);
            try {
                count += postCheckedProblemService.setReviewDepartmentReviewerTime(id, status, reviewConclusion, reason);
            } catch (Exception e) {
                e.printStackTrace();
                logger.error("复查部门审核人批量复查-失败记录：id = {} 失败提示信息：{}", id, e.getMessage());
            }

        }

        return toAjax(count);
    }


    /**
     * Tools-1、按问题属性分组统计问题类别
     * @param noParam
     * @return
     */
    @ApiOperation("岗检问题按问题属性分组统计问题类别")
    @GetMapping("/problemCategoryByAttribute")
    public AjaxResult problemCategoryByAttribute(ProblemCategory noParam) {

// GROUP BY problem_attribute -- 问题属性（1-完整问题；2-有效性问题；3-适宜性问题；）
// GROUP BY problem_category -- 问题类别（职责权限问题、工作内容问题、工作标准问题、考核奖励问题、任职资格问题、其他）

        List<ProblemCategory> problemCategoryByAttributeList = postCheckedProblemService.problemCategoryByAttribute();

        return success(problemCategoryByAttributeList);
    }

}
