package com.ruoyi.postCheck.service.impl;

import java.awt.dnd.DropTarget;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.constant.PostCheckedProblemEnum;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.postCheck.domain.PostAssessmentRating;
import com.ruoyi.postCheck.domain.PostAssessmentRules;
import com.ruoyi.postCheck.domain.PostList;
import com.ruoyi.postCheck.domain.PostPlan;
import com.ruoyi.postCheck.domain.PostPlanInfo;
import com.ruoyi.postCheck.domain.homePage.DeptProblemRectificationView;
import com.ruoyi.postCheck.domain.homePage.PostCheckedProblemView;
import com.ruoyi.postCheck.domain.homePage.PostCheckedUnCheckedView;
import com.ruoyi.postCheck.domain.homePage.PostTotalView;
import com.ruoyi.postCheck.domain.join.PostPlanJoinProblem;
import com.ruoyi.postCheck.domain.problemStatistics.ProblemNumberBase;
import com.ruoyi.postCheck.domain.tools.ProblemCategory;
import com.ruoyi.postCheck.mapper.PostAssessmentRatingMapper;
import com.ruoyi.postCheck.mapper.PostAssessmentRulesMapper;
import com.ruoyi.postCheck.mapper.PostListMapper;
import com.ruoyi.postCheck.mapper.PostPlanInfoMapper;
import com.ruoyi.postCheck.mapper.PostPlanMapper;
import com.ruoyi.postCheck.service.IPostPlanJoinProblemService;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.postCheck.mapper.PostCheckedProblemMapper;
import com.ruoyi.postCheck.domain.PostCheckedProblem;
import com.ruoyi.postCheck.service.IPostCheckedProblemService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;


/**
 * 岗检问题表Service业务层处理
 *
 * @author csg
 * @date 2023-12-23
 */
@Service
public class PostCheckedProblemServiceImpl extends ServiceImpl<PostCheckedProblemMapper, PostCheckedProblem> implements IPostCheckedProblemService {
    @Autowired
    private PostCheckedProblemMapper postCheckedProblemMapper;
    @Autowired
    private PostPlanInfoMapper postPlanInfoMapper;
    @Autowired
    private PostPlanMapper postPlanMapper;
    @Autowired
    private PostAssessmentRatingMapper postAssessmentRatingMapper;
    @Autowired
    private PostAssessmentRulesMapper postAssessmentRulesMapper;
    @Autowired
    private IPostPlanJoinProblemService postPlanJoinProblemService;
    @Autowired
    private PostListMapper postListMapper;
    /**
     * 查询岗检问题表
     *
     * @param id 岗检问题表主键
     * @return 岗检问题表
     */
    @Override
    public PostCheckedProblem selectPostCheckedProblemById(Long id) {
        return postCheckedProblemMapper.selectPostCheckedProblemById(id);
    }

    /**
     * 查询岗检问题表列表
     *
     * @param postCheckedProblem 岗检问题表
     * @return 岗检问题表
     */
    @Override
    public List<PostCheckedProblem> selectPostCheckedProblemList(PostCheckedProblem postCheckedProblem) {
        return postCheckedProblemMapper.selectPostCheckedProblemList(postCheckedProblem);
    }

