package nckd.jxccl.hrmp.hbpm.plugin.operate.hr.validator;

import kd.bos.dataentity.entity.DynamicObject;
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.hr.hbp.business.servicehelper.HRBaseServiceHelper;
import kd.sdk.hr.hdm.common.enums.reg.RegBillStatusEnum;
import nckd.jxccl.hrmp.hbpm.common.hr.PositionBillConstant;

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 岗位申请单删除校验器
 * @from: kd.hr.homs.opplugin.web.orgbatch.validator.AdminOrgBatchBillDeleteStatusValidator
 * @author: jtd
 * @date: 2026/1/2 15:56
 */
public class PositionBillDeleteStatusValidator extends AbstractValidator {
    @Override
    public void validate() {
        ExtendedDataEntity[] dataEntities = getDataEntities();
        if (dataEntities.length != 0) {
            List<Long> selectedRows = Arrays.stream(dataEntities).map((dataEntityx) -> dataEntityx.getDataEntity().getLong(PositionBillConstant.ID_KEY)).collect(Collectors.toList());
            HRBaseServiceHelper hrBaseServiceHelper = new HRBaseServiceHelper(PositionBillConstant.NCKD_POSITIONBILL_ENTITY);
            QFilter qFilter = new QFilter(PositionBillConstant.ID_KEY, QCP.in, selectedRows);
            qFilter.and(PositionBillConstant.BILL_STATUS_KEY, QCP.in, Arrays.asList(RegBillStatusEnum.ALREADYSUBMIT.getCode(), RegBillStatusEnum.APPROVING.getCode(), RegBillStatusEnum.APPROVEPASSED.getCode(), RegBillStatusEnum.APPROVEREJECTED.getCode(), RegBillStatusEnum.ABANDONED.getCode()));
            DynamicObject[] dynamicObjects = hrBaseServiceHelper.queryOriginalArray(PositionBillConstant.ID_KEY, new QFilter[]{qFilter});
            String msg = "只允许删除“暂存”或“待重新提交”状态的数据。";
            if (dynamicObjects.length > 0) {
                Set<Long> ids = Arrays.stream(dynamicObjects).map((dynamicObject) -> dynamicObject.getLong(PositionBillConstant.ID_KEY)).collect(Collectors.toSet());

                for(ExtendedDataEntity dataEntity : dataEntities) {
                    if (ids.contains(dataEntity.getDataEntity().getLong(PositionBillConstant.ID_KEY))) {
                        addErrorMessage(dataEntity, msg);
                    }
                }
            }

        }
    }
}
