package com.ruoyi.web.controller.task;


import com.ruoyi.common.core.domain.BaseEntity;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.ProposalInfo;
import com.ruoyi.system.domain.activity.ZxActivity;
import com.ruoyi.system.domain.activity.ZxActivityUser;
import com.ruoyi.system.domain.assessment.ZxAssessment;
import com.ruoyi.system.domain.bonus.ZxBonus;
import com.ruoyi.system.domain.conference.ZxConference;
import com.ruoyi.system.domain.conference.ZxConferenceUser;
import com.ruoyi.system.domain.member.MemberAssessmentInfo;
import com.ruoyi.system.domain.member.MemberInfo;
import com.ruoyi.system.domain.sqmy.SqmyInfo;
import com.ruoyi.system.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

import static com.ruoyi.common.constant.CommonConstants.*;

@Component
public class Task {
    @Autowired
    private MemberInfoMapper memberInfoMapper;
    @Autowired
    private ZxConferenceMapper zxConferenceMapper;
    @Autowired
    private ZxActivityMapper zxActivityMapper;

    @Autowired
    private ProposalInfoMapper proposalInfoMapper;

    @Autowired
    private SqmyInfoMapper sqmyInfoMapper;

    @Autowired
    private SysDictDataMapper dictDataMapper;

    @Autowired
    private ZxAssessmentMapper zxAssessmentMapper;
    @Autowired
    private ZxBonusMapper zxBonusMapper;

    @Autowired
    private MemberAssessmentInfoMapper memberAssessmentInfoMapper;

