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

import com.qmth.teachcloud.common.contant.SystemConstant;
import com.qmth.teachcloud.common.entity.BasicCourse;
import com.qmth.teachcloud.common.entity.SysOrg;
import com.qmth.teachcloud.common.entity.SysUser;
import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
import com.qmth.teachcloud.common.service.BasicCourseService;
import com.qmth.teachcloud.common.service.SysOrgService;
import com.qmth.teachcloud.common.service.SysUserService;
import com.qmth.teachcloud.report.business.entity.TAExamCourse;
import com.qmth.teachcloud.report.business.entity.TAExamCourseRecordDio;
import com.qmth.teachcloud.report.business.entity.TAExamTotal;
import com.qmth.teachcloud.report.business.entity.TBStudent;
import com.qmth.teachcloud.report.business.service.*;
import com.qmth.teachcloud.report.business.templete.service.ComplicatedCalculateAsyncService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @Description: 更改名称服务实现类
 * @Author: CaoZixuan
 * @Date: 2022-03-11
 */
@Service
public class ChangNamesServiceImpl implements ChangNamesService {
    @Resource
    private BasicCourseService basicCourseService;
    @Resource
    private SysOrgService sysOrgService;
    @Resource
    private SysUserService sysUserService;
    @Resource
    private TBCommonLevelConfigService tbCommonLevelConfigService;
    @Resource
    private TBCommonRankLevelConfigService tbCommonRankLevelConfigService;
    @Resource
    private TBDimensionService tbDimensionService;
    @Resource
    private TBExamCourseService tbExamCourseService;
    @Resource
    private TBExamStudentService tbExamStudentService;
    @Resource
    private TBModuleConfigService tbModuleConfigService;
    @Resource
    private TBModuleProficiencyService tbModuleProficiencyService;
    @Resource
    private TBPaperService tbPaperService;
    @Resource
    private TBTeacherService tbTeacherService;
    @Resource
    private TAExamCourseService taExamCourseService;
    @Resource
    private TAExamCourseClazzService taExamCourseClazzService;
    @Resource
    private TAExamCourseCollegeInspectService taExamCourseCollegeInspectService;
    @Resource
    private TAExamCourseCollegeInspectDioService taExamCourseCollegeInspectDioService;
    @Resource
    private TAExamCourseCollegePaperStructService taExamCourseCollegePaperStructService;
    @Resource
    private TAExamCourseCollegeTeacherService taExamCourseCollegeTeacherService;
    @Resource
    private TAExamCourseDifficultService taExamCourseDifficultService;
    @Resource
    private TAExamCourseDioService taExamCourseDioService;
    @Resource
    private TAExamCourseRecordDioService taExamCourseRecordDioService;
    @Resource
    private TAExamCourseRecordModService taExamCourseRecordModService;
    @Resource
    private TAExamCourseTeacherService taExamCourseTeacherService;
    @Resource
    private TAExamCourseTeacherCollegeDifficultService taExamCourseTeacherCollegeDifficultService;
    @Resource
    private TAExamCourseTeacherCollegeDioService taExamCourseTeacherCollegeDioService;
    @Resource
    private TAExamCourseTeacherCollegePaperStructService taExamCourseTeacherCollegePaperStructService;
    @Resource
    private TAExamCourseTeacherDifficultService taExamCourseTeacherDifficultService;
    @Resource
    private TAExamCourseTeacherDioService taExamCourseTeacherDioService;
    @Resource
    private TAExamCourseTeacherPaperStructService taExamCourseTeacherPaperStructService;
    @Resource
    private TAExamTotalService taExamTotalService;
    @Resource
    private TAPaperStructService taPaperStructService;
    @Resource
    private TBStudentService tbStudentService;
    @Resource
    private ComplicatedCalculateAsyncService complicatedCalculateAsyncService;

    private static final Logger log = LoggerFactory.getLogger(ChangNamesServiceImpl.class);


    /**
     * 查询 课程编号 - 课程名称 map
     *
     * @return map
     */
    private Map<String, String> findCourseMap() {
        Map<String, String> map = new HashMap<>();
        for (BasicCourse basicCourse : basicCourseService.list()) {
            map.put(basicCourse.getCode(), basicCourse.getName());
        }
        return map;
    }

