package com.yem.wm.es.Util;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;

import java.math.BigDecimal;

/**
 * @Description TODO
 * @Date 2024/3/14 16:22
 * @Created by ZZSLL
 */

public class CreditBackFillUtils {
    public static BigDecimal calcRemainingAmount(DynamicObject dynamicObject) {
        // yem_balance = yem_replylimit - yem_initoccupancy + yem_initcollectionamt - yem_totaloccupancy + yem_totalwriteoffamt
        // 剩余限额 = 批复金额（美元） - 期初占用额度 + 期初回款额度 - 投保总额（信保占用） + 回款总额（核销）
        BigDecimal yem_replylimit = dynamicObject.getBigDecimal("yem_replylimit");
        BigDecimal yem_initoccupancy = dynamicObject.getBigDecimal("yem_initoccupancy");
        BigDecimal yem_initcollectionamt = dynamicObject.getBigDecimal("yem_initcollectionamt");
//        BigDecimal yem_totaloccupancy = dynamicObject.getBigDecimal("yem_totaloccupancy");
        BigDecimal yem_totaloccupancy = BigDecimal.ZERO;
//        BigDecimal yem_totalwriteoffamt = dynamicObject.getBigDecimal("yem_totalwriteoffamt");
        BigDecimal yem_totalwriteoffamt = BigDecimal.ZERO;
        DynamicObjectCollection collection = dynamicObject.getDynamicObjectCollection("yem_im_info");
        for (DynamicObject object : collection) {
            yem_totaloccupancy = yem_totaloccupancy.add(object.getBigDecimal("yem_creditamtuse"));
            yem_totalwriteoffamt = yem_totalwriteoffamt.add(object.getBigDecimal("yem_actualsinkamt"));
        }
        return yem_replylimit.subtract(yem_initoccupancy).add(yem_initcollectionamt).subtract(yem_totaloccupancy).add(yem_totalwriteoffamt);
    }
}
