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

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Sets;
import kd.bos.dataentity.OperateOption;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.ILocaleString;
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.MultiLangEnum;
import nckd.jxccl.hrmp.hbpm.common.hr.PositionBillConstant;

import java.util.Map;
import java.util.Set;

/**
 * 岗位申请单分录名称校验器
 * @from: kd.hr.homs.opplugin.web.orgbatch.validator.AdminOrgBatchDetailNameValidator
 * @author: jtd
 * @date: 2025/12/23 21:16
 */
public class PositionBillEntryNameValidator extends AbstractValidator {
    @Override
    public void validate() {
        OperateOption option = validateContext.getOption();
        // 获取组织下的岗位同胞数据 {orgBoid:{positionId:positionName}}
        String billPositionNameMapStr = option.getVariableValue(PositionBillConstant.OP_BILL_SIBLING_NAMES_MAP, "");
        if (HRStringUtils.isNotEmpty(billPositionNameMapStr)) {
            JSONObject orgBoIdToSiblingNameMap = JSONObject.parseObject(billPositionNameMapStr);
            Set<String> errorIds = Sets.newHashSetWithExpectedSize(getDataEntities().length);

            for(ExtendedDataEntity data : getDataEntities()) {
                // 获取校验数据
                DynamicObject dataEntity = data.getDataEntity();
                // 获取组织数据
                DynamicObject adminOrgDy = dataEntity.getDynamicObject(PositionBillConstant.NCKD_ADMINORG);
                long orgBoId = adminOrgDy.getLong(PositionBillConstant.BOID_KEY);
                String adminOrgName = adminOrgDy.getString(PositionBillConstant.NAME_KEY);

                // 获取岗位数据
                ILocaleString positionNameMap = dataEntity.getLocaleString(PositionBillConstant.NAME_KEY);
                long positionId = dataEntity.getLong(String.join(".", PositionBillConstant.NCKD_POSITION_KEY, PositionBillConstant.ID_KEY));
                long positionBoId = dataEntity.getLong(String.join(".", PositionBillConstant.NCKD_POSITION_KEY, PositionBillConstant.BOID_KEY));
                // 获取该组织下的岗位名称数据
                JSONObject siblingNameMap = orgBoIdToSiblingNameMap.getJSONObject(String.valueOf(orgBoId));
                if (siblingNameMap != null) {
                    for(String siblingId : siblingNameMap.keySet()) {
                        if (!siblingId.equals(String.valueOf(positionId)) && !siblingId.equals(String.valueOf(positionBoId))) {
                            JSONObject nameMap = siblingNameMap.getJSONObject(siblingId);

                            for(Map.Entry<String, String> nameEntry : positionNameMap.entrySet()) {
                                String lang = nameEntry.getKey();
                                if (!ILocaleString.GLang.equals(lang)) {
                                    String siblingName = nameMap.getString(lang);
                                    if (HRStringUtils.isNotEmpty(siblingName) && siblingName.equals(nameEntry.getValue()) && !errorIds.contains(positionBoId + "_" + lang)) {
                                        addErrorMessage(data, String.format("本单中“%1$s”下已存在%2$s名称为“%3$s”的岗位，请修改。", adminOrgName, MultiLangEnum.getName(lang), siblingName));
                                        errorIds.add(positionBoId + "_" + lang);
                                    }
                                }
                            }
                        }
                    }
                }
            }

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

        }
    }
}
