package com.ecep.contract.cloud.u8;

import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Stream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ecep.contract.MessageHolder;
import com.ecep.contract.ds.contract.tasker.AbstContractRepairTasker;

/**
 * 用友U8系统-同步全量合同
 */
public class ContractSyncAllTask extends AbstContractRepairTasker {
    private static final Logger logger = LoggerFactory.getLogger(ContractSyncAllTask.class);


    @Override
    protected void repair(MessageHolder holder) {
        updateTitle("用友U8系统-同步全量合同");
        YongYouU8Repository repository = getBean(YongYouU8Repository.class);

        long total = repository.countAllContracts();
        AtomicInteger counter = new AtomicInteger(0);

        try (Stream<Map<String, Object>> stream = repository.queryAllContractForStream()) {
            stream.forEach(rs -> {
                if (isCancelled()) {
                    holder.debug("Cancelled");
                    return;
                }
                MessageHolder subHolder = holder.sub(counter.get() + " / " + total + "> ");
                try {
                    repairFromCMList(rs, subHolder);
                } catch (Exception e) {
                    updateMessage(e.getMessage());
                    logger.error("data = {}", rs, e);

                }
                updateProgress(counter.incrementAndGet(), total);
            });
        }
    }

}
