package com.huimv.apiservice.task;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.huimv.apiservice.dao.IndoorEnvironmentDao;
import com.huimv.apiservice.dao.OutdoorEnvironmentDao;
import com.huimv.apiservice.entity.IndoorEnvironmentEntity;
import com.huimv.apiservice.entity.OutdoorEnvironmentEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * <p>
 * 定时任务
 * </p>
 *
 * @author yinhao
 * @date 2021/5/26 15:16
 */
@Configuration
@EnableScheduling
@EnableAsync
public class CreateEverydayDataTask {

    @Autowired
    private IndoorEnvironmentDao indoorEnvironmentDao;

    @Autowired
    private OutdoorEnvironmentDao outdoorEnvironmentDao;

    @Async
    @Scheduled(cron = "1 0 0 * * ?")
    void createTodayIndoorEnvData() {

        System.out.println("createTodayIndoorEnvData begin------->");
        DateTime today = DateUtil.beginOfDay(new Date());
        Date collectTime = today;

        List<String> gas = new ArrayList<>();
        gas.add("优");
        gas.add("良");
        gas.add("轻度");
        gas.add("中度");
        gas.add("重度");
        gas.add("严重");

        Random random = new Random();
        List<IndoorEnvironmentEntity> list = new ArrayList<>();
        for (int i = 0; i < 288; i++) {
            IndoorEnvironmentEntity entity = new IndoorEnvironmentEntity();
            entity.setDate(today);
            entity.setPigstyId(1);
            BigDecimal bigDecimal = new BigDecimal(Math.random() * 3 + 23).setScale(1, BigDecimal.ROUND_HALF_UP);
            double temperature = bigDecimal.doubleValue();
            entity.setTemperature(temperature);
            BigDecimal bigDecimal2 = new BigDecimal(Math.random() * 0.3 + 0.3).setScale(2, BigDecimal.ROUND_HALF_UP);
            double humidity = bigDecimal2.doubleValue();
            entity.setHumidity(humidity);
            entity.setGas(gas.get(random.nextInt(5)));
            entity.setCollectTime(collectTime);
            collectTime = DateUtil.offsetMinute(collectTime,5);
//            System.out.println(entity);
            list.add(entity);
        }

        indoorEnvironmentDao.batchInsertData(list);
        System.out.println("createTodayIndoorEnvData end------->");
    }

    @Async
    @Scheduled(cron = "1 0 0 * * ?")
    //@Scheduled(cron = "1 52 18 * * ?")
    void createTodayOutdoorEnvData() {

        System.out.println("createTodayOutdoorEnvData begin------->");
        DateTime today = DateUtil.beginOfDay(new Date());
        Date collectTime = today;

        List<String> gas = new ArrayList<>();
        gas.add("优");
        gas.add("良");
        gas.add("轻度");
        gas.add("中度");
        gas.add("重度");
        gas.add("严重");

        Random random = new Random();
        List<OutdoorEnvironmentEntity> list = new ArrayList<>();
        for (int i = 0; i < 288; i++) {
            OutdoorEnvironmentEntity entity = new OutdoorEnvironmentEntity();
            entity.setDate(today);
            BigDecimal bigDecimal = new BigDecimal(Math.random() * 3 + 23).setScale(1, BigDecimal.ROUND_HALF_UP);
            double temperature = bigDecimal.doubleValue();
            entity.setTemperature(temperature);
            BigDecimal bigDecimal2 = new BigDecimal(Math.random() * 0.3 + 0.3).setScale(2, BigDecimal.ROUND_HALF_UP);
            double humidity = bigDecimal2.doubleValue();
            entity.setHumidity(humidity);
            entity.setGas(gas.get(random.nextInt(4)));
            entity.setCollectTime(collectTime);
            collectTime = DateUtil.offsetMinute(collectTime,5);
            list.add(entity);
        }

        outdoorEnvironmentDao.batchInsertData(list);
        System.out.println("createTodayOutdoorEnvData end------->");

    }

    @Async
    @Scheduled(cron = "1 0 0 * * ?")
    //@Scheduled(cron = "1 52 18 * * ?")
    void callPigHealthData() {
        System.out.println("callPigHealthData begin------->");
        indoorEnvironmentDao.callPigHealthData();
        System.out.println("callPigHealthData end------->");

    }




}
