package com.xjrsoft.module.evaluate.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.merge.LoopMergeStrategy;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.xjrsoft.common.enums.DeleteMark;
import com.xjrsoft.common.enums.EnabledMark;
import com.xjrsoft.common.model.result.RT;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.module.base.entity.BaseClass;
import com.xjrsoft.module.evaluate.dto.*;
import com.xjrsoft.module.evaluate.entity.*;
import com.xjrsoft.module.evaluate.mapper.*;
import com.xjrsoft.module.evaluate.service.IEvaluateResultService;
import com.xjrsoft.module.evaluate.vo.*;
import com.xjrsoft.module.teacher.entity.XjrUser;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.ByteArrayOutputStream;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @title: 评价结果
 * @Author szs
 * @Date: 2024-01-16
 * @Version 1.0
 */
@Service
@AllArgsConstructor
public class EvaluateResultServiceImpl extends MPJBaseServiceImpl<EvaluateResultMapper, EvaluateResult> implements IEvaluateResultService {

    private EvaluateObjectMapper evaluateObjectMapper;

    private EvaluateResultMapper evaluateResultMapper;

    private EvaluateManageMapper evaluateManageMapper;

    private EvaluateManageItemMapper evaluateManageItemMapper;

    private EvaluateSubmitRecordMapper evaluateSubmitRecordMapper;

    private EvaluateExecuterMapper evaluateExecuterMapper;

    @Override
    public IPage<EvaluateResultIndexPageVo> getIndexPage(EvaluateResultIndexPageDto dto) {
        IPage<EvaluateResultIndexPageVo> page = evaluateResultMapper.getIndexPage(ConventPage.getPage(dto), dto);
        return page;
    }

    @Override
    public IPage<EvaluateResultDetailPageVo> getDetailPage(EvaluateResultDetailPageDto dto) {
        //获取当前评价管理下的所有记录的page
        IPage<EvaluateResultDetailPageVo> page = evaluateResultMapper.getDetailPage(ConventPage.getPage(dto), dto);
        return page;
    }

    @Override
    public EvaluateResultDetailPageDetailVo getDetailPageDetail(EvaluateResultDetailPageDetailDto dto) {
        //根据评价管理主键编号，被评对象id，执行人id获取总得分，被评对象名字，执行人名字,评价管理名字，题目满分
        EvaluateResultDetailPageDetailVo evaluateResultDetailPageDetailVo = evaluateResultMapper.getDetailPageDetail(dto);

        if (evaluateResultDetailPageDetailVo == null) {
            return evaluateResultDetailPageDetailVo;
        }

        //获取list
        evaluateResultDetailPageDetailVo.setEvaluateResultDetailPageDetailList(evaluateResultMapper.getDetailPageDetailList(dto));
        return evaluateResultDetailPageDetailVo;
    }

