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

import com.xinyilu.common.enums.ItemCodeEnums;
import com.xinyilu.system.domain.testBodyScoreInfo.TestBodyScoreInfo;
import com.xinyilu.system.domain.testLevel.TestItemLevel;
import com.xinyilu.system.domain.vo.TestBodyScoreInfoVO;
import com.xinyilu.system.mapper.TestBodyScoreInfo.TestBodyScoreInfoMapper;
import com.xinyilu.system.service.TestBodyScoreInfo.ITestBodyScoreInfoService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.compress.utils.Lists;
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/9 14:55
 * 身体素质测试
 */
@Service
public class TestBodyScoreInfoServiceImpl implements ITestBodyScoreInfoService {

    @Resource
    private TestBodyScoreInfoMapper testBodyScoreInfoMapper;

    /**
     * @return List<TestBodyScoreInfoVO>
     * @date: 15:24
     * @author: ljh
     * @description: 分页身体素质测试列表
     */
    @Override
    public List<TestBodyScoreInfoVO> selectTestBodyScoreList(@Param("studentId") Long studentId, @Param("batchCode") String batchCode) {
        List<TestBodyScoreInfoVO> testBodyScoreInfos = testBodyScoreInfoMapper.selectTestBodyScoreList(studentId, batchCode, ItemCodeEnums.BMI_CODE.getInfo());
        List<Long> longList = testBodyScoreInfos.stream()
                .filter(Objects::nonNull)
                .filter(item -> Objects.nonNull(item.getTestItemId()))
                .map(TestBodyScoreInfo::getTestItemId).distinct().collect(Collectors.toList());

        // 查询区间 项目分数区间 list
        Map<Long, List<TestItemLevel>> collect = testBodyScoreInfoMapper.getTestItemLevel(longList).stream().filter(Objects::nonNull).filter(item -> Objects.nonNull(item.getItemId())).collect(Collectors.groupingBy(TestItemLevel::getItemId));
        testBodyScoreInfos.forEach(item -> {
            List<TestItemLevel> testItemLevelList = collect.getOrDefault(item.getTestItemId(), new ArrayList<>());
            item.setTestItemLevelList(testItemLevelList);
            testItemLevelList.forEach(score -> {
                if (Objects.nonNull(item.getItemScore())
                        && Objects.nonNull(score.getLevelMinScore()) && Objects.nonNull(score.getLevelMaxScore())) {
                    if (item.getItemScore().compareTo(score.getLevelMinScore()) >= 0
                            && item.getItemScore().compareTo(score.getLevelMaxScore()) <= 0) {
                        item.setLeveName(score.getLevelName());
                    }
                }
            });
            //找出每个区间中的最大值和最小值
            List<BigDecimal> scoreList = Lists.newArrayList();
            List<BigDecimal> collect1 = testItemLevelList.stream().filter(level -> Objects.nonNull(level.getLevelMinScore())).map(TestItemLevel::getLevelMinScore).collect(Collectors.toList());
            List<BigDecimal> collect2 = testItemLevelList.stream().filter(level -> Objects.nonNull(level.getLevelMaxScore())).map(TestItemLevel::getLevelMaxScore).collect(Collectors.toList());
            scoreList.addAll(collect1);
            scoreList.addAll(collect2);
            BigDecimal min = BigDecimal.ZERO;
            BigDecimal max = BigDecimal.ZERO;
            if (CollectionUtils.isNotEmpty(scoreList)) {
                min = Collections.min(scoreList);
                max = Collections.max(scoreList);
            }
            item.setMinScore(min);
            item.setMaxScore(max);
        });
        return testBodyScoreInfos;
    }

    /**
     * @return List<TestBodyScoreInfoVO>
     * @date: 14:43
     * @author: ljh
     * @description: 获得测试项目级别区间名称
     */
    @Override
    public List<TestBodyScoreInfoVO> getTestBodyStandardList(TestBodyScoreInfoVO testBodyScoreInfoVO) {
        List<TestBodyScoreInfoVO> testBodyStandardList = testBodyScoreInfoMapper.getTestBodyStandardList(testBodyScoreInfoVO);

        List<Long> longList = testBodyStandardList.stream()
                .filter(Objects::nonNull)
                .filter(item -> Objects.nonNull(item.getTestItemId()))
                .map(TestBodyScoreInfo::getTestItemId).distinct().collect(Collectors.toList());

        // 查询区间 项目分数区间 list
        Map<Long, List<TestItemLevel>> collect = testBodyScoreInfoMapper.getTestItemLevel(longList).stream()
                .filter(Objects::nonNull).filter(item -> Objects.nonNull(item.getItemId()))
                .collect(Collectors.groupingBy(TestItemLevel::getItemId));
        testBodyStandardList.forEach(item -> {
            List<TestItemLevel> testItemLevelList = collect.getOrDefault(item.getTestItemId(), new ArrayList<>());
            item.setTestItemLevelList(testItemLevelList);
            testItemLevelList.forEach(score -> {
                if (Objects.nonNull(item.getItemScore())
                        && Objects.nonNull(score.getLevelMinScore()) && Objects.nonNull(score.getLevelMaxScore())) {
                    if (item.getItemScore().compareTo(score.getLevelMinScore()) >= 0
                            && item.getItemScore().compareTo(score.getLevelMaxScore()) <= 0) {
                        item.setLeveName(score.getLevelName());
                    }
                }
            });

        });
        return testBodyStandardList;
    }
}
