package com.dxhy.core.task;

import com.dxhy.api.model.config.CollectionControlModel;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.core.dao.CoreSysEnterpriseConfigCoreDao;
import com.dxhy.core.job.entity.ScheduleJobEntity;
import com.dxhy.core.job.service.DkdjwspzService;
import com.dxhy.core.job.service.ScheduleJobService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

/**
 * @description  代扣代缴完税凭证任务
 * @author  Allen
 * @date    2023/12/7 14:49
*/
@SuppressWarnings({"rawtypes", "DuplicatedCode"})
@Component("dkdjwspzTask")
@Slf4j

public class DkdjwspzTask {
    @Resource
    private ScheduleJobService scheduleJobService;

    @Resource
    private CoreSysEnterpriseConfigCoreDao coreSysEnterpriseConfigCoreDao;

    @Autowired
    private DkdjwspzService dkdjwspzService;

    private static List<String> rzlxList = new ArrayList<>();

    /**
     * 增值税代扣代缴完税凭证归集任务
     * @throws
     */
    public void zzsdkdjwspzcj() throws Exception {
        log.info("增值税代扣代缴完税凭证归集任务执行开始-");
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        ScheduleJobEntity scheduleJobEntity = scheduleJobService.queryByBeanName("dkdjwspzTask", "zzsdkdjwspzcj");
        if (scheduleJobEntity != null && "0".equals(scheduleJobEntity.getJobStatus())) {
            try {
                scheduleJobEntity.setJobStatus("1");
                DynamicContextHolder.push(DbConstant.BASICS_WRITE);
                scheduleJobService.updateById(scheduleJobEntity);
                //查询大企业税号列表
                List<CollectionControlModel> list = coreSysEnterpriseConfigCoreDao.getDqyTaxNo();
                list.stream().forEach(ccm->{
                    dkdjwspzService.zzsdkdjwspzcj(ccm);
                });
                log.info("增值税代扣代缴完税凭证归集任务执行结束--");
            } catch (Exception e) {
                log.debug("增值税代扣代缴完税凭证归集任务异常：{}", e);
            } finally {
                log.info("增值税代扣代缴完税凭证归集任务执行完成--重置执行状态");
                scheduleJobEntity.setJobStatus("0");
                DynamicContextHolder.push(DbConstant.BASICS_WRITE);
                scheduleJobService.updateById(scheduleJobEntity);
            }
        }
    }

    /**
     * 增值税代扣代缴完税凭证认证申请任务
     * @throws
     */
    public void zzsdkdjwspzrzsq() throws Exception {
        log.info("增值税代扣代缴完税凭证认证申请任务执行开始-");
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        ScheduleJobEntity scheduleJobEntity = scheduleJobService.queryByBeanName("dkdjwspzTask", "zzsdkdjwspzrzsq");
        if (scheduleJobEntity != null && "0".equals(scheduleJobEntity.getJobStatus())) {
            try {
                scheduleJobEntity.setJobStatus("1");
                DynamicContextHolder.push(DbConstant.BASICS_WRITE);
                scheduleJobService.updateById(scheduleJobEntity);
                //查询大企业税号列表
                List<CollectionControlModel> list = coreSysEnterpriseConfigCoreDao.getDqyTaxNo();
                list.stream().forEach(ccm->{
                    dkdjwspzService.zzsdkdjwspzrzsq(ccm);
                });
                log.info("增值税代扣代缴完税凭证认证申请任务执行结束--");
            } catch (Exception e) {
                log.debug("增值税代扣代缴完税凭证认证申请任务异常：{}", e);
            } finally {
                log.info("增值税代扣代缴完税凭证认证申请任务执行完成--重置执行状态");
                scheduleJobEntity.setJobStatus("0");
                DynamicContextHolder.push(DbConstant.BASICS_WRITE);
                scheduleJobService.updateById(scheduleJobEntity);
            }
        }
    }


    /**
     * 增值税代扣代缴完税凭证认证结果查询
     * @throws
     */
    public void zzsdkdjwspzrzjgcx() throws Exception {
        log.info("增值税代扣代缴完税凭证认证结果查询任务执行开始-");
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        ScheduleJobEntity scheduleJobEntity = scheduleJobService.queryByBeanName("dkdjwspzTask", "zzsdkdjwspzrzjgcx");
        if (scheduleJobEntity != null && "0".equals(scheduleJobEntity.getJobStatus())) {
            try {
                scheduleJobEntity.setJobStatus("1");
                DynamicContextHolder.push(DbConstant.BASICS_WRITE);
                scheduleJobService.updateById(scheduleJobEntity);
                //查询大企业税号列表
                List<CollectionControlModel> list = coreSysEnterpriseConfigCoreDao.getDqyTaxNo();
                list.stream().forEach(ccm->{
                    dkdjwspzService.zzsdkdjwspzrzjgcx(ccm);
                });
                log.info("增值税代扣代缴完税凭证认证结果查询任务执行结束--");
            } catch (Exception e) {
                log.debug("增值税代扣代缴完税凭证认证结果查询任务异常：{}", e);
            } finally {
                log.info("增值税代扣代缴完税凭证认证结果查询任务执行完成--重置执行状态");
                scheduleJobEntity.setJobStatus("0");
                DynamicContextHolder.push(DbConstant.BASICS_WRITE);
                scheduleJobService.updateById(scheduleJobEntity);
            }
        }
    }
}
