package com.yem.wm.es.salesorder.op;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.args.EndOperationTransactionArgs;
import kd.bos.i18n.mservice.I18nServiceHelper;
import kd.bos.i18n.mservice.utils.AmountConvertResult;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Map;

import static com.yem.wm.syn.cmmp.addnew.OrderProReqBillWMI.getDownList;

//import static com.yem.wm.es.salesorder.utils.SalesOrderUtils.genTradeTerms;

/**
 * ljw
 * 保存时贸易术语赋值
 */
public class SalesOrderSaveOp extends AbstractOperationServicePlugIn {

    @Override
    public void endOperationTransaction(EndOperationTransactionArgs e) {
        super.endOperationTransaction(e);
        if ("save".equals(e.getOperationKey())) {
            DynamicObject[] bills = e.getDataEntities();
            for (DynamicObject bill : bills) {
                bill = BusinessDataServiceHelper.loadSingle(bill.getPkValue(), bill.getDynamicObjectType().getName());
//                Map<String, Object> downList = getDownList(bill.getDataEntityType().getName(), "yem_isgiftgiveparts");
//                genTradeTerms(bill);

                BigDecimal offeramount = bill.getBigDecimal("yem_offeramount");
                AmountConvertResult result = I18nServiceHelper.amountConvertUppercase("ZH", "CNY", offeramount.setScale(2, RoundingMode.HALF_UP).toString(), "false");
                if (result.isSuccess()) {
                    bill.set("yem_textamountzhcn", result.getResult());
                }
                bill.set("yem_iscopied", true);
                SaveServiceHelper.save(new DynamicObject[]{bill});
            }
        }
    }
}
