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

/**
 *
 * @author wx  <zhangweixin@sagacloud.com>
 * @date  2020/10/19 17:50
 */
object EquipApi {
    /** 配置实例 */
    private val opts: Opts by lazy {
        SSpringContextUtil.getBean(Opts::class.java) as Opts
    } // opts

    /**
     * 获取中台设备数据
     *
     * @param projectId     项目 id
     * @param page          页码
     * @return 返回值
     */
    fun equipSynApi(projectId: String,page: Int): MiddleGroundDataRespose{
        val url = "${opts.dataPlatform}/instance/object/query?projectId=${projectId}"
        val jsonObject = JSONObject()
        val jsonObject2 = JSONObject()
        jsonObject2["objType"] = "equipment"
        jsonObject2["valid"] = 1
        jsonObject["criteria"] = jsonObject2
        jsonObject["page"] = page
        jsonObject["size"] = 1000
        val postObject = SHttpUtil.postObject<MiddleGroundDataRespose>(url, jsonObject)
        if (postObject.result == "success"&&!postObject.data.isNullOrEmpty()) {
            return postObject
        }
        return postObject
    }

    /**
     * 获取设备和楼层的关系
     *
     * @param projectId     项目 id
     * @param equipId       设备 id
     * @return 返回结果
     */
    fun equipInFloor(projectId: String,equipId: String): ArrayList<HashMap<String, Any?>> {
        val url = "${opts.dataPlatform}/instance/relation/query?projectId=${projectId}"
        val jsonObject = JSONObject()
        val jsonObject2 =  JSONObject()
        jsonObject2["graphCode"] = "MechInArch"
        jsonObject2["relCode"] = "Eq2Fl"
        jsonObject2["objFrom"] = equipId
        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 equipId       设备 id
     * @return 返回结果
     */
    fun equipInBuilding(projectId: String,equipId: String): ArrayList<HashMap<String, Any?>> {
        val url = "${opts.dataPlatform}/instance/relation/query?projectId=${projectId}"
        val jsonObject = JSONObject()
        val jsonObject2 =  JSONObject()
        jsonObject2["graphCode"] = "MechInArch"
        jsonObject2["relCode"] = "Eq2Bd"
        jsonObject2["objFrom"] = equipId
        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?>>()
    }

}