package com.tax.file.service.async.apply;

import com.tax.file.constants.FileConstants;
import com.tax.file.model.TkTaxUser;
import com.tax.file.mq.rabbit.producer.RabbitMQMessageProducer;
import com.tax.file.service.api.TkTaxUserService;
import com.tax.file.service.async.ExecuteMqService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;


/**
 *
 * 通过cycle 将一个月分为dayOfMonth/cycle个周期
 * 通过当天时间计算应为哪一个周期范围内
 * example : 如果cycle=8, 本月是9月，30/8-1 = 3  依次为 1-8  9-16  17-24
 * 2-8号应申请上个周期遗留数据开票日期为lastMonth(22-月末)
 * 9-16号应申请开票日期为1-8号数据
 * 17-24号应申请开票日期为6-16号数据
 * 25-月末应申请开票日期为14-24号数据
 *
 */

@Component
public class FileApplyTask {

    private static final Logger logger = LoggerFactory.getLogger(FileApplyTask.class);
    @Autowired
    private TkTaxUserService tkTaxUserService;
    @Autowired
    private RabbitMQMessageProducer rabbitMQMessageProducer;
    @Autowired
    private ExecuteMqService asyncTkFileApplyService;


    public void applyDownFile(String uuidFlag) {
        List<TkTaxUser> tkTaxUsers = tkTaxUserService.queryApplyTaxnoList();
        try {
            logger.info(uuidFlag + " applyDownFile taxno number:" + tkTaxUsers.size());
            if(null!=tkTaxUsers && tkTaxUsers.size()>0){
                for(TkTaxUser tkTaxUser: tkTaxUsers){
                    if(FileConstants.isTest()){
                        asyncTkFileApplyService.execute(tkTaxUser);
                    }else {
                        rabbitMQMessageProducer.sendApplyDownFileTopic(tkTaxUser, tkTaxUser.getPublishNo());
                        logger.info(uuidFlag + " applyDownFile taxno " + tkTaxUser.getTaxno() + " send is success.");
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error(uuidFlag + " applyDownFile has error:" + e.getMessage());
        }

    }
}
