/*
 * *********************************************************************************************************************
 *
 *          !!
 *        .F88X
 *        X8888Y
 *      .}888888N;
 *        i888888N;        .:!              .I$WI:
 *          R888888I      .'N88~            i8}+8Y&8"l8i$8>8W~'>W8}8]KW+8IIN"8&
 *          .R888888I    .;N8888~          .X8'  "8I.!,/8"  !%NY8`"8I8~~8>,88I
 *            +888888N;  .8888888Y                                  "&&8Y.}8,
 *            ./888888N;  .R888888Y        .'}~    .>}'.`+>  i}!    "i'  +/'  .'i~  !11,.:">,  .~]!  .i}i
 *              ~888888%:  .I888888l      .]88~`1/iY88Ii+1'.R$8$8]"888888888>  Y8$  W8E  X8E  W8888'188Il}Y88$*
 *              18888888    E8888881    .]W%8$`R8X'&8%++N8i,8N%N8+l8%`  .}8N:.R$RE%N88N%N$K$R  188,FE$8%~Y88I
 *            .E888888I  .i8888888'      .:$8I;88+`E8R:/8N,.>881.`$8E/1/]N8X.Y8N`"KF&&FK!'88*."88K./$88%RN888+~
 *            8888888I  .,N888888~        ~88i"8W,!N8*.I88.}888%F,i$88"F88"  888:E8X.>88!i88>`888*.}Fl1]*}1YKi'
 *          i888888N'      I888Y          ]88;/EX*IFKFK88X  K8R  .l8W  88Y  ~88}'88E&%8W.X8N``]88!.$8K  .:W8I
 *        .i888888N;        I8Y          .&8$  .X88!  i881.:%888>I88  ;88]  +88+.';;;;:.Y88X  18N.,88l  .+88/
 *      .:R888888I
 *      .&888888I                                          Copyright (c) 2009-2020.  博锐尚格科技股份有限公司
 *        ~8888'
 *        .!88~                                                                     All rights reserved.
 *
 * *********************************************************************************************************************
 */

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

import com.alibaba.fastjson.JSONArray
import com.alibaba.fastjson.JSONObject
import com.persagy.base.extensions.toJson
import com.persagy.base.utils.SHttpUtil
import com.persagy.server.syn.Configure
import com.persagy.server.syn.Opts
import com.persagy.server.syn.models.obj.MiddleGroundDataRespose
import com.persagy.service.utils.SSpringContextUtil
import org.slf4j.LoggerFactory