    @Override
    public IPage<ResultViewingPageVo> pageResultViewing(ResultViewingPageDto dto) {
        List<ResultViewingPageVo> resultData = new ArrayList<>();
        //获取该评价管理下的被评对象-分页
        MPJLambdaWrapper<EvaluateObject> evaluateObjectMPJLambdaWrapper = new MPJLambdaWrapper<>();
        evaluateObjectMPJLambdaWrapper
                .distinct()
                .selectAs(EvaluateObject::getObjectId, ResultViewingPageVo::getObjectId)
                .select("ifnull(t1.name, t2.name) as objectIdCN")
                .leftJoin(XjrUser.class, XjrUser::getId, EvaluateObject::getObjectId)
                .leftJoin(BaseClass.class, BaseClass::getId, EvaluateObject::getObjectId)
                .eq(EvaluateObject::getEvaluateManageId, dto.getEvaluateManageId())
                .eq((dto.getBaseGradeId() != null && dto.getBaseGradeId() > 0), BaseClass::getGradeId, dto.getBaseGradeId())
                .eq((dto.getClassId() != null && dto.getClassId() > 0), BaseClass::getId, dto.getClassId())
                .like((dto.getName() != null && !("").equals(dto.getName())), XjrUser::getName, dto.getName())
                .disableSubLogicDel();
        IPage<ResultViewingPageVo> resultViewingPageVoIPage = evaluateObjectMapper.selectJoinPage(ConventPage.getPage(dto), ResultViewingPageVo.class, evaluateObjectMPJLambdaWrapper);
        //被评对象的id集合
        List<Long> evaluateObjectIdList = new ArrayList<>();
        for (ResultViewingPageVo resultViewingPageVo : resultViewingPageVoIPage.getRecords()) {
            evaluateObjectIdList.add(resultViewingPageVo.getObjectId());
        }

        //获取该评价管理下的所有题目-多个
        MPJLambdaWrapper<EvaluateManageItem> evaluateManageItemMPJLambdaWrapper = new MPJLambdaWrapper<>();
        evaluateManageItemMPJLambdaWrapper
                .eq(EvaluateManageItem::getEvaluateManageId, dto.getEvaluateManageId());
        List<EvaluateManageItem> evaluateManageItemList = evaluateManageItemMapper.selectList(evaluateManageItemMPJLambdaWrapper);
        //题目的id集合
        //将题目处理成Map
        List<Long> evaluateManageItemIdList = new ArrayList<>();
        Map<Long, EvaluateManageItem> evaluateManageItemMap = new HashMap<>();
        for (EvaluateManageItem evaluateManageItem : evaluateManageItemList) {
            evaluateManageItemIdList.add(evaluateManageItem.getId());
            evaluateManageItemMap.put(evaluateManageItem.getId(), evaluateManageItem);
        }

        //获取该评价管理下的被评对象的应打分人数，实际打分人数
        MPJLambdaWrapper<EvaluateExecuter> evaluateExecuterMPJLambdaWrapper = new MPJLambdaWrapper<>();
        evaluateExecuterMPJLambdaWrapper
                .select("count(t.id) as planExecuterNum")
                .selectAs(EvaluateObject::getObjectId, ResultViewingPageVo::getObjectId)
                .selectSum(EvaluateExecuter::getStatus, ResultViewingPageVo::getActualExecuterNum)//这里值得推敲
                .leftJoin(EvaluateObject.class, EvaluateObject::getId, EvaluateExecuter::getEvaluateObjectId)
                .eq(EvaluateExecuter::getEvaluateManageId, dto.getEvaluateManageId())
                .in(EvaluateObject::getObjectId, evaluateObjectIdList)
                .groupBy(EvaluateObject::getObjectId)
                .disableSubLogicDel();
        List<ResultViewingPageVo> planAndActualExecuterNum = evaluateExecuterMapper.selectJoinList(ResultViewingPageVo.class, evaluateExecuterMPJLambdaWrapper);
        //将应打分人数，实际打分人数处理成Map
        Map<Long, ResultViewingPageVo> planAndActualExecuterNumMap = new HashMap<>();
        for (ResultViewingPageVo resultViewingPageVo : planAndActualExecuterNum) {
            planAndActualExecuterNumMap.put(resultViewingPageVo.getObjectId(), resultViewingPageVo);
        }

        Map<Long, List<ResultViewingPageVo>> groupedByObjectIdList = new HashMap<>();
        //获取该评价管理下的所有被评对象的所有题的总得分
        if (evaluateManageItemIdList.size() > 0 && evaluateObjectIdList.size() > 0) {
            List<ResultViewingPageVo> listByObjectIdAndEvaluateItemId =  evaluateResultMapper.listByObjectIdAndEvaluateItemId(evaluateObjectIdList, evaluateManageItemIdList);
            //将所有被评对象的所有题的总得分处理成Map
            groupedByObjectIdList = listByObjectIdAndEvaluateItemId.stream()
                    .collect(Collectors.groupingBy(ResultViewingPageVo::getObjectId));

            /*MPJLambdaWrapper<EvaluateResult> evaluateResultMPJLambdaWrapper = new MPJLambdaWrapper<>();
            evaluateResultMPJLambdaWrapper
                    .selectSum(EvaluateResult::getScore, ResultViewingPageVo::getSumScore)
                    .selectAs(EvaluateObject::getObjectId, ResultViewingPageVo::getObjectId)
                    .selectAs(EvaluateResult::getEvaluateItemId, ResultViewingPageVo::getEvaluateManageItemId)
                    .leftJoin(EvaluateObject.class, EvaluateObject::getId, EvaluateResult::getEvaluatedObjectId)
                    .in(EvaluateObject::getObjectId, evaluateObjectIdList)
                    .in(EvaluateResult::getEvaluateItemId, evaluateManageItemIdList)
                    .groupBy(EvaluateResult::getEvaluateItemId, EvaluateObject::getEvaluateManageId)
                    .disableSubLogicDel();
            ResultViewingPageVo sumScore = evaluateResultMapper.selectJoinOne(ResultViewingPageVo.class, evaluateResultMPJLambdaWrapper);*/
        }
        //遍历分页记录
        for (ResultViewingPageVo resultViewingPageVo : resultViewingPageVoIPage.getRecords()) {
            List<ResultViewingPageVo> resultViewingPageVoList = groupedByObjectIdList.get(resultViewingPageVo.getObjectId());
            if(resultViewingPageVoList != null && resultViewingPageVoList.size() > 0){
                for (ResultViewingPageVo r : resultViewingPageVoList) {
                    resultData.add(new ResultViewingPageVo(){{
                        setObjectId(resultViewingPageVo.getObjectId());
                        setObjectIdCN(resultViewingPageVo.getObjectIdCN());
                        setEvaluateManageItemId(r.getEvaluateManageItemId());
                        setProblem(evaluateManageItemMap.get(r.getEvaluateManageItemId()).getProblem());
                        setTopic(evaluateManageItemMap.get(r.getEvaluateManageItemId()).getTopic());
                        setPlanExecuterNum(planAndActualExecuterNumMap.get(resultViewingPageVo.getObjectId()).getPlanExecuterNum());
                        int actualExecuterNum = planAndActualExecuterNumMap.get(resultViewingPageVo.getObjectId()).getActualExecuterNum();
                        setActualExecuterNum(actualExecuterNum);
                        setSumScore(r.getSumScore()/actualExecuterNum);
                    }});
                }
            }
        }
        resultViewingPageVoIPage.setRecords(resultData);

        /*for (ResultViewingPageVo resultViewingPageVo : resultViewingPageVoIPage.getRecords()) {
            //获取该评价管理下的单个被评对象的应打分人数，实际打分人数
            MPJLambdaWrapper<EvaluateExecuter> evaluateExecuterMPJLambdaWrapper = new MPJLambdaWrapper<>();
            evaluateExecuterMPJLambdaWrapper
                    .select("count(t.id) as planExecuterNum")
                    .selectSum(EvaluateExecuter::getStatus, ResultViewingPageVo::getActualExecuterNum)
                    .leftJoin(EvaluateObject.class, EvaluateObject::getId, EvaluateExecuter::getEvaluateObjectId)
                    .eq(EvaluateExecuter::getEvaluateManageId, dto.getEvaluateManageId())
                    .eq(EvaluateObject::getObjectId, resultViewingPageVo.getObjectId())
                    .disableSubLogicDel();
            ResultViewingPageVo planAndActualExecuterNum = evaluateExecuterMapper.selectJoinOne(ResultViewingPageVo.class, evaluateExecuterMPJLambdaWrapper);

            for (EvaluateManageItem evaluateManageItem : evaluateManageItemList) {
                //获取该评价管理下的单个被评对象的每一道题的总得分
                MPJLambdaWrapper<EvaluateResult> evaluateResultMPJLambdaWrapper = new MPJLambdaWrapper<>();
                evaluateResultMPJLambdaWrapper
                        .selectSum(EvaluateResult::getScore, ResultViewingPageVo::getSumScore)
                        .leftJoin(EvaluateObject.class, EvaluateObject::getId, EvaluateResult::getEvaluatedObjectId)
                        .eq(EvaluateResult::getEvaluateItemId, evaluateManageItem.getId())
                        .eq(EvaluateObject::getObjectId, resultViewingPageVo.getObjectId())
                        .disableSubLogicDel();
                ResultViewingPageVo sumScore = evaluateResultMapper.selectJoinOne(ResultViewingPageVo.class, evaluateResultMPJLambdaWrapper);

                resultData.add(new ResultViewingPageVo() {{
                    setObjectId(resultViewingPageVo.getObjectId());
                    setObjectIdCN(resultViewingPageVo.getObjectIdCN());
                    setEvaluateManageItemId(evaluateManageItem.getId());
                    setProblem(evaluateManageItem.getProblem());
                    setTopic(evaluateManageItem.getTopic());
                    if (planAndActualExecuterNum != null) {
                        setPlanExecuterNum(planAndActualExecuterNum.getPlanExecuterNum());
                        setActualExecuterNum(planAndActualExecuterNum.getActualExecuterNum());
                    }
                    if (sumScore != null) {
                        setSumScore(sumScore.getSumScore());
                    }
                }});
            }
        }*/

        /*for (ResultViewingPageVo resultViewingPageVo : resultViewingPageVoIPage.getRecords()) {
            //添加应打分人数，实际打分人数，综合评分
            MPJLambdaWrapper<EvaluateExecuter> evaluateExecuterPlanExecuterNum = new MPJLambdaWrapper<>();
            evaluateExecuterPlanExecuterNum
                    .selectSum(EvaluateExecuter::getStatus, ResultViewingPageVo::getActualExecuterNum)
                    .selectSum(EvaluateResult::getScore, ResultViewingPageVo::getActualExecuterNum)
                    .leftJoin(EvaluateResult.class, EvaluateResult::getEvaluatedObjectId, EvaluateExecuter::getEvaluateObjectId)
                    .eq(EvaluateExecuter::getEvaluateManageId, dto.getEvaluateManageId())
                    .eq(EvaluateExecuter::getEvaluateObjectId, resultViewingPageVo.getEvaluateObjectId());

            List<ResultViewingPageVo> resultViewingPageVoList = evaluateExecuterMapper.selectJoinList(ResultViewingPageVo.class, evaluateExecuterPlanExecuterNum);
            resultViewingPageVo.setPlanExecuterNum(resultViewingPageVoList.size());

            //查出所有的题目项目
            MPJLambdaWrapper<EvaluateManageItem> evaluateManageItemMPJLambdaWrapper = new MPJLambdaWrapper<>();
            evaluateManageItemMPJLambdaWrapper
                    .selectSum(EvaluateResult::getScore, AllEvaluateManageItemVo::getAllActualScore)
                    .selectAs(EvaluateManageItem::getProblem, AllEvaluateManageItemVo::getProblem)
                    .selectAs(EvaluateManageItem::getTopic, AllEvaluateManageItemVo::getTopic)
                    .leftJoin(EvaluateResult.class, EvaluateResult::getEvaluateItemId, EvaluateManageItem::getId)
                    .eq(EvaluateManageItem::getEvaluateManageId, dto.getEvaluateManageId())
                    .eq(EvaluateResult::getEvaluatedObjectId, resultViewingPageVo.getEvaluateObjectId())
                    .groupBy(EvaluateManageItem::getProblem, EvaluateManageItem::getTopic);

            List<AllEvaluateManageItemVo> allEvaluateManageItemVoList = evaluateManageItemMapper.selectJoinList(AllEvaluateManageItemVo.class, evaluateManageItemMPJLambdaWrapper);

            Map<String, List<AllEvaluateManageItemVo>> groupedAllEvaluateManageItemVo = allEvaluateManageItemVoList.stream()
                    .collect(Collectors.groupingBy(AllEvaluateManageItemVo::getTopic));

            List<AllEvaluateManageItemByTopicVo> allEvaluateManageItemByTopicVoList = new ArrayList<>();
            groupedAllEvaluateManageItemVo.forEach((topic, group) -> {
                allEvaluateManageItemByTopicVoList.add(new AllEvaluateManageItemByTopicVo(){{
                    setTopic(topic);
                    setAllEvaluateManageItemVoList(group);
                    int allTopicScore = 0;
                    for (AllEvaluateManageItemVo allEvaluateManageItemVo : group) {
                        allTopicScore += allEvaluateManageItemVo.getAllActualScore();
                    }
                    setAllTopicScore(allTopicScore);
                }});
            });
            resultViewingPageVo.setAllEvaluateManageItemByTopicVoList(allEvaluateManageItemByTopicVoList);
        }*/
        return resultViewingPageVoIPage;
    }

