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

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.datamodel.IDataModel;

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

/**
 * @author xwudd
 * @date 17:04
 * @Description
 **/
public class EncaseMentUtil {

    /**
     * @description 回填已包装数量和剩余数量
     * @author xwudd
     * @param[1] model
     * @param[2] map
     * return
     * @time 2023/8/29 17:33
     */
    public static void backFillResidueQtyFor(IDataModel model,Map<Long, BigDecimal> map,Boolean flag){
        DynamicObjectCollection materialInfos = model.getEntryEntity("yem_im_materialinfo_mi");
        for (int i = 0; i < materialInfos.size(); i++) {
            DynamicObject dataEntity = materialInfos.get(i);
            long id = dataEntity.getLong("id");
            if(map.containsKey(id)){
                BigDecimal shipmentQty = (BigDecimal)model.getValue("yem_shipmentqty", i);
                BigDecimal packingQty = (BigDecimal)model.getValue("yem_packingqty", i);
                if(flag){
                    packingQty = packingQty.add(map.get(id));
                    model.setValue("yem_packingqty",packingQty,i);
                    model.setValue("yem_residueqty",shipmentQty.subtract(packingQty),i);
                }else{
                    packingQty = packingQty.subtract(map.get(id));
                    model.setValue("yem_packingqty",packingQty,i);
                    model.setValue("yem_residueqty",shipmentQty.subtract(packingQty),i);
                }
            }
        }
    }


    /**
     * @description 配件汇总信息赋值
     * @author xwudd
     * @param[1] model
     * @param[2] map
     * return
     * @time 2023/8/30 17:03
     */

    public static void getSumPartSpa(IDataModel model,Map<String,Object> map){
        Boolean flag = true;
        String trayNoMap = (String)map.get("trayNo");
        String boxNoMap = (String)map.get("boxNo");
        int sumPartSpaRows = model.getEntryRowCount("yem_sumpartspack");
        for (int i = 0; i < sumPartSpaRows; i++) {
            String trayNo = (String)model.getValue("yem_trayno",i);
            String boxNo = (String)model.getValue("yem_boxno",i);
            if(trayNo.equals(trayNoMap) && boxNo.equals(boxNoMap)){
                BigDecimal pieceNum = (BigDecimal) model.getValue("yem_piecenum", i);
                BigDecimal quantity = (BigDecimal) model.getValue("yem_quantity", i);
                BigDecimal sumRoughWeight = (BigDecimal) model.getValue("yem_sumroughweight", i);
                BigDecimal sumVolumeM3 = (BigDecimal) model.getValue("yem_sumvolume", i);
                BigDecimal stuffingPiece = (BigDecimal) model.getValue("yem_stuffingpiece", i);
                BigDecimal quantityMap = (BigDecimal)map.get("quantity");
                BigDecimal pieceNumMap = (BigDecimal)map.get("pieceNum");
                BigDecimal sumRoughWeightMap = (BigDecimal)map.get("sumRoughWeight");
                BigDecimal sumVolumeM3Map = (BigDecimal)map.get("sumVolumeM3");
                model.setValue("yem_piecenum",pieceNum.add(pieceNumMap),i);
                model.setValue("yem_quantity",quantity.add(quantityMap),i);
                model.setValue("yem_sumroughweight",sumRoughWeight.add(sumRoughWeightMap),i);
                model.setValue("yem_sumvolume",sumVolumeM3.add(sumVolumeM3Map),i);
                model.setValue("yem_residuepiece",pieceNum.add(pieceNumMap).subtract(stuffingPiece),i);
                flag = false;
            }
        }
        if(flag){
            int entryRow = model.createNewEntryRow("yem_sumpartspack");
            model.setValue("yem_packname",map.get("packName"),entryRow);
            model.setValue("yem_trayno",map.get("trayNo"),entryRow);
            model.setValue("yem_boxno",map.get("boxNo"),entryRow);
            model.setValue("yem_packtype",map.get("packType"),entryRow);
            model.setValue("yem_piecenum",map.get("pieceNum"),entryRow);
            model.setValue("yem_quantity",map.get("quantity"),entryRow);
            model.setValue("yem_sumroughweight",map.get("sumRoughWeight"),entryRow);
            model.setValue("yem_sumvolume",map.get("sumVolumeM3"),entryRow);
            model.setValue("yem_residuepiece",map.get("pieceNum"),entryRow);
        }
    }



}
