package com.xinyilu.system.service.testActionDevelopmentInfo.Impl;


import com.xinyilu.common.constant.Constants;
import com.xinyilu.common.core.domain.AjaxResult;
import com.xinyilu.common.enums.ItemCodeEnums;
import com.xinyilu.system.domain.nationalphysiquegrade.NationalPhysiqueGrade;
import com.xinyilu.system.domain.testItemScoreinfo.TestItemScore;
import com.xinyilu.system.domain.vo.motionprescriptionlibrary.MotionPrescriptionLibraryVO;
import com.xinyilu.system.domain.vo.testActionDevelopment.TestActionDevelopmentVO;
import com.xinyilu.system.domain.vo.testItemAvgScore.ThisActionDevelopmentVO;
import com.xinyilu.system.mapper.testItemScoreinfo.TestItemScoreMapper;
import com.xinyilu.system.service.testActionDevelopmentInfo.ITestActionDevelopmentService;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author ljh
 * @version 1.0
 * Create by 2022/8/10 17:07
 * 动作发展检测Service层业务处理
 */
@Service
public class TestActionDevelopmentServiceImpl implements ITestActionDevelopmentService {

    @Resource
    private TestItemScoreMapper testItemScoreMapper;


    /**
     * 定义运动程度1分及以下
     **/
    private static final String ONE_POINT = "1分及以下";

    /**
     * 定义运动程度2分-3分
     **/
    private static final String TWO_POINT = "2分-3分";

    /**
     * 定义运动程度4分及以上
     **/
    private static final String FOUR_POINT = "4分及以上";

    /**
     * 定义分数 1分
     **/
    private static final BigDecimal ONE_SCORE = BigDecimal.valueOf(1);

    /**
     * 定义分数 2分
     **/
    private static final BigDecimal TWO_SCORE = BigDecimal.valueOf(2);

    /**
     * 定义分数 3分
     **/
    private static final BigDecimal THREE_SCORE = BigDecimal.valueOf(3);

