package com.heyu.api.schedule.impl;


import com.alibaba.fastjson.JSON;
import com.heyu.api.data.dao.vv.VvChartConfigDao;
import com.heyu.api.data.dao.vv.VvCreateDataConfigDao;
import com.heyu.api.data.dto.VvCreateDataConfigDTO;
import com.heyu.api.data.entity.vv.VvChartConfigEntity;
import com.heyu.api.data.entity.vv.VvCreateDataConfigEntity;
import com.heyu.api.data.utils.DateUtils;
import com.heyu.api.schedule.CronTriggerUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

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

@Component("testJob")
@Slf4j
public class TestJob {

    @Autowired
    private VvCreateDataConfigDao createDataConfigDao;

    @Value("${eb.config.rabbitQueue.createDataQueue}")
    private String createDataQueue;


    @Value("${eb.config.rabbitQueue.sendDingDingQueue}")
    private String sendDingDingQueue;

    @Autowired
    private RabbitTemplate rabbitTemplate;


    @Autowired
    private VvChartConfigDao vvChartConfigDao;


    public void test() {
        List<VvCreateDataConfigEntity> vvCreateDataConfigEntityList = createDataConfigDao.selectVvCreateDataConfigByAll();
        Date now = new Date();
        Date dateStr = DateUtils.formateDate(now, "yyyy-MM-dd HH:mm:00");
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = DateUtils.parseDate(dateFormat.format(dateStr));
        for (VvCreateDataConfigEntity vvCreateDataConfigEntity : vvCreateDataConfigEntityList) {
            boolean isRun = CronTriggerUtils.isRun(date, vvCreateDataConfigEntity.getCronExp());
            log.info("VvCreateDataConfigEntity current dateStr :{} , cronExp:{},isRun:{},date:{}", dateFormat.format(dateStr), vvCreateDataConfigEntity.getCronExp(), isRun,date);
            if (isRun) {

                VvCreateDataConfigDTO dto = new VvCreateDataConfigDTO();
                BeanUtils.copyProperties(vvCreateDataConfigEntity, dto);
                dto.setRunDate(dateFormat.format(dateStr));
                log.info("TestJob VvCreateDataConfigEntity:{}", JSON.toJSONString(dto));
                rabbitTemplate.convertAndSend(createDataQueue, JSON.toJSONString(dto));
            }
        }

        List<VvChartConfigEntity> vvChartConfigEntities = vvChartConfigDao.selectVvChartConfigByAll();
        for (VvChartConfigEntity vvChartConfigEntity : vvChartConfigEntities) {
            boolean isRun = CronTriggerUtils.isRun(now, vvChartConfigEntity.getCronExp());
            log.info("VvChartConfigEntity current dateStr :{} , cronExp:{},isRun:{},date:{}", dateFormat.format(dateStr), vvChartConfigEntity.getCronExp(), isRun,date);
            if (isRun) {
                log.info("TestJob VvChartConfigEntity:{}", JSON.toJSONString(vvChartConfigEntity));
                rabbitTemplate.convertAndSend(sendDingDingQueue, JSON.toJSONString(vvChartConfigEntity));
            }
        }
    }
}