    /***
     * 生成委员分数排名
     * */
    @Async
    //@Scheduled(cron = "0 19 0 * * ? ")
    public void assessmentScore() {
        System.out.println("开始计算委员分数定时任务");
        //查询的年份
        LocalDate currentDate = LocalDate.now();
        String year = String.valueOf(currentDate.getYear());
        //查询原先是否有数据
        MemberAssessmentInfo memberAssessmentInfo = new MemberAssessmentInfo();
        memberAssessmentInfo.setYear(year);
        List<MemberAssessmentInfo> memberAssessmentInfoList = memberAssessmentInfoMapper.selectMemberAssessmentInfoList(memberAssessmentInfo);
        //查询出所有委员信息
        List<MemberInfo> memberInfoSelect = memberInfoMapper.selectMemberInfoList(new MemberInfo());
        //查询履职考核标准
        List<ZxAssessment> zxAssessments = zxAssessmentMapper.selectZxAssessmentList(new ZxAssessment());
        if (memberInfoSelect != null && memberInfoSelect.size() > 0) {
            List<MemberAssessmentInfo> memberAssessmentInfoListNew = new ArrayList<>();
            for (MemberInfo memberInfo : memberInfoSelect) {
                Long userId = memberInfo.getUserId();
                MemberAssessmentInfo memberAssessmentInf = new MemberAssessmentInfo();
                memberAssessmentInf.setUserId(userId);
                memberAssessmentInf.setYear(year);
                //查询提案情况
                ProposalInfo proposalInfo = new ProposalInfo();
                proposalInfo.setProposalUserId(userId);
                proposalInfo.setYear(year);
                List<ProposalInfo> proposalInfos = proposalInfoMapper.selectProposalInfoListByUser(proposalInfo);
                //查询社情民意
                SqmyInfo sqmyInfo = new SqmyInfo();
                sqmyInfo.setSqmyUserId(userId);
                sqmyInfo.setYear(year);
                List<SqmyInfo> sqmyInfos = sqmyInfoMapper.selectSqmyInfoList(sqmyInfo);
                //查询政协会议
                ZxConference zxConference = new ZxConference();
                zxConference.setUserId(SecurityUtils.getUserId());
                zxConference.setYear(year);
                List<ZxConference> zxConferences = zxConferenceMapper.selectZxConferenceList(zxConference);
                //查询活动
                ZxActivity zxActivity = new ZxActivity();
                zxActivity.setUserId(SecurityUtils.getUserId());
                zxActivity.setYear(year);
                List<ZxActivity> zxActivities = zxActivityMapper.selectZxActivityList(zxActivity);
                //计算得分 勿删
                if (zxAssessments != null && zxAssessments.size() > 0) {
                    //获取分值上限
                    long assessmentTotal = 0L;
                    //定义当前分数
                    long nowScore = 0L;
                    //设置的单项分数
                    long assessmentScore = 0L;
                    //符合条件的项目数量
                    long proposalCount = 0L;
                    //计算提案得分
                    if (proposalInfos != null && proposalInfos.size() > 0) {
                        //获取分值上限
                        assessmentTotal = zxAssessments.stream().filter(e -> "assessment_proposal".equals(e.getAssessmentProjectId()) && 0 == e.getParentId()).collect(Collectors.toList())
                                .get(0).getAssessmentTotal();
                        //先去查询这个人是否有个人提案立案
                        proposalCount = proposalInfos.stream().filter(e -> e.getProposalUserId().equals(userId) && e.getIsJointly().equals(ONE) && !ONE.equals(e.getProposalProgress()) && !THR.equals(e.getProposalProgress())).count();
                        if (proposalCount > 0) {
                            //获取对应类型的分值
                            assessmentScore = zxAssessments.stream().filter(e -> "assessment_proposal".equals(e.getAssessmentProjectId()) && ONE.equals(e.getAssessmentTypeId())).collect(Collectors.toList())
                                    .get(0).getAssessmentScore();
                            //计算当前分数
                            nowScore = nowScore + proposalCount * assessmentScore;
                        }
                        //当前分数小于上限值，继续找下一个条件
                        if (nowScore < assessmentTotal) {
                            //查询这个人是否有个人提案未立案
                            proposalCount = proposalInfos.stream().filter(e -> e.getProposalUserId().equals(userId) && e.getIsJointly().equals(ONE) && (ONE.equals(e.getProposalProgress()) || THR.equals(e.getProposalProgress()))).count();
                            if (proposalCount > 0) {
                                //获取对应类型的分值
                                assessmentScore = zxAssessments.stream().filter(e -> "assessment_proposal".equals(e.getAssessmentProjectId()) && TWO.equals(e.getAssessmentTypeId())).collect(Collectors.toList())
                                        .get(0).getAssessmentScore();
                                //计算当前分数
                                nowScore = nowScore + proposalCount * assessmentScore;
                            }
                        }
                        //当前分数小于上限值，继续找下一个条件
                        if (nowScore < assessmentTotal) {
                            //查询这个人是否有联名提案、集体提案立案的，第一提案人，召集人
                            proposalCount = proposalInfos.stream().filter(e -> e.getProposalUserId().equals(userId) && "0".equals(e.getIsJointly()) && !ONE.equals(e.getProposalProgress()) && !THR.equals(e.getProposalProgress())).count();
                            if (proposalCount > 0) {
                                //获取对应类型的分值
                                assessmentScore = zxAssessments.stream().filter(e -> "assessment_proposal".equals(e.getAssessmentProjectId()) && THR.equals(e.getAssessmentTypeId())).collect(Collectors.toList())
                                        .get(0).getAssessmentScore();
                                //计算当前分数
                                nowScore = nowScore + proposalCount * assessmentScore;
                            }
                        }
                        //当前分数小于上限值，继续找下一个条件
                        if (nowScore < assessmentTotal) {
                            //查询这个人是否有联名提案、集体提案立案的，其余联名、参与者
                            proposalCount = proposalInfos.stream().filter(e -> !e.getProposalUserId().equals(userId) && "0".equals(e.getIsJointly()) && !ONE.equals(e.getProposalProgress()) && !THR.equals(e.getProposalProgress())).count();
                            if (proposalCount > 0) {
                                //获取对应类型的分值
                                assessmentScore = zxAssessments.stream().filter(e -> "assessment_proposal".equals(e.getAssessmentProjectId()) && FOR.equals(e.getAssessmentTypeId())).collect(Collectors.toList())
                                        .get(0).getAssessmentScore();
                                //计算当前分数
                                nowScore = nowScore + proposalCount * assessmentScore;
                            }
                        }
                        //当前分数小于上限值，继续找下一个条件
                        if (nowScore < assessmentTotal) {
                            //查询这个人是否有未立案联名提案、集体提案参与者
                            proposalCount = proposalInfos.stream().filter(e -> !e.getProposalUserId().equals(userId) && "0".equals(e.getIsJointly()) && (ONE.equals(e.getProposalProgress()) || THR.equals(e.getProposalProgress()))).count();
                            if (proposalCount > 0) {
                                //获取对应类型的分值
                                assessmentScore = zxAssessments.stream().filter(e -> "assessment_proposal".equals(e.getAssessmentProjectId()) && FOR.equals(e.getAssessmentTypeId())).collect(Collectors.toList())
                                        .get(0).getAssessmentScore();
                                //计算当前分数
                                nowScore = nowScore + proposalCount * assessmentScore;
                            }
                        }
                        if (nowScore > assessmentTotal) {
                            nowScore = assessmentTotal;
                        }

                        memberAssessmentInf.setProposalScore(nowScore);
                    }
                    //计算社情民义的分值
                    if (sqmyInfos != null && sqmyInfos.size() > 0) {
                        //获取分值上限
                        assessmentTotal = zxAssessments.stream().filter(e -> "proposal_sqmy".equals(e.getAssessmentProjectId()) && 0 == e.getParentId()).collect(Collectors.toList())
                                .get(0).getAssessmentTotal();
                        //定义当前分数
                        nowScore = 0L;
                        //查询这个人撰写并上报社情民意信息，被采用
                        proposalCount = sqmyInfos.stream().filter(e -> e.getSqmyUserId().equals(userId) && "0".equals(e.getIsRecord())).count();
                        if (proposalCount > 0) {
                            //获取对应类型的分值
                            assessmentScore = zxAssessments.stream().filter(e -> "proposal_sqmy".equals(e.getAssessmentProjectId()) && ONE.equals(e.getAssessmentTypeId())).collect(Collectors.toList())
                                    .get(0).getAssessmentScore();
                            //计算当前分数
                            nowScore = nowScore + proposalCount * assessmentScore;
                        }
                        //当前分数小于上限值，继续找下一个条件
                        if (nowScore < assessmentTotal) {
                            //查询这个人撰写并上报社情民意信息，未采用
                            proposalCount = sqmyInfos.stream().filter(e -> e.getSqmyUserId().equals(userId) && ONE.equals(e.getIsRecord())).count();
                            if (proposalCount > 0) {
                                //获取对应类型的分值
                                assessmentScore = zxAssessments.stream().filter(e -> "proposal_sqmy".equals(e.getAssessmentProjectId()) && TWO.equals(e.getAssessmentTypeId())).collect(Collectors.toList())
                                        .get(0).getAssessmentScore();
                                //计算当前分数
                                nowScore = nowScore + proposalCount * assessmentScore;
                            }
                        }
                        if (nowScore > assessmentTotal) {
                            nowScore = assessmentTotal;
                        }

                        memberAssessmentInf.setSqmyScore(nowScore);
                    }
                    //计算会议分值
                    if (zxConferences != null && zxConferences.size() > 0) {
                        //查询会议默认所有人一开始的20分，缺勤扣分
                        //获取分值上限
                        assessmentTotal = zxAssessments.stream().filter(e -> "assessment_conference".equals(e.getAssessmentProjectId()) && 0 == e.getParentId()).collect(Collectors.toList())
                                .get(0).getAssessmentTotal();
                        //定义当前分数
                        nowScore = 0L;
                        //查询这个人是否有全体会议期间，无故缺席大会或分组讨论
                        List<ZxConference> collect = zxConferences.stream().filter(e -> e.getConferenceType().equals(ONE)).collect(Collectors.toList());
                        if (collect.size() > 0) {
                            for (ZxConference conference : collect) {
                                List<ZxConferenceUser> zxConferenceUserList = conference.getZxConferenceUserList();
                                if (zxConferenceUserList != null && zxConferenceUserList.size() > 0) {
                                    //获取到不参加会议的数量
                                    proposalCount = proposalCount + zxConferenceUserList.stream().filter(e -> "N".equals(e.getIsJoin())).count();
                                }
                            }
                            if (proposalCount > 0) {
                                //获取对应类型的分值
                                assessmentScore = zxAssessments.stream().filter(e -> "assessment_conference".equals(e.getAssessmentProjectId()) && ONE.equals(e.getAssessmentTypeId())).collect(Collectors.toList())
                                        .get(0).getAssessmentScore();
                                //计算当前分数
                                nowScore = nowScore + proposalCount * assessmentScore;
                            }
                        }
                        //查询这个人是否无故缺席市政协办公室，各专委会，各界别组织召开的其他会议和学习培训活动
                        List<ZxConference> collectTwo = zxConferences.stream().filter(e -> !e.getConferenceType().equals(ONE)).collect(Collectors.toList());
                        if (collectTwo.size() > 0) {
                            for (ZxConference conference : collectTwo) {
                                List<ZxConferenceUser> zxConferenceUserList = conference.getZxConferenceUserList();
                                if (zxConferenceUserList != null && zxConferenceUserList.size() > 0) {
                                    //获取到不参加会议的数量
                                    proposalCount = proposalCount + zxConferenceUserList.stream().filter(e -> "N".equals(e.getIsJoin())).count();
                                }
                            }
                            if (proposalCount > 0) {
                                //获取对应类型的分值
                                assessmentScore = zxAssessments.stream().filter(e -> "assessment_conference".equals(e.getAssessmentProjectId()) && TWO.equals(e.getAssessmentTypeId())).collect(Collectors.toList())
                                        .get(0).getAssessmentScore();
                                //计算当前分数
                                nowScore = nowScore + proposalCount * assessmentScore;
                            }
                        }
                        //判断分是否扣完
                        nowScore = assessmentTotal + nowScore;
                        if (nowScore < 0) {
                            nowScore = 0;
                        }

                        memberAssessmentInf.setConferenceScore(nowScore);
                    }
                    //计算活动分值
                    if (zxActivities != null && zxActivities.size() > 0) {
                        //定义当前分数
                        nowScore = 0L;
                        //先去查询是否有学习培训活动未参加
                        List<ZxActivity> collect = zxActivities.stream().filter(e -> e.getActivityType().equals(ONE)).collect(Collectors.toList());
                        if (collect.size() > 0) {
                            for (ZxActivity activity : collect) {
                                List<ZxActivityUser> zxActivityUserList = activity.getZxActivityUserList();
                                if (zxActivityUserList != null && zxActivityUserList.size() > 0) {
                                    //获取到不参加会议的数量
                                    proposalCount = proposalCount + zxActivityUserList.stream().filter(e -> "N".equals(e.getIsJoin())).count();
                                }
                            }
                            if (proposalCount > 0) {
                                //获取对应类型的分值
                                assessmentScore = zxAssessments.stream().filter(e -> "assessment_conference".equals(e.getAssessmentProjectId()) && TWO.equals(e.getAssessmentTypeId())).collect(Collectors.toList())
                                        .get(0).getAssessmentScore();
                                //计算当前分数
                                nowScore = nowScore + proposalCount * assessmentScore;
                            }
                            Object sqmyInfoScore = memberAssessmentInf.getSqmyScore();
                            if (Integer.parseInt(sqmyInfoScore.toString()) > 0) {
                                //判断分是否扣完
                                nowScore = assessmentTotal + nowScore;
                                if (nowScore < 0) {
                                    nowScore = 0;
                                }
                                memberAssessmentInf.setSqmyScore(nowScore);
                            }
                        }
                        nowScore = 0L;
                        //获取分值上限
                        assessmentTotal = zxAssessments.stream().filter(e -> "assessment_activity".equals(e.getAssessmentProjectId()) && 0 == e.getParentId()).collect(Collectors.toList())
                                .get(0).getAssessmentTotal();
                        //获取所有活动设置的子类分项
                        List<ZxAssessment> collectZxAssessment = zxAssessments.stream().filter(e -> "assessment_activity".equals(e.getAssessmentProjectId()) && 0 != e.getParentId()).collect(Collectors.toList());
                        if (collectZxAssessment.size() > 0) {
                            for (ZxAssessment zxAssessment : collectZxAssessment) {
                                //获取到的对应活动分数
                                Long assessmentScore1 = zxAssessment.getAssessmentScore();
                                //判断是否参加过对应活动
                                String assessmentTypeId = zxAssessment.getAssessmentTypeId();
                                //获取参与数量
                                proposalCount = zxActivities.stream().filter(e -> e.getActivityType().equals(assessmentTypeId)).count();
                                if (proposalCount > 0) {
                                    nowScore = nowScore + assessmentScore1;
                                }
                            }
                            if (nowScore > assessmentTotal) {
                                nowScore = assessmentTotal;
                            }

                            memberAssessmentInf.setActivityScore(nowScore);
                        }
                    }
                    //计算加分
                    ZxBonus zxBonus = new ZxBonus();
                    zxBonus.setUserId(userId);
                    List<ZxBonus> zxBonuses = zxBonusMapper.selectZxBonusList(zxBonus);
                    if (zxBonuses != null && zxBonuses.size() > 0) {
                        long bonusScore = zxBonuses.stream().filter(e -> e.getScore() != null).mapToLong(BaseEntity::getScore).sum();

                        memberAssessmentInf.setBonusScore(bonusScore);
                    }
                }

                //总分
                long totalScore = memberAssessmentInf.getProposalScore() + memberAssessmentInf.getSqmyScore() + memberAssessmentInf.getConferenceScore() + memberAssessmentInf.getActivityScore() + memberAssessmentInf.getBonusScore();
                memberAssessmentInf.setTotalScore(totalScore);
                memberAssessmentInfoListNew.add(memberAssessmentInf);
            }
            //计算排名
            int index = 0;
            //最近的一次分数
            Long lastScore = 0L;
            List<MemberAssessmentInfo> collect = memberAssessmentInfoListNew.stream().sorted(Comparator.comparing(MemberAssessmentInfo::getTotalScore).reversed()).collect(Collectors.toList());
            for (int i = 0; i < collect.size(); i++) {
                MemberAssessmentInfo memberAssessmentInfoNew = collect.get(i);
                Long totalScore = memberAssessmentInfo.getTotalScore();
                if (!lastScore.equals(totalScore)) {
                    lastScore = totalScore;
                    index++;
                }
                memberAssessmentInfoNew.setRank(String.valueOf(index));
                //判断是更新还是新增
                if (memberAssessmentInfoList != null && memberAssessmentInfoList.size() > 0){
                    //更新
                    memberAssessmentInfoMapper.updateMemberAssessmentInfo(memberAssessmentInfoNew);
                }else {
                    memberAssessmentInfoMapper.insertMemberAssessmentInfo(memberAssessmentInfoNew);
                }
            }
        }
        System.out.println("定时任务结束");
    }
}
