package com.yem.wm.im.inquirylist.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.exception.KDBizException;
import kd.bos.exception.KDException;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;

public class InquirylistVaildator extends AbstractValidator {
    @Override
    public void validate() {
        ExtendedDataEntity[] entities = this.getDataEntities();
        for (ExtendedDataEntity entity : entities) {
            DynamicObject bill = entity.getDataEntity();
            long id = bill.getLong("id");
            if (id == 0) {
                throw new KDBizException("当前单据未保存，禁止提交！");
            }
            DynamicObject billstatus = bill.getDynamicObject("yem_billtype");
            if (billstatus != null) {
                String billstatusnumber = billstatus.getString("number");
                if ("yem_im_inquirylist_PJ".equals(billstatusnumber)) {
                    DynamicObject dynamicObject = bill.getDynamicObject("yem_companyorg");
                    if (dynamicObject != null) {
                        String number = dynamicObject.getString("number");
                        DynamicObjectCollection yemImEntryentity = bill.getDynamicObjectCollection("yem_im_detailedinfor");
                        if (!yemImEntryentity.isEmpty()) {
                            for (DynamicObject object : yemImEntryentity) {
                                DynamicObject yemMaterialnum = object.getDynamicObject("yem_material");
                                int seq = object.getInt("seq");
                                if (yemMaterialnum != null) {
                                    String yemMaterialnumnumber = yemMaterialnum.getString("number");
                                    DynamicObject dynamicObjec = BusinessDataServiceHelper.loadSingle("yem_materialcompanyinfo", "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核准后在提交单据！");
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
