package com.xjrsoft.module.job;

import cn.hutool.extra.spring.SpringUtil;
import com.xjrsoft.module.hikvision.service.IHikvisionDataService;
import com.xjrsoft.module.hikvision.util.OutInRecordUtil;
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 java.time.LocalDateTime;
import java.util.Map;

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

    OutInRecordUtil out_in_recordUtil = new OutInRecordUtil();

    @Autowired
    private IHikvisionDataService hikvisionDataService;

    @Scheduled(cron = "0 30 0 * * ?")
    public void execute() {
        String active = SpringUtil.getActiveProfile();
        if(!"prod".equals(active)){
            log.info("非正式环境，无法执行数据推送");
            return;
        }
        RefreshConnectionPool();
    }
    public void RefreshConnectionPool() {
        log.info("开始拉取海康威视基础数据");
        try {
            //获取时间，并计算出前一天的开始时间和结束时间
            LocalDateTime now = LocalDateTime.now();
            LocalDateTime startDateTime = now.plusDays(-1).withHour(0).withMinute(0).withSecond(0).withNano(0);
            LocalDateTime endDateTime = startDateTime.plusDays(1).plusSeconds(-1);

            //Map<String, String> userIdMap = hikvisionDataService.getUserIdMap();

            //教师&学生拉取数据
            out_in_recordUtil.GetTeacherAndStudentRecords(faceImportMapper, startDateTime, endDateTime);
            //拉取车辆数据
            out_in_recordUtil.GetVehicleRecord(faceImportMapper, startDateTime, endDateTime);
            //拉取访客数据
            out_in_recordUtil.GetVisitRecord(faceImportMapper, startDateTime, endDateTime);

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

