package com.lightinit.hsdataplatform.tasks.logger;

import com.lightinit.hsdataplatform.entity.RunningMonitoring;
import com.lightinit.hsdataplatform.entity.RunningMonitoringExample;
import com.lightinit.hsdataplatform.mapper.RunningMonitoringLogMapper;
import com.lightinit.hsdataplatform.mapper.RunningMonitoringMapper;
import com.lightinit.hsdataplatform.model.admin.ApiRuningMonitoringModel;
import org.apache.commons.lang.StringUtils;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;

/**
 * @Author: lidongyang
 * @Date: 2019/3/11
 * @Description:
 **/
@Aspect
@Component
public class StateTask implements ApplicationListener {

    public static String JOB_NAME = "运行状态监控";
    public static String TRIGGER_NAME = "运行状态监控";
    public static String JOB_GROUP_NAME = "STATE_GROUP";
    public static String TRIGGER_GROUP_NAME = "STATE_GROUP";

    @Value("${log4j.appender.E.File}")
    public String path;

    @Autowired
    private RunningMonitoringMapper runningMonitoringMapper;
     @Autowired
    private RunningMonitoringLogMapper runningMonitoringLogMapper;//在这里将mapper带给job类

    //程序开启监听添加定时器
    @Override
    public void onApplicationEvent(ApplicationEvent applicationEvent) {
        //System.out.println("【运行定时修改】");
        addJob();
    }

    public void addJob(){
        if(!QuartzManager.findJobByJobAndTriggerName(JOB_NAME, JOB_GROUP_NAME, TRIGGER_NAME, TRIGGER_GROUP_NAME)){
            RunningMonitoringExample example = new RunningMonitoringExample();
            example.createCriteria().andMonitoringTypeEqualTo(JOB_NAME);
            List<RunningMonitoring> monitorings = runningMonitoringMapper.selectByExample(example);
            if(monitorings==null || monitorings.size()<1){
                return;
            }
            RunningMonitoring monitoring=monitorings.get(0);
            String timeStrFrom = monitoring.getMonitoringTimeStrFrom();//10:30
            String timeStrTo = monitoring.getMonitoringTimeStrTo();//12:00
            Integer cycle = monitoring.getMonitoringCycle();//30
            String alertStart = monitoring.getAlertTimeStrFrom();//09:10
            String alertEnd = monitoring.getAlertTimeStrTo();//22:12
            //将参数传入到job中
            HashMap<String, Object> map = new HashMap<>();
            map.put("path",path);
            map.put("alertStart",alertStart);
            map.put("alertEnd",alertEnd);
            map.put("runningMonitoringLogMapper",runningMonitoringLogMapper);//带给job类
            if(StringUtils.isEmpty(timeStrFrom) || cycle==0 || StringUtils.isEmpty(timeStrTo)){
                return;
            }
            String[] timeStrFromStr = timeStrFrom.split(":");
            String start = timeStrFromStr[0];
            String[] timeStrToStr = timeStrTo.split(":");
            String end = timeStrToStr[0];
            String cron="0 0/"+cycle+" "+(start.startsWith("0") ? start.charAt(1) : start)+"-"+(end.startsWith("0") ? end.charAt(1) : end) +" * * ? *";
            QuartzManager.addJob(JOB_NAME, JOB_GROUP_NAME, TRIGGER_NAME, TRIGGER_GROUP_NAME, StateJob.class, cron,map);
        }
    }


    @After("execution(* com.lightinit.hsdataplatform.impl.admin.RuningMonitoringServiceImpl.add(..)) && args(apiRuningMonitoringModel,..)")
    public void modifyJob(ApiRuningMonitoringModel apiRuningMonitoringModel){
        System.out.println("【运行定时修改】");
        RunningMonitoringExample example = new RunningMonitoringExample();
        example.createCriteria().andMonitoringTypeEqualTo(JOB_NAME);
        List<RunningMonitoring> monitorings = runningMonitoringMapper.selectByExample(example);
        if(monitorings==null || monitorings.size()<1){
            return;
        }
        RunningMonitoring monitoring=monitorings.get(0);
        String timeStrFrom = monitoring.getMonitoringTimeStrFrom();//10:30
        String timeStrTo = monitoring.getMonitoringTimeStrTo();//12:00
        Integer cycle = monitoring.getMonitoringCycle();//30
        if(StringUtils.isEmpty(timeStrFrom) || cycle==0 ||StringUtils.isEmpty(timeStrTo)){
            return;
        }
        String[] timeStrFromStr = timeStrFrom.split(":");
        String[] timeStrToStr = timeStrTo.split(":");
        String cron="0 0/"+cycle+" "+timeStrFromStr[0]+"-"+timeStrToStr[0]+" * * ? *";
        QuartzManager.modifyJobTime(JOB_NAME, JOB_GROUP_NAME, TRIGGER_NAME, TRIGGER_GROUP_NAME,cron);
    }
}
