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

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.ExtendedDataEntity;
import kd.bos.entity.validate.AbstractValidator;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;

public class DeliverNoticeValidator extends AbstractValidator {
    @Override
    public void validate() {
        ExtendedDataEntity[] entities = this.getDataEntities();
        for (ExtendedDataEntity entity : entities) {
            DynamicObject bill = entity.getDataEntity();
            DynamicObject yem_supfinorg = bill.getDynamicObject("yem_companyorg");//财务组织
            DynamicObject yem_org = bill.getDynamicObject("yem_org");//库存组织
            if (yem_supfinorg!=null){
                String number = yem_supfinorg.getString("number");
                extracted(entity, bill, number,"财务","yem_material_inveninfo");
            }
            if (yem_org!=null){
                String number = yem_org.getString("number");
                extracted(entity, bill, number, "库存", "yem_materialcompanyinfo");
            }
        }
    }

    private void extracted(ExtendedDataEntity entity, DynamicObject bill, String yem_supfinorg, String string, String yemMaterialInveninfo) {
        DynamicObjectCollection yem_es_materialinfo = bill.getDynamicObjectCollection("yem_es_materialinfo");
        if (!yem_es_materialinfo.isEmpty()){
            for (DynamicObject dynamicObject : yem_es_materialinfo) {
                DynamicObject yem_materiel = dynamicObject.getDynamicObject("yem_materiel");
                if (yem_materiel!=null){
                    String number = yem_materiel.getString("number");
                    int seq = dynamicObject.getInt("seq");
                    DynamicObject dynamicObjec = BusinessDataServiceHelper.loadSingle(yemMaterialInveninfo, "id,number,createorg"
                            , new QFilter[]{new QFilter("createorg.number", QCP.equals, yem_supfinorg)
                                    , new QFilter("yem_material.number", QCP.equals, number)});
                    if (dynamicObjec == null) {
                        this.addErrorMessage(entity, "第" + seq + "行物料在"+string+"组织下，"+string+"信息未核准，请在EAS核准后在提交单据！");
                    }
                }
            }
        }
    }
}
