package com.yihu.wlyy.event;

import com.yihu.wlyy.job.QuartzHelper;
import com.yihu.wlyy.job.SignAgainJob;
import com.yihu.wlyy.job.SignEndJob;
import com.yihu.wlyy.job.consult.EvaluateScoreJob;
import com.yihu.wlyy.util.SystemConf;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * Created by lyr-pc on 2017/3/10.
 */
@Service
public class ApplicationEvent implements ApplicationListener<ContextRefreshedEvent> {

    @Autowired
    QuartzHelper quartzHelper;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent ContextRefreshedEvent) {
        try {
            // 启动签约到期处理JOB
            if (!quartzHelper.isExistJob("sign_end_job")) {
                String trigger = SystemConf.getInstance().getSystemProperties().getProperty("sign_end_job_trigger");
                quartzHelper.addJob(SignEndJob.class, trigger, "sign_end_job", new HashMap<String, Object>());
                System.out.println("sign end job start success");
            } else {
                System.out.println("sign end job exist");
            }
            if (!quartzHelper.isExistJob("evaluate_score_job")) {
                String trigger = SystemConf.getInstance().getSystemProperties().getProperty("evaluate_score_job");
                quartzHelper.addJob(EvaluateScoreJob.class, trigger, "evaluate_score_job", new HashMap<String, Object>());
                System.out.println("evaluate_score_job start success");
            } else {
                System.out.println("evaluate_score_job exist");
            }
             //启动群发续签图文消息
            if (!quartzHelper.isExistJob("sign_again_job")) {
                String trigger = SystemConf.getInstance().getSystemProperties().getProperty("sign_again_job_trigger");
                quartzHelper.addJob(SignAgainJob.class, trigger, "sign_again_job", new HashMap<String, Object>());
                System.out.println("sign_again_job start success");
            } else {
                System.out.println("sign_again_job exist");
            }

            //启动发送产检提醒模板消息
//            if (!quartzHelper.isExistJob("prenatal_inspector_job")) {
//                String trigger = SystemConf.getInstance().getSystemProperties().getProperty("prenatal_inspector_job_trigger");
//                quartzHelper.addJob(PrenatalInspectorJob.class, trigger, "prenatal_inspector_job", new HashMap<String, Object>());
//                System.out.println("prenatal_inspector_job start success");
//            } else {
//                System.out.println("prenatal_inspector_job exist");
//            }
        } catch (Exception e) {
            System.out.println("sign end job start failed");
        }
    }
}
