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 xwudd
 * @date 13:31
 * @Description
 **/
public class SaleOrderDelValidator 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());
            DynamicObject yem_billtype = dataEntity.getDynamicObject("yem_billtype");
            if (yem_billtype != null) {
                String number = yem_billtype.getString("number");
                if(number.equals("yem_es_salesorder_ZJ")||number.equals("yem_es_salesorder_PJ")
                        ||number.equals("yem_es_salesorder_nmzj")||number.equals("yem_es_salesorder_nmpj")){
                    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)) {
                            rows.add(i + 1 + "");
                        }
                    }
                    if (rows.size() > 0) {
                        this.addErrorMessage(dataEntitie, "明细分录第" + String.join(",", rows) + "行已经生成内审预算单，要删除合同请先删除相关内审预算单！");
                    }
                }
            }


        }
    }
}
