package com.yem.wm.im.orderproreqbill.validator;

import com.yem.wm.utils.YEM;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.ExtendedDataEntity;
import kd.bos.entity.validate.AbstractValidator;
import kd.bos.servicehelper.BusinessDataServiceHelper;

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

/**
 * @Classname OrderProReqBillSubmitValidator
 * @Description （关联订单产品需求单数量-关闭数量）是否超出备料数量
 * @Date 2023/12/27 15:47
 * @Created by ZZSLL
 */

public class OrderProReqBillSaveValidator extends AbstractValidator
{
    @Override
    public void validate()
    {
        ExtendedDataEntity[] dataEntities = this.getDataEntities();
        for (ExtendedDataEntity entry : dataEntities) {
            DynamicObject dataEntity = entry.getDataEntity();
            DynamicObjectCollection collection = dataEntity.getDynamicObjectCollection("yem_es_materialinfo");
            long sourcebillid = dataEntity.getLong("yem_sourcebillid");
            if (YEM.isEmpty(sourcebillid)) {
                return;
            }
            DynamicObject single = BusinessDataServiceHelper.loadSingle(sourcebillid, "yem_tracktaskconsole");
            if (single == null) {
                return;
            }
            DynamicObjectCollection materialinfo = single.getDynamicObjectCollection("yem_es_materialinfo");

            if (materialinfo == null) {
                return;
            }

            Map<Long, Map<String, BigDecimal>> map = new HashMap<>();
            for (DynamicObject materialinfoEntry : materialinfo) {
                DynamicObjectCollection salesorderR = single.getDynamicObjectCollection("yem_es_salesorder_r");
                Long id = (Long) materialinfoEntry.getPkValue();
                for (DynamicObject dynamicObject : salesorderR) {
                    long entrtyid = dynamicObject.getLong("yem_entrtyid");
                    if (entrtyid == id) {
                        Map<String, BigDecimal> subMap;
                        if (map.containsKey(entrtyid)) {
                            subMap = map.get(entrtyid);
                            // 关联订单需求表数量
                            subMap.put("yem_qtyfield6", subMap.get("yem_qtyfield6").add(dynamicObject.getBigDecimal("yem_qtyfield6")));
                            // 关闭数量
                            subMap.put("yem_closecount", subMap.get("yem_closecount").add(dynamicObject.getBigDecimal("yem_closecount")));
                        } else {
                            subMap = new HashMap<>();
                            // 关联订单需求表数量
                            subMap.put("yem_qtyfield6", dynamicObject.getBigDecimal("yem_qtyfield6"));
                            // 关闭数量l
                            subMap.put("yem_closecount", dynamicObject.getBigDecimal("yem_closecount"));
                        }
                        map.put(entrtyid, subMap);
                    }
                }
            }
            for (DynamicObject dynamicObject : collection) {
                long trackentryid = dynamicObject.getLong("yem_trackentryid");
                for (Map.Entry<Long, Map<String, BigDecimal>> mapEntry : map.entrySet()) {
                    if (trackentryid == mapEntry.getKey()) {
                        // 关联订单需求表数量
                        BigDecimal qtyfield6 = mapEntry.getValue().get("yem_qtyfield6");
                        // 关闭数量
                        BigDecimal closecount = mapEntry.getValue().get("yem_closecount");
                        // 备料数量
                        BigDecimal materialcount = dynamicObject.getBigDecimal("yem_stockqty");
                        if (materialcount.compareTo(qtyfield6.subtract(closecount)) < 0) {
                            this.addErrorMessage(entry, "关联订单产品需求单数量 - 关闭数量不能大于备料数量");
                        }
                    }
                }
            }
        }
    }
}
