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

import com.yem.wm.es.Util.TotalUtil;
import com.yem.wm.es.encasement.util.EncaseMentUtil;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.entity.datamodel.events.ChangeData;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.form.IFormView;
import kd.bos.form.control.Control;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.fi.ai.util.BigdecimalUtil;

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

/**
 * @author xwudd
 * @date 15:57
 * @Description
 **/
public class AccPackingBackEdit extends AbstractFormPlugin {

    @Override
    public void afterCreateNewData(EventObject e) {
        IFormView view = this.getView();
        IDataModel model = this.getModel();
        IFormView parentView = view.getParentView();
        IDataModel parentModel = parentView.getModel();
        DynamicObjectCollection materialInfos = parentModel.getEntryEntity("yem_im_materialinfo_mi");
        int materialInfoRows = parentModel.getEntryRowCount("yem_im_materialinfo_mi");
        DynamicObject yem_lenghtunit_total = null;
        BigDecimal yem_long = BigDecimal.ZERO;
        BigDecimal yem_wide = BigDecimal.ZERO;
        BigDecimal yem_high = BigDecimal.ZERO;
        BigDecimal yem_vol = BigDecimal.ZERO;
        BigDecimal yem_volm3 = BigDecimal.ZERO;
        BigDecimal yem_volm3sum = BigDecimal.ZERO;
        BigDecimal yem_piece = BigDecimal.ZERO;
        BigDecimal yem_grossweight = BigDecimal.ZERO;
        BigDecimal yem_grossweightsum = BigDecimal.ZERO;
        BigDecimal yem_packingqty = BigDecimal.ZERO;
        BigDecimal yem_residueqty = BigDecimal.ZERO;
        for (int i = 0; i < materialInfoRows; i++) {
            String businessSort = (String)parentModel.getValue("yem_businesssort",i);
            BigDecimal residueQty = (BigDecimal) parentModel.getValue("yem_residueqty",i);
            if("P".equals(businessSort) && residueQty.compareTo(BigDecimal.ZERO) > 0){
                int entryRow = model.createNewEntryRow("yem_materialinfo");
                model.setValue("yem_material",parentModel.getValue("yem_material",i),entryRow);//物料编码
                model.setValue("yem_residueqty",parentModel.getValue("yem_residueqty",i),entryRow);//剩余数量
                yem_residueqty = yem_residueqty.add((BigDecimal) parentModel.getValue("yem_residueqty",i));
                model.setValue("yem_pieceqty",parentModel.getValue("yem_pieceqty",i),entryRow);//单件数量
//                model.setValue("yem_packingqty",parentModel.getValue("yem_packingqty",i),entryRow);//包装数量
//                yem_packingqty = yem_packingqty.add((BigDecimal) parentModel.getValue("yem_packingqty",i));
                model.setValue("yem_piece",parentModel.getValue("yem_piece",i),entryRow);//件数
                yem_piece = yem_piece.add((BigDecimal) parentModel.getValue("yem_piece",i));
                model.setValue("yem_lenghtunit",parentModel.getValue("yem_lenghtunit",i),entryRow);//长度单位
                yem_lenghtunit_total = (DynamicObject) parentModel.getValue("yem_lenghtunit",i);
                model.setValue("yem_long",parentModel.getValue("yem_long",i),entryRow);//长
                yem_long = yem_long.add((BigDecimal) parentModel.getValue("yem_long",i));
                model.setValue("yem_wide",parentModel.getValue("yem_wide",i),entryRow);//宽
                yem_wide = yem_wide.add((BigDecimal) parentModel.getValue("yem_wide",i));
                model.setValue("yem_high",parentModel.getValue("yem_high",i),entryRow);//高
                yem_high = yem_high.add((BigDecimal) parentModel.getValue("yem_high",i));
                model.setValue("yem_grossweight",parentModel.getValue("yem_grossweight",i),entryRow);//毛重
                yem_grossweight = yem_grossweight.add((BigDecimal) parentModel.getValue("yem_grossweight",i));
                model.setValue("yem_vol",parentModel.getValue("yem_vol",i),entryRow);//体积
                yem_vol = yem_vol.add((BigDecimal) parentModel.getValue("yem_vol",i));
                model.setValue("yem_grossweightsum",parentModel.getValue("yem_grossweightsum",i),entryRow);//总毛重
                yem_grossweightsum = yem_grossweightsum.add((BigDecimal) parentModel.getValue("yem_grossweightsum",i));
                model.setValue("yem_volm3",parentModel.getValue("yem_volm3",i),entryRow);//体积M3
                yem_volm3 = yem_volm3.add((BigDecimal) parentModel.getValue("yem_volm3",i));
                model.setValue("yem_volm3sum",parentModel.getValue("yem_volm3sum",i),entryRow);//总体积M3
                yem_volm3sum = yem_volm3sum.add((BigDecimal) parentModel.getValue("yem_volm3sum",i));
                model.setValue("yem_materialinfoid",materialInfos.get(i).getPkValue(),entryRow);//明细信息分录行内码
            }
        }
        //表头汇总字段

        model.setValue("yem_lenghtunit_total",yem_lenghtunit_total);
        model.setValue("yem_packingqty_total",yem_packingqty);
    }

