package com.persagy.server.syn.models.api.obj

import com.alibaba.fastjson.JSONObject
import com.persagy.base.utils.SHttpUtil
import com.persagy.server.syn.Opts
import com.persagy.server.syn.models.obj.MiddleGroundDataRespose
import com.persagy.service.utils.SSpringContextUtil

/**
 * 空间 API
 *
 * @author wx  <zhangweixin@sagacloud.com>
 * @date  2020/10/20 9:18
 */
object SpaceApi {

    /** 配置实例 */
    private val opts: Opts by lazy {
        SSpringContextUtil.getBean(Opts::class.java) as Opts
    } // opts

    /**
     * 空间 - 获取中台空间数据
     *
     * @param projectId     项目 id
     * @return 返回结果
     */
    fun spaceSynApi(projectId: String): ArrayList<HashMap<String, Any?>>{
        val url = "${opts.dataPlatform}/rwd/instance/object/query?projectId=${projectId}"
        val jsonObject = JSONObject()
        val jsonObject2 = JSONObject()
        jsonObject2["objType"] = "space"
        jsonObject2["valid"] = 1
        jsonObject["criteria"] = jsonObject2
        val postObject = SHttpUtil.postObject<MiddleGroundDataRespose>(url, jsonObject)
        if (postObject.result == "success"&&!postObject.data.isNullOrEmpty()) {
            return postObject.data!!
        }
        return ArrayList<HashMap<String, Any?>>()
    }

    /**
     * 获取设备和建筑的关系
     *
     * @param projectId     项目 id
     * @param spaceId       设备 id
     * @return 返回结果
     */
    fun spaceInBuilding(projectId: String,spaceId: String): ArrayList<HashMap<String, Any?>> {
        val url = "${opts.dataPlatform}/rwd/instance/relation/query?projectId=${projectId}"
        val jsonObject = JSONObject()
        val jsonObject2 =  JSONObject()
        jsonObject2["graphCode"] = "ArchSubset"
        jsonObject2["relCode"] = "Bd2Sp"
        jsonObject2["objTo"] = spaceId
        jsonObject["criteria"] = jsonObject2
        val postObject = SHttpUtil.postObject<MiddleGroundDataRespose>(url, jsonObject)
        if (postObject.result == "success"&&!postObject.data.isNullOrEmpty()) {
            return postObject.data!!
        }
        return ArrayList<HashMap<String, Any?>>()
    }

    /**
     * 获取设备和建筑的关系
     *
     * @param projectId     项目 id
     * @param spaceId       设备 id
     * @return 返回结果
     */
    fun spaceInFloor(projectId: String,spaceId: String): ArrayList<HashMap<String, Any?>> {
        val url = "${opts.dataPlatform}/rwd/instance/relation/query?projectId=${projectId}"
        val jsonObject = JSONObject()
        val jsonObject2 =  JSONObject()
        jsonObject2["graphCode"] = "ArchSubset"
        jsonObject2["relCode"] = "Fl2Sp"
        jsonObject2["objTo"] = spaceId
        jsonObject["criteria"] = jsonObject2
        val postObject = SHttpUtil.postObject<MiddleGroundDataRespose>(url, jsonObject)
        if (postObject.result == "success"&&!postObject.data.isNullOrEmpty()) {
            return postObject.data!!
        }
        return ArrayList<HashMap<String, Any?>>()
    }

}