package com.ecep.contract.ds.contract.tasker;

import java.util.concurrent.atomic.AtomicReference;

import org.springframework.util.StringUtils;

import com.ecep.contract.ContractPayWay;
import com.ecep.contract.MessageHolder;
import com.ecep.contract.model.Contract;

import lombok.Getter;
import lombok.Setter;

/**
 * 合同修复任务
 */
public class ContractRepairTask extends AbstContractRepairTasker {
    @Getter
    @Setter
    private Contract contract;
    @Getter
    boolean repaired = false;
    

    private final ContractRepairComm comm = new ContractRepairComm();

    public ContractRepairTask() {
    }

    @Override
    protected void repair(MessageHolder holder) {
        updateTitle("修复合同 " + contract.toPrettyString());
        Contract parent = null;

        if (contract.getPayWay() == ContractPayWay.PAY) {
            if (StringUtils.hasText(contract.getParentCode())) {
                parent = contractCtx.findContractByCode(contract.getParentCode());
            }
        }

        AtomicReference<Contract> contractProperty = new AtomicReference<>(contract);
        if (repair(contractProperty, parent, holder, progress -> updateProgress(progress, 1))) {
            repaired = true;
        }

        setContract(contractProperty.get());

        updateProgress(1, 1);
    }


}
