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

import com.xinyilu.common.constant.Constants;
import com.xinyilu.common.enums.ItemCodeEnums;
import com.xinyilu.common.enums.ScoreExportEnum;
import com.xinyilu.common.enums.StudentSexEnum;
import com.xinyilu.common.enums.TestItemCategoryEnum;
import com.xinyilu.common.utils.AgeUtil;
import com.xinyilu.system.domain.dto.ImportTestProjectScoresDTO;
import com.xinyilu.system.domain.nationalphysiquegrade.NationalPhysiqueGrade;
import com.xinyilu.system.domain.vo.TestItemScoreUpdateVO;
import com.xinyilu.system.domain.vo.scoreExport.ScoreExportVO;
import com.xinyilu.system.mapper.studentInfo.StudentInfoMapper;
import com.xinyilu.system.mapper.testItemScoreinfo.TestItemScoreMapper;
import com.xinyilu.system.service.scoreExport.PersonalScoreExportService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

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

/**
 * 体侧成绩数据成绩导出
 *
 * @author zh
 * @create 2023/8/8 0008
 */
@Service
public class PersonalScoreExportServiceImpl implements PersonalScoreExportService {

    @Resource
    private TestItemScoreMapper testItemScoreMapper;
    @Resource
    private StudentInfoMapper studentInfoMapper;

    /**
     * 个人成绩导出集合查看
     *
     * @param scoreExport 幼儿园批次信息
     * @return 结果
     */
    @Override
    public List<ScoreExportVO> list(ImportTestProjectScoresDTO scoreExport) {
        if (Objects.isNull(scoreExport) || StringUtils.isBlank(scoreExport.getBatchCode()) || Objects.isNull(scoreExport.getKindergartenId()) || Objects.isNull(scoreExport.getClassId())) {
            return new ArrayList<>();
        }
        //查询学生信息
        List<ScoreExportVO> students = studentInfoMapper.selectStudentByKindergartenId(scoreExport);
        if (CollectionUtils.isEmpty(students)) {
            return new ArrayList<>();
        }
        List<Long> studentIds = students.stream().filter(Objects::nonNull).map(ScoreExportVO::getStudentId).distinct().collect(Collectors.toList());
        if (CollectionUtils.isEmpty(studentIds)) {
            return new ArrayList<>();
        }
        //查询学生成绩信息
        List<TestItemScoreUpdateVO> testItemScores = testItemScoreMapper.selectTestItemScoresByStudentIds(studentIds, (Objects.isNull(scoreExport.getClassId()) ? null : scoreExport.getClassId()), scoreExport.getBatchCode());
        if (CollectionUtils.isEmpty(testItemScores)) {
            return students;
        }
        Map<Long, List<TestItemScoreUpdateVO>> testItemScoresMap = testItemScores.stream().filter(item -> Objects.nonNull(item.getStudentId())).collect(Collectors.groupingBy(TestItemScoreUpdateVO::getStudentId));
        int i = 0;
        //查询等级信息
        List<NationalPhysiqueGrade> nationalPhysiqueGradeList = testItemScoreMapper.getNationalPhysiqueGradeList();
        for (ScoreExportVO studentInfo : students) {
            if (StudentSexEnum.MALE.getInfo().equals(studentInfo.getSex())) {
                studentInfo.setSex(Constants.MALE_NAME);
            } else {
                studentInfo.setSex(Constants.FEMALE_NAME);
            }
            List<TestItemScoreUpdateVO> testItemScoreList = testItemScoresMap.getOrDefault(Objects.isNull(studentInfo.getStudentId()) ? 0 : studentInfo.getStudentId(), Lists.newArrayList());
            i++;
            studentInfo.setSerial(i);
            //学生年龄
            if (Objects.nonNull(studentInfo.getBirthDate())) {
                studentInfo.setAge(BigDecimal.valueOf(AgeUtil.getStudentAgeMonth(studentInfo.getBirthDate().toString())));
            }
            //遍历项目塞值
            project(studentInfo, testItemScoreList);
            //二级分类、三级分类计算
            classificationCalculate(studentInfo, testItemScoreList, nationalPhysiqueGradeList);
        }
        return students;
    }

