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

import kd.bos.dataentity.entity.DynamicObject;
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;

import javax.xml.validation.Validator;

/**
 * @author ljw
 * @date 2024/5/16 16:46
 * @description DeliverNoticeEASValidator
 */
public class DeliverNoticeEASValidator extends AbstractValidator {
    @Override
    public void validate() {
        ExtendedDataEntity[] entities = this.getDataEntities();
        for (ExtendedDataEntity entity : entities) {
            DynamicObject dataEntity = entity.getDataEntity();
            DynamicObject yemBilltype = dataEntity.getDynamicObject("yem_billtype");
            if (yemBilltype != null) {
                if ("yem_es_delivernotice_pj".equals(yemBilltype.getString("number"))){
                    long aLong = dataEntity.getLong("yem_sourcebillid");
                    DynamicObject[] dynamicObject = BusinessDataServiceHelper.load("yem_transferorder", "id,billno,billstatus,yem_iseas,yem_sourcebillid"
                            , new QFilter[]{new QFilter("yem_sourcebillid", QCP.equals, aLong)});
                    Boolean bool = false;
                    if (dynamicObject.length>0){
                        for (DynamicObject object : dynamicObject) {
                            String billstatus = object.getString("billstatus");
                            boolean yemIseas = object.getBoolean("yem_iseas");
                            if (!"C".equals(billstatus)||!yemIseas){
                                bool = true;
                            }
                        }
                    }else {
                        bool = true;
                    }
                    if (bool){
                        DynamicObject yem_shippingdetails = BusinessDataServiceHelper.loadSingle("yem_shippingdetails", "id,billno"
                                , new QFilter[]{new QFilter("id", QCP.equals, aLong)});
                        if (yem_shippingdetails!=null){
                            this.addWarningMessage(entity,"当前销售出库对应的发运明细单"+yem_shippingdetails.getString("billno")+"，没有已审核且已同步EAS的调拨订单申请单，是否继续进行?");
                        }
                    }
                }
            }
        }
    }
}
