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.util.Out_In_RecordUtil;
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;

/**
 * @author dzx
 * @date 2024/5/8
 */
@Component
@Slf4j
public class InsertOutInRecordTask {
    @Autowired
    private FaceImportMapper faceImportMapper;

    Out_In_RecordUtil out_in_recordUtil = new Out_In_RecordUtil();

    @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);

            //教师&学生拉取数据
            out_in_recordUtil.GetTeacherAndStudentRecords(use, faceImportMapper);
            //拉取车辆数据
            out_in_recordUtil.GetVehicleRecord(use, faceImportMapper);

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