    @Override
    public void registerListener(EventObject e) {
        this.addClickListeners("btnok");
    }

    @Override
    public void click(EventObject e) {
        super.click(e);
        IFormView view = this.getView();
        IDataModel model = this.getModel();
        IFormView parentView = view.getParentView();
        IDataModel parentModel = parentView.getModel();
        Control control = (Control)e.getSource();
        if("btnok".equals(control.getKey())){
            Map<Long, BigDecimal> map = new HashMap<Long, BigDecimal>();
            int materialInfoRows = model.getEntryRowCount("yem_materialinfo");
            Map<String,Object> allMap = new HashMap<>();
            BigDecimal pieceNum = BigDecimal.ZERO;
            BigDecimal quantity = BigDecimal.ZERO;
            BigDecimal sumRoughWeight = BigDecimal.ZERO;
            BigDecimal sumVolumeM3 = BigDecimal.ZERO;
            for (int i = 0; i < materialInfoRows; i++) {
                BigDecimal packingQty = (BigDecimal) model.getValue("yem_packingqty",i);//包装数量
                if(packingQty.compareTo(BigDecimal.ZERO) > 0){
                    int entryRow = parentModel.createNewEntryRow("yem_pjpackageinfo");
                    long materialInfoId = (long)model.getValue("yem_materialinfoid", i);
                    BigDecimal packingQtyMaterial = (BigDecimal)model.getValue("yem_packingqty", i);
                    map.put(materialInfoId,packingQtyMaterial);
                    String packName = (String)model.getValue("yem_packname");
                    String trayNo = (String)model.getValue("yem_trayno");
                    String boxNo = (String)model.getValue("yem_boxno");
                    DynamicObject packType = (DynamicObject)model.getValue("yem_packtype");
                    BigDecimal piece = (BigDecimal)model.getValue("yem_piece", i);
                    BigDecimal grossWeightSum = (BigDecimal)model.getValue("yem_grossweightsum", i);
                    BigDecimal volM3Sum = (BigDecimal)model.getValue("yem_volm3sum", i);
                    parentModel.setValue("yem_bzmc",packName,entryRow);//包装名称
                    parentModel.setValue("yem_traynumbers",trayNo,entryRow);//托盘号
                    parentModel.setValue("yem_boxnumber",boxNo,entryRow);//箱号
                    parentModel.setValue("yem_packstyles",packType,entryRow);//包装方式
                    parentModel.setValue("yem_materiels",model.getValue("yem_material",i),entryRow);//物料编码
                    parentModel.setValue("yem_qtys",packingQtyMaterial,entryRow);//包装数量
                    parentModel.setValue("yem_pieces",piece,entryRow);//件数
                    parentModel.setValue("yem_pieceqtys",model.getValue("yem_pieceqty",i),entryRow);//单件数量
                    parentModel.setValue("yem_lenghtunits",model.getValue("yem_lenghtunit",i),entryRow);//长度单位
                    parentModel.setValue("yem_longs",model.getValue("yem_long",i),entryRow);//长
                    parentModel.setValue("yem_wides",model.getValue("yem_wide",i),entryRow);//宽
                    parentModel.setValue("yem_highs",model.getValue("yem_high",i),entryRow);//高
                    parentModel.setValue("yem_grossweights",model.getValue("yem_grossweight",i),entryRow);//毛重
                    parentModel.setValue("yem_vols",model.getValue("yem_vol",i),entryRow);//体积
                    parentModel.setValue("yem_grossweightsums",grossWeightSum,entryRow);//总毛重
                    parentModel.setValue("yem_volm3s",model.getValue("yem_volm3",i),entryRow);//体积M3
                    parentModel.setValue("yem_volm3sums",volM3Sum,entryRow);//总体积M3
                    parentModel.setValue("yem_bz",model.getValue("yem_remark",i),entryRow);//备注
                    parentModel.setValue("yem_materialinfoid",materialInfoId,entryRow);//明细信息分录行内码
                    quantity = quantity.add(packingQtyMaterial);
                    pieceNum = pieceNum.add(piece);
                    sumRoughWeight = sumRoughWeight.add(grossWeightSum);
                    sumVolumeM3 = sumVolumeM3.add(volM3Sum);
                    allMap.put("packName",packName);
                    allMap.put("trayNo",trayNo);
                    allMap.put("boxNo",boxNo);
                    allMap.put("packType",packType);
                    allMap.put("quantity",model.getValue("yem_packingqty_total"));
                    allMap.put("pieceNum",model.getValue("yem_piece_total"));
                    allMap.put("sumRoughWeight",model.getValue("yem_grossweightsum_total"));
                    allMap.put("sumVolumeM3",model.getValue("yem_volm3sum_total"));
                }
            }
            EncaseMentUtil.backFillResidueQtyFor(parentModel,map,true);
            EncaseMentUtil.getSumPartSpa(parentModel,allMap);
            view.sendFormAction(parentView);
            view.close();
        }
    }

