package com.xjrsoft.module.job;

import cn.hutool.db.Db;
import cn.hutool.extra.spring.SpringUtil;
import com.google.gson.JsonArray;
import com.xjrsoft.common.constant.GlobalConstant;
import com.xjrsoft.common.utils.DatasourceUtil;
import com.xjrsoft.module.schedule.entity.JianyueData;
import com.xjrsoft.module.schedule.util.DataUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
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("开始推送课表基础数据");
        DataSource datasource = DatasourceUtil.getDataSource(GlobalConstant.DEFAULT_DATASOURCE_KEY);
        try {
            Db use = Db.use(datasource);
            String sql = "SELECT distinct table_name FROM jianyue_data WHERE 1 = 1";
            List<JianyueData> query = use.query(sql, JianyueData.class);
            Set<String> tables = new HashSet<>();
            for (JianyueData jianyueData : query) {
                tables.add(jianyueData.getTableName());
            }
            sql = "SELECT * FROM jianyue_data WHERE 0 = 0";
            List<JianyueData> list = use.query(sql, JianyueData.class);
            Map<String, Map<Long, String>> dataMap = new HashMap<>();
            for (String table : tables) {
                Map<Long, String> tableData = new HashMap<>();
                for (JianyueData jianyueData : list) {
                    if(!table.equals(jianyueData.getTableName())){
                        continue;
                    }
                    tableData.put(jianyueData.getSourceId(), jianyueData.getJianyueId());
                }
                dataMap.put(table, tableData);
            }
            DataUtil dataUtil = new DataUtil();
            //查询校区
            JsonArray schoolDistrictData = dataUtil.getSchoolDistrictData();
            //推送年级
            String tableName = "base_grade";
            Map<Long, String> grade = dataUtil.insertGrade(use, tableName, schoolDistrictData.get(0).getAsString(), dataMap.get(tableName));
            if(grade.isEmpty() && dataMap.get(tableName) != null){
                grade = dataMap.get(tableName);
            }

            //推送学期
            tableName = "base_semester";
            dataUtil.insertSemester(use, tableName, dataMap.get(tableName));
            //推送标签
            tableName = "base_label";
            Map<Long, String> tagMap = dataUtil.insertCourseTag(use, tableName, dataMap.get(tableName));
            if(tagMap.isEmpty() && dataMap.get(tableName) != null){
                tagMap = dataMap.get(tableName);
            }
            //推送课程
            tableName = "base_course_subject";
            dataUtil.insertCourse(use, tableName, dataMap.get(tableName), tagMap);
            //推送教职工
            tableName = "base_teacher";
            Map<Long, String> teacherMap = dataUtil.insertTeacher(use, tableName, dataMap.get(tableName));
            if(teacherMap.isEmpty() && dataMap.get(tableName) != null){
                teacherMap = dataMap.get(tableName);
            }
            //推送学生
            tableName = "base_student";
            dataUtil.insertStudent(use, tableName, grade, dataMap.get(tableName));
            //推送教室
            tableName = "base_classroom";
            Map<Long, String> classroomMap = dataUtil.insertClassRoom(use, 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(use, tableName, grade, teacherMap, currenSemeter, dataMap.get(tableName), classroomMap);

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