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.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.ExtendedDataEntity;
import kd.bos.entity.validate.AbstractValidator;
import kd.bos.servicehelper.BusinessDataServiceHelper;

import java.math.BigDecimal;

/**
 * @author ljw
 * @date 2024/10/21 19:44
 * @description StorageTransSubmitValid
 */
public class StorageTransSubmitValid extends AbstractValidator {
    @Override
    public void validate() {
        ExtendedDataEntity[] dataEntities = this.getDataEntities();
        for (ExtendedDataEntity dataes : dataEntities) {
            DynamicObject dataEntitie = dataes.getDataEntity();
            dataEntitie = BusinessDataServiceHelper.loadSingle(dataEntitie.getPkValue(), dataEntitie.getDynamicObjectType().getName());
            DynamicObjectCollection creditDetails = dataEntitie.getDynamicObjectCollection("yem_creditdetail");
            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);
            if (!creditDetails.isEmpty()) {
                if (YEM.isNotEmpty(customer) && YEM.isNotEmpty(csrCreditLine)) {
                    BigDecimal decimal = CreditBackFillUtils.calcRemainingAmount(csrCreditLine);
                    if (decimal.compareTo(dataEntitie.getBigDecimal("yem_usacreditamt")) < 0) {
                        this.addMessage(dataes, "当前客户对应的信保额度申请剩余额度小于0，请确认！");
                    }
                } else {
                    this.addMessage(dataes, "找不到客户：" + customer.getString("name") + "的客户信保额度申请单");
                }
            }
        }
    }
}
