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

import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.datamodel.events.ChangeData;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.form.control.Control;
import kd.bos.form.control.EntryGrid;
import kd.bos.form.control.events.RowClickEventListener;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

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

/**
 * @author power
 * @ClassName： yem_stuffingmessage
 * @description: 回填装柜信息 动态表单插件
 * @date 2023/8/29
 */
public class StuffingMessageFromPlugin extends AbstractBillPlugIn implements RowClickEventListener {
    @Override
    public void registerListener(EventObject e) {
        // TODO Auto-generated method stub
        //监听点击的行数
        super.registerListener(e);
        this.addClickListeners("btncancel", "btnok");  //监听确认和取消按钮
    }

    @Override
    public void click(EventObject evt) {
        // TODO Auto-generated method stub
        super.click(evt);
        String key = ((Control) evt.getSource()).getKey();

        switch (key) {
            case "btnok" :
                DynamicObject dataEntity = this.getModel().getDataEntity(true);
                DynamicObjectCollection dataEntitys=new DynamicObjectCollection();
                DynamicObjectCollection dataEntityss=new DynamicObjectCollection();
                EntryGrid entryGrid = this.getView().getControl("yem_backentry");
                EntryGrid entryGridb = this.getView().getControl("yem_sumpartspack");
//                int[] selectRows = entryGrid.getSelectRows();
//                int[] selectRow = entryGridb.getSelectRows();
                Map<Integer, Integer> map = new HashMap<Integer, Integer>();
                Map<Integer, Integer> maps = new HashMap<Integer, Integer>();
                DynamicObjectCollection yem_backentrys=dataEntity.getDynamicObjectCollection("yem_backentry");
                int x=0;
                for (DynamicObject yem_backentry : yem_backentrys) {
                    int seq = yem_backentry.getInt("seq");
                    BigDecimal cabinetpiece = yem_backentry.getBigDecimal("yem_cabinetpiece");
                    if (cabinetpiece.compareTo(BigDecimal.ZERO) != 0) {
                        maps.put(x,seq);
                        x++;
                    }
                }
                for (int i = 0; i < maps.size(); i++) {
                    DynamicObject backentry = this.getModel().getEntryRowEntity("yem_backentry",maps.get(i)-1);
                    dataEntitys.add(i,backentry);
                }



//                int rowIndex = this.getModel().getEntryCurrentRowIndex("yem_backentry");
                DynamicObjectCollection yem_sumpartspacks=dataEntity.getDynamicObjectCollection("yem_sumpartspack");
                int j=0;
                for (DynamicObject yem_sumpartspack : yem_sumpartspacks) {
                    int seq = yem_sumpartspack.getInt("seq");
                    BigDecimal piecenum = yem_sumpartspack.getBigDecimal("yem_piecenum");
                    if (piecenum.compareTo(BigDecimal.ZERO) != 0) {
                        map.put(j,seq);
                        j++;
                    }
                }

//                for (int i=0; i < selectRows.length; i++) {
//                    DynamicObject backentry = this.getModel().getEntryRowEntity("yem_backentry",selectRows[i]);
//                    dataEntitys.add(i,backentry);
//                }
//                for (int i=0; i < selectRow.length; i++) {
//                    DynamicObject sumpartspack = this.getModel().getEntryRowEntity("yem_sumpartspack",selectRow[i]);
//                    dataEntityss.add(i,sumpartspack);
//                }

                for (int i = 0; i < map.size(); i++) {
                    DynamicObject sumpartspack = this.getModel().getEntryRowEntity("yem_sumpartspack",map.get(i)-1);
                    dataEntityss.add(i,sumpartspack);
                }


                DynamicObjectCollection[] dataEntit= new DynamicObjectCollection[]{dataEntitys, dataEntityss};

                this.getView().returnDataToParent(dataEntit);
                this.getView().close();
                break;
            case "btncancel":
                this.getView().close();
                break;
            default:
                this.getView().close();
                break;
        }
    }