    /**
     * 获取动作发展检测接口实现列表
     *
     * @param studentId 学生id
     * @param batchCode 批次编号
     * @return java.util.List<com.xinyilu.system.domain.vo.TestItemScoreInfoVO>
     **/
    @Override
    public List<TestActionDevelopmentVO> selectTestActionDevelopmentList(@Param("studentId") Long studentId, @Param("batchCode") String batchCode) {
        List<TestActionDevelopmentVO> actionList = testItemScoreMapper.selectTestNameScoreList(studentId, batchCode, ItemCodeEnums.BMI_CODE.getInfo());
        //筛选出国民体质测定的数据
        List<TestActionDevelopmentVO> developmentVOList = actionList.stream()
                .filter(item -> StringUtils.isNotBlank(item.getRootCategoryName()) && Objects.nonNull(item.getItemFraction()))
                .filter(category -> Constants.ROOT_CATEGORY_NAME.equals(category.getRootCategoryName()))
                .distinct().collect(Collectors.toList());
        //算出国民体质测定的总分
        BigDecimal rootCategoryScoreAll = developmentVOList.stream().map(TestItemScore::getItemFraction).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP);
        //查询 国民体质测定评判等级信息表 的信息
        List<NationalPhysiqueGrade> nationalPhysiqueGradeList = testItemScoreMapper.getNationalPhysiqueGradeList();
        //判断出 国民体质测定评判等级信息 的等级
        NationalPhysiqueGrade nationalPhysiqueGrade = nationalPhysiqueGradeList.stream()
                .filter(nate -> Objects.nonNull(nate.getStartFraction()) && Objects.nonNull(nate.getEndFraction()))
                .filter(nate -> rootCategoryScoreAll.compareTo(nate.getStartFraction()) >= 0 && rootCategoryScoreAll.compareTo(nate.getEndFraction()) <= 0)
                .findFirst().orElseGet(NationalPhysiqueGrade::new);
        // 查询这个分类中所有的项目以及项目分数
        Map<Long, List<TestActionDevelopmentVO>> actionMapScore = actionList.stream()
                .filter(Objects::nonNull)
                .filter(itemCategoryId -> Objects.nonNull(itemCategoryId.getItemCategoryId()))
                .collect(Collectors.groupingBy(TestActionDevelopmentVO::getItemCategoryId));
        actionList.forEach(item -> {
            //判断 国民体质测定评判等级信息表 相等的才塞值
            if (StringUtils.isNotBlank(item.getRootCategoryName()) && Constants.ROOT_CATEGORY_NAME.equals(item.getRootCategoryName())) {
                item.setRootCategoryScoreAll(rootCategoryScoreAll);
                item.setNationalPhysiqueRank(StringUtils.isBlank(nationalPhysiqueGrade.getRank()) ? "" : nationalPhysiqueGrade.getRank());
            }
            List<TestActionDevelopmentVO> categoryScoreList = actionMapScore.getOrDefault(item.getItemCategoryId(), Lists.newArrayList());
            item.setItemScoreAll(categoryScoreList.stream().filter(item1 -> Objects.nonNull(item1.getItemFraction()))
                    .map(TestItemScore::getItemFraction).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, BigDecimal.ROUND_HALF_UP));
        });
        return actionList;
    }

    /**
     * 动作发展检测列表  匹配运动处方库数据表
     *
     * @param studentId 学生id
     * @param batchCode 批次编号
     * @return java.util.List<com.xinyilu.system.domain.vo.testItemAvgScore.ThisActionDevelopmentVO>
     **/
    @Override
    public AjaxResult selectThisActionDevelopmentVO(Long studentId, String batchCode) {
        List<MotionPrescriptionLibraryVO> dataList = Lists.newArrayList();
        //查询数据库中的分数
        List<ThisActionDevelopmentVO> actionList = testItemScoreMapper.selectThisActionDevelopmentVO(studentId, batchCode, ItemCodeEnums.BMI_CODE.getInfo());
        //查询数据库中的运动处方库数据表信息
        List<MotionPrescriptionLibraryVO> motionPrescriptionLibraryList = testItemScoreMapper.getMotionPrescriptionLibraryList();
        actionList.forEach(item -> {
            String categoryName = StringUtils.isBlank(item.getCategoryName()) ? "" : item.getCategoryName();
            BigDecimal itemScore = Objects.isNull(item.getItemFraction()) ? BigDecimal.ZERO : item.getItemFraction();
            //进行分数匹配
            List<MotionPrescriptionLibraryVO> scoreList = motionPrescriptionLibraryList.stream()
                    .filter(library -> StringUtils.isNotBlank(library.getPhysicalQualityName()) && Objects.nonNull(library.getDegreeStartFraction()) && Objects.nonNull(library.getDegreeEndFraction()))
                    .filter(score -> StringUtils.equals(categoryName, score.getPhysicalQualityName()) && itemScore.compareTo(score.getDegreeStartFraction()) >= 0 && itemScore.compareTo(score.getDegreeEndFraction()) <= 0)
                    .collect(Collectors.toList());
            dataList.addAll(scoreList);
        });
        //匹配好的进行筛选出运动程度
        dataList.forEach(item -> {
            if (Objects.nonNull(item.getDegreeStartFraction()) && Objects.nonNull(item.getDegreeEndFraction()) &&
                    item.getDegreeStartFraction().compareTo(BigDecimal.ZERO) >= 0 && item.getDegreeEndFraction().compareTo(ONE_SCORE) <= 0) {
                item.setDegreeMovement(ONE_POINT);
            } else if (Objects.nonNull(item.getDegreeStartFraction()) && Objects.nonNull(item.getDegreeEndFraction()) &&
                    item.getDegreeStartFraction().compareTo(TWO_SCORE) >= 0 && item.getDegreeEndFraction().compareTo(THREE_SCORE) <= 0) {
                item.setDegreeMovement(TWO_POINT);
            } else {
                item.setDegreeMovement(FOUR_POINT);
            }
        });
        return AjaxResult.success(dataList);
    }
}
