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

import com.google.common.collect.Sets;
import com.yem.wm.utils.DynamicObjectUtil;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.args.AfterOperationArgs;
import kd.bos.entity.plugin.args.BeforeOperationArgs;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;

import java.math.BigDecimal;
import java.util.*;

/**
 * ljw
 * 箱型箱量赋值
 */
public class EncaseMentSaveOp extends AbstractOperationServicePlugIn {

    @Override
    public void afterExecuteOperationTransaction(AfterOperationArgs e) {
        super.afterExecuteOperationTransaction(e);
        for (DynamicObject dataEntity : e.getDataEntities()) {
            long id = dataEntity.getLong("id");
            String selectFields = DynamicObjectUtil.getSelectfields("yem_im_encasement", false);
            String entrySelectfields = DynamicObjectUtil.getEntrySelectfields(selectFields, "yem_im_encasement", "yem_im_materialinfo_mi", false);
            DynamicObject yemImEncasement = BusinessDataServiceHelper.loadSingle("yem_im_encasement", entrySelectfields, new QFilter[]{new QFilter("id", QCP.equals, id)});
            if (yemImEncasement != null) {
                DynamicObjectCollection yemImMaterialinfoMi = yemImEncasement.getDynamicObjectCollection("yem_im_materialinfo_mi");
                Map<Long, BigDecimal> map = new HashMap<>();
                for (DynamicObject dynamicObject : yemImMaterialinfoMi) {
                    DynamicObject yemBigcabinet = dynamicObject.getDynamicObject("yem_bigcabinet");
                    DynamicObject yemMincabinet = dynamicObject.getDynamicObject("yem_mincabinet");
                    BigDecimal yemBigcabinetnumbers = dynamicObject.getBigDecimal("yem_bigcabinetnumbers");
                    BigDecimal yemMincabinetnumbers = dynamicObject.getBigDecimal("yem_mincabinetnumbers");
                    extracted(yemBigcabinetnumbers, yemBigcabinet, map, yemBigcabinetnumbers);
                    extracted(yemMincabinetnumbers, yemMincabinet, map, yemMincabinetnumbers);
                }
                String string = "";
                Map<Long, BigDecimal> longBigDecimalMap = new HashMap<>();
                for (DynamicObject dynamicObject : yemImMaterialinfoMi) {
                    DynamicObject yemBigcabinet = dynamicObject.getDynamicObject("yem_bigcabinet");
                    DynamicObject yemMincabinet = dynamicObject.getDynamicObject("yem_mincabinet");
                    BigDecimal yemBigcabinetnumbers = dynamicObject.getBigDecimal("yem_bigcabinetnumbers");
                    BigDecimal yemMincabinetnumbers = dynamicObject.getBigDecimal("yem_mincabinetnumbers");
                    string = getString(yemBigcabinetnumbers, yemBigcabinet, yemBigcabinet, map, longBigDecimalMap, string);
                    string = getString(yemMincabinetnumbers, yemBigcabinet, yemMincabinet, map, longBigDecimalMap, string);
                }
                if (string.length() > 0) {
                    yemImEncasement.set("yem_cartontypequantity", string.substring(0, string.length() - 1));
                }
                SaveServiceHelper.update(yemImEncasement);
            }
        }
    }

    private static String getString(BigDecimal yemMincabinetnumbers, DynamicObject yemBigcabinet, DynamicObject yemMincabinet, Map<Long, BigDecimal> map, Map<Long, BigDecimal> longBigDecimalMap, String string) {
        if (yemMincabinetnumbers.compareTo(BigDecimal.ZERO) != 0) {
            if (yemBigcabinet != null) {
                long aLong = yemMincabinet.getLong("id");
                String name = yemMincabinet.getString("name");
                if (map.get(aLong) != null && longBigDecimalMap.get(aLong) == null) {
                    BigDecimal bigDecimal = map.get(aLong).setScale(0);
                    string = string + name + "*" + bigDecimal + ";";
                    longBigDecimalMap.put(aLong, yemMincabinetnumbers);
                }
            }
        }
        return string;
    }

    private static void extracted(BigDecimal yemMincabinetnumbers, DynamicObject yemMincabinet, Map<Long, BigDecimal> map, BigDecimal yemBigcabinetnumbers) {
        if (yemMincabinetnumbers.compareTo(BigDecimal.ZERO) != 0) {
            if (yemMincabinet != null) {
                long aLong = yemMincabinet.getLong("id");
                if (map.get(aLong) == null) {
                    map.put(aLong, yemMincabinetnumbers);
                } else {
                    BigDecimal bigDecimal = map.get(aLong);
                    BigDecimal Bigcabinetnumbers = bigDecimal.add(yemBigcabinetnumbers);
                    map.put(aLong, Bigcabinetnumbers);
                }
            }
        }
    }
}