    /**
     * 个人成绩导出
     *
     * @param scoreExport 成绩信息
     * @return ScoreExportVO
     */
    @Override
    public List<ScoreExportVO> export(ImportTestProjectScoresDTO scoreExport) {
        if (Objects.isNull(scoreExport) || StringUtils.isBlank(scoreExport.getBatchCode()) || Objects.isNull(scoreExport.getKindergartenId()) || Objects.isNull(scoreExport.getClassId())) {
            return new ArrayList<>();
        }
        List<ScoreExportVO> students = studentInfoMapper.selectStudentByKindergartenId(scoreExport);
        if (CollectionUtils.isEmpty(students)) {
            return new ArrayList<>();
        }
        List<Long> studentIds = students.stream().filter(Objects::nonNull).map(ScoreExportVO::getStudentId).distinct().collect(Collectors.toList());
        if (CollectionUtils.isEmpty(studentIds)) {
            return new ArrayList<>();
        }
        List<TestItemScoreUpdateVO> testItemScores = testItemScoreMapper.selectTestItemScoresByStudentIds(studentIds, (Objects.isNull(scoreExport.getClassId()) ? null : scoreExport.getClassId()), scoreExport.getBatchCode());
        if (CollectionUtils.isEmpty(testItemScores)) {
            return students;
        }
        int i = 0;
        List<NationalPhysiqueGrade> nationalPhysiqueGradeList = testItemScoreMapper.getNationalPhysiqueGradeList();
        Map<Long, List<TestItemScoreUpdateVO>> testItemScoresMap = testItemScores.stream().filter(item -> Objects.nonNull(item.getStudentId())).collect(Collectors.groupingBy(TestItemScoreUpdateVO::getStudentId));
        for (ScoreExportVO student : students) {
            if (StudentSexEnum.MALE.getInfo().equals(student.getSex())) {
                student.setSex(Constants.MALE_NAME);
            } else {
                student.setSex(Constants.FEMALE_NAME);
            }
            i++;
            student.setSerial(i);
            //学生年龄
            if (Objects.nonNull(student.getBirthDate())) {
                student.setAge(BigDecimal.valueOf(AgeUtil.getStudentAgeMonth(student.getBirthDate().toString())));
            }
            List<TestItemScoreUpdateVO> testItemScoreList = testItemScoresMap.getOrDefault(Objects.isNull(student.getStudentId()) ? 0 : student.getStudentId(), Lists.newArrayList());
            //遍历项目塞值
            project(student, testItemScoreList);
            //二级分类、三级分类计算
            classificationCalculate(student, testItemScoreList, nationalPhysiqueGradeList);
        }
        ScoreExportVO maleScore = new ScoreExportVO();
        ScoreExportVO femaleScore = new ScoreExportVO();
        ScoreExportVO allScore = new ScoreExportVO();
        Map<String, List<ScoreExportVO>> sexMap = students.stream().filter(item -> StringUtils.isNotBlank(item.getSex())).collect(Collectors.groupingBy(ScoreExportVO::getSex));
        List<ScoreExportVO> maleList = sexMap.getOrDefault(Constants.MALE_NAME, Lists.newArrayList());
        BigDecimal maleNumber = ((new BigDecimal(maleList.size())).compareTo(BigDecimal.ZERO) > 0) ? (new BigDecimal(maleList.size())) : BigDecimal.ONE;
        List<ScoreExportVO> femaleList = sexMap.getOrDefault(Constants.FEMALE_NAME, Lists.newArrayList());
        BigDecimal femaleNumber = ((new BigDecimal(femaleList.size())).compareTo(BigDecimal.ZERO) > 0) ? (new BigDecimal(femaleList.size())) : BigDecimal.ONE;
        BigDecimal allNumber = ((new BigDecimal(students.size())).compareTo(BigDecimal.ZERO) > 0) ? (new BigDecimal(students.size())) : BigDecimal.ONE;
        List<ScoreExportVO> allList = new ArrayList<>(students);
        //平均分计算
        i++;
        arithmeticMean(students, i, maleScore, maleList, maleNumber, StudentSexEnum.MALE.getInfo());
        i++;
        arithmeticMean(students, i, femaleScore, femaleList, femaleNumber, StudentSexEnum.FEMALE.getInfo());
        i++;
        arithmeticMean(students, i, allScore, allList, allNumber, "all");
        return students;
    }

