package com.dxhy.core.task;

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 com.dxhy.core.job.service.ViolationMsgService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;

/**
 * @author Gy Chen
 * @date 2020/11/25
 */
@Component
@Slf4j
public class ViolationMsgTask {
    @Resource
    ViolationMsgService violationMsgService;
    @Resource
    private ScheduleJobService scheduleJobService;

    // @Scheduled(cron = "0 0/5 * * * ?")
    public void selectViolationTaxno() {
        log.info("===============获取违规信息税号开始============");
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        ScheduleJobEntity scheduleJobEntity =
            scheduleJobService.queryByBeanName("violationMsgTask", "selectViolationTaxno");
        if (scheduleJobEntity != null && "0".equals(scheduleJobEntity.getJobStatus())) {
            try {
                scheduleJobEntity.setJobStatus("1");
                DynamicContextHolder.push(DbConstant.BASICS_WRITE);
                scheduleJobService.updateById(scheduleJobEntity);
                violationMsgService.selectViolationTaxno();
                log.info("=============违规信息税号执行结束=============");
            } catch (Exception e) {
                e.printStackTrace();
                log.info("=============违规信息税号获取失败=============");
            } finally {
                log.info("违规信息税号--重置执行状态");
                scheduleJobEntity.setJobStatus("0");
                DynamicContextHolder.push(DbConstant.BASICS_WRITE);
                scheduleJobService.updateById(scheduleJobEntity);
            }
        }
    }

    // @Scheduled(cron = "0 0/5 * * * ?")
    public void getViolation() {
        log.info("===============申请违规信息开始============");
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        ScheduleJobEntity scheduleJobEntity = scheduleJobService.queryByBeanName("violationMsgTask", "getViolation");
        if (scheduleJobEntity != null && "0".equals(scheduleJobEntity.getJobStatus())) {
            try {
                scheduleJobEntity.setJobStatus("1");
                DynamicContextHolder.push(DbConstant.BASICS_WRITE);
                scheduleJobService.updateById(scheduleJobEntity);
                violationMsgService.getViolation();
                log.info("=============申请违规信息执行结束=============");
            } catch (Exception e) {
                e.printStackTrace();
                log.info("=============申请违规信息获取失败=============");
            } finally {
                log.info("申请违规信息--重置执行状态");
                scheduleJobEntity.setJobStatus("0");
                DynamicContextHolder.push(DbConstant.BASICS_WRITE);
                scheduleJobService.updateById(scheduleJobEntity);
            }
        }
    }

    public void getViolationResult() {
        log.info("===============获取违规结果开始============");
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        ScheduleJobEntity scheduleJobEntity =
            scheduleJobService.queryByBeanName("violationMsgTask", "getViolationResult");
        if (scheduleJobEntity != null && "0".equals(scheduleJobEntity.getJobStatus())) {
            try {
                scheduleJobEntity.setJobStatus("1");
                DynamicContextHolder.push(DbConstant.BASICS_WRITE);
                scheduleJobService.updateById(scheduleJobEntity);
                violationMsgService.getViolationResult();
                log.info("=============获取违规结果执行结束=============");
            } catch (Exception e) {
                e.printStackTrace();
                log.info("=============获取违规结果获取失败=============");
            } finally {
                log.info("获取违规结果--重置执行状态");
                scheduleJobEntity.setJobStatus("0");
                DynamicContextHolder.push(DbConstant.BASICS_WRITE);
                scheduleJobService.updateById(scheduleJobEntity);
            }
        }
    }
    public void violationTDxRecordInvoice(){
        log.info("ViolationMsgTask.violationTDxRecordInvoice.starting");
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        ScheduleJobEntity scheduleJobEntity =
                scheduleJobService.queryByBeanName("violationMsgTask","violationTDxRecordInvoice");
        if(scheduleJobEntity!=null && "0".equals(scheduleJobEntity.getJobStatus())){
            try{
                scheduleJobEntity.setJobStatus("1");
                DynamicContextHolder.push(DbConstant.BASICS_WRITE);
                scheduleJobService.updateById(scheduleJobEntity);
                violationMsgService.updateTdxRecodeInvoce();
                log.info("ViolationMsgTask.violationTDxRecordInvoice.ending");
            }catch (Exception e){
                log.error("ViolationMsgTask.violationTDxRecordInvoice.error,{}",e);
            }finally {
                log.info("ViolationMsgTask.violationTDxRecordInvoice.reset");
                scheduleJobEntity.setJobStatus("0");
                DynamicContextHolder.push(DbConstant.BASICS_WRITE);
                scheduleJobService.updateById(scheduleJobEntity);
            }
        }
    }
}
