package com.unis.crk.Scheduler;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.unis.crk.basic.entity.OrgInfo;
import com.unis.crk.basic.service.IOrgInfoService;
import com.unis.crk.basic.service.ISysParamsService;
import com.unis.crk.common.service.IDataCenterUploadService;
import com.unis.crk.common.service.IUpdataLogService;
import com.unis.crk.hardware.controller.CRreader3SetController;
import com.unis.crk.hardware.util.Rfid3Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

//@Component
public class Scheduler {
    //@Scheduled(fixedRate=3000)：上一次开始执行时间点后3秒再次执行；
    //@Scheduled(fixedDelay=3000)：上一次执行完毕时间点3秒再次执行；
    //@Scheduled(initialDelay=1000, fixedDelay=3000)：第一次延迟1秒执行，然后在上一次执行完毕时间点3秒再次执行；
    //@Scheduled(cron="* * * * * ?")：按cron规则执行；
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
    @Autowired
    private IDataCenterUploadService dataCenterUploadService;
    @Autowired
    IUpdataLogService updataLogService;
    @Autowired
    ISysParamsService sysParamsService;
    @Autowired
    CRreader3SetController cRreader3SetController;
    @Autowired
    private IOrgInfoService orgInfoService;

    //每隔10分钟执行一次
    @Scheduled(fixedRate = 1000*60*10)
    public void testTasks1() {
       /* System.out.println("定时任务执行时间：" + dateFormat.format(new Date()));
        JSONObject jsonObject  = updataLogService.uploadData("");
        String errorLog = "";
        String operatLog = "";
        if(!StrUtil.equals("200",jsonObject.getString("status"))){ //有错误
            errorLog = jsonObject.getString("message");
        }else {
            operatLog = jsonObject.getString("message");
        }
        updataLogService.saveUploadLog(jsonObject.getString("unitId"),operatLog,errorLog);*/

    }

    //每隔5分钟执行一次
    @Scheduled(fixedRate = 1000*60*5)
    public void uploadDataTasks() {
      /*   List<OrgInfo> allChildren = orgInfoService.getAllChildren("00", "");
        Iterator<OrgInfo> iterator = allChildren.iterator();
       while (iterator.hasNext()){
            OrgInfo next = iterator.next();
            dataCenterUploadService.uploadSjzxData(next.getOrgId(),"","");
            dataCenterUploadService.deleteSjzxData(next.getOrgId(),"");
        }*/
          System.out.println("定时任务执行时间：" + dateFormat.format(new Date()));
    }
    //每隔1分钟执行一次Rfid超时关闭
    @Scheduled(fixedRate = 1000*60*1)
    public void testTasks() {
        cRreader3SetController.removeRfidService();
    }
    //每隔5分钟执行一次  政务外网下发至应急出入库
    @Scheduled(fixedRate = 1000*60*5)
    public void uploadDataYJCRK() {
     /*   List<OrgInfo> allChildren = orgInfoService.getAllChildren("00", "");
        if(allChildren.size()>4){
        Iterator<OrgInfo> iterator = allChildren.iterator();
        while (iterator.hasNext()){
            OrgInfo next = iterator.next();
            dataCenterUploadService.uploadSjzxData(next.getOrgId(),"","");
            dataCenterUploadService.deleteSjzxData(next.getOrgId(),"");
        }
        }*/
        System.out.println("定时任务执行时间：" + dateFormat.format(new Date()));
    }
    //每隔5分钟执行一次  应急出入库上传数据至政务外网
    @Scheduled(fixedRate = 1000*60*5)
    public void uploadDataCRK() {
  /*      List<OrgInfo> allChildren = orgInfoService.getAllChildren("00", "");
        if(allChildren.size()==1){}
        Iterator<OrgInfo> iterator = allChildren.iterator();
        while (iterator.hasNext()){
            OrgInfo next = iterator.next();
            dataCenterUploadService.uploadSjzxData(next.getOrgId(),"","");
            dataCenterUploadService.deleteSjzxData(next.getOrgId(),"");
        }*/
        System.out.println("定时任务执行时间：" + dateFormat.format(new Date()));
    }
}
