package com.ruoyi.system.service.impl;


import com.ruoyi.common.core.domain.BaseEntity;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.DateUtils;
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.member.MemberInfoTree;
import com.ruoyi.system.domain.speak.ZxSpeak;
import com.ruoyi.system.domain.sqmy.SqmyInfo;
import com.ruoyi.system.domain.vo.MemberInfoVo;
import com.ruoyi.system.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.service.IMemberInfoService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.ruoyi.common.constant.CommonConstants.*;
import static java.math.BigInteger.ZERO;

/**
 * 委员信息Service业务层处理
 *
 * @author boman
 * @date 2024-03-07
 */
@Service
public class MemberInfoServiceImpl implements IMemberInfoService {
    @Autowired
    private MemberInfoMapper memberInfoMapper;
    @Autowired
    private ZxConferenceMapper zxConferenceMapper;
    @Autowired
    private ZxActivityMapper zxActivityMapper;
    @Autowired
    private ZxSpeakMapper zxSpeakMapper;

    @Autowired
    private ProposalInfoMapper proposalInfoMapper;

    @Autowired
    private SqmyInfoMapper sqmyInfoMapper;

    @Autowired
    private SysDictDataMapper dictDataMapper;

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

    @Autowired
    private MemberAssessmentInfoMapper memberAssessmentInfoMapper;


    /**
     * 查询委员信息
     *
     * @param memberId 委员信息主键
     * @return 委员信息
     */
    @Override
    public MemberInfo selectMemberInfoByMemberId(Long memberId) {
        return memberInfoMapper.selectMemberInfoByMemberId(memberId);
    }

    /**
     * 获取委员履职信息详细信息
     */
    @Override
    public MemberInfoVo getInfoJop(MemberInfo memberInfos) {
        MemberInfoVo memberInfoVo = new MemberInfoVo();
        MemberInfo memberInfo = memberInfoMapper.selectMemberInfoByMemberId(memberInfos.getMemberId());
        memberInfoVo.setMemberInfo(memberInfo);
        ZxConference zxConference = new ZxConference();
        zxConference.setUserId(SecurityUtils.getUserId());
        List<ZxConference> zxConferences = zxConferenceMapper.selectZxConferenceList(zxConference);
        if (zxConferences != null) {
            memberInfoVo.setZxConferenceList(zxConferences);
        }
        ZxActivity zxActivity = new ZxActivity();
        zxActivity.setUserId(SecurityUtils.getUserId());
        List<ZxActivity> zxActivities = zxActivityMapper.selectZxActivityList(zxActivity);
        if (zxActivities != null) {
            memberInfoVo.setZxActivityList(zxActivities);
        }
        ZxSpeak zxSpeak = new ZxSpeak();
        zxSpeak.setUserId(SecurityUtils.getUserId());
        List<ZxSpeak> zxSpeaks = zxSpeakMapper.selectZxSpeakList(zxSpeak);
        if (zxSpeaks != null) {
            memberInfoVo.setZxSpeakList(zxSpeaks);
        }
        return memberInfoVo;
    }

