package com.yem.wm.es.salesorder.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 kd.bos.servicehelper.BusinessDataServiceHelper;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhouc
 * @date 2024/4/24 11:54
 * @className SalesOrderUnAuditValidator
 * @description 反审核校验
 */
public class SalesOrderUnAuditValidator extends AbstractValidator {
    @Override
    public void validate() {
        ExtendedDataEntity[] dataEntities = this.getDataEntities();
        for (ExtendedDataEntity dataEntitie : dataEntities) {
            DynamicObject dataEntity = dataEntitie.getDataEntity();
            dataEntity = BusinessDataServiceHelper.loadSingle(dataEntity.getPkValue(), dataEntity.getDynamicObjectType().getName());
            String yem_cancelstatus = dataEntity.getString("yem_cancelstatus");
            if (yem_cancelstatus.equals("B")) {
                this.addErrorMessage(dataEntitie, "当前单据作废状态为【已作废】，不允许反审核");
            }
            if (yem_cancelstatus.equals("C")) {
                this.addErrorMessage(dataEntitie, "当前单据作废状态为【作废中】，不允许反审核");
            }
            DynamicObjectCollection materialInfos = dataEntity.getDynamicObjectCollection("yem_es_materialinfo");
            List<String> rows = new ArrayList<>();
            for (int i = 0; i < materialInfos.size(); i++) {
                DynamicObject materialInfo = materialInfos.get(i);
                String intAuditBuSaNo = materialInfo.getString("yem_intauditbusano");
                long intAuditBuSaId = materialInfo.getLong("yem_intauditbusaid");
                if (YEM.isNotEmpty(intAuditBuSaNo) && YEM.isNotEmpty(intAuditBuSaId)) {
//                    DynamicObject single = BusinessDataServiceHelper.loadSingle(intAuditBuSaId, "yem_im_intauditbusa");
//                    String billstatus = single.getString("billstatus");
//                    if (!"A".equals(billstatus)) {
//                    }
                    rows.add(i + 1 + "");
                }
            }
            if (rows.size() > 0) {
                this.addErrorMessage(dataEntitie, "明细分录第" + String.join(",", rows) + "已生成内审预算单，不允许反审核！");
            }
        }
    }
}
