package cn.com.taiji.video.task;


import cn.com.taiji.video.model.HtDpVideoAnalysis;
import cn.com.taiji.video.service.IHtDpVideoAnalysisService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hikvision.artemis.sdk.ArtemisHttpUtil;
import com.hikvision.artemis.sdk.config.ArtemisConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description
 *
 * @Author  yangyue
 * @Date 2023/8/4
 */
@Component
@Slf4j
public class TimeTask {

    @Autowired
    private IHtDpVideoAnalysisService videoAnalysisService;

    @Value("${taiji.faceTaskId}")
    private String faceTaskId;

    @Value("${taiji.carTaskId}")
    private String carTaskId;

    @Value("${taiji.url}")
    private String analysisUrl;

    @Value("${taiji.ak}")
    private String ak;

    @Value("${taiji.sk}")
    private String sk;


    @Scheduled(cron = "0 0 5 * * ?")
    public void scheduleOne() {
        log.info("===========定时开始===========");
        LocalDate localDate = LocalDate.now().plusDays(-1);
        log.info("统计日期：{}",localDate);
        log.info("获取视频解析人员");
        this.querytime(1,localDate);
        log.info("获取视频解析车辆");
        this.querytime(2,localDate);
        log.info("===========定时结束===========");
    }

    public void querytime(Integer type, LocalDate localDate) {
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM");
        ArtemisConfig.host=analysisUrl;
        ArtemisConfig.appKey=ak;
        ArtemisConfig.appSecret=sk;
        final String artemispath = "/artemis";
        long startTimestamp = LocalDateTime.of(localDate, LocalTime.MIN).toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
        long endTimestamp = LocalDateTime.of(localDate, LocalTime.MAX).toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
        log.info("开始时间：{} ；结束时间：{}",startTimestamp,endTimestamp);
        HtDpVideoAnalysis videoAnalysis = new HtDpVideoAnalysis();
        videoAnalysis.setYear(String.valueOf(localDate.getYear()));
        videoAnalysis.setYearMonth(localDate.format(fmt));
        videoAnalysis.setStatDate(localDate);
        videoAnalysis.setType(type);
        String taskId = carTaskId;
        if (1 == type) {
            taskId = faceTaskId;
        }
        videoAnalysis.setTaskId(taskId);
        String previewurlsapi = artemispath + "/api/v2/tasks/picture/statistics/"+taskId+"/"+ startTimestamp +"/"+endTimestamp;
        log.info(previewurlsapi);

        Map<String, String> path = new HashMap<String, String>(2) {
            {
                put("https://", previewurlsapi);//根据现场环境部署确认是http还是https
            }
        };
        String contentType = "application/json";
        // post请求application/json类型参数
        String result = ArtemisHttpUtil.doGetArtemis(path, null, null, contentType, null);
        log.info(result);
        JSONObject jsonObject = JSONObject.parseObject(result).getJSONObject("data");
        videoAnalysis.setPicAnalysisTotal(jsonObject.getInteger("picAnalysisTotal"));
        videoAnalysis.setPicAnalysisFailCount(jsonObject.getInteger("picAnalysisFailCount"));
        videoAnalysis.setPicAnalysisSuccessCount(jsonObject.getInteger("picAnalysisSuccessCount"));
        videoAnalysis.setPicCaptureTotal(jsonObject.getInteger("picCaptureTotal"));
        videoAnalysis.setPicFilterCount(jsonObject.getInteger("picFilterCount"));
        videoAnalysis.setPicSubmitFailCount(jsonObject.getInteger("picSubmitFailCount"));
        videoAnalysis.setPicSubmitSuccessCount(jsonObject.getInteger("picSubmitSuccessCount"));
        videoAnalysisService.saveOrUpdate(
                videoAnalysis,
                Wrappers.<HtDpVideoAnalysis>lambdaUpdate()
                        .eq(HtDpVideoAnalysis::getType,type)
                        .eq(HtDpVideoAnalysis::getStatDate,localDate));
    }

}
