package com.yem.wm.es.salesorder.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.exception.KDBizException;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.QueryServiceHelper;

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

/**
 * @author zhouc
 * @date 2024/1/30 12:25
 * @className NewVersionValidator
 * @description
 */
public class NewVersionValidator extends AbstractValidator {
    @Override
    public void validate() {
        ExtendedDataEntity[] dataEntities = this.getDataEntities();
        for (ExtendedDataEntity dataEntitie : dataEntities) {
            List<String> rows = new ArrayList<>();
            DynamicObject dataEntity = dataEntitie.getDataEntity();
            dataEntity = BusinessDataServiceHelper.loadSingle(dataEntity.getPkValue(), dataEntity.getDynamicObjectType().getName());
            String billno = dataEntity.getString("billno");
            Object pkValue = dataEntity.getPkValue();
//            QFilter qFilter = new QFilter("yem_sourcebillno", QCP.equals, billno);//
//            qFilter.and(new QFilter("billstatus", QCP.not_equals, "C"));
//            String selectfield = DynamicObjectUtil.getSelectfields("yem_im_intauditbusa", false);
//            String selectfields = DynamicObjectUtil.getEntrySelectfields(selectfield, "yem_im_intauditbusa", "yem_im_intauditbusa_a", false);
//            DynamicObjectCollection data = QueryServiceHelper.query("yem_im_intauditbusa", 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, "单据编号为" + String.join(",", rows) + "的内审预算单未审核，请审核后在进行合同变更！");
//            }
            String yem_cancelstatus = dataEntity.getString("yem_cancelstatus");
            if (yem_cancelstatus.equals("B")) {
                this.addErrorMessage(dataEntitie, "当前单据作废状态为【已作废】，不允许变更");
            }
            if (yem_cancelstatus.equals("C")) {
                this.addErrorMessage(dataEntitie, "当前单据作废状态为【作废中】，不允许变更");
            }
        }
    }
}
