package com.dxhy.core.task;

import javax.annotation.Resource;

import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.core.job.entity.ScheduleJobEntity;
import com.dxhy.core.job.service.ScheduleJobService;
import org.springframework.stereotype.Component;

import com.dxhy.core.job.service.ApplyService;

import lombok.extern.slf4j.Slf4j;

/**
 * 申请统计，统计结果，申请确认，确认结果定时任务
 *
 * @author fth
 * @date 2019年05月28日
 **/
@Component("applyTask")
@Slf4j

public class ApplyTask {
    @Resource
    private ApplyService applyService;
    @Resource
    ScheduleJobService scheduleJobService;

    public void applyStat() {
        log.info("申请统计定时任务开始--");
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        ScheduleJobEntity scheduleJobEntity = scheduleJobService.queryByBeanName("applyTask", "applyStat");
        if (scheduleJobEntity != null && "0".equals(scheduleJobEntity.getJobStatus())) {
            try {
                scheduleJobEntity.setJobStatus("1");
                DynamicContextHolder.push(DbConstant.BASICS_WRITE);
                scheduleJobService.updateById(scheduleJobEntity);
                applyService.getDataForApply();
                log.info("申请统计定时任务结束--");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                log.info("申请统计定时--重置执行状态");
                scheduleJobEntity.setJobStatus("0");
                DynamicContextHolder.push(DbConstant.BASICS_WRITE);
                scheduleJobService.updateById(scheduleJobEntity);
            }
        }
    }

    public void getStatResult() {
        log.info("统计结果获取定时任务启动--");
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        ScheduleJobEntity scheduleJobEntity = scheduleJobService.queryByBeanName("applyTask", "getStatResult");
        if (scheduleJobEntity != null && "0".equals(scheduleJobEntity.getJobStatus())) {
            try {
                scheduleJobEntity.setJobStatus("1");
                DynamicContextHolder.push(DbConstant.BASICS_WRITE);
                scheduleJobService.updateById(scheduleJobEntity);
                applyService.getApplyStatResult();
                log.info("统计结果获取定时任务结束--");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                log.info("统计结果获取--重置执行状态");
                scheduleJobEntity.setJobStatus("0");
                DynamicContextHolder.push(DbConstant.BASICS_WRITE);
                scheduleJobService.updateById(scheduleJobEntity);
            }
        }
    }

    public void applyConfirm() {
        log.info("申请确认定时任务启动--");
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        ScheduleJobEntity scheduleJobEntity = scheduleJobService.queryByBeanName("applyTask", "applyConfirm");
        if (scheduleJobEntity != null && "0".equals(scheduleJobEntity.getJobStatus())) {
            try {
                scheduleJobEntity.setJobStatus("1");
                DynamicContextHolder.push(DbConstant.BASICS_WRITE);
                scheduleJobService.updateById(scheduleJobEntity);
                applyService.getDataForConfrim();
                log.info("申请确认定时任务结束--");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                log.info("申请确认--重置执行状态");
                scheduleJobEntity.setJobStatus("0");
                DynamicContextHolder.push(DbConstant.BASICS_WRITE);
                scheduleJobService.updateById(scheduleJobEntity);
            }
        }
    }

    public void confirmResult() {
        log.info("确认结果定时任务启动--");
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        ScheduleJobEntity scheduleJobEntity = scheduleJobService.queryByBeanName("applyTask", "confirmResult");
        if (scheduleJobEntity != null && "0".equals(scheduleJobEntity.getJobStatus())) {
            try {
                scheduleJobEntity.setJobStatus("1");
                DynamicContextHolder.push(DbConstant.BASICS_WRITE);
                scheduleJobService.updateById(scheduleJobEntity);
                applyService.getApplyConfirmResult();
                log.info("确认结果定时任务结束--");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                log.info("确认结果--重置执行状态");
                scheduleJobEntity.setJobStatus("0");
                DynamicContextHolder.push(DbConstant.BASICS_WRITE);
                scheduleJobService.updateById(scheduleJobEntity);
            }
        }
    }

    public void getHistoryMsg() {
        log.info("获取统计信息定时任务启动--");
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        ScheduleJobEntity scheduleJobEntity = scheduleJobService.queryByBeanName("applyTask", "getHistoryMsg");
        if (scheduleJobEntity != null && "0".equals(scheduleJobEntity.getJobStatus())) {
            try {
                scheduleJobEntity.setJobStatus("1");
                DynamicContextHolder.push(DbConstant.BASICS_WRITE);
                scheduleJobService.updateById(scheduleJobEntity);
                applyService.getHistoryMsg();
                log.info("获取统计信息定时任务结束--");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                log.info("获取统计信息--重置执行状态");
                scheduleJobEntity.setJobStatus("0");
                DynamicContextHolder.push(DbConstant.BASICS_WRITE);
                scheduleJobService.updateById(scheduleJobEntity);
            }
        }
    }
}
