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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qmth.teachcloud.report.business.entity.*;
import com.qmth.teachcloud.report.business.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

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

/**
 * @Description: 机构替换服务实现类
 * @Author: CaoZixuan
 * @Date: 2022-04-01
 */
@Service
public class InsteadOrgServiceImpl implements InsteadOrgService {
    @Resource
    private TBExamStudentService tbExamStudentService;
    @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 TAExamCourseRecordDioService taExamCourseRecordDioService;
    @Resource
    private TAExamCourseRecordModService taExamCourseRecordModService;
    @Resource
    private TAExamCourseTeacherCollegeDifficultService taExamCourseTeacherCollegeDifficultService;
    @Resource
    private TAExamCourseTeacherCollegeDioService taExamCourseTeacherCollegeDioService;
    @Resource
    private TAExamCourseTeacherCollegePaperStructService taExamCourseTeacherCollegePaperStructService;
    @Resource
    private TAExamTotalService taExamTotalService;
    @Resource
    private TAExamCourseRecordService taExamCourseRecordService;

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

    @Override
    public void insteadOrgBasicTable(Long schoolId, Map<Long, Long> orgIdMap) {
        Set<Long> orgIdSet = orgIdMap.keySet();
        // 更新表 't_b_exam_student' 考察机构id
        tbExamStudentService.updateBatchById(tbExamStudentService.list(new QueryWrapper<TBExamStudent>()
                        .lambda()
                        .eq(TBExamStudent::getSchoolId, schoolId)
                        .in(TBExamStudent::getInspectCollegeId, orgIdSet))
                .stream().peek(e -> {
                    if (orgIdMap.containsKey(e.getInspectCollegeId())) {
                        e.setInspectCollegeId(orgIdMap.get(e.getInspectCollegeId()));
                    }
                }).collect(Collectors.toSet()));
        log.info("'t_b_exam_student' 表更新成功");

        // 更新表 't_b_teacher' 机构id 把原考察机构换成开课机构（大抵教师都帮在开课学院下的）
        tbTeacherService.updateBatchById(tbTeacherService.list(new QueryWrapper<TBTeacher>()
                        .lambda()
                        .eq(TBTeacher::getSchoolId, schoolId)
                        .in(TBTeacher::getOrgId, orgIdSet))
                .stream().peek(e -> {
                    if (orgIdMap.containsKey(e.getOrgId())) {
                        e.setOrgId(orgIdMap.get(e.getOrgId()));
                    }
                }).collect(Collectors.toSet()));
        log.info("'t_b_teacher' 表更新成功");
    }

    @Override
    public void insteadOrgTAExamCourse(Long schoolId, Map<String, String> orgNameMap) {
        List<TAExamCourse> taExamCourseList = taExamCourseService.list(new QueryWrapper<TAExamCourse>().lambda().eq(TAExamCourse::getSchoolId, schoolId));
        taExamCourseService.updateBatchById(taExamCourseList.stream().peek(e -> {
            final String separator = "、";
            StringBuilder newStr = new StringBuilder();
            String inspectCollegeStr = e.getInspectCollegeNames();
            String[] inspectCollegeArr = inspectCollegeStr.split(separator);
            for (String s : inspectCollegeArr) {
                if (orgNameMap.containsKey(s)) {
                    s = orgNameMap.get(s);
                }
                newStr.append(s).append(separator);
            }
            e.setInspectCollegeNames(newStr.substring(0, newStr.length() - separator.length()));
        }).collect(Collectors.toSet()));
        log.info("'t_a_exam_course' 表更新成功");
    }

    @Override
    public void insteadOrgTAExamCourseClazz(Long schoolId, Map<String, String> orgNameMap) {
        taExamCourseClazzService.updateBatchById(taExamCourseClazzService.list(new QueryWrapper<TAExamCourseClazz>()
                        .lambda()
                        .eq(TAExamCourseClazz::getSchoolId, schoolId))
                .stream().peek(e -> {
                    if (orgNameMap.containsKey(e.getCollegeName())) {
                        e.setCollegeName(orgNameMap.get(e.getCollegeName()));
                    }
                }).collect(Collectors.toSet()));
        log.info("'t_a_exam_course_clazz' 表更新成功");
    }

