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

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.time.Instant;
import java.util.List;
import java.util.concurrent.CountDownLatch;

@Component
public class ClearPastDataTask {

    private static final Logger logger = LoggerFactory.getLogger(ClearPastDataTask.class);

    @Autowired
    protected ClearPastDataService clearPastDataService;
    @Autowired
    protected TkTaxUserService tkTaxUserService;

    public void clearPastData(String uuidFlag){

        Instant start = Instant.now();
        logger.info(uuidFlag + " clearPastData clear file is start.");
        try {
            List<TkTaxUser> taxUserList = tkTaxUserService.queryClearDataTaxnoList(FileConstants.pageNum);
            logger.info(uuidFlag + " clearPastData taxno number:" + taxUserList.size());
            if(taxUserList!=null && taxUserList.size()>0){
                CountDownLatch latch = new CountDownLatch(taxUserList.size());
                for(TkTaxUser taxUser: taxUserList){
                    clearPastDataService.doClearData(uuidFlag, latch, taxUser);
                }
                latch.await();
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.info(uuidFlag + " clearPastData clear file is error:" + e.getMessage());
        }
        //统计耗时
        Instant end = Instant.now();
        long duration = end.toEpochMilli() - start.toEpochMilli();
        logger.info(uuidFlag + " clearPastData clear file is end.cost is {} ms!",duration);
    }




    public static void main(String[] args) {

        System.out.println( DateUtils.getPastDate(FileConstants.pastDataDay));
    }

}
