package com.yem.wm.im.intauditbusa.validator;

import com.yem.wm.utils.DynamicObjectUtil;
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.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.QueryServiceHelper;

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

/**
 * @author zhouc
 * @date 2024/1/30 16:00
 * @className IntAuditBuSaUnAuditValidator
 * @description
 */
public class IntAuditBuSaUnAuditValidator extends AbstractValidator {
    @Override
    public void validate() {
        ExtendedDataEntity[] dataEntities = this.getDataEntities();
        for (ExtendedDataEntity dataEntitie : dataEntities) {
            DynamicObject dataEntity = dataEntitie.getDataEntity();
            List<String> rows = new ArrayList<>();
            long yem_sourcebillid = dataEntity.getLong("yem_sourcebillid");
            String yem_sourcebillno = dataEntity.getString("yem_sourcebillno");
            if (yem_sourcebillid > 0) {
                QFilter qFilter = new QFilter("yem_masterid", QCP.equals, yem_sourcebillid);//
                qFilter.and(new QFilter("billstatus", QCP.not_equals, "C"));
                String selectfield = DynamicObjectUtil.getSelectfields("yem_es_xsalesorder", false);
                String selectfields = DynamicObjectUtil.getEntrySelectfields(selectfield, "yem_es_xsalesorder", "yem_es_materialinfo", false);
                DynamicObjectCollection data = QueryServiceHelper.query("yem_es_xsalesorder", selectfields, new QFilter[]{qFilter}, null);
                for (int i = 0; i < data.size(); i++) {
                    DynamicObject dynamicObject = data.get(i);
                    String billno1 = dynamicObject.getString("billno");
                    rows.add(billno1);
                }
                if (rows.size() > 0) {
                    this.addErrorMessage(dataEntitie, "当前合同【" + yem_sourcebillno + "】存在未审核的变更单，请在变更单审核通过后在操作内审预算单！");
                }
            }
        }
    }
}
