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.CoreConfirmDao;
import com.dxhy.core.dao.CoreSysEnterpriseConfigCoreDao;
import com.dxhy.core.job.entity.ScheduleJobEntity;
import com.dxhy.core.job.entity.TAcOrg;
import com.dxhy.core.job.service.InvoiceDownService;
import com.dxhy.core.job.service.InvoiceInterfaceService;
import com.dxhy.core.job.service.ScheduleJobService;
import com.dxhy.core.service.rzInvoice.RzInvoiceDqyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

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

/**
 * @author wangruwei
 * @date 2023年9月15日10:50:48
 */
@SuppressWarnings({"rawtypes", "DuplicatedCode"})
@Component("rzDqyInvoiceTask")
@Slf4j
public class RzDqyInvoiceTask {
    @Resource
    private ScheduleJobService scheduleJobService;
    @Resource
    private CoreConfirmDao coreConfirmDao;

    @Resource
    private CoreSysEnterpriseConfigCoreDao coreSysEnterpriseConfigCoreDao;

    /**
     * 核心线程池大小
     */
    private int corePoolSize = 10;
    /**
     * 最大线程池大小
     */

    private int maximumPoolSize = 20;

    //入账业务
    @Resource
    private RzInvoiceDqyService rzInvoiceDqyService;

    /**RzInvoiceDqyServiceImpl
    * @Description 批量上传入账发票接口
    * @Return void
    * @Author wangruwei
    * @Date 2023/9/15 11:28
    **/
    public void rzInvoice() throws InterruptedException {
        log.info("----------批量上传入账发票接口执行开始----------");
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        ScheduleJobEntity scheduleJobEntity = scheduleJobService.queryByBeanName("rzDqyInvoiceTask", "rzInvoice");
        if (scheduleJobEntity != null && "0".equals(scheduleJobEntity.getJobStatus())) {
            try {
                scheduleJobEntity.setJobStatus("1");
                DynamicContextHolder.push(DbConstant.BASICS_WRITE);
                scheduleJobService.updateById(scheduleJobEntity);

                //获取可以执行大企业的税号信息
                List<TAcOrg> taxnoList = coreConfirmDao.getTaxno();
                for (TAcOrg collectionControlModel : taxnoList) {
                    rzInvoiceDqyService.rzInvoiceUpload(collectionControlModel);
                }
                log.info("批量上传入账发票接口任务结束--");
            } catch (Exception e) {
               log.info("业务处理异常：{}", e);
            } finally {
                log.info("批量上传入账发票接口--重置执行状态");
                scheduleJobEntity.setJobStatus("0");
                DynamicContextHolder.push(DbConstant.BASICS_WRITE);
                scheduleJobService.updateById(scheduleJobEntity);
            }
        }
        log.info("----------批量上传入账发票接口执行结束----------");
    }

    /**
     * @param
     * @Description 查询发票入账处理结果
     * @Return void
     * @Author wangruwei
     * @Date 2022/11/15 11:33
     **/
    public void rzInvoiceResult() throws InterruptedException {

        log.info("----------查询发票入账处理结果接口执行开始----------");
        ScheduleJobEntity scheduleJobEntity = scheduleJobService.queryByBeanName("rzDqyInvoiceTask", "rzInvoiceResult");
        if (scheduleJobEntity != null && "0".equals(scheduleJobEntity.getJobStatus())) {
            try {
                scheduleJobEntity.setJobStatus("1");
                DynamicContextHolder.push(DbConstant.BASICS_WRITE);
                scheduleJobService.updateById(scheduleJobEntity);

                //获取可以执行大企业的税号信息
                List<TAcOrg> taxnoList = coreConfirmDao.getTaxno();
                for (TAcOrg collectionControlModel : taxnoList) {
                    rzInvoiceDqyService.rzInvoiceResult(collectionControlModel);
                }
                log.info("查询发票入账处理结果接口任务结束--");
            } catch (Exception e) {
               log.info("业务处理异常：{}", e);
            } finally {
                log.info("查询发票入账处理结果接口--重置执行状态");
                scheduleJobEntity.setJobStatus("0");
                DynamicContextHolder.push(DbConstant.BASICS_WRITE);
                scheduleJobService.updateById(scheduleJobEntity);
            }
        }
        log.info("----------查询发票入账处理结果接口执行结束----------");
    }

    /**
     * @param
     * @Description 已入账发票采集
     * @Return void
     * @Author wangruwei
     * @Date 2023-12-8 09:40:33
     **/
    public void alreadyRzInvoiceGj() throws InterruptedException {

        log.info("----------已入账发票采集接口执行开始----------");
        ScheduleJobEntity scheduleJobEntity = scheduleJobService.queryByBeanName("rzDqyInvoiceTask", "alreadyRzInvoiceGj");
        if (scheduleJobEntity != null && "0".equals(scheduleJobEntity.getJobStatus())) {
            try {
                scheduleJobEntity.setJobStatus("1");
                DynamicContextHolder.push(DbConstant.BASICS_WRITE);
                scheduleJobService.updateById(scheduleJobEntity);

                //获取可以执行大企业的税号信息
                List<CollectionControlModel> taxnoList = coreSysEnterpriseConfigCoreDao.getDqyTaxNo();
                for (CollectionControlModel collectionControlModel : taxnoList) {
                    rzInvoiceDqyService.alreadyRzInvoiceGj(collectionControlModel);
                }
                log.info("已入账发票采集接口任务结束--");
            } catch (Exception e) {
               log.info("业务处理异常：{}", e);
            } finally {
                log.info("已入账发票采集接口--重置执行状态");
                scheduleJobEntity.setJobStatus("0");
                DynamicContextHolder.push(DbConstant.BASICS_WRITE);
                scheduleJobService.updateById(scheduleJobEntity);
            }
        }
        log.info("----------已入账发票采集接口执行结束----------");
    }
}
