package com.qmth.teachcloud.report.business.service.impl;

import com.qmth.teachcloud.common.contant.SystemConstant;
import com.qmth.teachcloud.report.business.bean.result.inspectCourseExamTotal.*;
import com.qmth.teachcloud.report.business.mapper.CourseReportMapper;
import com.qmth.teachcloud.report.business.service.CourseReportService;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Description:
 * @Author: CaoZixuan
 * @Date:
 */
@Service
public class CourseReportServiceImpl implements CourseReportService {
    @Resource
    private CourseReportMapper courseReportMapper;

    @Override
    @Cacheable(value = SystemConstant.COLLEGE_DEAN_REPORT, key = "#schoolId + '-' + #semesterId + '-' + #examId + '-' +#collegeId")
    public InspectCourseTotalReportResult findInfoInspectCourseExamTotal(Long schoolId, Long semesterId, Long examId, Long collegeId) {

        // 考试总览数据
        List<ExamBaseInfo> examBaseInfoList = courseReportMapper.findExamTotalByExamId(examId);
        if (Objects.isNull(examBaseInfoList) || examBaseInfoList.size() == 0) {
            return null;
        }

        InspectCourseTotalReportResult inspectCourseTotalReportResult = new InspectCourseTotalReportResult();
        inspectCourseTotalReportResult.setExamBaseInfo(examBaseInfoList.get(0));
        // 学院下各课程数据和学校对比数据
        List<CollegeCourseInfo> collegeCourseInfoList = courseReportMapper.findCollegeCourseInfo(examId, collegeId);
        if (Objects.isNull(collegeCourseInfoList) || collegeCourseInfoList.size() == 0) {
            return inspectCourseTotalReportResult;
        }

        // 低于平均分课程数据源
        List<CollegeCourseInfo> lowAvgDatasource = collegeCourseInfoList
                .stream().filter(e -> e.getColRelativePosition().doubleValue() < 0).collect(Collectors.toList());


        int lowAvgCount = lowAvgDatasource.size();
        List<LowAvgCourse> lowAvgCourseList = new ArrayList<>();
        for (CollegeCourseInfo lowCell : lowAvgDatasource) {
            String courseName = lowCell.getCourseName();
            String courseCode = lowCell.getCourseCode();
            LowAvgCourse lowAvgCourse = new LowAvgCourse();
            lowAvgCourse.setCourseName(courseName);
            lowAvgCourse.setCourseCode(courseCode);
            lowAvgCourseList.add(lowAvgCourse);
        }
        LowAvgInfo lowAvgInfo = new LowAvgInfo();// 低于平均分的学科信息
        lowAvgInfo.setLowAvgCount(lowAvgCount);
        lowAvgInfo.setLowAvgCourseList(lowAvgCourseList);

        CollegeInfo collegeInfo = new CollegeInfo();
        collegeInfo.setCollegeCourseInfoList(collegeCourseInfoList);
        collegeInfo.setLowAvgInfo(lowAvgInfo);

        inspectCourseTotalReportResult.setCollegeInfo(collegeInfo);

        return inspectCourseTotalReportResult;
    }
}