    @Override
    @Transactional
    public String saveBatch(EvaluateResultSaveDto dto) {
        Date createDate = new Date();

        //获取当前评价管理的相关信息
        MPJLambdaWrapper<EvaluateSubmitRecord> evaluateSubmitRecordMPJLambdaWrapper = new MPJLambdaWrapper<>();
        evaluateSubmitRecordMPJLambdaWrapper
                .selectAs(EvaluateSubmitRecord::getId, MobileResultPageVo::getEvaluateSubmitRecordId)
                .selectAs(EvaluateSubmitRecord::getEvaluateManageId, MobileResultPageVo::getEvaluateManageId)
                .selectAs(EvaluateSubmitRecord::getEvaluatedObjectId, MobileResultPageVo::getEvaluateObjectId)
                .selectAs(EvaluateSubmitRecord::getEvaluateExecuterId, MobileResultPageVo::getEvaluateUserId)
                .selectAs(EvaluateSubmitRecord::getStatus, MobileResultPageVo::getEvaluateStatus)
                .selectAs(EvaluateSubmitRecord::getEvaluatedObject, MobileResultPageVo::getEvaluateType)
                .selectAs(EvaluateManage::getIsAutoCreate, MobileResultPageVo::getIsAutoCreate)
                .leftJoin(EvaluateManage.class, EvaluateManage::getId, EvaluateSubmitRecord::getEvaluateManageId)
                .eq(EvaluateSubmitRecord::getId, dto.getEvaluateSubmitRecordId());
        MobileResultPageVo mobileResultPageVo = evaluateSubmitRecordMapper.selectJoinOne(MobileResultPageVo.class, evaluateSubmitRecordMPJLambdaWrapper);

        if(mobileResultPageVo == null){
            return "该评价已失效！";
        }

        if(mobileResultPageVo.getEvaluateStatus() == 2){
            return "已经评价，请勿重复评价";
        }

        //评价结果保存
        List<AddEvaluateResultDto> resultList = dto.getResultList();
        for (AddEvaluateResultDto addEvaluateResultDto : resultList) {
            EvaluateResult evaluateResult = BeanUtil.toBean(addEvaluateResultDto, EvaluateResult.class);
            evaluateResult.setEvaluatedObject(mobileResultPageVo.getEvaluateType());
            evaluateResult.setUserId(Long.parseLong(mobileResultPageVo.getEvaluateUserId()));
            evaluateResult.setEvaluatedObjectId(Long.parseLong(mobileResultPageVo.getEvaluateObjectId()));
            evaluateResult.setDeleteMark(DeleteMark.NODELETE.getCode());
            evaluateResult.setEnabledMark(EnabledMark.ENABLED.getCode());
            evaluateResult.setCreateDate(createDate);
            evaluateResult.setSubmitRecordId(mobileResultPageVo.getEvaluateSubmitRecordId());
            evaluateResultMapper.insert(evaluateResult);
        }

        //修改答题人状态
        evaluateExecuterMapper.updateById(new EvaluateExecuter(){{
            setId(Long.parseLong(mobileResultPageVo.getEvaluateUserId()));
            if(dto.getSubmitType() != null && dto.getSubmitType() == 1){
                setStatus(1);
            } else {
                setStatus(2);
            }
        }});

        //修改提交记录状态
        evaluateSubmitRecordMapper.updateById(new EvaluateSubmitRecord(){{
            setId(mobileResultPageVo.getEvaluateSubmitRecordId());
            if(dto.getSubmitType() != null && dto.getSubmitType() == 1){
                setStatus(1);
            } else {
                setStatus(2);
            }
        }});

        return "ok";
    }

