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

import com.xinyilu.common.core.domain.AjaxResult;
import com.xinyilu.common.enums.ItemCodeEnums;
import com.xinyilu.system.domain.vo.averageHeightAndWeight.AverageHeightAndWeightVO;
import com.xinyilu.system.domain.vo.averageHeightAndWeight.HeightAndWeightAvgScoreVO;
import com.xinyilu.system.mapper.testItemScoreinfo.TestItemScoreMapper;
import com.xinyilu.system.service.averageHeightAndWeight.AverageHeightAndWeightService;
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.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Description 幼儿园各年级平均身高和平均体重业务层实现类
 * @Author 纪寒
 * @Date 2022-08-15 14:17:18
 * @Version 1.0
 */
@Service
public class AverageHeightAndWeightServiceImpl implements AverageHeightAndWeightService {


    @Resource
    private TestItemScoreMapper testItemScoreMapper;

    /**
     * 学生信息为0的标识
     */
    private static final Integer ZERO = 0;

    /**
     * 根据幼儿id和批次编号查询各年级平均身高和平均成绩信息
     *
     * @param kindergartenId 幼儿园id
     * @param batchCode      批次编号
     * @return 平均身高和平均体重信息
     */
    @Override
    public AjaxResult getHeightAndWeightAvgScoreInfo(Long kindergartenId, String batchCode) {
        //查询学生的身高和体重信息
        List<String> itemCodeList = Lists.newArrayList();
        itemCodeList.add(ItemCodeEnums.HEIGHT_CODE.getInfo());
        itemCodeList.add(ItemCodeEnums.WEIGHT_CODE.getInfo());
        List<HeightAndWeightAvgScoreVO> avgHeightAndWeightInfo = testItemScoreMapper.getAvgHeightAndWeightInfo(kindergartenId, batchCode, itemCodeList);
        if (CollectionUtils.isEmpty(avgHeightAndWeightInfo)) {
            return AjaxResult.success();
        }
        //计算平均成绩以及设置班级名称
        this.setAvgScoreAndClassName(avgHeightAndWeightInfo);
        List<AverageHeightAndWeightVO> heightAndWeightList = Lists.newArrayList();
        //以班级类型进行分组
        Map<String, List<HeightAndWeightAvgScoreVO>> classTypeMap = avgHeightAndWeightInfo.stream().filter(Objects::nonNull).filter(item -> Objects.nonNull(item.getClassType())).collect(Collectors.groupingBy(HeightAndWeightAvgScoreVO::getClassType));
        for (Map.Entry<String, List<HeightAndWeightAvgScoreVO>> entry : classTypeMap.entrySet()) {
            AverageHeightAndWeightVO averageVO = new AverageHeightAndWeightVO();
            //班级类型
            averageVO.setClassType(StringUtils.isBlank(entry.getKey()) ? "" : entry.getKey());
            //平均身高和平均体重信息
            List<HeightAndWeightAvgScoreVO> avgScoreList = CollectionUtils.isEmpty(entry.getValue()) ? Lists.newArrayList() : entry.getValue();
            averageVO.setWeightAndHeightAvgScoreList(avgScoreList);
            //班级名称
            if (avgScoreList.size() == 0) {
                averageVO.setClassName("");
            } else {
                averageVO.setClassName(avgScoreList.get(0).getClassName());
            }
            heightAndWeightList.add(averageVO);
        }
        return AjaxResult.success(heightAndWeightList);
    }

    /**
     * 计算平均成绩以及设置班级名称
     *
     * @param weightAvgScoreList 成绩列表信息
     */
    private void setAvgScoreAndClassName(List<HeightAndWeightAvgScoreVO> weightAvgScoreList) {
        weightAvgScoreList.forEach(item -> {
            //计算平均成绩以及
            if (Objects.nonNull(item.getTotalStudentCount()) && Objects.nonNull(item.getTotalItemScore())
                    && !ZERO.equals(item.getTotalStudentCount())) {
                item.setAverageScore(item.getTotalItemScore().divide(BigDecimal.valueOf(item.getTotalStudentCount()), 2, BigDecimal.ROUND_HALF_UP));
            }
            //设置班级名称
            switch (StringUtils.isBlank(item.getClassType()) ? "" : item.getClassType()) {
                case "LARGE_CLASS":
                    item.setClassName("大班");
                    break;
                case "MIDDLE_CLASS":
                    item.setClassName("中班");
                    break;
                case "SMALL_CLASS":
                    item.setClassName("小班");
                    break;
                default:
                    item.setClassName("");
            }
        });
    }

}
