package com.admin.major.manager;

import com.admin.major.domain.AdjustResult;
import com.admin.major.domain.ClassInfo;
import com.admin.major.domain.ClassResult;
import com.admin.major.domain.dto.AdjustResultDTO;
import com.admin.major.service.IAdjustResultService;
import com.admin.major.service.IClassInfoService;
import com.admin.major.service.IClassResultService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;

/**
 * @author liangjunxing
 * @date 2022/5/18 0018
 */
@Service
@Slf4j
@AllArgsConstructor
public class ClassResultManager {
    @Autowired
    private IClassInfoService classInfoService;
    @Autowired
    private IAdjustResultService adjustResultService;
    @Autowired
    private IClassResultService classResultService;

    /**
     * 分班入口
     * @param year 年份
     * @param collegeId 学院
     */
    public void division(String year,Long collegeId) {
        //根据年份，学院获取对应班级详情
        List<ClassInfo> classInfos = classInfoService.selectClassInfoList(new ClassInfo() {
            {
                setCollegeId(collegeId);
                setYear(year);
                setStatus("1");
            }
        });
        //存在班级即进行分班
        if (classInfos != null) {
            //清空旧分班
            classResultService.deleteAll();
            List<AdjustResultDTO> adjustResultDTOS = null;
            for (int i = 0; i < classInfos.size(); i++) {
                Long classId = classInfos.get(i).getClassId();
                //获取各专业分流情况
                if (i == 0 || !classInfos.get(i).getMajorId().equals(classInfos.get(i - 1).getMajorId())) {
                    String majorId = classInfos.get(i).getMajorId();
                    adjustResultDTOS = adjustResultService.viewAdjustResult(new AdjustResultDTO() {
                        {
                            setAdmitMajorId(majorId);
                            setCollegeId(collegeId);
                            setBatchId(year);
                        }
                    });
                    if (adjustResultDTOS.size() > 0) {
                        adjustResultDTOS.sort(Comparator.comparingDouble(dto -> {
                            if (dto.getStudentNo() == null) {
                                return Double.MIN_VALUE;
                            } else {
                                return Double.valueOf(dto.getStudentNo());
                            }
                        }));
                    }
                }
                int index = 0;
                //录入分班表数据
                for (int j = 0; j < adjustResultDTOS.size(); j++) {
                    if (j<classInfos.get(i).getClassNumber()) {
                        String studentId = adjustResultDTOS.get(j).getStudentId();
                        String admitMajorId = adjustResultDTOS.get(j).getAdmitMajorId();
                        ClassResult classResult = ClassResult.builder()
                                .classId(classId).collegeId(collegeId).majorId(admitMajorId).studentId(studentId).year(year)
                                .build();
                        classResultService.insertClassResult(classResult);
                        index++;
                    }else continue;
                }
                //清除已分班的学生
                adjustResultDTOS.subList(0,index).clear();
            }
        }
    }
}
