package com.yem.wm.cm.exportfeert.form;

import com.alibaba.druid.util.StringUtils;
import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.datamodel.RowDataEntity;
import kd.bos.entity.datamodel.events.AfterAddRowEventArgs;
import kd.bos.entity.datamodel.events.ChangeData;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;


/**
 * @description: 出口费用申请
 * @author yhj
 * @date 2023/7/26 14:55
 * @version 1.0
 */
public class LogisticsFormPlugin extends AbstractBillPlugIn {
    public static final String BILL_ENTRY = "yem_cm_costapp_fy";  // 父分录标识
    public static final String KEY_ENTRYENTITY = "yem_cm_costapp_sub";// 子分录标识
    @Override
    public void propertyChanged(PropertyChangedArgs e) {
        super.propertyChanged(e);
        String name = e.getProperty().getName();
        ChangeData cd = e.getChangeSet()[0];
        ChangeData[] changeData = e.getChangeSet();
        int rowIndex = changeData[0].getRowIndex();//获取分录行数
        switch (name){
            case "yem_numerical":
            case "yem_storagetransno":
                GetAdvance(cd);
                break;
        }
    }
    /**
     * @description: 新增行赋值
     * @author yhj
     * @date 2023/7/26 19:06
     * @version 1.0
     */
    @Override
    public void afterAddRow(AfterAddRowEventArgs e) {
        int parentRow = e.getParentRow();
        RowDataEntity[] rowDataEntities = e.getRowDataEntities();
        if (StringUtils.equals(KEY_ENTRYENTITY, e.getEntryProp().getName())){
            DynamicObjectCollection sire = this.getModel().getEntryEntity("yem_cm_costapp_fy");
                String numerical = (String) sire.get(parentRow).get("yem_numerical");//提单号
                DynamicObject storagetransno= (DynamicObject) sire.get(parentRow).get("yem_storagetransno");//储运托单
            for(int i=0; i < rowDataEntities.length; i++){
                this.getModel().setValue("yem_numerical1",numerical,parentRow, i);
                this.getModel().setValue("yem_storagetransno1",storagetransno,parentRow, i);
            }
        }
    }


    /**
     * @description: 获取储运托单号
     * @author yhj
     * @date 2023/7/26 14:51
     * @version 1.0
     */
    private void GetAdvance(ChangeData cd) {
        int row = cd.getRowIndex();//分录行
        String value = (String) this.getModel().getValue("yem_numerical");
        QFilter[] qFilter = new QFilter[] {new QFilter("billstatus", QCP.equals, "C"),
                new QFilter("yem_noticeno", QCP.equals, value)};
        DynamicObject storetransfer = BusinessDataServiceHelper.loadSingle("yem_es_storagetrans",""
         ,qFilter);
        if (storetransfer != null) {
            this.getModel().setValue("yem_storagetransno",storetransfer,row);
        }else {
            this.getModel().setValue("yem_storagetransno",null,row);
           }
        }
    }