    @Override
    public void afterCreateNewData(EventObject e) {
        // TODO Auto-generated method stub
        super.afterCreateNewData(e);
        Map<String, Object> map = this.getView().getFormShowParameter().getCustomParams();
        JSONArray yem_backentry = (JSONArray) map.get("yem_backentry");//集装箱信息数据
        DynamicObjectCollection backentry = this.getModel().getEntryEntity("yem_backentry");
        backentry.clear();
        for (int i = 0; i < yem_backentry.size(); i++) {
            JSONObject parseObject = JSONObject.parseObject(JSONObject.toJSONString(yem_backentry.get(i)));
            int index = this.getModel().createNewEntryRow("yem_backentry");
            JSONObject yem_basedatafield_bac = parseObject.getJSONObject("yem_resourceid_bac");
            if (yem_basedatafield_bac != null) {
                this.getModel().setValue("yem_resourceid_bac", yem_basedatafield_bac.getLong("id"), index);
            }
            JSONObject yem_productmodel_bac = parseObject.getJSONObject("yem_productmodel_bac");
            if (yem_productmodel_bac != null) {
                this.getModel().setValue("yem_productmodel_bac", yem_productmodel_bac.getLong("id"), index);
            }
            JSONObject yem_picno = parseObject.getJSONObject("yem_picno");
            if (yem_picno != null) {
                this.getModel().setValue("yem_picno", yem_picno.getLong("id"), index);
            }
            this.getModel().setValue("yem_picnoz", parseObject.getString("yem_picnoz"),index);
            this.getModel().setValue("yem_qtyremain", parseObject.getBigDecimal("yem_qtyremain"),index);
            this.getModel().setValue("yem_weight", parseObject.getBigDecimal("yem_weight"),index);
            this.getModel().setValue("yem_pillarsid", parseObject.getString("id"),index);
        }
        JSONArray yem_sumpartspack = (JSONArray) map.get("yem_sumpartspack");//集装箱信息数据
        DynamicObjectCollection sumpartspack = this.getModel().getEntryEntity("yem_sumpartspack");
        sumpartspack.clear();
        for (int i = 0; i < yem_sumpartspack.size(); i++) {
            JSONObject parseObject = JSONObject.parseObject(JSONObject.toJSONString(yem_sumpartspack.get(i)));
            int index = this.getModel().createNewEntryRow("yem_sumpartspack");
            this.getModel().setValue("yem_packname", parseObject.getString("yem_packname"),index);
            this.getModel().setValue("yem_trayno", parseObject.getString("yem_trayno"),index);
            this.getModel().setValue("yem_boxno", parseObject.getString("yem_boxno"),index);
            this.getModel().setValue("yem_sumroughweight",parseObject.getBigDecimal("yem_sumroughweight"),index);
            this.getModel().setValue("yem_sumvolume",parseObject.getBigDecimal("yem_sumvolume"),index);
            JSONObject yem_packtype = parseObject.getJSONObject("yem_packtype");
            if (yem_packtype != null) {
                this.getModel().setValue("yem_packtype", yem_packtype.getLong("id"), index);
            }
            this.getModel().setValue("yem_quantity", parseObject.getBigDecimal("yem_residuepiece"),index);
            this.getModel().setValue("yem_partsid",parseObject.getString("id"),index);
            BigDecimal piecenum = parseObject.getBigDecimal("yem_piecenum");
            BigDecimal sumroughweight = parseObject.getBigDecimal("yem_sumroughweight");
            BigDecimal sumvolume = parseObject.getBigDecimal("yem_sumvolume");
            if (BigDecimal.ZERO.compareTo(piecenum)!=0){
                this.getModel().setValue("yem_constantone", sumroughweight.divide(piecenum,2,BigDecimal.ROUND_HALF_UP),index);
                this.getModel().setValue("yem_constanttwo", sumvolume.divide(piecenum,2,BigDecimal.ROUND_HALF_UP),index);
            }
        }
    }

    @Override
    public void propertyChanged(PropertyChangedArgs e) {
        super.propertyChanged(e);
        String propertyName = e.getProperty().getName();
        boolean stopClear;
        ChangeData cd = e.getChangeSet()[0];
        ChangeData[] changeData = e.getChangeSet();
        int rowIndex = changeData[0].getRowIndex();//获取分录行数
        switch (propertyName){
            case "yem_cabinetpiece":
                cabinetpiece(rowIndex);
                break;
            case "yem_piecenum":
                piecenum(rowIndex);
                break;
            default:
                break;
        }
    }

    public void cabinetpiece(int row){
        BigDecimal qtyremain = (BigDecimal) this.getModel().getValue("yem_qtyremain", row);
        BigDecimal cabinetpiece = (BigDecimal) this.getModel().getValue("yem_cabinetpiece", row);
        if (cabinetpiece.compareTo(qtyremain)  == 1) {
            this.getModel().setValue("yem_cabinetpiece",BigDecimal.ZERO,row);
            this.getView().showErrorNotification("装柜件数不允许超出剩余数量");
        }
    }
    public void piecenum(int row){
        BigDecimal quantity = (BigDecimal) this.getModel().getValue("yem_quantity", row);
        BigDecimal piecenum = (BigDecimal) this.getModel().getValue("yem_piecenum", row);
        if (piecenum.compareTo(quantity)  == 1) {
            this.getModel().setValue("yem_piecenum",BigDecimal.ZERO,row);
            this.getView().showErrorNotification("件数不允许超出剩余件数");
        }
//        else {
//            if (BigDecimal.ZERO.compareTo(piecenum)!=0){
//                BigDecimal constantone = (BigDecimal) this.getModel().getValue("yem_constantone", row);
//                BigDecimal constanttwo = (BigDecimal) this.getModel().getValue("yem_constanttwo", row);
//                this.getModel().setValue("yem_sumroughweight",constantone.multiply(piecenum),row);
//                this.getModel().setValue("yem_sumvolume",constantone.multiply(constanttwo),row);
//            }
//            if (BigDecimal.ZERO.compareTo(piecenum)==0){
//                this.getModel().setValue("yem_sumroughweight",BigDecimal.ZERO,row);
//                this.getModel().setValue("yem_sumvolume",BigDecimal.ZERO,row);
//            }
//        }
    }

}
