package com.xinyilu.system.service.testAllStudentScoreinfo.impl;


import com.xinyilu.system.domain.vo.TestItemStudentScore.AllClassByVO;
import com.xinyilu.system.domain.vo.TestItemStudentScore.AllStudentScoreVO;
import com.xinyilu.system.domain.vo.TestItemStudentScore.TestAllStudentScoreVO;
import com.xinyilu.system.domain.vo.TestItemStudentScore.TestAllStudentScoresVO;
import com.xinyilu.system.mapper.testItemScoreinfo.TestItemScoreMapper;
import com.xinyilu.system.service.testAllStudentScoreinfo.ITestAllStudentScoreService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;


/**
 * @author ljh
 * @version 1.0
 * Create by 2022/8/12 19:58
 */
@Service
public class TestAllStudentScoreServiceImpl implements ITestAllStudentScoreService {

    @Resource
    private TestItemScoreMapper testItemScoreMapper;


    /**
     * 获取所有学生的学生姓名以及成绩
     *
     * @param kindergartenId 幼儿园id
     * @param batchCode      批次编号
     * @return com.xinyilu.system.domain.vo.TestItemScoreinfoVO
     **/
    @Override
    public TestAllStudentScoresVO selectAllStudentScoreList(@Param("kindergartenId") Long kindergartenId, @Param("batchCode") String batchCode) {
        TestAllStudentScoresVO testAllStudentScoresVO = new TestAllStudentScoresVO();
        // 全园优秀幼儿排行
        List<TestAllStudentScoreVO> testAllStudentScoreList = testItemScoreMapper.selectAllStudentScoreList(kindergartenId, batchCode);
        //班级综合体测成绩排名
        List<AllStudentScoreVO> allStudentScoreList = testItemScoreMapper.selectAllClassScoreList(kindergartenId, batchCode);
        //班级优秀幼儿榜
        List<AllClassByVO> allClassById = testItemScoreMapper.selectAllClassById(kindergartenId, batchCode);
        // 全园优秀幼儿排行
        List<TestAllStudentScoreVO> testItemStudentList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(testAllStudentScoreList)) {
            Map<Long, List<TestAllStudentScoreVO>> testAllStudentScore = testAllStudentScoreList.stream().filter(Objects::nonNull).filter(student -> Objects.nonNull(student.getStudentId())).collect(Collectors.groupingBy(TestAllStudentScoreVO::getStudentId));
            List<TestAllStudentScoreVO> testAllStudentList = new ArrayList<>();
            testAllStudentScore.forEach((studentId, allClassBy) -> {
                TestAllStudentScoreVO testAllStudentScoreVO = allClassBy.get(0);
                BigDecimal itemFractionSum = allClassBy.stream().filter(Objects::nonNull).filter(score -> Objects.nonNull(score.getItemFraction())).map(TestAllStudentScoreVO::getItemFraction)
                        .reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
                testAllStudentScoreVO.setItemFraction(itemFractionSum);
                testAllStudentList.add(testAllStudentScoreVO);
            });
            List<TestAllStudentScoreVO> testStudentList = testAllStudentList.stream().sorted(Comparator.comparing(TestAllStudentScoreVO::getItemFraction).reversed()).collect(Collectors.toList());
            if (testStudentList.size() > 10) {
                testItemStudentList = testStudentList.stream().filter(Objects::nonNull).collect(Collectors.toList()).subList(0, 10);
            } else {
                testItemStudentList = testStudentList.stream().filter(Objects::nonNull).collect(Collectors.toList());
            }
        }
        //班级优秀幼儿榜
        List<AllClassByVO> allClassByIdList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(allClassById)) {
            Map<Long, List<AllClassByVO>> allStudentIdClass = allClassById.stream().filter(Objects::nonNull).filter(student -> Objects.nonNull(student.getStudentId())).collect(Collectors.groupingBy(AllClassByVO::getStudentId));
            List<AllClassByVO> classByVOList = new ArrayList<>();
            allStudentIdClass.forEach((studentId, allClassBy) -> {
                AllClassByVO allClassByVO = allClassBy.get(0);
                BigDecimal itemFractionSum = allClassBy.stream().filter(Objects::nonNull).filter(score -> Objects.nonNull(score.getItemFraction())).map(AllClassByVO::getItemFraction)
                        .reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
                allClassByVO.setItemFraction(itemFractionSum);
                classByVOList.add(allClassByVO);
            });
            List<AllClassByVO> allClassByReversedList = classByVOList.stream().sorted(Comparator.comparing(AllClassByVO::getItemFraction).reversed()).collect(Collectors.toList());
            List<AllClassByVO> allClassByFirstList = new ArrayList<>();
            Map<Long, List<AllClassByVO>> classIdMap = allClassByReversedList.stream().filter(Objects::nonNull).filter(classId -> Objects.nonNull(classId.getClassId())).collect(Collectors.groupingBy(AllClassByVO::getClassId));
            classIdMap.forEach((classId, classIdList) -> {
                AllClassByVO allClassByFirst = classIdList.get(0);
                allClassByFirstList.add(allClassByFirst);
            });
            allClassByIdList = allClassByFirstList.stream().sorted(Comparator.comparing(AllClassByVO::getItemFraction).reversed()).collect(Collectors.toList());
        }
        //班级综合体测成绩排名
        List<AllStudentScoreVO> allClassByList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(allStudentScoreList)) {
            Map<Long, List<AllStudentScoreVO>> allStudentList = allStudentScoreList.stream().filter(Objects::nonNull).filter(student -> Objects.nonNull(student.getClassId())).collect(Collectors.groupingBy(AllStudentScoreVO::getClassId));
            List<AllStudentScoreVO> allStudentScoreVOList = new ArrayList<>();
            allStudentList.forEach((classId, allClassBy) -> {
                AllStudentScoreVO allClassByVO = allClassBy.get(0);
                BigDecimal itemFractionSum = allClassBy.stream().filter(Objects::nonNull).filter(score -> Objects.nonNull(score.getItemFraction())).map(AllStudentScoreVO::getItemFraction)
                        .reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
                allClassByVO.setItemScoreAll(itemFractionSum);
                allStudentScoreVOList.add(allClassByVO);
            });
            List<AllStudentScoreVO> allStudentScoreVO = allStudentScoreVOList.stream().filter(Objects::nonNull).filter(allClass -> Objects.nonNull(allClass.getClassId())).distinct().collect(Collectors.toList());
            allClassByList = allStudentScoreVO.stream().sorted(Comparator.comparing(AllStudentScoreVO::getItemScoreAll).reversed()).collect(Collectors.toList());
        }
        if (CollectionUtils.isEmpty(testItemStudentList) && CollectionUtils.isEmpty(allClassByList) && CollectionUtils.isEmpty(allClassByIdList)) {
            return testAllStudentScoresVO;
        }
        // 把数据放入到bean中   全园优秀幼儿排行
        testAllStudentScoresVO.setTestAllStudentScoreVOList(testItemStudentList);
        // 把数据放入到bean中  班级综合体测成绩排名
        testAllStudentScoresVO.setAllStudentScoreVOList(allClassByList);
        // 把数据放入到bean中   班级优秀幼儿榜排名
        testAllStudentScoresVO.setAllClassByVOList(allClassByIdList);
        return testAllStudentScoresVO;
    }
}