    @Override
    public void insteadOrgTAExamCourseCollegeInspect(Long schoolId, Map<Long, Long> orgIdMap) {
        taExamCourseCollegeInspectService.updateBatchById(taExamCourseCollegeInspectService.list(new QueryWrapper<TAExamCourseCollegeInspect>()
                        .lambda()
                        .eq(TAExamCourseCollegeInspect::getSchoolId, schoolId)
                        .in(TAExamCourseCollegeInspect::getCollegeId, orgIdMap.keySet()))
                .stream().peek(e -> {
                    if (orgIdMap.containsKey(e.getCollegeId())) {
                        e.setCollegeId(orgIdMap.get(e.getCollegeId()));
                    }
                }).collect(Collectors.toSet()));
        log.info("'t_a_exam_course_college_inspect' 表更新成功");
    }

    @Override
    public void insteadOrgTAExamCourseCollegeInspectDio(Long schoolId, Map<Long, Long> orgIdMap, Map<String, String> orgNameMap) {
        taExamCourseCollegeInspectDioService.updateBatchById(taExamCourseCollegeInspectDioService.list(new QueryWrapper<TAExamCourseCollegeInspectDio>()
                        .lambda()
                        .eq(TAExamCourseCollegeInspectDio::getSchoolId, schoolId)
                        .in(TAExamCourseCollegeInspectDio::getInspectCollegeId, orgIdMap.keySet()))
                .stream()
                .peek(e -> {
                    if (orgIdMap.containsKey(e.getInspectCollegeId())) {
                        e.setInspectCollegeId(orgIdMap.get(e.getInspectCollegeId()));
                    }
                    if (orgNameMap.containsKey(e.getInspectCollegeName())) {
                        e.setInspectCollegeName(orgNameMap.get(e.getInspectCollegeName()));
                    }
                }).collect(Collectors.toSet()));
        log.info("'t_a_exam_course_college_inspect_dio' 表更新成功");
    }

    @Override
    public void insteadOrgTAExamCourseCollegePaperStruct(Long schoolId, Map<Long, Long> orgIdMap, Map<String, String> orgNameMap) {
        taExamCourseCollegePaperStructService.updateBatchById(taExamCourseCollegePaperStructService.list(new QueryWrapper<TAExamCourseCollegePaperStruct>()
                        .lambda()
                        .eq(TAExamCourseCollegePaperStruct::getSchoolId, schoolId)
                        .in(TAExamCourseCollegePaperStruct::getInspectCollegeId, orgIdMap.keySet()))
                .stream()
                .peek(e -> {
                    if (orgIdMap.containsKey(e.getInspectCollegeId())) {
                        e.setInspectCollegeId(orgIdMap.get(e.getInspectCollegeId()));
                    }
                    if (orgNameMap.containsKey(e.getInspectCollegeName())) {
                        e.setInspectCollegeName(orgNameMap.get(e.getInspectCollegeName()));
                    }
                }).collect(Collectors.toSet()));
        log.info("'t_a_exam_course_college_paper_struct' 表更新成功");
    }

    @Override
    public void insteadOrgTAExamCourseCollegeTeacher(Long schoolId, Map<Long, Long> orgIdMap, Map<String, String> orgNameMap) {
        taExamCourseCollegeTeacherService.updateBatchById(taExamCourseCollegeTeacherService.list(new QueryWrapper<TAExamCourseCollegeTeacher>()
                        .lambda()
                        .eq(TAExamCourseCollegeTeacher::getSchoolId, schoolId)
                        .in(TAExamCourseCollegeTeacher::getInspectCollegeId, orgIdMap.keySet()))
                .stream()
                .peek(e -> {
                    if (orgIdMap.containsKey(e.getInspectCollegeId())) {
                        e.setInspectCollegeId(orgIdMap.get(e.getInspectCollegeId()));
                    }
                    if (orgNameMap.containsKey(e.getInspectCollegeName())) {
                        e.setInspectCollegeName(orgNameMap.get(e.getInspectCollegeName()));
                    }
                }).collect(Collectors.toSet()));
        log.info("'t_a_exam_course_college_teacher' 表更新成功");
    }

