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

import com.yem.wm.utils.DynamicObjectUtil;
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;

/**
 * @author ljw
 * @date 2024/8/31 16:11
 * @description TransportCostsSaveValidator
 */
public class TransportCostsSaveValidator extends AbstractValidator {
    @Override
    public void validate() {
        ExtendedDataEntity[] extendedDataEntities = this.getDataEntities();
        for (ExtendedDataEntity extendedDataEntity : extendedDataEntities) {
            DynamicObject eDataEntity = extendedDataEntity.getDataEntity();
            DynamicObjectCollection dynamicObjectCollection = eDataEntity.getDynamicObjectCollection("yem_bd_logistics");
            long aLong = eDataEntity.getLong("id");
            for (DynamicObject dynamicObject : dynamicObjectCollection) {
                DynamicObject yemShippingmethod = dynamicObject.getDynamicObject("yem_shippingmethod");
                DynamicObject yemExpenseitems = dynamicObject.getDynamicObject("yem_expenseitems");
                DynamicObject yemPort = dynamicObject.getDynamicObject("yem_port");
                QFilter qFilter = new QFilter("id", QCP.not_equals, aLong);
                if (yemExpenseitems!=null){
                    long yem_expenseitems = yemExpenseitems.getLong("id");
                    qFilter.and("yem_bd_logistics.yem_expenseitems.id", QCP.equals, yem_expenseitems);
                }else {
                    this.addMessage(extendedDataEntity,"费用项目不允许为空");
                    return;
                }
                if (yemPort!=null){
                    long yem_port = yemPort.getLong("id");
                    qFilter.and("yem_bd_logistics.yem_port.id", QCP.equals, yem_port);
                }else {
                    this.addMessage(extendedDataEntity,"运输方式不允许为空");
                    return;
                }
                if (yemShippingmethod!=null){
                    long yem_padexpense = yemShippingmethod.getLong("id");
                    qFilter.and("yem_bd_logistics.yem_shippingmethod.id", QCP.equals, yem_padexpense);
                }else {
                    this.addMessage(extendedDataEntity,"港口不允许为空");
                    return;
                }
                String selectfield = DynamicObjectUtil.getSelectfields("yem_bd_transportcosts", false);
                String selectfields = DynamicObjectUtil.getEntrySelectfields(selectfield, "yem_bd_transportcosts", "yem_bd_logistics", false);
                DynamicObject dynamicObject1 = BusinessDataServiceHelper.loadSingle("yem_bd_transportcosts", selectfields, new QFilter[]{qFilter});
                if (dynamicObject1 != null) {
                    this.addMessage(extendedDataEntity,"运输方式+费用项目+港口唯一");
                }
            }
        }
    }
}
