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

import com.yem.tws.common1.BigDecimalUtils;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.ExtendedDataEntity;
import kd.bos.entity.botp.plugin.AbstractConvertPlugIn;
import kd.bos.entity.botp.plugin.args.AfterConvertEventArgs;
import kd.bos.entity.botp.plugin.args.BeforeCreateTargetEventArgs;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * @author 张立攀
 * @version 1.0
 * @description  储运下推出口报关单
 * @date 2023-11-10 17:54
 */
public class StorgeTransToDeclareDocxConvert extends AbstractConvertPlugIn {


    @Override
    public void beforeCreateTarget(BeforeCreateTargetEventArgs e) {
        super.beforeCreateTarget(e);
    }

    @Override
    public void afterConvert(AfterConvertEventArgs e) {
        String tgtName = this.getTgtMainType().getName();
        String name = this.getSrcMainType().getName();
        ExtendedDataEntity[] findByEntityKeys = e.getTargetExtDataEntitySet().FindByEntityKey(tgtName);
        for(ExtendedDataEntity findByEntityKey : findByEntityKeys){
            // 出口报关单
            DynamicObject dataEntity = findByEntityKey.getDataEntity();

            DynamicObjectCollection collection = dataEntity.getDynamicObjectCollection("yem_es_materialinfo");
            BigDecimal sumAmt = BigDecimal.ZERO;
            for (DynamicObject dynamicObject : collection) {
                BigDecimal sellamt = dynamicObject.getBigDecimal("yem_sellamt");
                sumAmt = sumAmt.add(sellamt);
            }

            // 订舱金额（价格条款）
            BigDecimal priceterms = dataEntity.getBigDecimal("yem_priceterms");

            BigDecimal premiumamt = dataEntity.getBigDecimal("yem_premiumamt");

            BigDecimal calc = BigDecimalUtils.div(sumAmt, priceterms, 12).multiply(premiumamt).setScale(2, RoundingMode.HALF_UP);
            dataEntity.set("yem_premiumamt", calc);
        }
    }
}
