package com.persagy.dmp.rwd.basic.utils;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.persagy.dmp.rwd.basic.constant.DigitalObjectType;
import com.persagy.dmp.rwd.basic.constant.DigitalObjectTypeShort;

/**
 * 数据孪生通用工具
 * @author Charlie Yu
 * @date 2021-06-25
 */
public class DigitalCommonUtils {

    /**
     * 生成对象主键
     * @param projectId 项目ID
     * @param category 对象类型
     * @return 两位prefix + 无前缀projectId + simpleUUID
     */
    public static String fetchObjectId(String projectId, String category) {
        if (DigitalObjectType.PROJECT.getIndex().equalsIgnoreCase(category)) {
            return projectId;
        }
        String prefix = getIdPrefix(category);
        if(StrUtil.isBlank(prefix)) {
            return IdUtil.simpleUUID();
        }
        return prefix + projectId.substring(2) + IdUtil.simpleUUID();
    }

    /**
     * 根据对象类型获取Id前缀
     * @param category 对象类型
     * @return 两位的前缀 - 空字符串表示无效的对象类型
     */
    private static String getIdPrefix(String category) {
        if(DigitalObjectType.PROJECT.getIndex().equalsIgnoreCase(category)) {
            return DigitalObjectTypeShort.PROJECT.getIndex();
        } else if (DigitalObjectType.BUILDING.getIndex().equalsIgnoreCase(category)) {
            return DigitalObjectTypeShort.BUILDING.getIndex();
        } else if (DigitalObjectType.FLOOR.getIndex().equalsIgnoreCase(category)) {
            return DigitalObjectTypeShort.FLOOR.getIndex();
        } else if (DigitalObjectType.SPACE.getIndex().equalsIgnoreCase(category)) {
            return DigitalObjectTypeShort.SPACE.getIndex();
        } else if (DigitalObjectType.SYSTEM.getIndex().equalsIgnoreCase(category)) {
            return DigitalObjectTypeShort.SYSTEM.getIndex();
        } else if (DigitalObjectType.EQUIPMENT.getIndex().equalsIgnoreCase(category)) {
            return DigitalObjectTypeShort.EQUIPMENT.getIndex();
        } else if (DigitalObjectType.SHAFT.getIndex().equalsIgnoreCase(category)) {
            return DigitalObjectTypeShort.SHAFT.getIndex();
        } else if (DigitalObjectType.COMPONENT.getIndex().equalsIgnoreCase(category)) {
            return DigitalObjectTypeShort.COMPONENT.getIndex();
        } else if (DigitalObjectType.VIRTUAL.getIndex().equalsIgnoreCase(category)) {
            return DigitalObjectTypeShort.VIRTUAL.getIndex();
        } else {
            return "";
        }
    }
}
