package com.xjrsoft.module.job;

import cn.hutool.extra.spring.SpringUtil;
import com.google.gson.JsonArray;
import com.xjrsoft.common.mybatis.SqlRunnerAdapter;
import com.xjrsoft.common.utils.SqlRunnerAdapterUtil;
import com.xjrsoft.module.schedule.entity.JianyueData;
import com.xjrsoft.module.schedule.util.DataUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author dzx
 * @date 2024/1/12
 */
@Component
@Slf4j
public class JianyuekbBaseDataTask {


    @Scheduled(cron = "0 */15 * * * ?")
    public void RefreshConnectionPool() {
        String active = SpringUtil.getActiveProfile();
        if(!"prod".equals(active)){
            log.info("非正式环境，无法执行数据推送");
            return;
        }
        log.info("开始推送课表基础数据");
        try {
            String sql = "SELECT distinct table_name FROM jianyue_data WHERE 1 = 1";
            List<Map<String, Object>> query = SqlRunnerAdapter.db().selectList(sql, JianyueData.class);
            Set<String> tables = new HashSet<>();
            for (Map<String, Object> queryMap : query) {
                String[] strings = SqlRunnerAdapterUtil.convertMapToStringArray(queryMap);
                tables.add(strings[0]);
            }
            sql = "SELECT * FROM jianyue_data WHERE 0 = 0";
            List<Map<String, Object>> list = SqlRunnerAdapter.db().selectList(sql, JianyueData.class);
            Map<String, Map<String, String>> dataMap = new HashMap<>();
            for (String table : tables) {
                Map<String, String> tableData = new HashMap<>();
                for (Map<String, Object> queryMap : list) {
                    if(!table.equals(queryMap.get("table_name").toString())){
                        continue;
                    }
                    tableData.put(queryMap.get("source_id").toString(), queryMap.get("jianyue_id").toString());
                }
                dataMap.put(table, tableData);
            }
            DataUtil dataUtil = new DataUtil();
            //查询校区
            String tableName = "xjr_department";
            Map<String, String> districtMap = dataUtil.insertSchoolDistrict(tableName, dataMap.get(tableName));
            if(districtMap.isEmpty() && dataMap.get(tableName) != null){
                districtMap = dataMap.get(tableName);
            }

            //推送年级
            JsonArray schoolDistrictData = dataUtil.getSchoolDistrictData();
            tableName = "base_grade";
            Map<String, String> grade = dataUtil.insertGrade(tableName, districtMap, dataMap.get(tableName));
            if(grade.isEmpty() && dataMap.get(tableName) != null){
                grade = dataMap.get(tableName);
            }

            //推送学期
            tableName = "base_semester";
            dataUtil.insertSemester(tableName, dataMap.get(tableName));
            //推送标签
            tableName = "base_label";
            Map<String, String> tagMap = dataUtil.insertCourseTag(tableName, dataMap.get(tableName));
            if(tagMap.isEmpty() && dataMap.get(tableName) != null){
                tagMap = dataMap.get(tableName);
            }
            //推送课程
            tableName = "base_course_subject";
            dataUtil.insertCourse(tableName, dataMap.get(tableName), tagMap);
            //推送教职工
            tableName = "base_teacher";
            Map<String, String> teacherMap = dataUtil.insertTeacher(tableName, dataMap.get(tableName));
            if(teacherMap.isEmpty() && dataMap.get(tableName) != null){
                teacherMap = dataMap.get(tableName);
            }
            //推送学生
            tableName = "base_student";
            dataUtil.insertStudent(tableName, grade, dataMap.get(tableName));
            //推送教室
            tableName = "base_classroom";
            Map<String, String> classroomMap = dataUtil.insertClassRoom(tableName, schoolDistrictData.get(0).getAsString(), dataMap.get(tableName));
            if(classroomMap.isEmpty() && dataMap.get(tableName) != null){
                classroomMap = dataMap.get(tableName);
            }
            //推送行政班，先查询当前学期id
            String currenSemeter = dataUtil.getCurrenSemeter();
            tableName = "base_class";
            dataUtil.insertClass(tableName, grade, teacherMap, currenSemeter, dataMap.get(tableName), classroomMap);

            log.info("数据推送完成");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }
}
