package com.yem.wm.im.tracktaskconsole.validator;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.ExtendedDataEntity;
import kd.bos.entity.validate.AbstractValidator;

/**
 * @author zhouc
 * @date 2023/8/12 19:29
 * @className TracktaskconsoleSubmitvalidator
 * @description 跟单任务控制台校验器
 */
public class TracktaskconsoleSubmitvalidator extends AbstractValidator {
    @Override
    public void validate() {
        ExtendedDataEntity[] dataEntities = this.getDataEntities();
        for (ExtendedDataEntity dataEntitie : dataEntities) {
            DynamicObject dataEntity = dataEntitie.getDataEntity();
            yemIsvmi(dataEntity,dataEntitie);
            DynamicObject yemBilltype = dataEntity.getDynamicObject("yem_billtype");
            String typeNumber = "";
            if(yemBilltype != null){
                typeNumber = yemBilltype.getString("number");
            }
            //排除下单审批中的备料处理
            if(!"yem_placeexamine_".equals(typeNumber)){
                DynamicObjectCollection yem_es_materialinfo = dataEntity.getDynamicObjectCollection("yem_es_materialinfo");
                for (int i = 0; i < yem_es_materialinfo.size(); i++) {
                    DynamicObject materialinfo = yem_es_materialinfo.get(i);
                    int yem_nqty = materialinfo.getInt("yem_nqty");
                    //商品信息第**行[**物料编码] 末备料数量[*]大于0，请执行备料!
                    if(yem_nqty>0){
                        DynamicObject yem_materiel = materialinfo.getDynamicObject("yem_materiel");
                        if(yem_materiel!=null){
                            int seq = i+1;
                            String materiel  = yem_materiel.getString("number");
                            this.addErrorMessage(dataEntitie,String.format("商品信息第"+seq+"行["+materiel+"] 末备料数量["+yem_nqty+"]大于0，请执行备料!"));
                        }
                    }
                    if(yem_nqty<0){
                        DynamicObject yem_materiel = materialinfo.getDynamicObject("yem_materiel");
                        if(yem_materiel!=null){
                            int seq = i+1;
                            String materiel  = yem_materiel.getString("number");
                            this.addErrorMessage(dataEntitie,String.format("商品信息第"+seq+"行["+materiel+"] 末备料数量["+yem_nqty+"]小于0，提交失败!"));
                        }
                    }
                }
            }

//            DynamicObject billType = dataEntity.getDynamicObject("yem_billtype");//单据类型
        }
    }

    private void yemIsvmi(DynamicObject dataEntity, ExtendedDataEntity dataEntities) {
        DynamicObjectCollection yemEsSalesorderR = dataEntity.getDynamicObjectCollection("yem_es_salesorder_r");
        if (yemEsSalesorderR!=null){
            for (DynamicObject dynamicObject : yemEsSalesorderR) {
                String yemStockway = dynamicObject.getString("yem_stockway");
                if ("E".equals(yemStockway)){
                    String yemIsvmi = dynamicObject.getString("yem_isvmi");
                    int seq = dynamicObject.getInt("seq");
                    if ("".equals(yemIsvmi)){
                        this.addErrorMessage(dataEntities,"备料（生产、采购）信息 第"+seq+"行 仓库类型未填写，请填写后提交！！！");
                    }
                }
            }
        }
    }
}
