package com.xhkjedu.sapi.service.system;

import com.xhkjedu.sapi.mapper.gradeclass.ClassStudentMapper;
import com.xhkjedu.sapi.mapper.gradeclass.GroupMapper;
import com.xhkjedu.sapi.mapper.gradeclass.GroupStudentMapper;
import com.xhkjedu.sapi.mapper.system.SchoolUpMapper;
import com.xhkjedu.sapi.model.gradeclass.TClassStudent;
import com.xhkjedu.sapi.model.gradeclass.TGroup;
import com.xhkjedu.sapi.model.gradeclass.TGroupStudent;
import com.xhkjedu.sapi.vo.gradeclass.ClassVo;
import com.xhkjedu.utils.N_Utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @Description：处理学年升级问题
 * @Author: WN
 * @Date: 2024/8/28 15:38:53
 **/
@Slf4j
@Service
public class SchoolUpService {
    @Resource
    private SchoolUpMapper schoolUpMapper;
    @Resource
    private ClassStudentMapper classStudentMapper;
    @Resource
    private GroupMapper groupMapper;
    @Resource
    private GroupStudentMapper groupStudentMapper;

    @Transactional(rollbackFor = Exception.class)
    public void schoolUpHandle() {
        log.info("开始处理");
        Integer schoolid = 1;
        //获取新生成的虚拟班
        List<ClassVo> classList = schoolUpMapper.listClassByYearAndGrade(2024,9,schoolid);
        //原班级
        List<ClassVo> classListOld = schoolUpMapper.listClassByYearAndGrade(2023,8,schoolid);
        Map<String, ClassVo> classMap = classListOld.stream().collect(Collectors.toMap(ClassVo::getClassname, Function.identity(), (key1, key2) -> key2));

        for (ClassVo classVo : classList) {
            Integer classid = classVo.getClassid();
            //获取班级下学生数量
            Integer stunum = schoolUpMapper.getClassStunum(classid);

            //找原班级id
            ClassVo oldClassVo = classMap.get(classVo.getClassname());

            if(!N_Utils.isTrueInteger(stunum)){
                //说明班级下无学生，
                //获取班级下学生id集合
                List<Integer> studentList = schoolUpMapper.listStudentidByClassid(oldClassVo.getClassid());

                List<TClassStudent> csList = new ArrayList<>();
                for(Integer studentId : studentList){
                    TClassStudent tClassStudent = new TClassStudent();
                    tClassStudent.setClassid(classid);
                    tClassStudent.setStudentid(studentId);
                    tClassStudent.setCreateid(classVo.getCreateid());
                    tClassStudent.setCreatetime(classVo.getCreatetime());
                    tClassStudent.setSchoolid(schoolid);
                    csList.add(tClassStudent);
                }
                classStudentMapper.insertList(csList);
            }

            //获取班级下全班分组id
            Integer groupid = schoolUpMapper.getGroupidByClassid(classid);
            if(!N_Utils.isTrueInteger(groupid)){
               //说明无分组
                TGroup tGroup = new TGroup();
                tGroup.setClassid(classid);
                tGroup.setGroupname("全班分组");
                tGroup.setGrouptype(1);
                tGroup.setGroupnum(classVo.getClassnum());
                tGroup.setCreateid(classVo.getCreateid());
                tGroup.setCreatetime(classVo.getCreatetime());
                tGroup.setSchoolid(schoolid);
                groupMapper.insertUseGeneratedKeys(tGroup);
                groupid = tGroup.getId();

                saveGroupStudent(groupid, classVo,oldClassVo.getClassid(),schoolid);

            }else {
                log.info("有分组！！！");
                //说明有分组，获取分组下学生数量
                Integer groupStunum = schoolUpMapper.getGroupStunum(groupid);
                if(!N_Utils.isTrueInteger(groupStunum)){
                    saveGroupStudent(groupid, classVo,oldClassVo.getClassid(),schoolid);
                }
            }
        }
        log.info("处理完成");

    }

    private void saveGroupStudent(Integer groupid, ClassVo classVo,Integer oldClassid,Integer schoolid) {
        //获取班级下学生id集合
        List<Integer> studentList = schoolUpMapper.listStudentidByClassid(oldClassid);
        List<TGroupStudent> gsList = new ArrayList<>();
        for(Integer studentId : studentList) {
            TGroupStudent tGroupStudent = new TGroupStudent();
            tGroupStudent.setGroupid(groupid);
            tGroupStudent.setClassid(classVo.getClassid());
            tGroupStudent.setStudentid(studentId);
            tGroupStudent.setCreateid(classVo.getCreateid());
            tGroupStudent.setCreatetime(classVo.getCreatetime());
            tGroupStudent.setSchoolid(schoolid);
            gsList.add(tGroupStudent);
        }
        groupStudentMapper.insertList(gsList);
    }


}