    @Override
    public ResponseEntity<byte[]> getList(ResultViewingExcelDto dto) {
        List<ResultViewingExcelVo> customerList = new ArrayList<>();
        //获取该评价管理下的被评对象-分页
        MPJLambdaWrapper<EvaluateObject> evaluateObjectMPJLambdaWrapper = new MPJLambdaWrapper<>();
        evaluateObjectMPJLambdaWrapper
                .distinct()
                .selectAs(EvaluateObject::getObjectId, ResultViewingPageVo::getObjectId)
                .select("ifnull(t1.name, t2.name) as objectIdCN")
                .leftJoin(XjrUser.class, XjrUser::getId, EvaluateObject::getObjectId)
                .leftJoin(BaseClass.class, BaseClass::getId, EvaluateObject::getObjectId)
                .eq(EvaluateObject::getEvaluateManageId, dto.getEvaluateManageId())
                .disableSubLogicDel();
        List<ResultViewingPageVo> resultViewingPageVoList = evaluateObjectMapper.selectJoinList(ResultViewingPageVo.class, evaluateObjectMPJLambdaWrapper);
        //被评对象的id集合
        List<Long> evaluateObjectIdList = new ArrayList<>();
        for (ResultViewingPageVo resultViewingPageVo : resultViewingPageVoList) {
            evaluateObjectIdList.add(resultViewingPageVo.getObjectId());
        }

        //获取该评价管理下的所有题目-多个
        MPJLambdaWrapper<EvaluateManageItem> evaluateManageItemMPJLambdaWrapper = new MPJLambdaWrapper<>();
        evaluateManageItemMPJLambdaWrapper
                .eq(EvaluateManageItem::getEvaluateManageId, dto.getEvaluateManageId());
        List<EvaluateManageItem> evaluateManageItemList = evaluateManageItemMapper.selectList(evaluateManageItemMPJLambdaWrapper);
        //题目的id集合
        //将题目处理成Map
        List<Long> evaluateManageItemIdList = new ArrayList<>();
        Map<Long, EvaluateManageItem> evaluateManageItemMap = new HashMap<>();
        for (EvaluateManageItem evaluateManageItem : evaluateManageItemList) {
            evaluateManageItemIdList.add(evaluateManageItem.getId());
            evaluateManageItemMap.put(evaluateManageItem.getId(), evaluateManageItem);
        }
        //题目的总数
        int evaluateManageItemNum = evaluateManageItemList.size();

        //获取该评价管理下的被评对象的应打分人数，实际打分人数
        MPJLambdaWrapper<EvaluateExecuter> evaluateExecuterMPJLambdaWrapper = new MPJLambdaWrapper<>();
        evaluateExecuterMPJLambdaWrapper
                .select("count(t.id) as planExecuterNum")
                .selectAs(EvaluateObject::getObjectId, ResultViewingPageVo::getObjectId)
                .selectSum(EvaluateExecuter::getStatus, ResultViewingPageVo::getActualExecuterNum)//这里值得推敲
                .leftJoin(EvaluateObject.class, EvaluateObject::getId, EvaluateExecuter::getEvaluateObjectId)
                .eq(EvaluateExecuter::getEvaluateManageId, dto.getEvaluateManageId())
                .in(EvaluateObject::getObjectId, evaluateObjectIdList)
                .groupBy(EvaluateObject::getObjectId)
                .disableSubLogicDel();
        List<ResultViewingPageVo> planAndActualExecuterNum = evaluateExecuterMapper.selectJoinList(ResultViewingPageVo.class, evaluateExecuterMPJLambdaWrapper);
        //将应打分人数，实际打分人数处理成Map
        Map<Long, ResultViewingPageVo> planAndActualExecuterNumMap = new HashMap<>();
        for (ResultViewingPageVo resultViewingPageVo : planAndActualExecuterNum) {
            planAndActualExecuterNumMap.put(resultViewingPageVo.getObjectId(), resultViewingPageVo);
        }

        Map<Long, List<ResultViewingPageVo>> sumScoreMap = new HashMap<>();
        Map<Long, List<ResultViewingPageVo>> topicScoreMap = new HashMap<>();
        Map<Long, Integer> synthesisScoreMap = new HashMap<>();
        if (evaluateManageItemIdList.size() > 0 && evaluateObjectIdList.size() > 0) {
            //获取该评价管理下的所有被评对象的所有题的总得分
            List<ResultViewingPageVo> listByObjectIdAndEvaluateItemId = evaluateResultMapper.listByObjectIdAndEvaluateItemId(evaluateObjectIdList, evaluateManageItemIdList);
            //将所有被评对象的所有题的总得分处理成Map
            sumScoreMap = listByObjectIdAndEvaluateItemId.stream()
                    .collect(Collectors.groupingBy(ResultViewingPageVo::getObjectId));

            /*//获取该评价管理下的所有被评对象的标题的总得分
            List<ResultViewingPageVo> listTopicScoreByObjectIdAndTopic = evaluateResultMapper.listTopicScoreByObjectIdAndTopic(evaluateObjectIdList, evaluateManageItemIdList);
            //将所有被评对象的标题的总得分处理成Map
            topicScoreMap = listTopicScoreByObjectIdAndTopic.stream()
                    .collect(Collectors.groupingBy(ResultViewingPageVo::getObjectId));*/

            //获取该评价管理下的所有被评对象的总得分
            List<ResultViewingPageVo> listSynthesisScore = evaluateResultMapper.listSynthesisScore(evaluateObjectIdList, evaluateManageItemIdList);
            //将所有被评对象的总得分处理成Map
            for (ResultViewingPageVo r : listSynthesisScore) {
                synthesisScoreMap.put(r.getObjectId(), r.getSynthesisScore());
            }

            /*MPJLambdaWrapper<EvaluateResult> evaluateResultMPJLambdaWrapper = new MPJLambdaWrapper<>();
            evaluateResultMPJLambdaWrapper
                    .selectSum(EvaluateResult::getScore, ResultViewingPageVo::getSumScore)
                    .selectAs(EvaluateObject::getObjectId, ResultViewingPageVo::getObjectId)
                    .selectAs(EvaluateResult::getEvaluateItemId, ResultViewingPageVo::getEvaluateManageItemId)
                    .leftJoin(EvaluateObject.class, EvaluateObject::getId, EvaluateResult::getEvaluatedObjectId)
                    .in(EvaluateObject::getObjectId, evaluateObjectIdList)
                    .in(EvaluateResult::getEvaluateItemId, evaluateManageItemIdList)
                    .groupBy(EvaluateResult::getEvaluateItemId, EvaluateObject::getEvaluateManageId)
                    .disableSubLogicDel();
            ResultViewingPageVo sumScore = evaluateResultMapper.selectJoinOne(ResultViewingPageVo.class, evaluateResultMPJLambdaWrapper);*/
        }
        //遍历分页记录
        for (ResultViewingPageVo resultViewingPageVo : resultViewingPageVoList) {
            List<ResultViewingPageVo> sumScoreList = sumScoreMap.get(resultViewingPageVo.getObjectId());
            if(sumScoreList != null && sumScoreList.size() > 0){
                for (ResultViewingPageVo r : sumScoreList) {
                    customerList.add(new ResultViewingExcelVo(){{
                        setObjectId(resultViewingPageVo.getObjectId());
                        setObjectIdCN(resultViewingPageVo.getObjectIdCN());
                        setEvaluateManageItemId(r.getEvaluateManageItemId());
                        setProblem(evaluateManageItemMap.get(r.getEvaluateManageItemId()).getProblem());
                        setTopic(evaluateManageItemMap.get(r.getEvaluateManageItemId()).getTopic());
                        setPlanExecuterNum(planAndActualExecuterNumMap.get(resultViewingPageVo.getObjectId()).getPlanExecuterNum());
                        int actualExecuterNum = planAndActualExecuterNumMap.get(resultViewingPageVo.getObjectId()).getActualExecuterNum();
                        setActualExecuterNum(actualExecuterNum);
                        setSumScore(r.getSumScore()/actualExecuterNum);
                        setSynthesisScore(synthesisScoreMap.get(resultViewingPageVo.getObjectId())/actualExecuterNum);
                    }});
                }
            }
        }
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
//        for (TextbookIssueRecordPageVo textbookIssueRecordPageVo : customerList) {
//            if(textbookIssueRecordPageVo.getCreateDate() == null){
//                continue;
//            }
//            textbookIssueRecordPageVo.setCreateDateStr(sdf.format(textbookIssueRecordPageVo.getCreateDate()));
//            dataList.add(BeanUtil.toBean(textbookIssueRecordPageVo, TextbookIssueRecordExcelVo.class));
//        }


        //返回流
        ByteArrayOutputStream bot = new ByteArrayOutputStream();

        // 设置排除的属性 也可以在数据模型的字段上加@ExcelIgnore注解排除
        /*Set<String> excludeField = new HashSet<>();
        excludeField.add("hireDate");
        excludeField.add("salary");
        // 写Excel
        EasyExcel.write(filename, User.class)
            .excludeColumnFiledNames(excludeField)
            .sheet("用户信息")
            .doWrite(getUserData());*/

        // 方法2 自定义合并单元格策略
        String fileName = "ResultViewing" + ExcelTypeEnum.XLSX.getValue();
        // 每隔2行会合并 把eachColumn 设置成 3 也就是我们数据的长度，所以就第一列会合并。当然其他合并策略也可以自己写
        LoopMergeStrategy loopMergeStrategy = new LoopMergeStrategy(evaluateManageItemNum, 0);
        LoopMergeStrategy loopMergeStrategy1 = new LoopMergeStrategy(evaluateManageItemNum, 1);
        LoopMergeStrategy loopMergeStrategy2 = new LoopMergeStrategy(evaluateManageItemNum, 2);
        LoopMergeStrategy loopMergeStrategy3 = new LoopMergeStrategy(evaluateManageItemNum, 5);

        EasyExcel.write(bot, ResultViewingExcelVo.class)
                .automaticMergeHead(false)
                .excelType(ExcelTypeEnum.XLSX)
                .registerWriteHandler(loopMergeStrategy)
                .registerWriteHandler(loopMergeStrategy1)
                .registerWriteHandler(loopMergeStrategy2)
                .registerWriteHandler(loopMergeStrategy3)
                .sheet()
                .doWrite(customerList);
        return RT.fileStream(bot.toByteArray(), fileName);
    }
}