    /**
     * 新增岗检问题表
     *
     * @param postCheckedProblemList 前端提交的岗检问题集合，一次提交多个问题
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertPostCheckedProblem(List<PostCheckedProblem> postCheckedProblemList) {
        if (CollectionUtils.isEmpty(postCheckedProblemList)) {
            return 0;
        }

        PostCheckedProblem data = postCheckedProblemList.get(0);
        Long planInfoId = data.getPlanInfoId();
        PostPlanJoinProblem param = new PostPlanJoinProblem();
        param.setPlanInfoId(planInfoId);
        List<PostPlanJoinProblem> list = postPlanJoinProblemService.selectPostPlanJoinProblemList(param);
        if (CollectionUtils.isEmpty(list)) {
            return 0;
        }
        PostPlanJoinProblem postPlanJoinProblem = list.get(0);
        Long checkGroupId = postPlanJoinProblem.getCheckGroupId();
        Long checkTeamId = postPlanJoinProblem.getCheckTeamId();
        Long checkedDeptId = postPlanJoinProblem.getCheckedDeptId();
        Date checkTime = postPlanJoinProblem.getCheckTime();

        int count = 0;
        for (PostCheckedProblem postCheckedProblem : postCheckedProblemList) {
            count++;
            //TODO 自增主键修改为-MyBatis-Plus雪花算法(截取5位解决VUE精度丢失BUG)-系统登录日志
//            if (postCheckedProblem.getId() == null)
            postCheckedProblem.setId(Long.parseLong(IdWorker.getIdStr().substring(4)));
            // postCheckedProblem.setCreateTime(DateUtils.getNowDate());
            postCheckedProblem.setCreateDefault();

            postCheckedProblem.setProblemNo("GJ-" + DateUtils.getTime());
            postCheckedProblem.setCheckWorkgroupId(checkGroupId);
            postCheckedProblem.setCheckGroupId(checkTeamId);
            postCheckedProblem.setCheckedDeptId(checkedDeptId);
            //检查时间取值=岗检计划的开始时间
            postCheckedProblem.setCheckTime(checkTime);

            int insertCount = postCheckedProblemMapper.insertPostCheckedProblem(postCheckedProblem);
            if (insertCount == 0) {
                throw new BaseException("第" + count + "行更新失败请检查！");
            }
        }
        return 1;
    }

    public void jobInspectionScore(List<PostCheckedProblem> postCheckedProblemList) {
        /**
         * 业务职责有效性 1
         * 党建及党风廉政建设职责有效性 2
         * 安全环保职责有效性 3
         * 合规与保密职责有效性 4
         * 任职资格与能力素质要求符合性 5
         * 岗位责任制文件完整性与适宜性 6
         */
        if (postCheckedProblemList.isEmpty()) {
            return;
        }
        Long planInfoId = postCheckedProblemList.get(0).getPlanInfoId();
        Long departmentId = postCheckedProblemList.get(0).getCheckedDeptId();
        List<PostAssessmentRules> postAssessmentRules = postAssessmentRulesMapper.selectList(Wrappers.query());
        PostPlanInfo postPlanInfo = postPlanInfoMapper.selectById(planInfoId);
        PostPlan postPlan = postPlanMapper.selectById(postPlanInfo.getPostPlanId());
        List<PostCheckedProblem> postCheckedProblems = postCheckedProblemMapper.selectList(Wrappers.<PostCheckedProblem>lambdaQuery()
                .eq(PostCheckedProblem::getCheckedDeptId, departmentId)
                .between(PostCheckedProblem::getCreateTime, postPlan.getStartTime(), postPlan.getEndTime())

        );
        List<PostAssessmentRating> postAssessmentRatings = postAssessmentRatingMapper.selectList(Wrappers.<PostAssessmentRating>lambdaQuery()
                .eq(PostAssessmentRating::getPlanId, postPlanInfo.getPostPlanId())
                .eq(PostAssessmentRating::getDeptId, postPlanInfo.getCheckedDeptId())
        );
        if (postAssessmentRatings.isEmpty()) {
            return;
        }
        BigDecimal score = new BigDecimal("100");
        for (PostCheckedProblem postCheckedProblem : postCheckedProblems) {
            Optional<PostAssessmentRules> first = postAssessmentRules.stream().filter(d -> d.getDutyType().equals(postCheckedProblem.getDutyType())).findFirst();
            if (first.isPresent() && score.compareTo(new BigDecimal("0")) > 0) {
                // 计算岗检得分
                if (postCheckedProblem.getProblemLevel().equals("1")) {
                    score = score.subtract(first.get().getProblemLevelOne());
                } else if (postCheckedProblem.getProblemLevel().equals("2")) {
                    score = score.subtract(first.get().getProblemLevelTwo());
                }
            }
        }

