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

import com.yem.wm.utils.YEM;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.ExtendedDataEntity;
import kd.bos.entity.validate.AbstractValidator;

/**
 * @Description: 调拨订单申请单提交必录校验，配置的校验不生效
 * @Date: 2024/5/30 14:24
 * @Created: by ZZSLL
 */

public class TransFerOrderSubmitMustInputValidator extends AbstractValidator {

    @Override
    public void validate() {
        ExtendedDataEntity[] extendedDataEntities = this.getDataEntities();
        for (ExtendedDataEntity extendedDataEntity : extendedDataEntities) {
            DynamicObject dataEntity = extendedDataEntity.getDataEntity();
            DynamicObjectCollection collection = dataEntity.getDynamicObjectCollection("yem_es_materialinfo");
            StringBuilder sb = new StringBuilder();
            for (DynamicObject dynamicObject : collection) {
                int index = collection.indexOf(dynamicObject);
                validateMustInput(dynamicObject, sb, "yem_materiel", "物料编码", index);
                validateMustInput(dynamicObject, sb, "yem_materialname", "商品名称", index);
                validateMustInput(dynamicObject, sb, "yem_shippersalesorg", "发货方销售组织", index);
                validateMustInput(dynamicObject, sb, "yem_deliveryinventoryorg", "发货库存组织", index);
                validateMustInput(dynamicObject, sb, "yem_entryoutwarehouse", "调出仓库", index);
                validateMustInput(dynamicObject, sb, "yem_receinventoryorg", "收货库存组织", index);
                validateMustInput(dynamicObject, sb, "yem_entryinwarehouse", "调入仓库", index);
                validateMustInput(dynamicObject, sb, "yem_deliverywarehouse", "发货仓库", index);
                validateMustInput(dynamicObject, sb, "yem_receinventory", "收货仓库", index);
                validateMustInput(dynamicObject, sb, "yem_supwarehouse", "供应仓库", index);
                validateMustInput(dynamicObject, sb, "yem_cuswarehouse", "需求仓库", index);
                validateMustInput(dynamicObject, sb, "yem_unit", "计量单位", index);
                validateMustInput(dynamicObject, sb, "yem_baseunit", "基本计量单位", index);
                validateMustInput(dynamicObject, sb, "yem_planindate", "计划调入日期", index);
                validateMustInput(dynamicObject, sb, "yem_planoutdate", "计划调出日期", index);
            }

            if (sb.length() > 0) {
                this.addErrorMessage(extendedDataEntity, sb.toString());
            }
        }
    }

    private void validateMustInput(DynamicObject dynamicObject, StringBuilder sb, String number, String name, int index) {
        if (YEM.isEmpty(dynamicObject.get(number))) {
            sb.append(String.format("商品明细分录第 [ %s ] 行, [ %s ] 行为空，请填写后再提交！", index + 1, name)).append("\r\n");
        }
    }
}
