package com.xjrsoft.module.job;

import cn.hutool.extra.spring.SpringUtil;
import com.xjrsoft.common.mybatis.SqlRunnerAdapter;
import com.xjrsoft.common.utils.SqlRunnerAdapterUtil;
import com.xjrsoft.module.hikvision.entity.HikvisionData;
import com.xjrsoft.module.hikvision.mapper.HikvisionDataMapper;
import com.xjrsoft.module.hikvision.util.DataUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
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/5/8
 */
@Component
@Slf4j
public class HikvisionBaseDataTask {

    @Autowired
    private HikvisionDataMapper hikvisionDataMapper;

    @Scheduled(cron = "0 */15 * * * ?")
    public void execute() {
        RefreshConnectionPool();
    }
    public void RefreshConnectionPool() {
        String active = SpringUtil.getActiveProfile();
        if(!"prod".equals(active)){
            log.info("非正式环境，无法执行数据推送");
            return;
        }
        log.info("开始推送海康威视基础数据");
        try {
            String sql = "SELECT distinct table_name FROM hikvision_data WHERE 1 = 1";
            List<Map<String, Object>> maps = SqlRunnerAdapter.db().selectList(sql, HikvisionData.class);
            Set<String> tables = new HashSet<>();
            for (Map<String, Object> map : maps) {
                String[] strings = SqlRunnerAdapterUtil.convertMapToStringArray(map);
                tables.add(strings[0]);
            }
            sql = "SELECT * FROM hikvision_data WHERE 0 = 0";
            List<Map<String, Object>> list = SqlRunnerAdapter.db().selectList(sql, HikvisionData.class);
            Map<String, Map<String, String>> dataMap = new HashMap<>();
            for (String table : tables) {
                Map<String, String> tableData = new HashMap<>();
                for (Map<String, Object> hikvision : list) {

                    if(!table.equals(hikvision.get("table_name").toString())){
                        continue;
                    }
                    tableData.put(hikvision.get("source_id").toString(), hikvision.get("hikvision_id").toString());
                }
                dataMap.put(table, tableData);
            }
            DataUtil dataUtil = new DataUtil();
            //查询根机构
            //JsonArray schoolDistrictData = dataUtil.getSchoolDistrictData();
            //推送组织机构
            String tableName = "xjr_department";
            Map<String, String> department = new HashMap<>();
            try {
                department = dataUtil.insertDepartment(tableName, dataMap.get(tableName));
            }catch (Exception e){
                log.info("推送组织机构报错", e);
            }
            if(department.isEmpty() && dataMap.get(tableName) != null){
                department = dataMap.get(tableName);
            }


            //推送教职工
            tableName = "base_teacher";
            try {
                dataUtil.insertTeacher(tableName, dataMap.get(tableName), department);
            }catch (Exception e){
                log.info("推送教职工报错", e);
            }

            //推送车辆
            String carTableName = "car_message_apply";
            Map<String, String> baseCar = dataMap.get(carTableName);
            try {
                dataUtil.insertCar(carTableName, baseCar, hikvisionDataMapper);
            }catch (Exception e){
                log.info("同步车辆数据报错", e);
            }


            tableName = "base_class";
            Map<String, String> baseClass = dataMap.get(tableName);
            try {
                dataUtil.insertClass(tableName, baseClass);
            }catch (Exception e){
                log.info("同步车辆数据报错", e);
            }


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

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