    /**
     * 遍历项目塞值
     *
     * @param student           学生信息
     * @param testItemScoreList 项目信息
     */
    private void project(ScoreExportVO student, List<TestItemScoreUpdateVO> testItemScoreList) {
        for (TestItemScoreUpdateVO testItemScore : testItemScoreList) {
            if (ScoreExportEnum.HEIGHT_CODE.getInfo().equals(testItemScore.getItemCode())) {
                student.setHeightScore(testItemScore.getItemScore());
                student.setHeightFraction(testItemScore.getItemFraction());
            }
            if (ScoreExportEnum.WEIGHT_CODE.getInfo().equals(testItemScore.getItemCode())) {
                student.setWeightScore(testItemScore.getItemScore());
                student.setWeightFraction(testItemScore.getItemFraction());
            }
            if (ScoreExportEnum.BMI_CODE.getInfo().equals(testItemScore.getItemCode())) {
                student.setBmiScore(testItemScore.getItemScore());
                student.setBmiFraction(testItemScore.getItemFraction());
            }
            if (ScoreExportEnum.LONG_JUMP_CODE.getInfo().equals(testItemScore.getItemCode())) {
                student.setLongJumpScore(testItemScore.getItemScore());
                student.setLongJumpFraction(testItemScore.getItemFraction());
            }
            if (ScoreExportEnum.CONTINUOUS_JUMP_CODE.getInfo().equals(testItemScore.getItemCode())) {
                student.setContinuousJumpScore(testItemScore.getItemScore());
                student.setContinuousJumpFraction(testItemScore.getItemFraction());
            }
            if (ScoreExportEnum.TENNIS_CODE.getInfo().equals(testItemScore.getItemCode())) {
                student.setTennisScore(testItemScore.getItemScore());
                student.setTennisFraction(testItemScore.getItemFraction());
            }
            if (ScoreExportEnum.BALANCE_BEAM_CODE.getInfo().equals(testItemScore.getItemCode())) {
                student.setBalanceBeamScore(testItemScore.getItemScore());
                student.setBalanceBeamFraction(testItemScore.getItemFraction());
            }
            if (ScoreExportEnum.SIT_AND_REACH_CODE.getInfo().equals(testItemScore.getItemCode())) {
                student.setSitAndReachScore(testItemScore.getItemScore());
                student.setSitAndReachFraction(testItemScore.getItemFraction());
            }
            if (ScoreExportEnum.SHUTTLE_RUN_CODE.getInfo().equals(testItemScore.getItemCode())) {
                student.setShuttleRunScore(testItemScore.getItemScore());
                student.setShuttleRunFraction(testItemScore.getItemFraction());
            }
            if (ScoreExportEnum.T_RUN_CODE.getInfo().equals(testItemScore.getItemCode())) {
                student.setTrunScore(testItemScore.getItemFraction());
            }
            if (ScoreExportEnum.T_LONG_JUMP_CODE.getInfo().equals(testItemScore.getItemCode())) {
                student.setTlongJumpScore(testItemScore.getItemFraction());
            }
            if (ScoreExportEnum.T_HOP_CODE.getInfo().equals(testItemScore.getItemCode())) {
                student.setThopScore(testItemScore.getItemFraction());
            }
            if (ScoreExportEnum.T_FORWARD_JUMP_CODE.getInfo().equals(testItemScore.getItemCode())) {
                student.setTforwardJumpScore(testItemScore.getItemFraction());
            }
            if (ScoreExportEnum.T_FORWARD_SLIDING_STEP_CODE.getInfo().equals(testItemScore.getItemCode())) {
                student.setTforwardSlidingStepScore(testItemScore.getItemFraction());
            }
            if (ScoreExportEnum.T_SIDE_STEP_CODE.getInfo().equals(testItemScore.getItemCode())) {
                student.setTsideStepScore(testItemScore.getItemFraction());
            }
            if (ScoreExportEnum.T_RACKET_BALL_CODE.getInfo().equals(testItemScore.getItemCode())) {
                student.setTracketBallScore(testItemScore.getItemFraction());
            }
            if (ScoreExportEnum.T_PLAY_FOOTBALL_CODE.getInfo().equals(testItemScore.getItemCode())) {
                student.setTplayFootballScore(testItemScore.getItemFraction());
            }
            if (ScoreExportEnum.T_TWO_HANDED_CATCH_CODE.getInfo().equals(testItemScore.getItemCode())) {
                student.setTtwoHandedCatchScore(testItemScore.getItemFraction());
            }
            if (ScoreExportEnum.T_FIXED_BALL_CODE.getInfo().equals(testItemScore.getItemCode())) {
                student.setTfixedBallScore(testItemScore.getItemFraction());
            }
            if (ScoreExportEnum.T_OVERHAND_PITCHING_CODE.getInfo().equals(testItemScore.getItemCode())) {
                student.setToverhandPitchingScore(testItemScore.getItemFraction());
            }
            if (ScoreExportEnum.T_GROUNDER_CODE.getInfo().equals(testItemScore.getItemCode())) {
                student.setTgrounderScore(testItemScore.getItemFraction());
            }
        }
    }

