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

import com.yem.wm.utils.YEM;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.PreparePropertysEventArgs;
import kd.bos.entity.plugin.args.BeforeOperationArgs;

import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

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

/**
 * @author 张立攀
 * @version 1.0
 * @description      保存后海运费=商品明细的销售金额合计值/财务信息的金额（价格条款）*储运托单的海运费；
 * 注：如果一张储运托单生成了多张报关单，需要在最后一次生成报关单时处理尾差。每次生成报关单时，判断该报关单是否由储运全部下推生成，
 * 如果不是，那么按照公式计算海运费，如果是，需要判断该报关单的海运费+其他报关单的海运费是否完全等于上游储运的海运费，
 * 如果相等不需特殊处理，如果不相等需要将尾差在该报关进行处理。
 * @date 2023-10-31 15:21
 */
public class DeclareDocxSaveOp extends AbstractOperationServicePlugIn {

    @Override
    public void onPreparePropertys(PreparePropertysEventArgs e)
    {
        super.onPreparePropertys(e);
        List<String> keys = e.getFieldKeys();
        keys.add("yem_salerorderno");
        keys.add("yem_delivernoticeno");
        keys.add("yem_es_materialinfo");
        keys.add("yem_contractnumbers_m");
    }

    @Override
    public void beforeExecuteOperationTransaction(BeforeOperationArgs e)
    {
        super.beforeExecuteOperationTransaction(e);
        String operationKey = e.getOperationKey();
        DynamicObject[] dataEntities = e.getDataEntities();
        if ("save".equals(operationKey)) {
            for (DynamicObject dataEntity : dataEntities) {
                genContractNo(dataEntity);
//                genTradeTerms(dataEntity);
            }
        }
    }

    /**
     * 出口报关单保存时，将明细信息的合同号和销售出库单单号去重合并到表头的外销合同号和销售出库单号，拼接格式：合同1；合同2；合同3....
     */
    private void genContractNo(DynamicObject dataEntity)
    {
        // 商品明细
        DynamicObjectCollection collection = dataEntity.getDynamicObjectCollection("yem_es_materialinfo");
        Set<String> contractnumbersMSet = new LinkedHashSet<>();
        Set<String> sgnenumberSet = new LinkedHashSet<>();
        for (DynamicObject dynamicObject : collection) {
            String contractnumbersM = dynamicObject.getString("yem_contractnumbers_m");
            String sgnenumber = dynamicObject.getString("yem_sgnenumber");
            if (YEM.isNotEmpty(contractnumbersM)) {
                contractnumbersMSet.add(contractnumbersM);
            }
            if (YEM.isNotEmpty(sgnenumber)) {
                sgnenumberSet.add(sgnenumber);
            }
        }

        StringBuilder contractSb = new StringBuilder();
        for (String no : contractnumbersMSet) {
            contractSb.append(no).append("\r\n");
        }
        if (contractSb.length() > 1) {
            dataEntity.set("yem_salerorderno", contractSb);
        }

        StringBuilder sgnenumberSb = new StringBuilder();
        for (String no : sgnenumberSet) {
            sgnenumberSb.append(no).append("\r\n");
        }
        if (sgnenumberSb.length() > 1) {
            dataEntity.set("yem_delivernoticeno", sgnenumberSb);
        }
    }
}
