package com.yem.wm.es.xsalesorder.validator;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.ExtendedDataEntity;
import kd.bos.entity.validate.AbstractValidator;

import java.math.BigDecimal;

/**
 * @author zhouc
 * @date 2024/1/25 22:08
 * @className XSalesOrderSubmitValidator
 * @description
 */
public class XSalesOrderSubmitValidator extends AbstractValidator {
    @Override
    public void validate() {
        ExtendedDataEntity[] dataEntities = this.getDataEntities();
        for (ExtendedDataEntity dataEntitie : dataEntities) {
            DynamicObject dataEntity = dataEntitie.getDataEntity();
            DynamicObject billType = dataEntity.getDynamicObject("yem_billtype");
            if (billType != null) {
                String number = billType.getString("number");
                DynamicObjectCollection yem_es_materialinfo = dataEntity.getDynamicObjectCollection("yem_es_materialinfo");
                for (int i = 0; i < yem_es_materialinfo.size(); i++) {
                    DynamicObject materialinfo = yem_es_materialinfo.get(i);
                    int seq = i + 1;
                    BigDecimal yem_qty = materialinfo.getBigDecimal("yem_qty");
                    BigDecimal yem_shippingqty = materialinfo.getBigDecimal("yem_shippingqty");
                    BigDecimal yem_orderqty = materialinfo.getBigDecimal("yem_orderqty");
                    if (yem_shippingqty.compareTo(BigDecimal.ZERO) == 1 && yem_shippingqty.compareTo(yem_qty) == 1) {
                        String mag = "第【" + seq + "】行，数量【" + yem_qty.setScale(2,BigDecimal.ROUND_HALF_UP) + "】小于已出运数量【" + yem_shippingqty.setScale(2,BigDecimal.ROUND_HALF_UP) + "】，请修改后重试！";
                        this.addErrorMessage(dataEntitie, mag);
                    }
                    if (yem_orderqty.compareTo(BigDecimal.ZERO) == 1 && yem_orderqty.compareTo(yem_qty) == 1) {
                        String mag = "第【" + seq + "】行，数量【" + yem_qty.setScale(2,BigDecimal.ROUND_HALF_UP) + "】小于已下单数量【" + yem_orderqty.setScale(2,BigDecimal.ROUND_HALF_UP) + "】，请修改后重试！";
                        this.addErrorMessage(dataEntitie, mag);
                    }
                }
            }
        }
    }
}
