package com.yem.wm.es.storagetrans.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.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;

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

/**
 * @author xwudd
 * @Description
 * @date 16:27
 **/
public class StorageTransDelValidator extends AbstractValidator {
    @Override
    public void validate() {
        ExtendedDataEntity[] dataEntities = this.getDataEntities();
        for(ExtendedDataEntity dataEntitie : dataEntities){
            isDeliver(dataEntitie);
            //删除前校验是否包装完成
            boolean flag = true;
            DynamicObject dataEntity = dataEntitie.getDataEntity();
            dataEntity = BusinessDataServiceHelper.loadSingle(dataEntity.getPkValue(), dataEntity.getDynamicObjectType().getName());
            DynamicObjectCollection yem_es_materialinfo = dataEntity.getDynamicObjectCollection("yem_es_materialinfo");
            for(DynamicObject dt:yem_es_materialinfo){
                BigDecimal yemUnpackingqty = dt.getBigDecimal("yem_unpackingqty");
                if(BigDecimal.ZERO.compareTo(yemUnpackingqty) < 0){
                    flag = false;
                }
            }
            if(flag){
                this.addErrorMessage(dataEntitie,"装箱方案已包装完成，无法删除");
            }
        }
    }

    /**
     * @description 判断是否已发货
     * @author xwudd
     * @param[1] dataEntitie
     * return
     * @time 2023/11/13 16:46
     */

    private void isDeliver(ExtendedDataEntity dataEntitie){
        DynamicObject dataEntity = dataEntitie.getDataEntity();
        dataEntity = BusinessDataServiceHelper.loadSingle(dataEntity.getPkValue(), dataEntity.getDynamicObjectType().getName());
        List<Long> entryIds = new ArrayList();
        DynamicObjectCollection materialInfos = dataEntity.getDynamicObjectCollection("yem_es_materialinfo");
        for (int i = 0; i < materialInfos.size(); i++) {
            DynamicObject materialInfo = materialInfos.get(i);
            long sourceEntryId = materialInfo.getLong("yem_sourceentryid");
            entryIds.add(sourceEntryId);
        }

        Map<Long, Map<String, BigDecimal>> allMap = new HashMap<>();
        DynamicObject[] shippingDetails = BusinessDataServiceHelper.load("yem_shippingdetails", "yem_es_materialinfo.yem_storageqty,yem_es_materialinfo.yem_deliveryqty",
                new QFilter[]{new QFilter("yem_es_materialinfo.id", QCP.in, entryIds)});
        for(DynamicObject shippingDetail : shippingDetails){
            DynamicObjectCollection shippingMaterialInfos = shippingDetail.getDynamicObjectCollection("yem_es_materialinfo");
            for(DynamicObject shippingMaterialInfo : shippingMaterialInfos){
                long materialInfoId = shippingMaterialInfo.getLong("id");
                if(entryIds.contains(materialInfoId)){
                    HashMap<String, BigDecimal> map = new HashMap<>();
                    BigDecimal storageQty = shippingMaterialInfo.getBigDecimal("yem_storageqty");
                    BigDecimal deliveryQty = shippingMaterialInfo.getBigDecimal("yem_deliveryqty");
                    map.put("storageQty",storageQty);
                    map.put("deliveryQty",deliveryQty);
                    allMap.put(materialInfoId,map);
                }
            }
        }

        for (int i = 0; i < materialInfos.size(); i++) {
            DynamicObject materialInfo = materialInfos.get(i);
            long sourceEntryId = materialInfo.getLong("yem_sourceentryid");
            Map<String, BigDecimal> map = allMap.get(sourceEntryId);
            if(YEM.isNotEmpty(map)){
                BigDecimal storageQty = map.get("storageQty");
                BigDecimal deliveryQty = map.get("deliveryQty");
                if(deliveryQty.subtract(storageQty).compareTo(BigDecimal.ZERO) >= 0){
                    this.addErrorMessage(dataEntitie,String.format("储运托单第%s行分录存在发货信息，请取消发货后，再删除储运托单！",i + 1));
                }
            }
        }
    }
}
