package com.miaxis.score.service.impl;

import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.miaxis.common.utils.DateUtils;
import com.miaxis.score.dto.ScoreInfoAllDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.apache.commons.lang3.StringUtils;
import com.miaxis.score.mapper.ScoreInfoMapper;
import com.miaxis.score.domain.ScoreInfo;
import com.miaxis.score.service.IScoreInfoService;

/**
 * 模拟考成绩Service业务层处理
 *
 * @author miaxis
 * @date 2021-08-23
 */
@Service
public class ScoreInfoServiceImpl extends ServiceImpl<ScoreInfoMapper, ScoreInfo> implements IScoreInfoService {
    @Autowired
    private ScoreInfoMapper scoreInfoMapper;

    /**
     * 查询模拟考成绩列表
     *
     * @param scoreInfo 模拟考成绩
     * @return 模拟考成绩
     */
    @Override
    public List<ScoreInfo> selectScoreInfoList(ScoreInfo scoreInfo){
        return scoreInfoMapper.selectScoreInfoList(scoreInfo);
    }

    @Override
    public int getAveScore(ScoreInfo scoreInfo) {
        int avgScore= scoreInfoMapper.getAvgScore(scoreInfo);
        return avgScore;
    }

    @Override
    public int getForecastScore(ScoreInfo scoreInfo) {
        List<Integer> list = scoreInfoMapper.getLast10Score(scoreInfo);
        if(list!=null && list.size()>=3) {
            list.remove(0);
            list.remove(list.size() - 1);
        }
        int total = 0;
        for (int a: list) {
            total +=a;
        }
        if(total==0){
            return 0;
        } else {
            int forecastScore= total/list.size();
            return forecastScore;
        }
    }



    @Override
    public int getMaxScore(ScoreInfo scoreInfo) {
        int maxScore = scoreInfoMapper.getMaxScore(scoreInfo);
        return maxScore;
    }


    @Override
    public ScoreInfoAllDTO getScoreInfoAll(ScoreInfo scoreInfo) {
        ScoreInfoAllDTO scoreInfoAllDTO = new ScoreInfoAllDTO();
        scoreInfoAllDTO.setAvgScore(this.getAveScore(scoreInfo));
        scoreInfoAllDTO.setForecastScore(this.getForecastScore(scoreInfo));
        scoreInfoAllDTO.setMaxScore(this.getMaxScore(scoreInfo));
        return scoreInfoAllDTO;
    }

}
