package com.hzya.frame.web.init;

import com.alibaba.fastjson.JSONObject;
import com.hzya.frame.sysnew.application.plugin.entity.SysApplicationPluginEntity;
import com.hzya.frame.sysnew.integtationTask.IIntegrationTaskCacheableService;
import com.hzya.frame.sysnew.integtationTask.dao.IIntegrationTaskDao;
import com.hzya.frame.sysnew.integtationTask.entity.IntegrationTaskEntity;
import com.hzya.frame.sysnew.integtationTaskLiving.dao.IIntegrationTaskLivingDao;
import com.hzya.frame.sysnew.integtationTaskLiving.entity.IntegrationTaskLivingEntity;
import com.hzya.frame.web.quartz.QuartzJobFactoryUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Content
 * @Author 👻👻👻👻👻👻👻👻👻👻 gjh
 * @Date 2023-06-20 17:07
 * @Version 1.0
 */
@Component
@Order(998)
public class MyApplicationListener implements ApplicationListener<ApplicationReadyEvent> {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Resource
    private IIntegrationTaskDao integrationTaskDao;
    @Resource
    private IIntegrationTaskLivingDao iIntegrationTaskLivingDao;
    @Resource
    private IIntegrationTaskCacheableService iIntegrationTaskCacheableService;
    @Value("${data.use}")
    private boolean dataUse;
    @Override
    public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) {
        initTask();
    }

    private void initTask() {
        if(!dataUse){
            return;
        }
        //查询所有任务，添加到任务执行计划中
        IntegrationTaskEntity queryEntity = new IntegrationTaskEntity();
        //任务状态1、启用
        queryEntity.setTaskStatus("1");
        queryEntity.setSts("Y");
        List<IntegrationTaskEntity> integrationTaskEntities = integrationTaskDao.queryBase(queryEntity);
        //没有任务直接返回
        if (integrationTaskEntities == null || integrationTaskEntities.size() == 0) {
            return;
        }
        //循环任务，查看插件是否启用
        for (int i = 0; i < integrationTaskEntities.size(); i++) {
            IntegrationTaskEntity entity = integrationTaskEntities.get(i);
            if (entity.getTaskPlugin() != null && !"".equals(entity.getTaskPlugin())) {
                SysApplicationPluginEntity sysApplicationPluginEntity = iIntegrationTaskCacheableService.getSysApplicationPluginEntity(entity.getTaskPlugin());
                //实例表
                IntegrationTaskLivingEntity integrationTaskLivingEntity = new IntegrationTaskLivingEntity();
                integrationTaskLivingEntity.setTaskId(entity.getId());
                integrationTaskLivingEntity.setSts("Y");
                List<IntegrationTaskLivingEntity> integrationTaskLivingEntities = iIntegrationTaskLivingDao.queryBase(integrationTaskLivingEntity);
                if (integrationTaskLivingEntities != null && integrationTaskLivingEntities.size() == 1) {
                    integrationTaskLivingEntity = integrationTaskLivingEntities.get(0);
                } else {
                    logger.error("系统错误，请联系管理员");
                }
                //启用停用（1启用2停用）
                if (sysApplicationPluginEntity != null && sysApplicationPluginEntity.getPluginStatus() != null && "1".equals(sysApplicationPluginEntity.getPluginStatus())) {
                    JSONObject jsonObjectMap = new JSONObject();
                    jsonObjectMap.put("integrationTaskId", entity.getId());//任务id
                    jsonObjectMap.put("integrationTaskLivingId", integrationTaskLivingEntity.getId());//实例id
                    //根据cron，定时执行任务
                    try {
                        QuartzJobFactoryUtils.createJob(jsonObjectMap);
                    } catch (Exception e) {
                        logger.error("创建任务失败");
                    }
                }
            }
        }
    }

}