    @Override
    public void propertyChanged(PropertyChangedArgs e) {
        IFormView view = this.getView();
        IDataModel model = this.getModel();
        String name = e.getProperty().getName();
        ChangeData changeData = e.getChangeSet()[0];
        int rowIndex = changeData.getRowIndex();
        BigDecimal one  = BigDecimal.ONE;
        BigDecimal yem_volm3_total = (BigDecimal) this.getModel().getValue("yem_volm3_total");
        BigDecimal yem_piece_total = (BigDecimal) this.getModel().getValue("yem_piece_total");
        BigDecimal yem_piece_total_new = (BigDecimal) this.getModel().getValue("yem_piece_total");
        BigDecimal yem_grossweight_total = (BigDecimal) this.getModel().getValue("yem_grossweight_total");
        DynamicObject dataEntity = model.getDataEntity(true);
        switch(name){
            case "yem_lenghtunit" :
            case "yem_vol" :
                TotalUtil.countVolM3(model,rowIndex,"yem_lenghtunit","yem_vol","yem_volm3");
                break;
            case "yem_packingqty" :
                BigDecimal packingQty = (BigDecimal)model.getValue("yem_packingqty",rowIndex);
                BigDecimal residueQty = (BigDecimal)model.getValue("yem_residueqty",rowIndex);
                if(packingQty.compareTo(residueQty) > 0){
                    model.setValue("yem_packingqty",changeData.getOldValue(),rowIndex);
                    view.showErrorNotification("包装数量不能大于剩余数量！");
                }
                DynamicObjectCollection yem_materialinfo = dataEntity.getDynamicObjectCollection("yem_materialinfo");
                BigDecimal yem_packingqtyNew = BigDecimal.ZERO;
                for(DynamicObject dt:yem_materialinfo){
                    yem_packingqtyNew = yem_packingqtyNew.add(dt.getBigDecimal("yem_packingqty"));
                }
                this.getModel().setValue("yem_packingqty_total",yem_packingqtyNew);
                break;
            case "yem_pieceqty":
                BigDecimal yemPieceqty = (BigDecimal) this.getModel().getValue("yem_pieceqty",rowIndex);
                BigDecimal yemPackingqty =(BigDecimal)  this.getModel().getValue("yem_packingqty",rowIndex);
                if(yemPieceqty.compareTo(BigDecimal.ZERO) != 0){
                    this.getModel().setValue("yem_piece",yemPackingqty.divide(yemPieceqty,BigDecimal.ROUND_HALF_UP),rowIndex);
                }
                break;
            //表头计算 体积=长*宽*高
            case "yem_long_total":
            case "yem_wide_total":
            case "yem_high_total":
                BigDecimal yem_long_total = (BigDecimal) this.getModel().getValue("yem_long_total");
                BigDecimal yem_wide_total = (BigDecimal) this.getModel().getValue("yem_wide_total");
                BigDecimal yem_high_total = (BigDecimal) this.getModel().getValue("yem_high_total");

                this.getModel().setValue("yem_vol_total",one.multiply(yem_long_total).multiply(yem_wide_total).multiply(yem_high_total));
                break;
            //，体积M3=体积/1000000（10的6次方），
            case "yem_vol_total":
                BigDecimal yem_vol_total = (BigDecimal) this.getModel().getValue("yem_vol_total");
                this.getModel().setValue("yem_volm3_total",yem_vol_total.divide(new BigDecimal(1000000)),5,BigDecimal.ROUND_HALF_UP);
                break;
             //总体积M3=体积M3*件数；
            case "yem_volm3_total":
                this.getModel().setValue("yem_volm3sum_total",one.multiply(yem_volm3_total).multiply(yem_piece_total));
                break;
            case "yem_piece_total":
                this.getModel().setValue("yem_volm3sum_total",one.multiply(yem_volm3_total).multiply(yem_piece_total));
                this.getModel().setValue("yem_grossweightsum_total",one.multiply(yem_piece_total_new).multiply(yem_grossweight_total));
                break;
            //（3）总毛重=毛重*件数；
            case "yem_grossweight_total":
                this.getModel().setValue("yem_grossweightsum_total",one.multiply(yem_piece_total_new).multiply(yem_grossweight_total));
                break;
        }
    }


    @Override
    public void beforePropertyChanged(PropertyChangedArgs e) {
        IFormView view = this.getView();
        IDataModel model = this.getModel();
        String name = e.getProperty().getName();
        ChangeData changeData = e.getChangeSet()[0];
        int rowIndex = changeData.getRowIndex();
        switch(name){

        }
    }
}
