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

import com.alibaba.fastjson.JSONObject
import com.persagy.base.utils.SHttpUtil
import com.persagy.server.datacenter.models.entities.objects.Building
import com.persagy.server.syn.Opts
import com.persagy.server.syn.models.obj.MiddleGroundDataRespose
import com.persagy.server.syn.models.tools.BuildingUtil
import com.persagy.service.utils.SSpringContextUtil

/**
 * 建筑 api 中台至数据中心
 *
 * @author wx  <zhangweixin@sagacloud.com>
 * @date  2020/10/19 9:10
 */
object BuildingApi {

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

    /**
     * 获取建筑
     *
     * @param projectId     项目 id
     * @param type          类型
     * @return              返回结果
     */
    fun buildingSyn(projectId: String,type: String): ArrayList<HashMap<String, Any?>> {
        val url = "${opts.dataPlatform}/instance/object/query?projectId=${projectId}"
        val jsonObject = JSONObject()
        val jsonObject2 = JSONObject()
        jsonObject2["classCode"] = type
        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 building      建筑对象
     * @return 是否成功
     */
    fun  createBuilding(projectId: String, building:Building): Boolean {
        val url = "${opts.dataPlatform}/instance/object/create?projectId=${projectId}&appId=datacenter"
        val buildingTools = BuildingUtil.buildingTools(projectId, building)
        val postObject = SHttpUtil.postObject<MiddleGroundDataRespose>(url, buildingTools)
        if (postObject.result == "success") {
            return true
        }
        return false
    }

    /**
     * 中台更新建筑
     *
     * @param projectId     项目 id
     * @param building      建筑对象
     * @return 是否成功
     */
    fun  updateBuilding(projectId: String, building:Building): Boolean {
        val url = "${opts.dataPlatform}/instance/object/update?projectId=${projectId}&appId=datacenter"
        val buildingTools = BuildingUtil.buildingTools(projectId, building)
        val postObject = SHttpUtil.postObject<MiddleGroundDataRespose>(url, buildingTools)
        if (postObject.result == "success") {
            return true
        }
        return false
    }

    /**
     * 中台删除建筑
     *
     * @param projectId     项目 id
     * @param building      建筑对象
     * @return 是否成功
     */
    fun  delBuilding(projectId: String, buildingId: String): Boolean {
        val url = "${opts.dataPlatform}/instance/object/delete?projectId=${projectId}&appId=datacenter"
        val listId = ArrayList<String>()
        listId.add(buildingId)
        val postObject = SHttpUtil.postObject<MiddleGroundDataRespose>(url, listId)
        if (postObject.result == "success") {
            return true
        }
        return false
    }

    /**
     * 根据 id 查询对象
     *
     * @param projectId     项目 id
     * @param id             对象 id
     * @return 返回结果
     */
    fun queryBuilding(projectId: String,id: String): ArrayList<HashMap<String, Any?>> {
        val url = "${opts.dataPlatform}/instance/object/query?projectId=${projectId}"
        val jsonObject = JSONObject()
        val jsonObject2 = JSONObject()
        jsonObject2["id"] = id
        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?>>()
    }

}