    /**
     * 分类计算
     *
     * @param student           学生信息
     * @param testItemScoreList 项目信息
     */
    private void classificationCalculate(ScoreExportVO student, List<TestItemScoreUpdateVO> testItemScoreList, List<NationalPhysiqueGrade> nationalPhysiqueGradeList) {
        Map<String, List<TestItemScoreUpdateVO>> categoryNameMap = testItemScoreList.stream().filter(item -> Objects.nonNull(item.getCategoryName())).collect(Collectors.groupingBy(TestItemScoreUpdateVO::getCategoryName));
        List<TestItemScoreUpdateVO> coreList = categoryNameMap.getOrDefault(TestItemCategoryEnum.CORE_CODE.getInfo(), Lists.newArrayList());
        BigDecimal coreScore = coreList.stream().filter(Objects::nonNull).map(TestItemScoreUpdateVO::getItemFraction).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
        List<TestItemScoreUpdateVO> agilityList = categoryNameMap.getOrDefault(TestItemCategoryEnum.AGILITY_CODE.getInfo(), Lists.newArrayList());
        BigDecimal agilityScore = agilityList.stream().filter(Objects::nonNull).map(TestItemScoreUpdateVO::getItemFraction).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
        List<TestItemScoreUpdateVO> balancedQualityList = categoryNameMap.getOrDefault(TestItemCategoryEnum.BALANCED_QUALITY_CODE.getInfo(), Lists.newArrayList());
        BigDecimal balancedQualityScore = balancedQualityList.stream().filter(Objects::nonNull).map(TestItemScoreUpdateVO::getItemFraction).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
        List<TestItemScoreUpdateVO> coordinationQualityList = categoryNameMap.getOrDefault(TestItemCategoryEnum.COORDINATION_QUALITY_CODE.getInfo(), Lists.newArrayList());
        BigDecimal coordinationQualityScore = coordinationQualityList.stream().filter(Objects::nonNull).map(TestItemScoreUpdateVO::getItemFraction).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
        List<TestItemScoreUpdateVO> explosiveQualityList = categoryNameMap.getOrDefault(TestItemCategoryEnum.EXPLOSIVE_QUALITY_CODE.getInfo(), Lists.newArrayList());
        BigDecimal explosiveQualityScore = explosiveQualityList.stream().filter(Objects::nonNull).map(TestItemScoreUpdateVO::getItemFraction).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
        List<TestItemScoreUpdateVO> flexibilityList = categoryNameMap.getOrDefault(TestItemCategoryEnum.FLEXIBILITY_CODE.getInfo(), Lists.newArrayList());
        BigDecimal flexibilityScore = flexibilityList.stream().filter(Objects::nonNull).map(TestItemScoreUpdateVO::getItemFraction).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
        List<TestItemScoreUpdateVO> lowerLimbFitnessList = categoryNameMap.getOrDefault(TestItemCategoryEnum.LOWER_LIMB_FITNESS_CODE.getInfo(), Lists.newArrayList());
        BigDecimal lowerLimbFitnessScore = lowerLimbFitnessList.stream().filter(Objects::nonNull).map(TestItemScoreUpdateVO::getItemFraction).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
        List<TestItemScoreUpdateVO> upperLimbFitnessList = categoryNameMap.getOrDefault(TestItemCategoryEnum.UPPER_LIMB_FITNESS_CODE.getInfo(), Lists.newArrayList());
        BigDecimal upperLimbFitnessScore = upperLimbFitnessList.stream().filter(Objects::nonNull).map(TestItemScoreUpdateVO::getItemFraction).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
        List<TestItemScoreUpdateVO> bodyShapeCodeList = categoryNameMap.getOrDefault(TestItemCategoryEnum.BODY_SHAPE_CODE.getInfo(), Lists.newArrayList());
        bodyShapeCodeList.removeAll(bodyShapeCodeList.stream().filter(Objects::nonNull).filter(item -> StringUtils.isNotBlank(item.getItemCode()) && ItemCodeEnums.BMI_CODE.getInfo().equals(item.getItemCode())).collect(Collectors.toList()));
        BigDecimal bodyShapeCodeScore = bodyShapeCodeList.stream().filter(Objects::nonNull).map(TestItemScoreUpdateVO::getItemFraction).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
        List<TestItemScoreUpdateVO> controlMotionList = categoryNameMap.getOrDefault(TestItemCategoryEnum.CONTROL_MOTION_CODE.getInfo(), Lists.newArrayList());
        BigDecimal controlMotionScore = controlMotionList.stream().filter(Objects::nonNull).map(TestItemScoreUpdateVO::getItemFraction).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
        List<TestItemScoreUpdateVO> displacementMotionList = categoryNameMap.getOrDefault(TestItemCategoryEnum.DISPLACEMENT_MOTION_CODE.getInfo(), Lists.newArrayList());
        BigDecimal displacementMotionScore = displacementMotionList.stream().filter(Objects::nonNull).map(TestItemScoreUpdateVO::getItemFraction).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
        student.setCoreScore(coreScore);
        student.setAgilityScore(agilityScore);
        student.setBalancedQualityScore(balancedQualityScore);
        student.setCoordinationQualityScore(coordinationQualityScore);
        student.setExplosiveQualityScore(explosiveQualityScore);
        student.setFlexibilityScore(flexibilityScore);
        student.setLowerLimbFitnessScore(lowerLimbFitnessScore);
        student.setUpperLimbFitnessScore(upperLimbFitnessScore);
        student.setBodyShapeScore(bodyShapeCodeScore);
        student.setControlMotionScore(controlMotionScore);
        student.setDisplacementMotionScore(displacementMotionScore);
        List<TestItemScoreUpdateVO> distinctTestItemScore = testItemScoreList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(TestItemScoreUpdateVO::getId))), ArrayList::new));
        distinctTestItemScore.removeAll(distinctTestItemScore.stream().filter(Objects::nonNull).filter(item -> StringUtils.isNotBlank(item.getItemCode()) && ItemCodeEnums.BMI_CODE.getInfo().equals(item.getItemCode())).collect(Collectors.toList()));
        BigDecimal testSumScore = distinctTestItemScore.stream().filter(Objects::nonNull).map(TestItemScoreUpdateVO::getItemFraction).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
        student.setTestSumScore(testSumScore);
        Map<String, List<TestItemScoreUpdateVO>> parentTestItemScore = distinctTestItemScore.stream().filter(item -> Objects.nonNull(item.getParentName())).collect(Collectors.groupingBy(TestItemScoreUpdateVO::getParentName));
        List<TestItemScoreUpdateVO> nationalPhysiqueList = parentTestItemScore.getOrDefault(TestItemCategoryEnum.NATIONAL_PHYSIQUE_CODE.getInfo(), Lists.newArrayList());
        BigDecimal nationalPhysiqueScore = nationalPhysiqueList.stream().filter(Objects::nonNull).map(TestItemScoreUpdateVO::getItemFraction).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
        List<TestItemScoreUpdateVO> tExerciseTestingList = parentTestItemScore.getOrDefault(TestItemCategoryEnum.T_EXERCISE_TESTING_CODE.getInfo(), Lists.newArrayList());
        BigDecimal tExerciseTestingScore = tExerciseTestingList.stream().filter(Objects::nonNull).map(TestItemScoreUpdateVO::getItemFraction).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
        student.setNationalPhysiqueScore(nationalPhysiqueScore);
        NationalPhysiqueGrade nationalPhysiqueGrade = nationalPhysiqueGradeList.stream()
                .filter(nate -> Objects.nonNull(nate.getStartFraction()) && Objects.nonNull(nate.getEndFraction()))
                .filter(nate -> nationalPhysiqueScore.compareTo(nate.getStartFraction()) >= 0 && nationalPhysiqueScore.compareTo(nate.getEndFraction()) <= 0)
                .findFirst().orElseGet(NationalPhysiqueGrade::new);
        student.setNationalPhysiqueRank(nationalPhysiqueGrade.getRank());
        student.setTexerciseTestingScore(tExerciseTestingScore);
    }

    /**
     * 计算平均分
     *
     * @param students      学生信息
     * @param i             序号
     * @param maleScore     性别平均分
     * @param maleList      性别集合
     * @param studentNumber 人数
     */
    private void arithmeticMean(List<ScoreExportVO> students, int i, ScoreExportVO maleScore, List<ScoreExportVO> maleList, BigDecimal studentNumber, String sex) {
        maleScore.setHeightScore((maleList.stream().filter(Objects::nonNull).map(ScoreExportVO::getHeightScore).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add)).divide(studentNumber, 2, BigDecimal.ROUND_HALF_UP));
        maleScore.setHeightFraction((maleList.stream().filter(Objects::nonNull).map(ScoreExportVO::getHeightFraction).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add)).divide(studentNumber, 2, BigDecimal.ROUND_HALF_UP));
        maleScore.setWeightScore((maleList.stream().filter(Objects::nonNull).map(ScoreExportVO::getWeightScore).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add)).divide(studentNumber, 2, BigDecimal.ROUND_HALF_UP));
        maleScore.setWeightFraction((maleList.stream().filter(Objects::nonNull).map(ScoreExportVO::getWeightFraction).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add)).divide(studentNumber, 2, BigDecimal.ROUND_HALF_UP));
        maleScore.setBmiScore((maleList.stream().filter(Objects::nonNull).map(ScoreExportVO::getBmiScore).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add)).divide(studentNumber, 2, BigDecimal.ROUND_HALF_UP));
        maleScore.setBmiFraction((maleList.stream().filter(Objects::nonNull).map(ScoreExportVO::getBmiFraction).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add)).divide(studentNumber, 2, BigDecimal.ROUND_HALF_UP));
        maleScore.setLongJumpScore((maleList.stream().filter(Objects::nonNull).map(ScoreExportVO::getLongJumpScore).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add)).divide(studentNumber, 2, BigDecimal.ROUND_HALF_UP));
        maleScore.setLongJumpFraction((maleList.stream().filter(Objects::nonNull).map(ScoreExportVO::getLongJumpFraction).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add)).divide(studentNumber, 2, BigDecimal.ROUND_HALF_UP));
        maleScore.setContinuousJumpScore((maleList.stream().filter(Objects::nonNull).map(ScoreExportVO::getContinuousJumpScore).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add)).divide(studentNumber, 2, BigDecimal.ROUND_HALF_UP));
        maleScore.setContinuousJumpFraction((maleList.stream().filter(Objects::nonNull).map(ScoreExportVO::getContinuousJumpFraction).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add)).divide(studentNumber, 2, BigDecimal.ROUND_HALF_UP));
        maleScore.setTennisScore((maleList.stream().filter(Objects::nonNull).map(ScoreExportVO::getTennisScore).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add)).divide(studentNumber, 2, BigDecimal.ROUND_HALF_UP));
        maleScore.setTennisFraction((maleList.stream().filter(Objects::nonNull).map(ScoreExportVO::getTennisFraction).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add)).divide(studentNumber, 2, BigDecimal.ROUND_HALF_UP));
        maleScore.setBalanceBeamScore((maleList.stream().filter(Objects::nonNull).map(ScoreExportVO::getBalanceBeamScore).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add)).divide(studentNumber, 2, BigDecimal.ROUND_HALF_UP));
        maleScore.setBalanceBeamFraction((maleList.stream().filter(Objects::nonNull).map(ScoreExportVO::getBalanceBeamFraction).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add)).divide(studentNumber, 2, BigDecimal.ROUND_HALF_UP));
        maleScore.setSitAndReachScore((maleList.stream().filter(Objects::nonNull).map(ScoreExportVO::getSitAndReachScore).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add)).divide(studentNumber, 2, BigDecimal.ROUND_HALF_UP));
        maleScore.setSitAndReachFraction((maleList.stream().filter(Objects::nonNull).map(ScoreExportVO::getSitAndReachFraction).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add)).divide(studentNumber, 2, BigDecimal.ROUND_HALF_UP));
        maleScore.setShuttleRunScore((maleList.stream().filter(Objects::nonNull).map(ScoreExportVO::getShuttleRunScore).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add)).divide(studentNumber, 2, BigDecimal.ROUND_HALF_UP));
        maleScore.setShuttleRunFraction((maleList.stream().filter(Objects::nonNull).map(ScoreExportVO::getShuttleRunFraction).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add)).divide(studentNumber, 2, BigDecimal.ROUND_HALF_UP));
        maleScore.setTrunScore((maleList.stream().filter(Objects::nonNull).map(ScoreExportVO::getTrunScore).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add)).divide(studentNumber, 2, BigDecimal.ROUND_HALF_UP));
        maleScore.setTlongJumpScore((maleList.stream().filter(Objects::nonNull).map(ScoreExportVO::getTlongJumpScore).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add)).divide(studentNumber, 2, BigDecimal.ROUND_HALF_UP));
        maleScore.setThopScore((maleList.stream().filter(Objects::nonNull).map(ScoreExportVO::getThopScore).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add)).divide(studentNumber, 2, BigDecimal.ROUND_HALF_UP));
        maleScore.setTforwardJumpScore((maleList.stream().filter(Objects::nonNull).map(ScoreExportVO::getTforwardJumpScore).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add)).divide(studentNumber, 2, BigDecimal.ROUND_HALF_UP));
        maleScore.setTforwardSlidingStepScore((maleList.stream().filter(Objects::nonNull).map(ScoreExportVO::getTforwardSlidingStepScore).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add)).divide(studentNumber, 2, BigDecimal.ROUND_HALF_UP));
        maleScore.setTsideStepScore((maleList.stream().filter(Objects::nonNull).map(ScoreExportVO::getTsideStepScore).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add)).divide(studentNumber, 2, BigDecimal.ROUND_HALF_UP));
        maleScore.setTracketBallScore((maleList.stream().filter(Objects::nonNull).map(ScoreExportVO::getTracketBallScore).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add)).divide(studentNumber, 2, BigDecimal.ROUND_HALF_UP));
        maleScore.setTplayFootballScore((maleList.stream().filter(Objects::nonNull).map(ScoreExportVO::getTplayFootballScore).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add)).divide(studentNumber, 2, BigDecimal.ROUND_HALF_UP));
        maleScore.setTtwoHandedCatchScore((maleList.stream().filter(Objects::nonNull).map(ScoreExportVO::getTtwoHandedCatchScore).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add)).divide(studentNumber, 2, BigDecimal.ROUND_HALF_UP));
        maleScore.setTfixedBallScore((maleList.stream().filter(Objects::nonNull).map(ScoreExportVO::getTfixedBallScore).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add)).divide(studentNumber, 2, BigDecimal.ROUND_HALF_UP));
        maleScore.setToverhandPitchingScore((maleList.stream().filter(Objects::nonNull).map(ScoreExportVO::getToverhandPitchingScore).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add)).divide(studentNumber, 2, BigDecimal.ROUND_HALF_UP));
        maleScore.setTgrounderScore((maleList.stream().filter(Objects::nonNull).map(ScoreExportVO::getTgrounderScore).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add)).divide(studentNumber, 2, BigDecimal.ROUND_HALF_UP));
        maleScore.setAge((maleList.stream().filter(Objects::nonNull).map(ScoreExportVO::getAge).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add)).divide(studentNumber, 2, BigDecimal.ROUND_HALF_UP));
        maleScore.setCoreScore((maleList.stream().filter(Objects::nonNull).map(ScoreExportVO::getCoreScore).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add)).divide(studentNumber, 2, BigDecimal.ROUND_HALF_UP));
        maleScore.setAgilityScore((maleList.stream().filter(Objects::nonNull).map(ScoreExportVO::getAgilityScore).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add)).divide(studentNumber, 2, BigDecimal.ROUND_HALF_UP));
        maleScore.setBalancedQualityScore((maleList.stream().filter(Objects::nonNull).map(ScoreExportVO::getBalancedQualityScore).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add)).divide(studentNumber, 2, BigDecimal.ROUND_HALF_UP));
        maleScore.setCoordinationQualityScore((maleList.stream().filter(Objects::nonNull).map(ScoreExportVO::getCoordinationQualityScore).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add)).divide(studentNumber, 2, BigDecimal.ROUND_HALF_UP));
        maleScore.setExplosiveQualityScore((maleList.stream().filter(Objects::nonNull).map(ScoreExportVO::getExplosiveQualityScore).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add)).divide(studentNumber, 2, BigDecimal.ROUND_HALF_UP));
        maleScore.setFlexibilityScore((maleList.stream().filter(Objects::nonNull).map(ScoreExportVO::getFlexibilityScore).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add)).divide(studentNumber, 2, BigDecimal.ROUND_HALF_UP));
        maleScore.setLowerLimbFitnessScore((maleList.stream().filter(Objects::nonNull).map(ScoreExportVO::getLowerLimbFitnessScore).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add)).divide(studentNumber, 2, BigDecimal.ROUND_HALF_UP));
        maleScore.setUpperLimbFitnessScore((maleList.stream().filter(Objects::nonNull).map(ScoreExportVO::getUpperLimbFitnessScore).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add)).divide(studentNumber, 2, BigDecimal.ROUND_HALF_UP));
        maleScore.setControlMotionScore((maleList.stream().filter(Objects::nonNull).map(ScoreExportVO::getControlMotionScore).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add)).divide(studentNumber, 2, BigDecimal.ROUND_HALF_UP));
        maleScore.setDisplacementMotionScore((maleList.stream().filter(Objects::nonNull).map(ScoreExportVO::getDisplacementMotionScore).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add)).divide(studentNumber, 2, BigDecimal.ROUND_HALF_UP));
        maleScore.setNationalPhysiqueScore((maleList.stream().filter(Objects::nonNull).map(ScoreExportVO::getNationalPhysiqueScore).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add)).divide(studentNumber, 2, BigDecimal.ROUND_HALF_UP));
        maleScore.setTexerciseTestingScore((maleList.stream().filter(Objects::nonNull).map(ScoreExportVO::getTexerciseTestingScore).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add)).divide(studentNumber, 2, BigDecimal.ROUND_HALF_UP));
        maleScore.setTestSumScore((maleList.stream().filter(Objects::nonNull).map(ScoreExportVO::getTestSumScore).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add)).divide(studentNumber, 2, BigDecimal.ROUND_HALF_UP));
        maleScore.setBodyShapeScore((maleList.stream().filter(Objects::nonNull).map(ScoreExportVO::getBodyShapeScore).filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add)).divide(studentNumber, 2, BigDecimal.ROUND_HALF_UP));
        maleScore.setSerial(i);
        if (StudentSexEnum.MALE.getInfo().equals(sex)) {
            maleScore.setStudentName("男生平均分");
        } else if (StudentSexEnum.FEMALE.getInfo().equals(sex)) {
            maleScore.setStudentName("女生平均分");
        } else if ("all".equals(sex)) {
            maleScore.setStudentName("总平均分");
        }
        students.add(maleScore);
    }
}