    @Override
    public void insteadOrgTAExamCourseDifficult(Long schoolId, Map<Long, Long> orgIdMap) {
        taExamCourseDifficultService.updateBatchById(taExamCourseDifficultService.list(new QueryWrapper<TAExamCourseDifficult>()
                        .lambda()
                        .eq(TAExamCourseDifficult::getSchoolId, schoolId)
                        .in(TAExamCourseDifficult::getCollegeId, orgIdMap.keySet()))
                .stream()
                .peek(e -> {
                    if (orgIdMap.containsKey(e.getCollegeId())) {
                        e.setCollegeId(orgIdMap.get(e.getCollegeId()));
                    }
                }).collect(Collectors.toSet()));
        log.info("'t_a_exam_course_difficult' 表更新成功");
    }

    @Override
    public void insteadOrgTAExamCourseRecord(Long schoolId, Map<Long, Long> orgIdMap) {
        taExamCourseRecordService.updateBatchById(taExamCourseRecordService.list(new QueryWrapper<TAExamCourseRecord>()
                        .lambda()
                        .eq(TAExamCourseRecord::getSchoolId, schoolId)
                        .in(TAExamCourseRecord::getInspectCollegeId, orgIdMap.keySet()))
                .stream()
                .peek(e -> {
                    if (orgIdMap.containsKey(e.getInspectCollegeId())) {
                        e.setInspectCollegeId(orgIdMap.get(e.getInspectCollegeId()));
                    }
                }).collect(Collectors.toSet()));
        log.info("'t_a_exam_course_record' 表更新成功");
    }

    @Override
    public void insteadOrgTAExamCourseRecordDio(Long schoolId, Map<Long, Long> orgIdMap, Map<String, String> orgNameMap) {
        taExamCourseRecordDioService.updateBatchById(taExamCourseRecordDioService.list(new QueryWrapper<TAExamCourseRecordDio>()
                        .lambda()
                        .eq(TAExamCourseRecordDio::getSchoolId, schoolId)
                        .in(TAExamCourseRecordDio::getInspectCollegeId, orgIdMap.keySet()))
                .stream()
                .peek(e -> {
                    if (orgIdMap.containsKey(e.getInspectCollegeId())) {
                        e.setInspectCollegeId(orgIdMap.get(e.getInspectCollegeId()));
                    }
                    if (orgNameMap.containsKey(e.getInspectCollegeName())) {
                        e.setInspectCollegeName(orgNameMap.get(e.getInspectCollegeName()));
                    }
                }).collect(Collectors.toSet()));
        log.info("'t_a_exam_course_record_dio' 表更新成功");
    }

    @Override
    public void insteadOrgTAExamCourseRecordMod(Long schoolId, Map<Long, Long> orgIdMap, Map<String, String> orgNameMap) {
        taExamCourseRecordModService.updateBatchById(taExamCourseRecordModService.list(new QueryWrapper<TAExamCourseRecordMod>()
                        .lambda()
                        .eq(TAExamCourseRecordMod::getSchoolId, schoolId)
                        .in(TAExamCourseRecordMod::getInspectCollegeId, orgIdMap.keySet()))
                .stream()
                .peek(e -> {
                    if (orgIdMap.containsKey(e.getInspectCollegeId())) {
                        e.setInspectCollegeId(orgIdMap.get(e.getInspectCollegeId()));
                    }
                    if (orgNameMap.containsKey(e.getInspectCollegeName())) {
                        e.setInspectCollegeName(orgNameMap.get(e.getInspectCollegeName()));
                    }
                }).collect(Collectors.toSet()));
        log.info("'t_a_exam_course_record_mod' 表更新成功");
    }


