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

import com.alibaba.fastjson.JSONObject;
import kd.bos.dataentity.OperateOption;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.ExtendedDataEntity;
import kd.bos.entity.validate.AbstractValidator;
import kd.hr.hbp.common.util.HRStringUtils;
import nckd.jxccl.hrmp.hbpm.common.hr.PositionBillConstant;

/**
 * 岗位申请单分录编码校验器
 * @from: kd.hr.homs.opplugin.web.orgbatch.validator.AdminOrgBatchDetailNumberValidator
 * @author: jtd
 * @date: 2025/12/24 11:04
 */
public class PositionBillEntryNumberValidator extends AbstractValidator {
    @Override
    public void validate() {
        OperateOption option = validateContext.getOption();
        // 获取岗位编码数据 {positionId:number}
        String billPositionNumberMapStr = option.getVariableValue(PositionBillConstant.OP_BILL_NUMBER_MAP, "");
        if (HRStringUtils.isNotEmpty(billPositionNumberMapStr)) {
            JSONObject numberMap = JSONObject.parseObject(billPositionNumberMapStr);

            for(ExtendedDataEntity data : getDataEntities()) {
                DynamicObject dataEntity = data.getDataEntity();
                String number = dataEntity.getString(PositionBillConstant.NUMBER_KEY);
                if (HRStringUtils.isNotEmpty(number)) {
                    long positionId = dataEntity.getLong(String.join(".", PositionBillConstant.NCKD_POSITION_KEY, PositionBillConstant.ID_KEY));

                    for(String siblingId : numberMap.keySet()) {
                        if (!siblingId.equals(String.valueOf(positionId)) && number.equals(numberMap.getString(siblingId))) {
                            addErrorMessage(data, "岗位编码在本单已存在，请修改。");
                        }
                    }
                }
            }
        }

        if (option.containsVariable("isimport") && Boolean.parseBoolean(option.getVariableValue("isimport"))) {
            // 导入业务逻辑
        }

    }
}