    /**
     * 查询 机构id - 机构名称 map
     *
     * @return map
     */
    private Map<Long, String> findOrgMap() {
        Map<Long, String> map = new HashMap<>();
        for (SysOrg sysOrg : sysOrgService.list()) {
            map.put(sysOrg.getId(), sysOrg.getName());
        }
        return map;
    }

    /**
     * 查询 用户登录名（工号） - 用户真实名
     *
     * @return map
     */
    private Map<String, String> findUserCodeNameMap() {
        Map<String, String> map = new HashMap<>();
        for (SysUser sysUser : sysUserService.list()) {
            map.put(sysUser.getLoginName(), sysUser.getRealName());
        }
        return map;
    }

    /**
     * 查询 用户id - 用户真实名
     *
     * @return map
     */
    private Map<Long, String> findUserIdNameMap() {
        Map<Long, String> map = new HashMap<>();
        for (SysUser sysUser : sysUserService.list()) {
            map.put(sysUser.getId(), sysUser.getRealName());
        }
        return map;
    }

    /**
     * 查询 基础学生id - 基础学生名称
     *
     * @return map
     */
    private Map<Long, String> findStudentIdNameMap() {
        Map<Long, String> map = new HashMap<>();
        for (TBStudent tbStudent : tbStudentService.list()) {
            map.put(tbStudent.getId(), tbStudent.getName());
        }
        return map;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void changeBasicTables() {
        Map<String, String> courseMap = this.findCourseMap();
        Map<Long, String> orgMap = this.findOrgMap();
        Map<String, String> userMap = this.findUserCodeNameMap();
        Map<Long, String> studentMap = this.findStudentIdNameMap();
        // t_b_common_level_config
        tbCommonLevelConfigService.updateBatchById(tbCommonLevelConfigService.list().stream().filter(e -> courseMap.containsKey(e.getCourseCode())).peek(e -> e.setCourseName(courseMap.get(e.getCourseCode()))).collect(Collectors.toList()));
        // t_b_common_rank_level_config
        tbCommonRankLevelConfigService.updateBatchById(tbCommonRankLevelConfigService.list().stream().filter(e -> courseMap.containsKey(e.getCourseCode())).peek(e -> e.setCourseName(courseMap.get(e.getCourseCode()))).collect(Collectors.toList()));
        // t_b_dimension
        tbDimensionService.updateBatchById(tbDimensionService.list().stream().filter(e -> courseMap.containsKey(e.getCourseCode())).peek(e -> e.setCourseName(courseMap.get(e.getCourseCode()))).collect(Collectors.toList()));
        // t_b_exam_course
        tbExamCourseService.updateBatchById(tbExamCourseService.list().stream().peek(e -> {
            if (courseMap.containsKey(e.getCourseCode())) {
                e.setCourseName(courseMap.get(e.getCourseCode()));
            }
            if (orgMap.containsKey(e.getTeachCollegeId())) {
                e.setTeachCollegeName(orgMap.get(e.getTeachCollegeId()));
            }
        }).collect(Collectors.toList()));
        // t_b_exam_student
        tbExamStudentService.updateBatchById(tbExamStudentService.list().stream()
                .peek(e -> {
                    e.setCourseName(courseMap.get(e.getCourseCode()));
                    e.setName(studentMap.get(e.getStudentId()));
                }).collect(Collectors.toList()));
        // t_b_module_config
        tbModuleConfigService.updateBatchById(tbModuleConfigService.list().stream().filter(e -> courseMap.containsKey(e.getCourseCode())).peek(e -> e.setCourseName(courseMap.get(e.getCourseCode()))).collect(Collectors.toList()));
        // t_b_module_proficiency
        tbModuleProficiencyService.updateBatchById(tbModuleProficiencyService.list().stream().filter(e -> courseMap.containsKey(e.getCourseCode())).peek(e -> e.setCourseName(courseMap.get(e.getCourseCode()))).collect(Collectors.toList()));
        // t_b_paper
        tbPaperService.updateBatchById(tbPaperService.list().stream().filter(e -> courseMap.containsKey(e.getCourseCode())).peek(e -> e.setCourseName(courseMap.get(e.getCourseCode()))).collect(Collectors.toList()));
        // t_b_teacher
        tbTeacherService.updateBatchById(tbTeacherService.list().stream().filter(e -> userMap.containsKey(e.getTeacherCode())).peek(e -> e.setTeacherName(userMap.get(e.getTeacherCode()))).collect(Collectors.toList()));
        log.info("基础表't_b'更新完成");
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void changeTableTAExamCourse() {
        Map<String, String> courseMap = this.findCourseMap();
        Map<Long, String> orgMap = this.findOrgMap();
        List<SysOrg> sysOrgList = sysOrgService.list();

        List<TAExamCourse> taExamCourseList = taExamCourseService.list();
        for (TAExamCourse taExamCourse : taExamCourseList) {
            Long schoolId = taExamCourse.getSchoolId();

            if (courseMap.containsKey(taExamCourse.getCourseCode())) {
                taExamCourse.setCourseName(courseMap.get(taExamCourse.getCourseCode()));
            }
            if (orgMap.containsKey(taExamCourse.getTeachCollegeId())) {
                taExamCourse.setTeachCollegeName(orgMap.get(taExamCourse.getTeachCollegeId()));
            }
            String inspectCollegeNameStr = taExamCourse.getInspectCollegeNames();
            List<String> newInspect = new ArrayList<>();
            String[] inspectCollegeNameList = inspectCollegeNameStr.split("、");
            for (String s : inspectCollegeNameList) {
                List<SysOrg> cellList = sysOrgList.stream().filter(e -> e.getHistoricName().equals(s) && e.getSchoolId().equals(schoolId)).collect(Collectors.toList());
                if (cellList.size() > 0) {
                    if (cellList.size() > 1) {
                        throw ExceptionResultEnum.ERROR.exception("数据异常");
                    }
                    SysOrg cell = cellList.get(0);
                    newInspect.add(cell.getName());
                } else {
                    // 如果没有匹配到的话就按照原来的学院名称（因为演示方法可能多次执行，但执行完成一次后机构表的历史数据记录不会替换，下次执行就可能导致由于历史名称和机构名称组不匹配引发丢失机构）
                    newInspect.add(s);
                }
            }
            if (newInspect.size() > 0) {
                taExamCourse.setInspectCollegeNames(String.join("、", newInspect));
            }
        }
        taExamCourseService.updateBatchById(taExamCourseList);
        log.info("复杂表 't_a_exam_course' 更新完成");
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void changeTableTAExamCourseClazz() {
        Map<String, String> courseMap = this.findCourseMap();
        List<SysOrg> sysOrgList = sysOrgService.list();
        taExamCourseClazzService.updateBatchById(taExamCourseClazzService.list().stream().peek(e -> {
            if (courseMap.containsKey(e.getCourseCode())) {
                e.setCourseName(courseMap.get(e.getCourseCode()));
            }
            List<SysOrg> cellList = sysOrgList.stream().filter(o -> o.getHistoricName().equals(e.getCollegeName()) && o.getSchoolId().equals(e.getSchoolId())).collect(Collectors.toList());
            if (cellList.size() > 1) {
                throw ExceptionResultEnum.ERROR.exception("数据异常");
            }
            if (cellList.size() == 1) {
                e.setCollegeName(cellList.get(0).getName());
            }
        }).collect(Collectors.toList()));
        log.info("表 't_a_exam_course_clazz' 更新完成");
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void changeTableTAExamCourseCollegeInspect() {
        Map<String, String> courseMap = this.findCourseMap();
        taExamCourseCollegeInspectService.updateBatchById(taExamCourseCollegeInspectService.list().stream().filter(e -> courseMap.containsKey(e.getCourseCode())).peek(e -> e.setCourseName(courseMap.get(e.getCourseCode()))).collect(Collectors.toList()));
        log.info("表 't_a_exam_course_college_inspect' 更新完成");
    }

    @Override
    public void changeTableTAExamCourseCollegeInspectDio() {
        Map<String, String> courseMap = this.findCourseMap();
        Map<Long, String> orgMap = this.findOrgMap();
        taExamCourseCollegeInspectDioService.updateBatchById(taExamCourseCollegeInspectDioService.list().stream().peek(e -> {
            if (courseMap.containsKey(e.getCourseCode())) {
                e.setCourseName(courseMap.get(e.getCourseCode()));
            }
            if (orgMap.containsKey(e.getInspectCollegeId())) {
                e.setInspectCollegeName(orgMap.get(e.getInspectCollegeId()));
            }
        }).collect(Collectors.toList()));
        log.info("表 't_a_exam_course_college_inspect_dio' 更新完成");
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void changeTableTAExamCourseCollegePaperStruct() {
        Map<String, String> courseMap = this.findCourseMap();
        Map<Long, String> orgMap = this.findOrgMap();
        taExamCourseCollegePaperStructService.updateBatchById(taExamCourseCollegePaperStructService.list().stream().peek(e -> {
            if (courseMap.containsKey(e.getCourseCode())) {
                e.setCourseName(courseMap.get(e.getCourseCode()));
            }
            if (orgMap.containsKey(e.getInspectCollegeId())) {
                e.setInspectCollegeName(orgMap.get(e.getInspectCollegeId()));
            }
        }).collect(Collectors.toList()));
        log.info("表 't_a_exam_course_college_paper_struct' 更新完成");
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void changeTableTAExamCourseCollegeTeacher() {
        Map<String, String> courseMap = this.findCourseMap();
        Map<Long, String> orgMap = this.findOrgMap();
        Map<Long, String> userMap = this.findUserIdNameMap();
        taExamCourseCollegeTeacherService.updateBatchById(taExamCourseCollegeTeacherService.list().stream().peek(e -> {
            if (courseMap.containsKey(e.getCourseCode())) {
                e.setCourseName(courseMap.get(e.getCourseCode()));
            }
            if (orgMap.containsKey(e.getInspectCollegeId())) {
                e.setInspectCollegeName(orgMap.get(e.getInspectCollegeId()));
            }
            if (orgMap.containsKey(e.getTeachCollegeId())) {
                e.setTeachCollegeName(orgMap.get(e.getTeachCollegeId()));
            }
            if (userMap.containsKey(e.getTeacherId())) {
                e.setTeacherName(userMap.get(e.getTeacherId()));
            }
        }).collect(Collectors.toList()));
        log.info("表 't_a_exam_course_college_teacher' 更新完成");
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void changeTableTAExamCourseDifficultService() {
        Map<String, String> courseMap = this.findCourseMap();
        taExamCourseDifficultService.updateBatchById(taExamCourseDifficultService.list().stream()
                .filter(e -> courseMap.containsKey(e.getCourseCode()))
                .peek(e -> e.setCourseName(courseMap.get(e.getCourseCode())))
                .collect(Collectors.toList()));
        log.info("表 't_a_exam_course_difficult' 更新完成");
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void changeTableTAExamCourseDio() {
        Map<String, String> courseMap = this.findCourseMap();
        taExamCourseDioService.updateBatchById(taExamCourseDioService.list().stream().filter(e -> courseMap.containsKey(e.getCourseCode())).peek(e -> e.setCourseName(courseMap.get(e.getCourseCode()))).collect(Collectors.toList()));
        log.info("表 't_a_exam_course_dio' 更新完成");
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void changeTableTAExamCourseRecordDio() {
        Map<String, String> courseMap = this.findCourseMap();
        Map<Long, String> orgMap = this.findOrgMap();
        Map<Long, String> userMap = this.findUserIdNameMap();
        List<TAExamCourseRecordDio> taExamCourseRecordDioList = taExamCourseRecordDioService.list().stream().peek(e -> {
            if (courseMap.containsKey(e.getCourseCode())) {
                e.setCourseName(courseMap.get(e.getCourseCode()));
            }
            if (orgMap.containsKey(e.getInspectCollegeId())) {
                e.setInspectCollegeName(orgMap.get(e.getInspectCollegeId()));
            }
            if (userMap.containsKey(e.getTeacherId())) {
                e.setTeacherName(userMap.get(e.getTeacherId()));
            }
        }).collect(Collectors.toList());
        List<List<TAExamCourseRecordDio>> stepList = SystemConstant.getStepList(taExamCourseRecordDioList);
        stepList.forEach(e -> complicatedCalculateAsyncService.updateTableTAExamCourseRecordDioBatchById(e));
//        log.info("表 't_a_exam_course_record_dio' 更新完成");
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void changeTableTAExamCourseRecordMod() {
        Map<String, String> courseMap = this.findCourseMap();
        Map<Long, String> orgMap = this.findOrgMap();
        taExamCourseRecordModService.updateBatchById(taExamCourseRecordModService.list().stream().peek(e -> {
            if (courseMap.containsKey(e.getCourseCode())) {
                e.setCourseName(courseMap.get(e.getCourseCode()));
            }
            if (orgMap.containsKey(e.getInspectCollegeId())) {
                e.setInspectCollegeName(orgMap.get(e.getInspectCollegeId()));
            }
        }).collect(Collectors.toList()));
        log.info("表 't_a_exam_course_record_mod' 更新完成");
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void changeTableTAExamCourseTeacher() {
        Map<String, String> courseMap = this.findCourseMap();
        Map<Long, String> userMap = this.findUserIdNameMap();
        taExamCourseTeacherService.updateBatchById(taExamCourseTeacherService.list().stream().peek(e -> {
            if (courseMap.containsKey(e.getCourseCode())) {
                e.setCourseName(courseMap.get(e.getCourseCode()));
            }
            if (userMap.containsKey(e.getTeacherId())) {
                e.setTeacherName(userMap.get(e.getTeacherId()));
            }
        }).collect(Collectors.toList()));
        log.info("表 't_a_exam_course_teacher' 更新完成");
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void changeTableTAExamCourseTeacherCollegeDifficult() {
        Map<String, String> courseMap = this.findCourseMap();
        Map<Long, String> orgMap = this.findOrgMap();
        Map<Long, String> userMap = this.findUserIdNameMap();
        taExamCourseTeacherCollegeDifficultService.updateBatchById(taExamCourseTeacherCollegeDifficultService.list().stream().peek(e -> {
            if (courseMap.containsKey(e.getCourseCode())) {
                e.setCourseName(courseMap.get(e.getCourseCode()));
            }
            if (orgMap.containsKey(e.getInspectCollegeId())) {
                e.setInspectCollegeName(orgMap.get(e.getInspectCollegeId()));
            }
            if (userMap.containsKey(e.getTeacherId())) {
                e.setTeacherName(userMap.get(e.getTeacherId()));
            }
        }).collect(Collectors.toList()));
        log.info("表 't_a_exam_course_teacher_college_difficult' 更新完成");
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void changeTableTAExamCourseTeacherCollegeDio() {
        Map<String, String> courseMap = this.findCourseMap();
        Map<Long, String> orgMap = this.findOrgMap();
        Map<Long, String> userMap = this.findUserIdNameMap();
        taExamCourseTeacherCollegeDioService.updateBatchById(taExamCourseTeacherCollegeDioService.list().stream().peek(e -> {
            if (courseMap.containsKey(e.getCourseCode())) {
                e.setCourseName(courseMap.get(e.getCourseCode()));
            }
            if (orgMap.containsKey(e.getInspectCollegeId())) {
                e.setInspectCollegeName(orgMap.get(e.getInspectCollegeId()));
            }
            if (userMap.containsKey(e.getTeacherId())) {
                e.setTeacherName(userMap.get(e.getTeacherId()));
            }
        }).collect(Collectors.toList()));
        log.info("表 't_a_exam_course_teacher_college_dio' 更新完成");
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void changeTableTAExamCourseTeacherCollegePaperStruct() {
        Map<String, String> courseMap = this.findCourseMap();
        Map<Long, String> orgMap = this.findOrgMap();
        Map<Long, String> userMap = this.findUserIdNameMap();
        taExamCourseTeacherCollegePaperStructService.updateBatchById(taExamCourseTeacherCollegePaperStructService.list().stream().peek(e -> {
            if (courseMap.containsKey(e.getCourseCode())) {
                e.setCourseName(courseMap.get(e.getCourseCode()));
            }
            if (orgMap.containsKey(e.getInspectCollegeId())) {
                e.setInspectCollegeName(orgMap.get(e.getInspectCollegeId()));
            }
            if (userMap.containsKey(e.getTeacherId())) {
                e.setTeacherName(userMap.get(e.getTeacherId()));
            }
        }).collect(Collectors.toList()));
        log.info("表 't_a_exam_course_teacher_college_paper_struct' 更新完成");
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void changeTableTAExamCourseTeacherDifficult() {
        Map<String, String> courseMap = this.findCourseMap();
        Map<Long, String> userMap = this.findUserIdNameMap();
        taExamCourseTeacherDifficultService.updateBatchById(taExamCourseTeacherDifficultService.list().stream().peek(e -> {
            if (courseMap.containsKey(e.getCourseCode())) {
                e.setCourseName(courseMap.get(e.getCourseCode()));
            }
            if (userMap.containsKey(e.getTeacherId())) {
                e.setTeacherName(userMap.get(e.getTeacherId()));
            }
        }).collect(Collectors.toList()));
        log.info("表 't_a_exam_course_teacher_difficult' 更新完成");
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void changeTableTAExamCourseTeacherDio() {
        Map<String, String> courseMap = this.findCourseMap();
        Map<Long, String> userMap = this.findUserIdNameMap();
        taExamCourseTeacherDioService.updateBatchById(taExamCourseTeacherDioService.list().stream().peek(e -> {
            if (courseMap.containsKey(e.getCourseCode())) {
                e.setCourseName(courseMap.get(e.getCourseCode()));
            }
            if (userMap.containsKey(e.getTeacherId())) {
                e.setTeacherName(userMap.get(e.getTeacherId()));
            }
        }).collect(Collectors.toList()));
        log.info("表 't_a_exam_course_teacher_dio' 更新完成");
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void changeTableTAExamCourseTeacherPaperStruct() {
        Map<String, String> courseMap = this.findCourseMap();
        Map<Long, String> userMap = this.findUserIdNameMap();
        taExamCourseTeacherPaperStructService.updateBatchById(taExamCourseTeacherPaperStructService.list().stream().peek(e -> {
            if (courseMap.containsKey(e.getCourseCode())) {
                e.setCourseName(courseMap.get(e.getCourseCode()));
            }
            if (userMap.containsKey(e.getTeacherId())) {
                e.setTeacherName(userMap.get(e.getTeacherId()));
            }
        }).collect(Collectors.toList()));
        log.info("表 't_a_exam_course_teacher_paper_struct' 更新完成");
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void changeTableTAExamTotal() {
        List<SysOrg> sysOrgList = sysOrgService.list();
        List<TAExamTotal> taExamTotalList = taExamTotalService.list();
        for (TAExamTotal taExamTotal : taExamTotalList) {
            String collegeNameStr = taExamTotal.getCollegeNames();
            List<String> newCollegeList = new ArrayList<>();
            String[] collegeNameList = collegeNameStr.split("、");
            for (String s : collegeNameList) {
                List<SysOrg> cellList = sysOrgList.stream().filter(e -> e.getHistoricName().equals(s) && e.getSchoolId().equals(taExamTotal.getSchoolId())).collect(Collectors.toList());
                if (cellList.size() > 0) {
                    if (cellList.size() > 1) {
                        throw ExceptionResultEnum.ERROR.exception("数据异常");
                    }
                    SysOrg cell = cellList.get(0);
                    newCollegeList.add(cell.getName());
                } else {
                    // 如果没有匹配到的话就按照原来的学院名称（因为演示方法可能多次执行，但执行完成一次后机构表的历史数据记录不会替换，下次执行就可能导致由于历史名称和机构名称组不匹配引发丢失机构）
                    newCollegeList.add(s);
                }
            }
            if (newCollegeList.size() > 0) {
                taExamTotal.setCollegeNames(String.join("、", newCollegeList));
            }
        }
        log.info("表 't_a_exam_total' 更新完成");
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void changeTableTAPaperStruct() {
        Map<String, String> courseMap = this.findCourseMap();
        taPaperStructService.updateBatchById(taPaperStructService.list().stream().peek(e -> {
            if (courseMap.containsKey(e.getCourseCode())) {
                e.setCourseName(courseMap.get(e.getCourseCode()));
            }
        }).collect(Collectors.toList()));
        log.info("表 't_a_paper_struct' 更新完成");
    }
}
