package com.yem.wm.im.debcrednotenew.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 java.math.BigDecimal;

/**
 * @author ljw
 * @date 2024/12/23 17:43
 * @description DebCredNewUnAuditValidator
 */
public class DebCredNewUnAuditValidator extends AbstractValidator {

    @Override
    public void validate() {
        ExtendedDataEntity[] dataEntities = this.getDataEntities();
        for(ExtendedDataEntity dataEntitie : dataEntities){
            DynamicObject dataEntity = dataEntitie.getDataEntity();
            String billno = dataEntity.getString("billno");
            if (billno.length()>4){
                BigDecimal bigDecimal = BigDecimal.ZERO;
                String modifiedString = billno.substring(0, billno.length() - 4);
                String substringed = billno.substring(billno.length() - 4);
                DynamicObject[] loaded = BusinessDataServiceHelper.load("yem_debcrednote", "id,billno", new QFilter[]{new QFilter("billno", QCP.in, modifiedString + "%")});
                for (DynamicObject dynamicObject : loaded) {
                    String dynamicObjectString = dynamicObject.getString("billno");
                    String substring = dynamicObjectString.substring(dynamicObjectString.length() - 4);
                    BigDecimal decimal = BigDecimal.valueOf(Double.parseDouble(substring));
                    if (decimal.compareTo(bigDecimal)>0){
                        bigDecimal = decimal;
                    }
                }
                BigDecimal decimal = BigDecimal.valueOf(Double.parseDouble(substringed));
                if (decimal.compareTo(bigDecimal)!=0){
                    this.addMessage(dataEntitie,"当前单不是最大单号，请从最大单号开始删除！！！");
                }
            }
        }
    }
}