/**
 * 设备 api
 *
 * @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


    /** 日志 */
    private val logger = LoggerFactory.getLogger(EquipApi::class.java)


    /**
     * 获取中台设备数据
     *
     * @param projectId     项目 id
     * @param page          页码
     * @return 返回值
     */
    fun equipSynApi(groupCode: String,projectId: String,page: Int): MiddleGroundDataRespose{
        val jsonObject = JSONObject()
        val jsonObject2 = JSONObject()
        jsonObject2["objType"] = "equipment"
        jsonObject2["valid"] = 1
        jsonObject["criteria"] = jsonObject2
        jsonObject["page"] = page
        jsonObject["size"] = 1000
        /** 直接 http 请求还是通过注册中心 */
        if (!Configure.customProperties.eurekaorhttp) {
            val url = "${opts.dataPlatform}/rwd/instance/object/query?groupCode=$groupCode&projectId=${projectId}"
            val postObject = SHttpUtil.postObject<MiddleGroundDataRespose>(url, jsonObject)
            if (postObject.result == "success"&&!postObject.data.isNullOrEmpty()) {
                return postObject
            }
            return postObject
        } else {
            val url = "http://${Configure.serviceNameConfig.DMPRWD}/rwd/instance/object/query?groupCode=$groupCode&projectId=${projectId}"
            val postObject = Configure.restTemplate.postForObject(url, jsonObject, MiddleGroundDataRespose::class.java)
            if (postObject.result == "success"&&!postObject.data.isNullOrEmpty()) {
                return postObject
            }
            return postObject
        }
    }
    /**
     * 获取中台设备数据
     *
     * @param projectId     项目 id
     * @param page          页码
     * @return 返回值
     */
    fun equipSynApiAll(groupCode: String,projectId: String): MiddleGroundDataRespose{
        val jsonObject = JSONObject()
        val jsonObject2 = JSONObject()
        jsonObject2["objType"] = "equipment"
        jsonObject2["valid"] = 1
        jsonObject["criteria"] = jsonObject2
        /** 直接 http 请求还是通过注册中心 */
        if (!Configure.customProperties.eurekaorhttp) {
            val url = "${opts.dataPlatform}/rwd/instance/object/query?groupCode=$groupCode&projectId=${projectId}"
            val postObject = SHttpUtil.postObject<MiddleGroundDataRespose>(url, jsonObject)
            if (postObject.result == "success"&&!postObject.data.isNullOrEmpty()) {
                return postObject
            }
            return postObject
        } else {
            val url = "http://${Configure.serviceNameConfig.DMPRWD}/rwd/instance/object/query?groupCode=$groupCode&projectId=${projectId}"
            val postObject = Configure.restTemplate.postForObject(url, jsonObject, MiddleGroundDataRespose::class.java)
            if (postObject.result == "success"&&!postObject.data.isNullOrEmpty()) {
                return postObject
            }
            return postObject
        }

    }

    /**
     * 获取设备和楼层的关系
     *
     * @param projectId     项目 id
     * @param equipId       设备 id
     * @return 返回结果
     */
    fun equipInFloor(groupCode: String,projectId: String,equipId: String): ArrayList<HashMap<String, Any?>> {
        val jsonObject = JSONObject()
        val jsonObject2 =  JSONObject()
        jsonObject2["graphCode"] = "MechInArch"
        jsonObject2["relCode"] = "Eq2Fl"
        jsonObject2["objFrom"] = equipId
        jsonObject["criteria"] = jsonObject2
        /** 直接 http 请求还是通过注册中心 */
        if (!Configure.customProperties.eurekaorhttp) {
            val url = "${opts.dataPlatform}/rwd/instance/relation/query?groupCode=$groupCode&projectId=${projectId}"
            val postObject = SHttpUtil.postObject<MiddleGroundDataRespose>(url, jsonObject)
            if (postObject.result == "success"&&!postObject.data.isNullOrEmpty()) {
                return postObject.data!!
            }
        } else {
            val url = "http://${Configure.serviceNameConfig.DMPRWD}/rwd/instance/relation/query?groupCode=$groupCode&projectId=${projectId}"
            val postObject = Configure.restTemplate.postForObject(url, jsonObject, MiddleGroundDataRespose::class.java)
            if (postObject.result == "success"&&!postObject.data.isNullOrEmpty()) {
                return postObject.data!!
            }
        }

        return ArrayList<HashMap<String, Any?>>()
    }

    /**
     * 获取设备和建筑的关系
     *
     * @param projectId     项目 id
     * @param equipId       设备 id
     * @return 返回结果
     */
    fun equipInBuilding(groupCode: String,projectId: String,equipId: String): ArrayList<HashMap<String, Any?>> {
        val jsonObject = JSONObject()
        val jsonObject2 =  JSONObject()
        jsonObject2["graphCode"] = "MechInArch"
        jsonObject2["relCode"] = "Eq2Bd"
        jsonObject2["objFrom"] = equipId
        jsonObject["criteria"] = jsonObject2

        /** 直接 http 请求还是通过注册中心 */
        if (!Configure.customProperties.eurekaorhttp) {
            val url = "${opts.dataPlatform}/rwd/instance/relation/query?groupCode=$groupCode&projectId=${projectId}"
            logger.debug("设备建筑url = ${url}")
            logger.debug("设备建筑boby= ${jsonObject.toJson()}")
            val postObject = SHttpUtil.postObject<MiddleGroundDataRespose>(url, jsonObject)
            if (postObject.result == "success"&&!postObject.data.isNullOrEmpty()) {
                return postObject.data!!
            }
        } else {
            val url = "http://${Configure.serviceNameConfig.DMPRWD}/rwd/instance/relation/query?groupCode=$groupCode&projectId=${projectId}"
            val postObject = Configure.restTemplate.postForObject(url, jsonObject, MiddleGroundDataRespose::class.java)
            if (postObject.result == "success"&&!postObject.data.isNullOrEmpty()) {
                return postObject.data!!
            }
        }
        return ArrayList<HashMap<String, Any?>>()
    }


    /**
     * 获取设备和建筑的关系
     *
     * @param groupCode     集团 id
     * @param projectId     项目 id
     * @param jsonObject    关系对象
     * @return 返回结果
     */
    fun queryRelation(groupCode: String,projectId: String,jsonObject: JSONObject): ArrayList<HashMap<String, Any?>> {
        /** 直接 http 请求还是通过注册中心 */
        if (!Configure.customProperties.eurekaorhttp) {
            val url = "${opts.dataPlatform}/rwd/instance/relation/query?groupCode=$groupCode&projectId=${projectId}"
            val postObject = SHttpUtil.postObject<MiddleGroundDataRespose>(url, jsonObject)
            if (postObject.result == "success"&&!postObject.data.isNullOrEmpty()) {
                return postObject.data!!
            }
        } else {
            val url = "http://${Configure.serviceNameConfig.DMPRWD}/rwd/instance/relation/query?groupCode=$groupCode&projectId=${projectId}"
            val postObject = Configure.restTemplate.postForObject(url, jsonObject, MiddleGroundDataRespose::class.java)
            if (postObject.result == "success"&&!postObject.data.isNullOrEmpty()) {
                return postObject.data!!
            }
        }
        return ArrayList<HashMap<String, Any?>>()
    }


    /**
     * 中台创建
     *
     * @param projectId     项目 id
     * @param floor        对象
     * @return 是否成功
     */
    fun  createObj(groupCode: String,projectId: String, listObj: ArrayList<HashMap<String,Any?>>): Boolean {
        /** 直接 http 请求还是通过注册中心 */
        if (!Configure.customProperties.eurekaorhttp) {
            val url = "${opts.dataPlatform}/rwd/instance/object/create?groupCode=$groupCode&projectId=${projectId}&appId=datacenter"
            val postObject = SHttpUtil.postObject<MiddleGroundDataRespose>(url, listObj)
            if (postObject.result == "success") {
                return true
            }
        } else {
            val url = "http://${Configure.serviceNameConfig.DMPRWD}/rwd/instance/object/create?groupCode=$groupCode&projectId=${projectId}&appId=datacenter"
            val postObject = Configure.restTemplate.postForObject(url, listObj, MiddleGroundDataRespose::class.java)
            if (postObject.result == "success") {
                return true
            }
        }

        return false
    }

    /**
     * 中台更新
     *
     * @param projectId     项目 id
     * @param listObj       对象列表
     * @return 是否成功
     */
    fun  updateObj(groupCode: String,projectId: String, listObj: ArrayList<HashMap<String,Any?>>): Boolean {
        /** 直接 http 请求还是通过注册中心 */
        try {
            if (!Configure.customProperties.eurekaorhttp) {
                val url = "${opts.dataPlatform}/rwd/instance/object/update?groupCode=$groupCode&projectId=${projectId}&appId=datacenter"
                logger.debug("url= ${url}")
                logger.debug("boby= ${listObj.toJson()}")
                val postObject = SHttpUtil.postObject<MiddleGroundDataRespose>(url, listObj)
                logger.debug("结果= ${postObject.toJson()}")
                if (postObject.result == "success") {
                    return true
                }
            }else {
                val url = "http://${Configure.serviceNameConfig.DMPRWD}/rwd/instance/object/update?groupCode=$groupCode&projectId=${projectId}&appId=datacenter"
                val postObject = Configure.restTemplate.postForObject(url, listObj, MiddleGroundDataRespose::class.java)
                if (postObject.result == "success") {
                    return true
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return false
    }

    /**
     * 中台删除
     *
     * @param projectId     项目 id
     * @param id            对象 id
     * @return 是否成功
     */
    fun  deleteObj(groupCode: String,projectId: String, id: String): Boolean {
        val listId = ArrayList<String>()
        listId.add(id)
        /** 直接 http 请求还是通过注册中心 */
        if (!Configure.customProperties.eurekaorhttp) {
            val url = "${opts.dataPlatform}/rwd/instance/object/delete?groupCode=$groupCode&projectId=${projectId}&appId=datacenter"
            val postObject = SHttpUtil.postObject<MiddleGroundDataRespose>(url, listId)
            if (postObject.result == "success") {
                return true
            }
        } else {
            val url = "http://${Configure.serviceNameConfig.DMPRWD}/rwd/instance/object/delete?groupCode=$groupCode&projectId=${projectId}&appId=datacenter"
            val postObject = Configure.restTemplate.postForObject(url, listId, MiddleGroundDataRespose::class.java)
            if (postObject.result == "success") {
                return true
            }
        }

        return false
    }


    /**
     * 创建关系
     */
    fun createRel(groupCode: String, projectId: String, jsonArray:JSONArray): Boolean {
        /** 直接 http 请求还是通过注册中心 */
        if (!Configure.customProperties.eurekaorhttp) {
            val url = "${opts.dataPlatform}/rwd/instance/relation/create?groupCode=$groupCode&projectId=${projectId}&appId=datacenter"
            val postObject = SHttpUtil.postObject<MiddleGroundDataRespose>(url, jsonArray)
            if (postObject.result == "success") {
                return true
            }
        } else {
            val url = "http://${Configure.serviceNameConfig.DMPRWD}/rwd/instance/relation/create?groupCode=$groupCode&projectId=${projectId}&appId=datacenter"
            val postObject = Configure.restTemplate.postForObject(url, jsonArray, MiddleGroundDataRespose::class.java)
            if (postObject.result == "success") {
                return true
            }
        }

        return false
    }

    fun delRel(groupCode: String, projectId: String, jsonObject: JSONObject): Boolean {
        /** 直接 http 请求还是通过注册中心 */
        if (!Configure.customProperties.eurekaorhttp) {
            val url = "${opts.dataPlatform}/rwd/instance/relation/delete?groupCode=$groupCode&projectId=${projectId}&appId=datacenter"
            val postObject = SHttpUtil.postObject<MiddleGroundDataRespose>(url, jsonObject)
            if (postObject.result == "success") {
                return true
            }
        } else {
            val url = "http://${Configure.serviceNameConfig.DMPRWD}/rwd/instance/relation/delete?groupCode=$groupCode&projectId=${projectId}&appId=datacenter"
            val postObject = Configure.restTemplate.postForObject(url, jsonObject, MiddleGroundDataRespose::class.java)
            if (postObject.result == "success") {
                return true
            }
        }
        return false
    }


    /**
     * 获取关系
     *
     * @param projectId     项目 id
     * @param equipId       设备 id
     * @return 返回结果
     */
    fun getRel(groupCode: String,projectId: String,graphCode: String,relCode: String): ArrayList<HashMap<String, Any?>> {
        val jsonObject = JSONObject()
        val jsonObject2 =  JSONObject()
        jsonObject2["graphCode"] = graphCode
        jsonObject2["relCode"] = relCode
        jsonObject["criteria"] = jsonObject2
        /** 直接 http 请求还是通过注册中心 */
        if (!Configure.customProperties.eurekaorhttp) {
            val url = "${opts.dataPlatform}/rwd/instance/relation/query?groupCode=$groupCode&projectId=${projectId}"
            val postObject = SHttpUtil.postObject<MiddleGroundDataRespose>(url, jsonObject)
            if (postObject.result == "success"&&!postObject.data.isNullOrEmpty()) {
                return postObject.data!!
            }
        } else {
            val url = "http://${Configure.serviceNameConfig.DMPRWD}/rwd/instance/relation/query?groupCode=$groupCode&projectId=${projectId}"
            val postObject = Configure.restTemplate.postForObject(url, jsonObject, MiddleGroundDataRespose::class.java)
            if (postObject.result == "success"&&!postObject.data.isNullOrEmpty()) {
                return postObject.data!!
            }
        }
        return ArrayList<HashMap<String, Any?>>()
    }



}