package com.yem.wm.im.orderproreqbill.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 OrderProReqBillValidator extends AbstractValidator {
    @Override
    public void validate() {
        ExtendedDataEntity[] entities = this.getDataEntities();
        for (ExtendedDataEntity entity : entities) {
            DynamicObject bill = entity.getDataEntity();
            DynamicObject billstatus = bill.getDynamicObject("yem_billtype");
            if (billstatus != null) {
                String billstatusnumber = billstatus.getString("number");
                if ("yem_orderproreqbill_WG".equals(billstatusnumber)) {
                    DynamicObject dynamicObject = bill.getDynamicObject("yem_dept");
                    if (dynamicObject != null) {
                        String number = dynamicObject.getString("number");
                        DynamicObjectCollection yemImEntryentity = bill.getDynamicObjectCollection("yem_es_materialinfo");
                        if (!yemImEntryentity.isEmpty()) {
                            for (DynamicObject object : yemImEntryentity) {
                                DynamicObject yemMaterialnum = object.getDynamicObject("yem_materiel");
                                int seq = object.getInt("seq");
                                if (yemMaterialnum != null) {
                                    String yemMaterialnumnumber = yemMaterialnum.getString("number");
                                    DynamicObject dynamicObjec = BusinessDataServiceHelper.loadSingle("yem_material_procinfo", "id,number,createorg"
                                            , new QFilter[]{new QFilter("createorg.number", QCP.equals, number)
                                                    , new QFilter("yem_material.number", QCP.equals, yemMaterialnumnumber)});
                                    if (dynamicObjec == null) {
                                        this.addErrorMessage(entity, "第" + seq + "行物料采购信息未核准，请在EAS核准后在提交单据！");
                                    }
                                    DynamicObject yem_material_inveninfo = BusinessDataServiceHelper.loadSingle("yem_material_inveninfo", "id,number,createorg"
                                            , new QFilter[]{new QFilter("createorg.number", QCP.equals, number)
                                                    , new QFilter("yem_material.number", QCP.equals, yemMaterialnumnumber)});
                                    if (yem_material_inveninfo == null) {
                                        this.addErrorMessage(entity, "第" + seq + "行物料库存信息未核准，请在EAS核准后在提交单据！");
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
