package com.yem.wm.es.storagetrans.validator;

import com.yem.wm.es.Util.CreditBackFillUtils;
import com.yem.wm.es.Util.TotalUtil;
import com.yem.wm.utils.YEM;
import kd.bos.dataentity.OperateOption;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.ExtendedDataEntity;
import kd.bos.entity.operate.result.OperationResult;
import kd.bos.entity.validate.AbstractValidator;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.OperationServiceHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;

/**
 * @author ljw
 * @date 2024/10/21 19:31
 * @description StorageAuditValid
 */
public class StorageAuditValid extends AbstractValidator {

    private static final Logger log = LoggerFactory.getLogger(StorageAuditValid.class);

    @Override
    public void validate() {
        ExtendedDataEntity[] dataEntities = this.getDataEntities();
        for (ExtendedDataEntity dataes : dataEntities) {
            DynamicObject dataEntitie = dataes.getDataEntity();
            dataEntitie = BusinessDataServiceHelper.loadSingle(dataEntitie.getPkValue(), dataEntitie.getDynamicObjectType().getName());
            DynamicObject customer = dataEntitie.getDynamicObject("yem_customer");
            DynamicObject method = TotalUtil.getSettlementMethod(dataEntitie);
            DynamicObject company = dataEntitie.getDynamicObject("yem_company.yem_erporg");
            String swiftCode = TotalUtil.getSwiftCode(dataEntitie);
            DynamicObject csrCreditLine = TotalUtil.getCsrCreditLine(customer, method, company, swiftCode);
            DynamicObjectCollection creditDetails = dataEntitie.getDynamicObjectCollection("yem_creditdetail");
            boolean isNeedVail = false;
            for (DynamicObject creditDetail : creditDetails) {
                DynamicObject salecontractno = creditDetail.getDynamicObject("yem_salecontractno");
                if (salecontractno == null) continue;
                salecontractno = BusinessDataServiceHelper.loadSingle(salecontractno.getPkValue(), salecontractno.getDynamicObjectType().getName());
                boolean issinosurecon = salecontractno.getBoolean("yem_issinosurecon");
                if (issinosurecon) {
                    isNeedVail = true;
                }
            }

            if (YEM.isNotEmpty(customer) && YEM.isNotEmpty(csrCreditLine)) {

                log.info("客户信保限额申请：[{}]", csrCreditLine.getString("billno"));
                BigDecimal decimal = CreditBackFillUtils.calcRemainingAmount(csrCreditLine);
                BigDecimal usacreditamt = dataEntitie.getBigDecimal("yem_usacreditamt");
                log.info("{} >= {}", decimal, usacreditamt);
                if (decimal.compareTo(usacreditamt) >= 0) {
                    csrCreditLine.set("yem_balance", decimal);
                    log.info("{} >= {} 不执行提示！", decimal, usacreditamt);
                } else {
                    this.addMessage(dataes, String.format("当前客户对应的信保额度申请剩余额度小于0，请确认！剩余额度[%s] : 投保金额[%s]", decimal, usacreditamt));
                    return;
                }
            } else if (isNeedVail) {
                this.addMessage(dataes, "找不到客户："+customer.getString("name")+"的客户信保额度申请单");
            }
        }
    }
}
