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

import com.tax.file.model.FileReport;
import com.tax.file.model.TkTaxUser;
import com.tax.file.service.api.FileReportService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;

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

@Component
public class FileDayReportService {

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

    @Autowired
    protected FileReportService fileReportService;

    @Async("resetExecutor")
    public Future<String> saveFileDayReport(String uuidFlag, CountDownLatch latch, TkTaxUser taxUser) {
        String taxno = taxUser.getTaxno();
        try {
            List<FileReport> fileDayReportList = fileReportService.queryInitFileDayReport(taxUser);
            for(FileReport dayReport:fileDayReportList) {
                fileReportService.saveDayReport(uuidFlag, dayReport, taxUser);
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.info(uuidFlag + " saveDayReport " + taxno + " error is " + e.getMessage());
        }finally {
            latch.countDown();
        }
        return new AsyncResult<>("saveDayReport taxno="+taxno+" accomplished!");
    }

}