    @Override
    public void insteadOrgTAExamCourseTeacherCollegeDifficult(Long schoolId, Map<Long, Long> orgIdMap, Map<String, String> orgNameMap) {
        taExamCourseTeacherCollegeDifficultService.updateBatchById(taExamCourseTeacherCollegeDifficultService.list(new QueryWrapper<TAExamCourseTeacherCollegeDifficult>()
                        .lambda()
                        .eq(TAExamCourseTeacherCollegeDifficult::getSchoolId, schoolId)
                        .in(TAExamCourseTeacherCollegeDifficult::getInspectCollegeId, orgIdMap.keySet()))
                .stream()
                .peek(e -> {
                    if (orgIdMap.containsKey(e.getInspectCollegeId())) {
                        e.setInspectCollegeId(orgIdMap.get(e.getInspectCollegeId()));
                    }
                    if (orgNameMap.containsKey(e.getInspectCollegeName())) {
                        e.setInspectCollegeName(orgNameMap.get(e.getInspectCollegeName()));
                    }
                }).collect(Collectors.toSet()));
        log.info("'t_a_exam_course_teacher_college_difficult' 表更新成功");
    }

    @Override
    public void insteadOrgTAExamCourseTeacherCollegeDio(Long schoolId, Map<Long, Long> orgIdMap, Map<String, String> orgNameMap) {
        taExamCourseTeacherCollegeDioService.updateBatchById(taExamCourseTeacherCollegeDioService.list(new QueryWrapper<TAExamCourseTeacherCollegeDio>()
                        .lambda()
                        .eq(TAExamCourseTeacherCollegeDio::getSchoolId, schoolId)
                        .in(TAExamCourseTeacherCollegeDio::getInspectCollegeId, orgIdMap.keySet()))
                .stream()
                .peek(e -> {
                    if (orgIdMap.containsKey(e.getInspectCollegeId())) {
                        e.setInspectCollegeId(orgIdMap.get(e.getInspectCollegeId()));
                    }
                    if (orgNameMap.containsKey(e.getInspectCollegeName())) {
                        e.setInspectCollegeName(orgNameMap.get(e.getInspectCollegeName()));
                    }
                }).collect(Collectors.toSet()));
        log.info("'t_a_exam_course_teacher_college_dio' 表更新成功");
    }

    @Override
    public void insteadOrgTAExamCourseTeacherCollegePaperStruct(Long schoolId, Map<Long, Long> orgIdMap, Map<String, String> orgNameMap) {
        taExamCourseTeacherCollegePaperStructService.updateBatchById(taExamCourseTeacherCollegePaperStructService.list(new QueryWrapper<TAExamCourseTeacherCollegePaperStruct>()
                        .lambda()
                        .eq(TAExamCourseTeacherCollegePaperStruct::getSchoolId, schoolId)
                        .in(TAExamCourseTeacherCollegePaperStruct::getInspectCollegeId, orgIdMap.keySet()))
                .stream()
                .peek(e -> {
                    if (orgIdMap.containsKey(e.getInspectCollegeId())) {
                        e.setInspectCollegeId(orgIdMap.get(e.getInspectCollegeId()));
                    }
                    if (orgNameMap.containsKey(e.getInspectCollegeName())) {
                        e.setInspectCollegeName(orgNameMap.get(e.getInspectCollegeName()));
                    }
                }).collect(Collectors.toSet()));
        log.info("'t_a_exam_course_teacher_college_paper_struct' 表更新成功");
    }

    @Override
    public void insteadOrgTAExamTotal(Long schoolId, Map<String, String> orgNameMap) {
        List<TAExamTotal> taExamTotalList = taExamTotalService.list(new QueryWrapper<TAExamTotal>().lambda().eq(TAExamTotal::getSchoolId, schoolId));
        taExamTotalService.updateBatchById(taExamTotalList.stream().peek(e -> {
            final String separator = "、";
            StringBuilder newStr = new StringBuilder();
            String inspectCollegeStr = e.getCollegeNames();
            String[] inspectCollegeArr = inspectCollegeStr.split(separator);
            for (String s : inspectCollegeArr) {
                if (orgNameMap.containsKey(s)) {
                    s = orgNameMap.get(s);
                }
                newStr.append(s).append(separator);
            }
            e.setCollegeNames(newStr.substring(0, newStr.length() - separator.length()));
        }).collect(Collectors.toSet()));
        log.info("'t_a_exam_total' 表更新成功");
    }
}
