package com.yem.wm.im.clientdemand.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.util.ArrayList;
import java.util.List;

/**
 * @author xwudd
 * @date 13:38
 * @Description
 **/
public class ClientDelValidator extends AbstractValidator {
    @Override
    public void validate() {
        ExtendedDataEntity[] dataEntities = this.getDataEntities();
        for(ExtendedDataEntity dataEntitie : dataEntities){
            DynamicObject dataEntity = dataEntitie.getDataEntity();
            DynamicObjectCollection materialInfos = dataEntity.getDynamicObjectCollection("yem_im_detailedinfor");
            List<String> rows = new ArrayList<>();
            for (int i = 0; i < materialInfos.size(); i++) {
                DynamicObject materialInfo = materialInfos.get(i);
                String intAuditBuSaNo = materialInfo.getString("yem_intauditbusano");
                Object object = materialInfo.get("yem_intauditbusaid");
                long intAuditBuSaId = 0L;
                if (object != null) {
                    intAuditBuSaId = materialInfo.getLong("yem_intauditbusaid");
                }
                if(YEM.isNotEmpty(intAuditBuSaNo) && YEM.isNotEmpty(intAuditBuSaId)){
                    rows.add(i + 1 + "");
                }
            }
            if(rows.size() > 0){
                this.addErrorMessage(dataEntitie,"明细分录第" + String.join(",",rows) + "行已经生成内审预算单，要删除合同请先删除相关内审预算单！");
            }
        }
    }
}
