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

import com.tax.file.model.FIncomeInvoice;
import com.tax.file.model.FIncomeInvoiceExample;
import com.tax.file.service.api.FIncomeInvoiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;

@Component
public class IncomeDownTask {
    @Autowired
    private FIncomeInvoiceService incomeInvoiceService;

    @Autowired
    private AsynIncomeDownService asynIncomeDownService;

    public void down(){
        CountDownLatch latch = null;
        FIncomeInvoiceExample example = new FIncomeInvoiceExample();
        example.createCriteria().andApplyStatusEqualTo("1").andDownStatusEqualTo("0");
        List<FIncomeInvoice> invoices = incomeInvoiceService.selectByExample(example);

        if(invoices!=null && invoices.size()>0) {
            Map<String, List<FIncomeInvoice>> map = IncomeUtil.splitToTaxno(invoices);
            latch = new CountDownLatch(map.size());
            for (Map.Entry<String,List<FIncomeInvoice>> entry : map.entrySet()) {
                asynIncomeDownService.doIncomeDown(latch,entry.getKey(),entry.getValue());
            }
            try {
                latch.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}
