package com.yem.wm.im.transferorder.vaildator;

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 TransFerOrderSubmitValidator extends AbstractValidator {
    @Override
    public void validate() {
        ExtendedDataEntity[] entities = this.getDataEntities();
        for (ExtendedDataEntity entity : entities) {
            DynamicObject bill = entity.getDataEntity();
            DynamicObject yem_supfinorg = bill.getDynamicObject("yem_supfinorg");//供应方财务组织
            DynamicObject yem_supstockorg = bill.getDynamicObject("yem_supstockorg");//供应方库存组织
            DynamicObject yem_cusfinorg = bill.getDynamicObject("yem_cusfinorg");//需求方财务组织
            DynamicObject yem_cusstockorg = bill.getDynamicObject("yem_cusstockorg");//需求方库存组织
            if (yem_supfinorg != null) {
                String number = yem_supfinorg.getString("number");
                extracted(entity, bill, number,"yem_materialcompanyinfo","供应方财务组织","财务组织");
            }
            if (yem_cusfinorg != null) {
                String number = yem_cusfinorg.getString("number");
                extracted(entity, bill, number, "yem_materialcompanyinfo", "需求方财务组织", "财务组织");
            }
            if (yem_supstockorg != null) {
                String number = yem_supstockorg.getString("number");
                extracted(entity, bill, number, "yem_material_inveninfo", "供应方库存组织", "库存组织");
            }
            if (yem_cusstockorg != null) {
                String number = yem_cusstockorg.getString("number");
                extracted(entity, bill, number, "yem_material_inveninfo", "需求方库存组织", "库存组织");
            }
        }
    }

    private void extracted(ExtendedDataEntity entity, DynamicObject bill, String number, String yemMaterialcompanyinfo, String string, String data) {
        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(yemMaterialcompanyinfo, "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 + "行物料在"+string+"下，"+data+"信息未核准，请在EAS核准后在提交单据！");
                    //第***行物料在***组织下，***信息未核准，请在EAS核准后在提交单据！
                    }
                }
            }
        }
    }
}
