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

import com.tax.file.constants.FileConstants;
import com.tax.file.model.TkTaxUser;
import com.tax.file.service.api.TkTaxUserService;
import com.yxcheng.token.utils.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CountDownLatch;

@Component
public class ResetApplyTask {

    private static final Logger logger = LoggerFactory.getLogger(ResetApplyTask.class);
    @Autowired
    private TkTaxUserService tkTaxUserService;
    @Autowired
    private ResetApplyService resetApplyService;

    public void resetApply(String uuidFlag){

        List<TkTaxUser> taxUserList = tkTaxUserService.queryResetApplyTaxnoList();
        logger.info(uuidFlag + " resetApply taxno number:" + taxUserList.size());
        try {
            if(taxUserList!=null && taxUserList.size()>0){
                CountDownLatch latch = new CountDownLatch(taxUserList.size());
                for(TkTaxUser taxUser: taxUserList){
                    resetApplyService.doResetApply(uuidFlag, latch, taxUser);
                }
                latch.await();
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.info(uuidFlag + " resetApply is error:" + e.getMessage());
        }
    }

    public void resetMonthApply(String uuidFlag){
        Date cyDate = DateUtils.getPreDateCurMonth(DateUtils.getDate(), FileConstants.applyCycle);
        String resetDay = DateUtils.getDayStr(DateUtils.getDate());
        List<TkTaxUser> taxUserList = tkTaxUserService.queryResetMonthApplyTaxnoList(cyDate, resetDay);
        logger.info(uuidFlag + " resetMonthApply taxno number:" + taxUserList.size());
        try {
            if(taxUserList!=null && taxUserList.size()>0){
                CountDownLatch latch = new CountDownLatch(taxUserList.size());
                for(TkTaxUser taxUser: taxUserList){
                    resetApplyService.doResetMonthApply(uuidFlag, latch, taxUser);
                }
                latch.await();
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.info(uuidFlag + " resetMonthApply is error:" + e.getMessage());
        }
    }

    public static void main(String[] args) {
        Date cyDate = DateUtils.getPreDateCurMonth(DateUtils.getDate(), FileConstants.applyCycle);
        Date ddd = DateUtils.toDate(cyDate, new SimpleDateFormat("yyyy-MM-dd"));
        System.out.println(ddd);
    }
}
