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

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import kd.bos.coderule.util.MultiLangEnumBridge;
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.PositionBillConstant;

import java.util.Map;

/**
 * 岗位申请单分录校验器
 * @author: jtd
 * @date: 2025-11-08 17:23
 */
public class PositionBillEntryValidator extends AbstractValidator {

    private enum MultiLangEnum {
        CHINES_SIMP("zh_CN", new MultiLangEnumBridge("简体中文", "MultiLangEnum_0", "hrmp-haos-common")),
        CHINES_TRAD("zh_TW", new MultiLangEnumBridge("繁体中文", "MultiLangEnum_1", "hrmp-haos-common")),
        ENGLISH("en_US", new MultiLangEnumBridge("英文", "MultiLangEnum_2", "hrmp-haos-common"));

        private String code;
        private MultiLangEnumBridge bridge = null;

        private MultiLangEnum(String code, MultiLangEnumBridge bridge) {
            this.code = code;
            this.bridge = bridge;
        }

        public static String getName(String code) {
            if (code == null) {
                return null;
            } else {
                for(MultiLangEnum se : values()) {
                    if (code.equals(se.getCode())) {
                        return se.getName();
                    }
                }

                return null;
            }
        }

        public String getCode() {
            return this.code;
        }

        public String getName() {
            return this.bridge.loadKDString();
        }
    }

    @Override
    public void validate() {
        // 获取Op参数 校验岗位编码重复和岗位名称重复
        OperateOption option = validateContext.getOption();
        // 获取组织下的岗位同胞数据 {orgBoId: {id:positionid, number:positionnumber, name:positionname}}
        String billPositionSiblingMapStr = option.getVariableValue("OP_BILL_SIBLING_MAP", "");
        if (HRStringUtils.isNotEmpty(billPositionSiblingMapStr)) {
            JSONObject orgBoIdToSiblingPositionMap = JSONObject.parseObject(billPositionSiblingMapStr);
            // 遍历保存数据
            for (ExtendedDataEntity data : getDataEntities()) {
                // 获取单据对象
                DynamicObject positionDy = data.getDataEntity();
                DynamicObject orgDy = positionDy.getDynamicObject(PositionBillConstant.NCKD_ADMINORG);
                long orgBoId = orgDy.getLong(PositionBillConstant.BOID_KEY);
                long positionId = positionDy.getLong(String.format("%s.id", PositionBillConstant.NCKD_POSITION));
                String positionNumber = positionDy.getString(PositionBillConstant.NUMBER_KEY);
                ILocaleString positionNameMap = positionDy.getLocaleString(PositionBillConstant.NAME_KEY);
                // 获取岗位同胞数据进行遍历处理
                JSONArray siblingPositionArr = orgBoIdToSiblingPositionMap.getJSONArray(String.valueOf(orgBoId));
                if (siblingPositionArr != null) {
                    for (int i = 0, size = siblingPositionArr.size(); i < size; i++) {
                        JSONObject positionObj = siblingPositionArr.getJSONObject(i);
                        // 如果是当前岗位则跳过
                        if (positionObj.getString(PositionBillConstant.ID_KEY).equals(positionId)) {
                            continue;
                        }

                        // 如果编码重复
                        if (positionObj.getString(PositionBillConstant.NUMBER_KEY).equals(positionNumber)) {
                            addErrorMessage(data, "岗位编码在本单已存在，请修改。");
                            continue;
                        }

                        // 如果名称重复 多语言校验
                        for (Map.Entry<String, String> nameEntry : positionNameMap.entrySet()) {
                            String lang = nameEntry.getKey();
                            if ("GLang".equals(lang)) {
                                continue;
                            }

                            String name = positionObj.getJSONObject(PositionBillConstant.NAME_KEY).getString(lang);
                            if (name.equals(nameEntry.getValue())) {
                                addErrorMessage(data, String.format("本单中“%1$s”下已存在%2$s名称为“%3$s”的岗位，请修改。", orgDy.getString(PositionBillConstant.NAME_KEY), MultiLangEnum.getName(lang), name));
                            }
                        }
                    }
                }
            }
        }
    }

}
