package com.yem.wm.im.materialpriceing;

import kd.bos.bill.AbstractBillPlugIn;
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 java.math.BigDecimal;

/**
 * @author ljw
 * @date 2024/9/6 10:05
 * @description materialPriceingPlugin
 */
public class materialPriceingPlugin extends AbstractBillPlugIn {
    @Override
    public void propertyChanged(PropertyChangedArgs e) {
        super.propertyChanged(e);
        super.propertyChanged(e);
        String name = e.getProperty().getName();
        ChangeData[] changeSet = e.getChangeSet();
        switch (name) {
            case "yem_from":
                yemFrom(e);
                break;
            case "yem_reach":
                yemReach(e);
                break;
        }
    }

    /**
     * 至不允许小于等于从
     *
     * @param e
     */
    private void yemReach(PropertyChangedArgs e) {
        ChangeData changeData = e.getChangeSet()[0];
        int rowIndex = changeData.getRowIndex();
        int parentRowIndex = changeData.getParentRowIndex();
        BigDecimal newValue = (BigDecimal) changeData.getNewValue();
        DynamicObjectCollection yemBdLogistics = this.getModel().getEntryEntity("yem_entryentity");
        BigDecimal bigDecimal = yemBdLogistics.get(parentRowIndex).getDynamicObjectCollection("yem_subentryentity").get(rowIndex).getBigDecimal("yem_from");
        if (bigDecimal.compareTo(newValue) >= 0) {

            this.getModel().setValue("yem_reach",0,rowIndex,parentRowIndex);

            this.getView().showMessage("至不允许小于等于从!!!");
        }
    }

    /**
     * 从不能小于前一行的至
     *
     * @param e
     */
    private void yemFrom(PropertyChangedArgs e) {
        ChangeData changeData = e.getChangeSet()[0];
        int rowIndex = changeData.getRowIndex();
        if (rowIndex - 1 < 0) {
            return;
        }
        int parentRowIndex = changeData.getParentRowIndex();
        BigDecimal newValue = (BigDecimal) changeData.getNewValue();
        DynamicObjectCollection yemBdLogistics = this.getModel().getEntryEntity("yem_entryentity");
        BigDecimal bigDecimal = yemBdLogistics.get(parentRowIndex).getDynamicObjectCollection("yem_subentryentity").get(rowIndex - 1).getBigDecimal("yem_reach");
        if (bigDecimal.compareTo(newValue) > 0) {

            this.getModel().setValue("yem_from",0,rowIndex,parentRowIndex);
            this.getView().showMessage("从不能小于前一行的至!!!");
        }
    }
}
