package nckd.jimin.jyyy.fi.plugin.operate.validator;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.ExtendedDataEntity;
import kd.bos.entity.validate.AbstractValidator;
import kd.bos.orm.ORM;
import kd.bos.orm.query.QFilter;
import nckd.jimin.jyyy.fi.common.constant.travelcheck.BusTravelCheckBillConstant;

import java.util.Arrays;

public class FinanceBillConfirmValidator extends AbstractValidator {
    @Override
    public void validate() {
        String operateKey = this.getOperateKey();
        ExtendedDataEntity[] dataEntities = getDataEntities();
        if("unaudit".equals(operateKey)){
            Arrays.stream(dataEntities).forEach(r -> checkCreateCheckBill(r));
        }
    }

    protected void checkCreateCheckBill(ExtendedDataEntity dataEntity) {
        DynamicObject billInfo = dataEntity.getDataEntity();
        Object billId = billInfo.getPkValue();
        QFilter hasCreateCheckFilter = new QFilter(BusTravelCheckBillConstant.NCKD_NCKD_SOURCEBILLID, QFilter.equals, billId);
        if(ORM.create().exists(BusTravelCheckBillConstant.ENTITYID, hasCreateCheckFilter.toArray())){
            this.addErrorMessage(dataEntity, "已生成商旅对账单，不允许反审核单据。");
            return;
        }
    }
}