    /**
     * 获取委员履职年度报告
     */
    @Override
    public MemberInfoVo getMemberReport(MemberInfo memberInfo) {
        MemberInfoVo memberInfoVo = new MemberInfoVo();
        //查询的年份
        String year = memberInfo.getYear();
        //查询出委员信息
        Long userId = memberInfo.getUserId();
        MemberInfo memberInfoSelect = memberInfoMapper.selectMemberInfoByUserId(memberInfo.getUserId());
        memberInfoVo.setMemberInfo(memberInfoSelect);
        //履职状况初始化
        Map<String, Object> map = new HashMap<>(5);
        //提案
        map.put("proposalInfoNum", 0);
        //社情民意
        map.put("sqmyInfoNum", 0);
        //会议
        map.put("conferenceNum", 0);
        //活动
        map.put("activityNum", 0);
        //发言数量
        map.put("speakNum", 0);
/*        //查询履职考核标准
        List<ZxAssessment> zxAssessments = zxAssessmentMapper.selectZxAssessmentList(new ZxAssessment());*/
        //查询提案情况
        ProposalInfo proposalInfo = new ProposalInfo();
        proposalInfo.setProposalUserId(userId);
        proposalInfo.setYear(year);
        List<ProposalInfo> proposalInfos = proposalInfoMapper.selectProposalInfoListByUser(proposalInfo);
        memberInfoVo.setProposalInfoList(proposalInfos);
        //查询社情民意
        SqmyInfo sqmyInfo = new SqmyInfo();
        sqmyInfo.setSqmyUserId(userId);
        sqmyInfo.setYear(year);
        List<SqmyInfo> sqmyInfos = sqmyInfoMapper.selectSqmyInfoList(sqmyInfo);
        memberInfoVo.setSqmyInfoList(sqmyInfos);
        //查询政协会议
        ZxConference zxConference = new ZxConference();
        zxConference.setUserId(SecurityUtils.getUserId());
        zxConference.setYear(year);
        List<ZxConference> zxConferences = zxConferenceMapper.selectZxConferenceList(zxConference);
        memberInfoVo.setZxConferenceList(zxConferences);
        //查询活动
        ZxActivity zxActivity = new ZxActivity();
        zxActivity.setUserId(SecurityUtils.getUserId());
        zxActivity.setYear(year);
        List<ZxActivity> zxActivities = zxActivityMapper.selectZxActivityList(zxActivity);
        memberInfoVo.setZxActivityList(zxActivities);
        //查询发言不计算分
        ZxSpeak zxSpeak = new ZxSpeak();
        zxSpeak.setUserId(SecurityUtils.getUserId());
        zxSpeak.setYear(year);
        List<ZxSpeak> zxSpeaks = zxSpeakMapper.selectZxSpeakList(zxSpeak);
        memberInfoVo.setZxSpeakList(zxSpeaks);
        //查询委员得分和排名
        MemberAssessmentInfo memberAssessmentInfo = new MemberAssessmentInfo();
        memberAssessmentInfo.setUserId(userId);
        memberAssessmentInfo.setYear(year);
        List<MemberAssessmentInfo> memberAssessmentInfos = memberAssessmentInfoMapper.selectMemberAssessmentInfoList(memberAssessmentInfo);
        if (memberAssessmentInfos != null && memberAssessmentInfos.size() >0){
            MemberAssessmentInfo memberAssessmentInfoNew = memberAssessmentInfos.get(0);
            memberInfoVo.setMemberAssessmentInfo(memberAssessmentInfoNew);
        }
/*        //计算得分 勿删
        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;
                }
                map.put("proposalInfoScore", 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;
                }
                map.put("sqmyInfoScore", 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;
                }
                map.put("sqmyInfoScore", 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 = map.get("sqmyInfoScore");
                    if (Integer.parseInt(sqmyInfoScore.toString()) > 0) {
                        //判断分是否扣完
                        nowScore = assessmentTotal + nowScore;
                        if (nowScore < 0) {
                            nowScore = 0;
                        }
                        map.put("sqmyInfoScore", 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;
                    }
                    map.put("activityScore", nowScore);
                }
            }
            //计算加分
            ZxBonus zxBonus = new ZxBonus();
            zxBonus.setUserId(userId);
            List<ZxBonus> zxBonuses = zxBonusMapper.selectZxBonusList(zxBonus);
            if (zxBonuses != null && zxBonuses.size() > 0){
                long zxBonusNum = zxBonuses.stream().filter(e -> e.getScore() != null).mapToLong(BaseEntity::getScore).sum();
                map.put("zxBonusScore",zxBonusNum);
            }
        }*/
        memberInfoVo.setMap(map);
        return memberInfoVo;
    }

    /**
     * 查询委员信息列表
     *
     * @param memberInfo 委员信息
     * @return 委员信息
     */
    @Override
    public List<MemberInfo> selectMemberInfoList(MemberInfo memberInfo) {
        return memberInfoMapper.selectMemberInfoList(memberInfo);
    }

    /**
     * 获取委员菜单下拉树列表
     */
    @Override
    public List<MemberInfoTree> buildMemberInfoTreeSelect(List<MemberInfo> memberInfo) {
        //查询字典值，政协委员级别
        List<MemberInfoTree> memberInfoTreeList = new ArrayList<>();
        if (memberInfo != null && memberInfo.size() > 0) {
            List<SysDictData> dictDataList = dictDataMapper.selectDictDataByType("grade");
            if (dictDataList != null && dictDataList.size() > 0) {
                for (SysDictData sysDictData : dictDataList) {
                    MemberInfoTree memberInfoTree = new MemberInfoTree();
                    memberInfoTree.setUserId(sysDictData.getDictValue());
                    memberInfoTree.setName(sysDictData.getDictLabel());
                    memberInfoTreeList.add(memberInfoTree);
                }
                for (MemberInfoTree memberInfoTree : memberInfoTreeList) {
                    //委员级别id
                    String id = memberInfoTree.getUserId();
                    List<MemberInfo> memberInfoList = new ArrayList<>();
                    for (MemberInfo info : memberInfo) {
                        if (info.getGrade().contains(id)) {
                            memberInfoList.add(info);
                        }
                    }
                    memberInfoTree.setChildren(memberInfoList);
                }
            }
        }
        return memberInfoTreeList;
    }

    /**
     * 新增委员信息
     *
     * @param memberInfo 委员信息
     * @return 结果
     */
    @Override
    public int insertMemberInfo(MemberInfo memberInfo) {
        memberInfo.setUserId(SecurityUtils.getUserId());
        memberInfo.setCreateTime(DateUtils.getNowDate());
        return memberInfoMapper.insertMemberInfo(memberInfo);
    }

    /**
     * 修改委员信息
     *
     * @param memberInfo 委员信息
     * @return 结果
     */
    @Override
    public int updateMemberInfo(MemberInfo memberInfo) {
        memberInfo.setUpdateTime(DateUtils.getNowDate());
        return memberInfoMapper.updateMemberInfo(memberInfo);
    }

    /**
     * 批量删除委员信息
     *
     * @param memberIds 需要删除的委员信息主键
     * @return 结果
     */
    @Override
    public int deleteMemberInfoByMemberIds(Long[] memberIds) {
        return memberInfoMapper.deleteMemberInfoByMemberIds(memberIds);
    }

    /**
     * 删除委员信息信息
     *
     * @param memberId 委员信息主键
     * @return 结果
     */
    @Override
    public int deleteMemberInfoByMemberId(Long memberId) {
        return memberInfoMapper.deleteMemberInfoByMemberId(memberId);
    }
}
