package com.ruoyi.scheduleTask;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.framework.web.service.ConfigService;
import com.ruoyi.monitorInterface.inserctpests.service.PlantDiseasesAndInsectPestsService;
import com.ruoyi.monitorInterface.sennor.service.SennorService;
import org.apache.commons.lang3.time.FastDateFormat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import javax.annotation.Resource;
import java.text.ParseException;
import java.util.Calendar;

/**
 * @author limeng
 * @date 2023年11月30日 8:50
 */
@Configuration
@EnableScheduling
public class StaticScheduleTask {

    protected static final Logger logger = LoggerFactory.getLogger(StaticScheduleTask.class);

    private static FastDateFormat simpleDateFormat = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");

    @Resource
    private SennorService sennorService;

    @Resource
    private PlantDiseasesAndInsectPestsService plantDiseasesAndInsectPestsService;

    @Resource
    private ConfigService configService;

    //添加定时任务 每2小时执行
    @Scheduled(cron = "0 0 0/2 * * ?")
    //添加定时任务 每三分钟执行
//    @Scheduled(cron = "0 */3 * * * ?")
    private void configureTasks() {

        logger.info("执行定时任务=>{}", DateUtils.dateTimeNow());
        String types = configService.getKey("sennorDeviceTypes");
        JSONArray jsonArray = sennorService.getDevideCodeMapByTypes(types);
        for (Object object : jsonArray){
            JSONObject jsonObject = JSONObject.parseObject(object.toString());
            sennorService.getDeviceInfo(jsonObject.getString("device_code"));
        }
    }

    //添加定时任务 每2小时执行
    @Scheduled(cron = "0 0 0/2 * * ?")
    private void configureTask() {

        logger.info("执行定时任务=>{}", DateUtils.dateTimeNow());
        String types = configService.getKey("sennorDeviceType");
        JSONArray jsonArray = plantDiseasesAndInsectPestsService.getDevideCodeMapByType(types);
        for (Object object : jsonArray){
            JSONObject jsonObject = JSONObject.parseObject(object.toString());
            plantDiseasesAndInsectPestsService.plantDiseasesAndInsectPestsMessageReceiver(jsonObject.getString("device_code"));
        }
    }


    /**
     * 比较两个日期大小
     **/
    private boolean daysBefore(String time) {
        boolean result = false;
        try {
            Calendar calendar = getCalendar(time);
            Calendar now = Calendar.getInstance();
            result = calendar.before(now);
        } catch (ParseException e) {
            logger.error(e.getMessage());
        }
        return result;
    }

    /**
     * 将String时间转换为Calendar
     **/
    private static Calendar getCalendar(String time) throws ParseException {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(simpleDateFormat.parse(time));
        return calendar;
    }
}