        // todo 公式计算
        if (postCheckedProblems.size() > 0) {
            BigDecimal bigDecimal = new BigDecimal("1");
            List<PostPlanInfo> postPlanInfos = postPlanInfoMapper.selectList(Wrappers.<PostPlanInfo>lambdaQuery()
                    .eq(PostPlanInfo::getPostPlanId, postPlan.getId())
            );
            if (postPlanInfos.size() <= 0) {
                score = new BigDecimal("0");
            } else {
                BigDecimal divide = new BigDecimal(postCheckedProblems.size()).divide(new BigDecimal(postPlanInfos.size()).multiply(new BigDecimal("6")));
                if (divide.compareTo(new BigDecimal("0")) <= 0 || divide.compareTo(new BigDecimal("1")) > 0) {
                    score = new BigDecimal("0");
                } else {
                    score = bigDecimal.subtract(divide);
                }
            }
        } else {
            score = new BigDecimal("0");
        }
        postAssessmentRatingMapper.update(null, Wrappers.<PostAssessmentRating>lambdaUpdate()
                .set(PostAssessmentRating::getPositionScore, score)
                .eq(PostAssessmentRating::getDeptId, departmentId)
                .eq(PostAssessmentRating::getPlanId, postPlan.getId())
        );
    }


    /**
     * 修改岗检问题表
     *
     * @param postCheckedProblemList 岗检问题集合
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updatePostCheckedProblem(List<PostCheckedProblem> postCheckedProblemList) {
        if (CollectionUtils.isEmpty(postCheckedProblemList)) {
            return 0;
        }
        int count = 0;
        for (PostCheckedProblem postCheckedProblem : postCheckedProblemList) {
            count++;
            if (postCheckedProblem.getId() == null) {
                throw new BaseException("id为空请检查！");
            }
            // postCheckedProblem.setUpdateTime(DateUtils.getNowDate());
            postCheckedProblem.setUpdateDefault();
            int updateCount = postCheckedProblemMapper.updatePostCheckedProblem(postCheckedProblem);
            if (updateCount == 0) {
                throw new BaseException("第" + count + "行更新失败请检查！");
            }
        }
        return 1;
    }


    /**
     * 批量删除岗检问题表
     *
     * @param ids 需要删除的岗检问题表主键
     * @return 结果
     */
    @Override
    public int deletePostCheckedProblemByIds(Long[] ids) {
        return postCheckedProblemMapper.deletePostCheckedProblemByIds(ids);
    }

    /**
     * 删除岗检问题表信息
     *
     * @param id 岗检问题表主键
     * @return 结果
     */
    @Override
    public int deletePostCheckedProblemById(Long id) {
        return postCheckedProblemMapper.deletePostCheckedProblemById(id);
    }

    /**
     * 指定方案填写人：设置整改责任人
     *
     * @param ids                            多个主键英文逗号拼接
     * @param rectificationResponsibleUserId 整改责任人ID
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int setRectificationResponsibleUserId(String ids, Long rectificationResponsibleUserId) {
        if (StringUtils.isBlank(ids) || rectificationResponsibleUserId == null) {
            return 0;
        }
        String[] idArr = ids.split(",");
        if (idArr.length == 0) {
            return 0;
        }
        int count = 0;
        for (String idStr : idArr) {
            Long id = Long.parseLong(idStr);
            PostCheckedProblem data = new PostCheckedProblem();
            data.setId(id);
            data.setStatus(PostCheckedProblemEnum.EDIT_ONE.getValue());//待填写方案
            data.setRectificationResponsibleUserId(rectificationResponsibleUserId);
            count += postCheckedProblemMapper.updatePostCheckedProblem(data);
        }
        return count;
    }

    /**
     * 方案整改：整改实施人填写方案
     *
     * @param id                             问题主键
     * @param problemCause                   问题原因
     * @param isImmediatelyCorrected         是否立行立改(1-Yes；2-No；)
     * @param rectificationMeasure           整改措施/建议
     * @param rectificationResponsibleUserId 整改责任人ID
     * @param rectificationPeriod            整改期限
     * @return
     */
    @Override
    public int updateRectificationMeasure(Long id, String problemCause, Integer isImmediatelyCorrected, String rectificationMeasure, Long rectificationResponsibleUserId, Date rectificationPeriod) {

        if (id == null || StringUtils.isBlank(problemCause) || isImmediatelyCorrected == null || StringUtils.isBlank(rectificationMeasure) || rectificationResponsibleUserId == null || rectificationPeriod == null) {
            return 0;
        }
        PostCheckedProblem data = new PostCheckedProblem();
        data.setId(id);
        data.setProblemCause(problemCause);
        data.setIsImmediatelyCorrected(isImmediatelyCorrected);
        data.setRectificationMeasure(rectificationMeasure);
        data.setRectificationResponsibleUserId(rectificationResponsibleUserId);
        data.setRectificationPeriod(rectificationPeriod);

        return postCheckedProblemMapper.updatePostCheckedProblem(data);
    }

    /**
     * 指定专业部门的审核人：整改责任人填写
     *
     * @param id                             问题主键ID
     * @param specialityDepartmentReviewerId 专业部门的审核人ID
     * @return
     */
    @Override
    public int setSpecialityDepartmentReviewerId(Long id, Long specialityDepartmentReviewerId) {
        if (id == null || specialityDepartmentReviewerId == null) {
            return 0;
        }
        PostCheckedProblem data = new PostCheckedProblem();
        data.setId(id);
        data.setSpecialityDepartmentReviewerId(specialityDepartmentReviewerId);
        return postCheckedProblemMapper.updatePostCheckedProblem(data);
    }


    /**
     * 审核1：部门经理审核
     *
     * @param id
     * @param status 10-部门审核通过；11-部门审核驳回；
     * @param reason 未通过理由
     * @return
     */
    @Override
    public int setDeptApproveTime(Long id, Integer status, String reason) {
        if (id == null || status == null) {
            return 0;
        }

        //状态错误
        if (status != PostCheckedProblemEnum.DEPT_PASS.getValue() && status != PostCheckedProblemEnum.DEPT_BACK.getValue()) {
            return 0;
        }


        //驳回时必须填写原因
        if (status == PostCheckedProblemEnum.DEPT_BACK.getValue() && StringUtils.isBlank(reason)) {
            return 0;
        }

        PostCheckedProblem data = new PostCheckedProblem();
        data.setId(id);
        data.setStatus(status);
        data.setReason(reason);
        data.setDeptApproveTime(new Date());
        return postCheckedProblemMapper.updatePostCheckedProblem(data);
    }

    /**
     * 审核2：专业部门审核人审核
     *
     * @param id
     * @param status 20-专业部门人审核（非必填）通过；21-专业部门人审核驳回（非必填）；
     * @param reason 未通过理由
     * @return
     */
    @Override
    public int setSpecialityDepartmentReviewTime(Long id, Integer status, String reason) {
        if (id == null || status == null) {
            return 0;
        }

        //状态错误
        if (status != PostCheckedProblemEnum.ZHUAN_YE_PASS.getValue() && status != PostCheckedProblemEnum.ZHUAN_YE_BACK.getValue()) {
            return 0;
        }


        //驳回时必须填写原因
        if (status == PostCheckedProblemEnum.ZHUAN_YE_BACK.getValue() && StringUtils.isBlank(reason)) {
            return 0;
        }

        PostCheckedProblem data = new PostCheckedProblem();
        data.setId(id);
        data.setStatus(status);
        data.setReason(reason);
        data.setSpecialityDepartmentReviewTime(new Date());
        return postCheckedProblemMapper.updatePostCheckedProblem(data);
    }

    /**
     * 实施整改：上传实施照片
     *
     * @param id                          问题主键ID
     * @param isRectificationCompleted    整改情况-是否完成(1-Yes；2-No；)
     * @param rectificationCompletionTime 整改完成时间
     * @param rectificationCompletionImg  整改完成图片磁盘路径
     * @return
     */
    @Override
    public int updateRectificationCompletionImg(Long id, Integer isRectificationCompleted, Date rectificationCompletionTime, String rectificationCompletionImg) {

        if (id == null || isRectificationCompleted == null || StringUtils.isBlank(rectificationCompletionImg)) {
            return 0;
        }
        if (rectificationCompletionTime == null)
            rectificationCompletionTime = new Date();

        PostCheckedProblem data = new PostCheckedProblem();
        data.setId(id);
        data.setIsRectificationCompleted(isRectificationCompleted);
        data.setRectificationCompletionTime(rectificationCompletionTime);
        data.setRectificationCompletionImg(rectificationCompletionImg);

        return postCheckedProblemMapper.updatePostCheckedProblem(data);
    }

    /**
     * 指定复查人
     *
     * @param id                         问题主键ID
     * @param reviewDepartmentReviewerId 复查人ID
     * @return
     */
    @Override
    public int setReviewDepartmentReviewerId(Long id, Long reviewDepartmentReviewerId) {
        if (id == null || reviewDepartmentReviewerId == null) {
            return 0;
        }
        PostCheckedProblem data = new PostCheckedProblem();
        data.setId(id);
        data.setReviewDepartmentReviewerId(reviewDepartmentReviewerId);
        return postCheckedProblemMapper.updatePostCheckedProblem(data);
    }

    /**
     * 审核3：复查部门的审核人复查
     *
     * @param id               问题主键ID
     * @param status           30-复查部门人审核通过；31-复查部门人审核驳回
     * @param reviewConclusion 复查结论
     * @param reason           未通过理由
     */
    @Override
    public int setReviewDepartmentReviewerTime(Long id, Integer status, String reviewConclusion, String reason) {
        if (id == null || status == null) {
            return 0;
        }

        //状态错误
        if (status != PostCheckedProblemEnum.FU_CHA_PASS.getValue() && status != PostCheckedProblemEnum.FU_CHA_BACK.getValue()) {
            return 0;
        }


        //驳回时必须填写原因
        if (status == PostCheckedProblemEnum.FU_CHA_BACK.getValue() && StringUtils.isBlank(reason)) {
            return 0;
        }

        PostCheckedProblem data = new PostCheckedProblem();
        data.setId(id);
        data.setStatus(status);
        data.setReviewConclusion(reviewConclusion);
        data.setReason(reason);
        data.setReviewDepartmentReviewerTime(new Date());
        return postCheckedProblemMapper.updatePostCheckedProblem(data);
    }

    /**
     * 1、责任制清单：post_list表
     * 2、检查出的问题：post_checked_problem表
     * 3、岗检数量-已查：post_plan t1 inner join post_plan_info t2 两张表连接，t2.status(岗检状态：1-未检；2-已检；)
     * 4、岗检数量-未查：post_plan t1 inner join post_plan_info t2 两张表连接，t2.status(岗检状态：1-未检；2-已检；)
     */
    @Override
    public PostTotalView totalView() {
        PostTotalView postTotalView = new PostTotalView();
        // 1
        List<PostList> postLists = postListMapper.selectList(Wrappers.lambdaQuery());
        postTotalView.setListCount(postLists.size());
        // 2
        List<PostCheckedProblem> postCheckedProblems = postCheckedProblemMapper.selectList(Wrappers.lambdaQuery());
        postTotalView.setProblemCount(postCheckedProblems.size());
        // 3
        Integer checkedPostCount = postCheckedProblemMapper.selectCheckedPostCount(1);
        postTotalView.setCheckedPostCount(checkedPostCount);
        Integer uncheckedPostCount = postCheckedProblemMapper.selectCheckedPostCount(2);
        postTotalView.setUncheckedPostCount(uncheckedPostCount);
        return postTotalView;
    }

    @Override
    public PostCheckedProblemView checkedProblem() {
        List<PostCheckedProblem> postCheckedProblems = postCheckedProblemMapper.selectList(Wrappers.lambdaQuery());
        Integer problemComplete = 0;
        Integer problemSuitable = 0;
        Integer problemEffective = 0;
        //（1-完整问题；2-有效性问题；3-适宜性问题；）
        if (!postCheckedProblems.isEmpty())
            for (PostCheckedProblem postCheckedProblem : postCheckedProblems) {
                if (postCheckedProblem.getProblemAttribute()!=null && postCheckedProblem.getProblemAttribute() == 1)
                    problemComplete++;
                if (postCheckedProblem.getProblemAttribute()!=null && postCheckedProblem.getProblemAttribute() == 3)
                    problemEffective++;
                if (postCheckedProblem.getProblemAttribute()!=null && postCheckedProblem.getProblemAttribute() == 2)
                    problemSuitable++;
            }
        PostCheckedProblemView post = new PostCheckedProblemView();
        post.setProblemTotal(postCheckedProblems.size());
        post.setProblemComplete(problemComplete);
        post.setProblemSuitable(problemSuitable);
        post.setProblemEffective(problemEffective);
        return post;
    }

    @Override
    public Integer userTotal() {
        return postListMapper.userTotal();
    }

    @Override
    public Map<Integer, BigDecimal> problemRectification() {
        List<PostCheckedProblem> postCheckedProblems = postCheckedProblemMapper.selectList(Wrappers.lambdaQuery());
        Integer problemComplete = 0;
        Integer overproblemComplete = 0;
        Integer problemSuitable = 0;
        Integer overproblemSuitable = 0;
        Integer problemEffective = 0;
        Integer overproblemEffective = 0;
        Integer total = 0;
        Integer overtotal = 0;
        //（1-完整问题；2-有效性问题；3-适宜性问题；4 总数）
        if (!postCheckedProblems.isEmpty())
            for (PostCheckedProblem postCheckedProblem : postCheckedProblems) {
                if (postCheckedProblem.getProblemAttribute()!=null&&postCheckedProblem.getProblemAttribute() == 1) {
                    problemComplete++;
                    if (postCheckedProblem.getIsImmediatelyCorrected()!=null&&postCheckedProblem.getIsImmediatelyCorrected() == 1) {
                        overproblemComplete++;
                    }
                }
                if (postCheckedProblem.getProblemAttribute()!=null&&postCheckedProblem.getProblemAttribute() == 3) {
                    problemEffective++;
                    if (postCheckedProblem.getIsImmediatelyCorrected()!=null&&postCheckedProblem.getIsImmediatelyCorrected() == 1) {
                        overproblemEffective++;
                    }
                }
                if (postCheckedProblem.getProblemAttribute()!=null&&postCheckedProblem.getProblemAttribute() == 2) {
                    problemSuitable++;
                    if (postCheckedProblem.getIsImmediatelyCorrected()!=null&&postCheckedProblem.getIsImmediatelyCorrected() == 1) {
                        overproblemSuitable++;
                    }
                }
                total++;
                if (postCheckedProblem.getIsImmediatelyCorrected()!=null&&postCheckedProblem.getIsImmediatelyCorrected() == 1) {
                    overtotal++;
                }
            }
        Map<Integer, BigDecimal> map = new HashMap<>();
        map.put(1, new BigDecimal(overproblemComplete).divide(new BigDecimal(problemComplete),4, RoundingMode.HALF_UP));
        map.put(2, new BigDecimal(overproblemSuitable).divide(new BigDecimal(problemSuitable),4, RoundingMode.HALF_UP));
        map.put(3, new BigDecimal(overproblemEffective).divide(new BigDecimal(problemEffective),4, RoundingMode.HALF_UP));
        map.put(4, new BigDecimal(overtotal).divide(new BigDecimal(total),4, RoundingMode.HALF_UP));
        return map;

    }

    @Override
    public List<DeptProblemRectificationView> deptProblemRectification() {
        List<DeptProblemRectificationView> dataList = new ArrayList<>();
        List<PostCheckedProblem> postCheckedProblems = postCheckedProblemMapper.selectList(Wrappers.lambdaQuery());
        if (!postCheckedProblems.isEmpty()){
            Map<Long, List<PostCheckedProblem>> postMapListMap = postCheckedProblems.stream().collect(Collectors.groupingBy(PostCheckedProblem::getCheckedDeptId));
            for (Map.Entry<Long, List<PostCheckedProblem>> entry : postMapListMap.entrySet()) {
                DeptProblemRectificationView data1 = new DeptProblemRectificationView();
                data1.setDeptId(entry.getKey());
                data1.setDeptName(postAssessmentRatingMapper.getSysDeptName(entry.getKey()));
                Integer problemTotal = 0;
                Integer problemComplete = 0;
                for (PostCheckedProblem postCheckedProblem : entry.getValue()) {
                    problemTotal++;
                    if (postCheckedProblem.getIsRectificationCompleted()!=null&&postCheckedProblem.getIsRectificationCompleted() == 1){
                        problemComplete++;
                    }
                }
                data1.setProblemTotal(problemTotal);
                data1.setProblemComplete(problemComplete);
                data1.setProblemRectificationRate(new BigDecimal(problemComplete).divide(new BigDecimal(problemTotal),4, RoundingMode.HALF_UP));
                dataList.add(data1);
            }
        }
        return dataList;
    }

    @Override
    public List<PostCheckedUnCheckedView> postChecked(Integer i) {
        return postListMapper.postChecked(i);
    }

    @Override
    public Integer getPostCountByDeptId(Long deptId) {
        return postListMapper.getPostCountByDeptId(deptId);
    }

    /**
     * 按问题属性分组统计问题类别
     *
     * @return
     */
    @Override
    public List<ProblemCategory> problemCategoryByAttribute() {
//        @ApiModelProperty("职责权限问题")
//        private Integer problemCategoryOne;
//        @ApiModelProperty("工作内容问题")
//        private Integer problemCategoryTwo;
//        @ApiModelProperty("工作标准问题")
//        private Integer problemCategoryThree;
//        @ApiModelProperty("考核奖励问题")
//        private Integer problemCategoryFour;
//        @ApiModelProperty("任职资格问题")
//        private Integer problemCategoryFive;
//        @ApiModelProperty("其他")
//        private Integer problemCategorySix;
//        @ApiModelProperty("总数量")
//        private Integer problemTotal;

        List<ProblemCategory> list = postCheckedProblemMapper.problemCategoryByAttribute();
        ProblemCategory totalData = new ProblemCategory();
        totalData.setProblemAttribute(999);
        totalData.setProblemAttributeView("总计");
        int problemCategoryOne = 0;
        int problemCategoryTwo = 0;
        int problemCategoryThree = 0;
        int problemCategoryFour = 0;
        int problemCategoryFive = 0;
        int problemCategorySix = 0;
        for (ProblemCategory data : list) {
//            Integer problemAttribute = data.getProblemAttribute();
//            switch (problemAttribute) {
//                case 1: {
//                    setProblemAttributeView("完整问题");
//
//                    break;
//                }
//                case 2: {
//                    setProblemAttributeView("有效性问题");
//
//                    break;
//                }
//                case 3: {
//                    setProblemAttributeView("适宜性问题");
//
//                    break;
//                }
//
//                default:
//                    setProblemAttributeView("Other");
//                    break;
//            }

            problemCategoryOne += data.getProblemCategoryOne();
            problemCategoryTwo += data.getProblemCategoryTwo();
            problemCategoryThree += data.getProblemCategoryThree();
            problemCategoryFour += data.getProblemCategoryFour();
            problemCategoryFive += data.getProblemCategoryFive();
            problemCategorySix += data.getProblemCategorySix();
        }
        totalData.setProblemCategoryOne(problemCategoryOne);
        totalData.setProblemCategoryTwo(problemCategoryTwo);
        totalData.setProblemCategoryThree(problemCategoryThree);
        totalData.setProblemCategoryFour(problemCategoryFour);
        totalData.setProblemCategoryFive(problemCategoryFive);
        totalData.setProblemCategorySix(problemCategorySix);
        list.add(totalData);
        return list;
    }
}

