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

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;

import java.math.BigDecimal;

/**
 * @author power
 * @ClassName
 * @description: 服务表单插件
 * @date 2023/12/6
 */
public class ShippingDetailAuditValidator  extends AbstractValidator {
    @Override
    public void validate() {
        ExtendedDataEntity[] dataEntities = this.getDataEntities();
        for(ExtendedDataEntity dataEntitie : dataEntities){
            DynamicObject dataEntity = dataEntitie.getDataEntity();
            DynamicObjectCollection materialInfos = dataEntity.getDynamicObjectCollection("yem_es_materialinfo");
            String billtype = dataEntity.getString("yem_billtype.number");
            if("yem_shippingdetails_zj".equals(billtype)||"yem_shippingdetails_nmzj".equals(billtype)){
                for (int i = 0; i < materialInfos.size(); i++) {
                    DynamicObject materialInfo = materialInfos.get(i);
                    String businesssort = materialInfo.getString("yem_businesssort");
                    if ("Z".equals(businesssort)){
                        BigDecimal qty = materialInfo.getBigDecimal("yem_qty");
                        DynamicObject resourceNog = materialInfo.getDynamicObject("yem_resourcenog");
                        String resourceNogNumber = YEM.isNotEmpty(resourceNog)?resourceNog.getString("number"):"";
                        DynamicObjectCollection  completeProductions  = materialInfo.getDynamicObjectCollection("yem_es_salesorder_z");
                        int completeProductionRows = completeProductions.size();
                        if(qty.compareTo(new BigDecimal(completeProductionRows)) != 0){
                            this.addErrorMessage(dataEntitie,String.format("明细信息第%s行%s资源号的出运数量与对应的整机数量不一致，请检查数据后,点击保存，在点击提交！！",i+1,resourceNogNumber));
                        }
                    }
                }
            }
        }
    }
}
