package com.persagy.server.utils

import java.util.*


/**
 * id生成帮助类
 *
 * @author 张维新
 */
object IdUtils {

    /**
     * 生成项目id
     *
     * @param   code    城市六位编码
     * @param   num     城市存在建筑的数量
     * @return          返回项目id
     */
    fun projectIdCreate(code: String, num: Int): String {
        try {
            if (num + 1 > 9999) {
                throw  SecurityException("建筑数量已超出最大值")
            }
            val nums = num + 1
            val genId = String.format("%1$4s", nums).replace(' ', '0')
            val sb = StringBuilder()
            val appendId = sb.append("Pj").append(code).append(genId)

            return appendId.toString()
        } catch (e: Throwable) {
            e.printStackTrace()
        }
        throw  SecurityException("id生成失败")
    } // Function ProjectId

    /**
     * 生成建筑Id
     *
     * @param   projectId   项目id
     * @return              建筑id
     */
    fun buildIdCreate(projectId: String): String {
        val bdid = projectId.replace("Pj", "Bd")
        val sb = StringBuilder()
        val uuid = UUID.randomUUID().toString()
        val uuId = uuid.replace("-", "")
        val buildId = sb.append(bdid).append(uuId)
        return buildId.toString()
    } // Function buildIdCreate

    /**
     * 生成楼层Id
     *
     * @param   projectId   项目id
     * @return              楼层id
     */
    fun floorIdCreate(projectId: String): String {
        val bdid = projectId.replace("Pj", "Fl")
        val sb = StringBuilder()
        val uuid = UUID.randomUUID().toString()
        val uuId = uuid.replace("-", "")
        val floorId = sb.append(bdid).append(uuId)
        return floorId.toString()
    } // Function floorIdCreate

    /**
     * 生成空间Id
     *
     * @param   projectId   项目id
     * @return              空间id
     */
    fun spaceIdCreate(projectId: String): String {
        val bdid = projectId.replace("Pj", "Sp")
        val sb = StringBuilder()
        val uuid = UUID.randomUUID().toString()
        val uuId = uuid.replace("-", "")
        val spaceId = sb.append(bdid).append(uuId)
        return spaceId.toString()
    } // Function spaceIdCreate

    /**
     * 生成元空间Id
     *
     * @param   projectId   项目id
     * @return              元空间id
     */
    fun iSpaceIdCreate(projectId: String): String {
        val bdid = projectId.replace("Pj", "Si")
        val sb = StringBuilder()
        val uuid = UUID.randomUUID().toString()
        val uuId = uuid.replace("-", "")
        val siId = sb.append(bdid).append(uuId)
        return siId.toString()
    } // Function iSpaceIdCreate

    /**
     * 生成系统Id
     *
     * @param   projectId   项目id
     * @return              系统id
     */
    fun systemIdCreate(projectId: String): String {
        val bdid = projectId.replace("Pj", "Sy")
        val sb = StringBuilder()
        val uuid = UUID.randomUUID().toString()
        val uuId = uuid.replace("-", "")
        val systemId = sb.append(bdid).append(uuId)
        return systemId.toString()
    } // Function systemIdCreate

    /**
     * 生成设备Id
     *
     * @param   projectId   项目id
     * @return              设备id
     */
    fun equipmentIdCreate(projectId: String): String {
        val bdid = projectId.replace("Pj", "Eq")
        val sb = StringBuilder()
        val uuid = UUID.randomUUID().toString()
        val uuId = uuid.replace("-", "")
        val eqId = sb.append(bdid).append(uuId)
        return eqId.toString()
    } // Function equipmentIdCreate

    /**
     * 生成设备部件Id
     *
     * @param   projectId   项目id
     * @return              设备部件id
     */
    fun equipmentComponentIdCreate(projectId: String): String {
        val bdid = projectId.replace("Pj", "Ec")
        val sb = StringBuilder()
        val uuid = UUID.randomUUID().toString()
        val uuId = uuid.replace("-", "")
        val ecId = sb.append(bdid).append(uuId)
        return ecId.toString()
    } // Function equipmentComponentIdCreate

    /**
     * 生成设施Id
     *
     * @param   projectId   项目id
     * @return              设施id
     */
    fun structureIdCreate(projectId: String): String {
        val bdid = projectId.replace("Pj", "St")
        val sb = StringBuilder()
        val uuid = UUID.randomUUID().toString()
        val uuId = uuid.replace("-", "")
        val stId = sb.append(bdid).append(uuId)
        return stId.toString()
    } // Function structureIdCreate

    /**
     * 生成设施部件Id
     *
     * @param   projectId   项目id
     * @return              设施部件id
     */
    fun structureComponentIdCreate(projectId: String): String {
        val bdid = projectId.replace("Pj", "Sc")
        val sb = StringBuilder()
        val uuid = UUID.randomUUID().toString()
        val uuId = uuid.replace("-", "")
        val scId = sb.append(bdid).append(uuId)
        return scId.toString()
    } // Function structureComponentIdCreate

    /**
     * 生成资产编码Id
     *
     * @param   projectId   项目id
     * @return              设备资产编码id
     */
    fun peIdCreate(projectId: String): String {
        val bdid = "Pe"
        val sb = StringBuilder()
        val uuid = UUID.randomUUID().toString()
        val uuId = uuid.replace("-", "")
        val peId = sb.append(bdid).append(uuId)
        return peId.toString()
    } // Function peIdCreate

    /**
     * 生成竖井Id
     *
     * @param   projectId   项目id
     * @return              竖井id
     */
    fun shaftIdCreate(projectId: String): String {
        val bdid = projectId.replace("Pj", "Sh")
        val sb = StringBuilder()
        val uuid = UUID.randomUUID().toString()
        val uuId = uuid.replace("-", "")
        val shId = sb.append(bdid).append(uuId)
        return shId.toString()
    } // Function shaftIdCreate

    /**
     * 生成租户Id
     *
     * @param   projectId   项目id
     * @return              租户id
     */
    fun tenantIdCreate(projectId: String): String {
        val bdid = projectId.replace("Pj", "VOTn")
        val sb = StringBuilder()
        val uuid = UUID.randomUUID().toString()
        val uuId = uuid.replace("-", "")
        val shId = sb.append(bdid).append(uuId)
        return shId.toString()
    } // Function shaftIdCreate

    /**
     * 生成uuid
     *
     * @return              uuid
     */
    fun uuidCreate(): String {
        val uuid = UUID.randomUUID().toString()
        val uuId = uuid.replace("-", "")
        return uuId
    } // Function uuidCreate

    /**
     * 编码审查
     */
    fun  codeExanine(){
        try {
            Thread.sleep(100)
        } catch (e: InterruptedException) {
            e.printStackTrace()
        }
    }

} // Object IdUtils