package com.xjrsoft.module.job;

import cn.hutool.db.Db;
import cn.hutool.extra.spring.SpringUtil;
import com.xjrsoft.common.constant.GlobalConstant;
import com.xjrsoft.common.utils.DatasourceUtil;
import com.xjrsoft.module.hikvision.entity.HikvisionData;
import com.xjrsoft.module.hikvision.util.DataUtil;
import com.xjrsoft.module.schedule.entity.JianyueData;
import com.xjrsoft.module.teacher.mapper.FaceImportMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
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/5/8
 */
@Component
@Slf4j
public class HikvisionBaseDataTask {
    @Autowired
    private FaceImportMapper faceImportMapper;

    @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 hikvision_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 hikvision_data WHERE 0 = 0";
            List<HikvisionData> list = use.query(sql, HikvisionData.class);
            Map<String, Map<String, String>> dataMap = new HashMap<>();
            for (String table : tables) {
                Map<String, String> tableData = new HashMap<>();
                for (HikvisionData hikvisiondata : list) {
                    if(!table.equals(hikvisiondata.getTableName())){
                        continue;
                    }
                    tableData.put(hikvisiondata.getSourceId(), hikvisiondata.getHikvisionId());
                }
                dataMap.put(table, tableData);
            }
            DataUtil dataUtil = new DataUtil();
            //查询根机构
            //JsonArray schoolDistrictData = dataUtil.getSchoolDistrictData();
            //推送组织机构
            String tableName = "xjr_department";
            Map<String, String> department = dataUtil.insertDepartment(use, tableName, dataMap.get(tableName));
            if(department.isEmpty() && dataMap.get(tableName) != null){
                department = dataMap.get(tableName);
            }

            //推送教职工
            tableName = "base_teacher";
            dataUtil.insertTeacher(use, tableName, dataMap.get(tableName), department);

            //推送车辆
            String carTableName = "car_message_apply";
            Map<String, String> baseCar = dataMap.get(carTableName);
            dataUtil.insertCar(use, carTableName, baseCar, faceImportMapper);

            tableName = "base_class";
            Map<String, String> baseClass = dataMap.get(tableName);
            Map<String, String> classes = dataUtil.insertClass(use, tableName, baseClass);


            tableName = "base_student";
            Map<String, String> baseStudent = dataMap.get(tableName);
            Map<String, String> student = dataUtil.insertStudentOne(use, tableName, baseStudent);

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