/*
 * *********************************************************************************************************************
 *
 *          !!
 *        .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.services.rel

import com.persagy.base.extensions.toJson
import com.persagy.database.SFilter
import com.persagy.mybatis.SMybatisDao
import com.persagy.server.datacenter.models.entities.rel.RSiInSpBase
import com.persagy.server.datacenter.models.entities.rel.RSiInSpBaseList
import com.persagy.server.datacenter.models.entities.rel.RSp2Si
import com.persagy.server.datacenter.models.entities.rel.RSpInSiBaseList
import com.persagy.server.synchronize.MessageHandler
import com.persagy.service.SObjectService
import com.persagy.service.SPageContext
import com.persagy.service.models.enums.SResponseType
import com.persagy.service.models.requests.SCreateRequest
import com.persagy.service.models.responses.SBaseResponse
import com.persagy.service.models.responses.SCreateResponse
import com.persagy.service.utils.SSpringContextUtil
import org.json.JSONObject

/**
 * 业务空间内的元空间关系服务
 *
 * @author 付国宝
 */
object RSpInSiBaseService: SObjectService<RSiInSpBase>(SMybatisDao(RSiInSpBase::class.java)) {

    /** MQ消息对象 */
    val messageHandler by lazy {
        SSpringContextUtil.getBean(MessageHandler::class.java) as MessageHandler
    }

    /**
     * 创建业务空间内的元空间关系
     *
     * @param   rSiInSpBase    业务空间内的元空间关系对象
     */
    fun createLists(type: String, rSiInSpBaseList: SCreateRequest<RSiInSpBase>): SCreateResponse<RSiInSpBase> {
        return try {
            val projectId = SPageContext.getHeader("projectId")

            for (rSpInSiBase in rSiInSpBaseList.content){
                val rSp2Si = RSp2Si()

                rSp2Si.projectId = projectId
                rSp2Si.id1 = rSpInSiBase.spaceId
                rSp2Si.id2 = rSpInSiBase.ispaceId
                rSp2Si.zoneType = type
                rSp2Si.sign = 1
                rSp2Si.type = "si2sp"
                val isInsert = RSp2SiService.insert(rSp2Si)
                if (isInsert){
                    val jsonObject = JSONObject()
                    jsonObject.put("type", "11")
                    jsonObject.put("projectId", projectId)
                    jsonObject.put("id", rSpInSiBase.spaceId)
                    jsonObject.put("obj_type", "ElementSptoSpace")
                    jsonObject.put("linkId",rSpInSiBase.ispaceId)

                    /** 发送消息 */
//                    messageHandler.sendMessage(jsonObject.toString())

//                    kafkaService.sendNotice("datacenterrel", RelationNotice(projectId!!, "ElementSptoSpace", "11", IdUtils.uuidCreate(),  rSpInSiZoneHeating.spaceId!!, rSpInSiZoneHeating.ispaceId!!),false)

//                    val rDataCenterLog = RDataCenterLog()
//                    rDataCenterLog.projectId = projectId
//                    rDataCenterLog.fromId = rSpInSiZoneHeating.spaceId
//                    rDataCenterLog.toId = rSpInSiZoneHeating.ispaceId
//                    rDataCenterLog.objectType = "ElementSptoSpace"
//                    rDataCenterLogService.insert(rDataCenterLog)

                }else{

                }
            }
            SCreateResponse(SResponseType.success)

//            if (type == "HeatingZone"){
//                RSpInSiZoneHeatingService.create(rSiInSpBaseList.content)
//            }else if (type == "CleanZone"){
//                RSpInSiZoneCleanService.create(rSiInSpBaseList.content)
//            }else if (type == "DomesticWaterSupplyZone"){
//                RSpInSiZoneDomesticWaterSupplyService.create(rSiInSpBaseList.content)
//            }else if (type == "NetworkZone" ){
//                RSpInSiZoneNetworkService.create(rSiInSpBaseList.content)
//            }else if (type == "TenantZone"){
//                RSpInSiZoneTenantService.create(rSiInSpBaseList.content)
//            }else if (type == "AirConditioningZone"){
//                RSpInSiZoneAirConditioningService.create(rSiInSpBaseList.content)
//            }else if (type == "FunctionZone"){
//                RSpInSiZoneFunctionService.create(rSiInSpBaseList.content)
//            }else if (type == "FireZone"){
//                RSpInSiZoneFireService.create(rSiInSpBaseList.content)
//            }else if (type == "SecurityZone"){
//                RSpInSiZoneSecurityService.create(rSiInSpBaseList.content)
//            }else if (type == "GeneralZone"){
//                RSpInSiZoneGeneralService.create(rSiInSpBaseList.content)
//            }else if (type == "PowerSupplyZone"){
//                RSpInSiZonePowerSupplyService.create(rSiInSpBaseList.content)
//            }else if (type == "LightingZone"){
//                RSpInSiZoneLightingService.create(rSiInSpBaseList.content)
//            }else{
//                val sCreateResponse = SCreateResponse<RSiInSpBase>(SResponseType.failure)
//                sCreateResponse.message = "空间分区类型不存在：$type"
//                sCreateResponse
//            }
        } catch (e: Exception) {
            e.printStackTrace()
            SCreateResponse(SResponseType.failure,e.message.toString())
        }
    } // Function create()

    /**
     * 根据对象删除
     *
     * @param   RSp2Si    关系对象实例
     * @return
     */
    fun deletes(type: String, rSiInSpBaseList: ArrayList<RSiInSpBase>): SBaseResponse {
        return try {
            val projectId = SPageContext.getHeader("projectId")
            for (rSpInSiBase in rSiInSpBaseList){
                val rSp2Si = RSp2Si()
                rSp2Si.id1 = rSpInSiBase.spaceId
                rSp2Si.id2 = rSpInSiBase.ispaceId
                rSp2Si.projectId = projectId
                rSp2Si.type = type
                rSp2Si.type = "si2sp"
                val isDelete = RSp2SiService.delete(rSp2Si)
                if (isDelete) {
                    val jsonObject = JSONObject()
                    jsonObject.put("type", "12")
                    jsonObject.put("projectId", projectId)
                    jsonObject.put("id", rSpInSiBase.spaceId)
                    jsonObject.put("obj_type", "ElementSptoSpace")
                    jsonObject.put("linkId",rSpInSiBase.ispaceId)

                    /** 发送消息 */
//                    messageHandler.sendMessage(jsonObject.toString())
//                    kafkaService.sendNotice("datacenterrel", RelationNotice(projectId!!, "ElementSptoSpace", "12", IdUtils.uuidCreate(),  rSpInSiZoneHeating.spaceId!!, rSpInSiZoneHeating.ispaceId),false)
                } else {

                }
            }
            val sBaseResponse = SBaseResponse(SResponseType.success)
            sBaseResponse
//            if (type == "HeatingZone"){
//                RSpInSiZoneHeatingService.deletes(rSiInSpBaseList)
//            }else if (type == "CleanZone"){
//                RSpInSiZoneCleanService.deletes(rSiInSpBaseList)
//            }else if (type == "DomesticWaterSupplyZone"){
//                RSpInSiZoneDomesticWaterSupplyService.deletes(rSiInSpBaseList)
//            }else if (type == "NetworkZone" ){
//                RSpInSiZoneNetworkService.deletes(rSiInSpBaseList)
//            }else if (type == "TenantZone"){
//                RSpInSiZoneTenantService.deletes(rSiInSpBaseList)
//            }else if (type == "AirConditioningZone"){
//                RSpInSiZoneAirConditioningService.deletes(rSiInSpBaseList)
//            }else if (type == "FunctionZone"){
//                RSpInSiZoneFunctionService.deletes(rSiInSpBaseList)
//            }else if (type == "FireZone"){
//                RSpInSiZoneFireService.deletes(rSiInSpBaseList)
//            }else if (type == "SecurityZone"){
//                RSpInSiZoneSecurityService.deletes(rSiInSpBaseList)
//            }else if (type == "GeneralZone"){
//                RSpInSiZoneGeneralService.deletes(rSiInSpBaseList)
//            }else if (type == "PowerSupplyZone"){
//                RSpInSiZonePowerSupplyService.deletes(rSiInSpBaseList)
//            }else if (type == "LightingZone"){
//                RSpInSiZoneLightingService.deletes(rSiInSpBaseList)
//            }else{
//                val sCreateResponse = SCreateResponse<RSiInSpBase>(SResponseType.failure)
//                sCreateResponse.message = "空间分区类型不存在：$type"
//                sCreateResponse
//            }
        } catch (e: Exception) {
            e.printStackTrace()
            return SBaseResponse(SResponseType.failure, e.message!!)
        }
    } // Function deletes()

    /**
     * 创建元空间关联业务空间
     *
     * @param    rSpInSiBaseList    业务空间内的元空间关系对象列表
     */
    fun createSp(type: String,rSpInSiBaseList: RSpInSiBaseList): SCreateResponse<RSiInSpBase> {
        return try {
            /** 成功集合 */
            val successList = ArrayList<Any>()
            /** 失败集合 */
            val failureList = ArrayList<Any>()
//            delete("ispaceId = '${rSpInSiBaseList.ispaceId}'")
            RSp2SiService.delete("id2 ='${rSpInSiBaseList.ispaceId} ")
            val projectId = SPageContext.getHeader("projectId")
            if(!rSpInSiBaseList.spaceIdList.isNullOrEmpty()) {
                for (spaceId in rSpInSiBaseList.spaceIdList!!) {
                    val rSp2Si = RSp2Si()
                    rSp2Si.id2 = rSpInSiBaseList.ispaceId
                    rSp2Si.id1 = spaceId
                    rSp2Si.projectId = projectId
                    rSp2Si.sign = 1
                    rSp2Si.zoneType = type
                    rSp2Si.type = "si2sp"
                    val replace = RSp2SiService.replace(rSp2Si)
                    if (replace) {
                        successList.add(rSp2Si)
                    } else {
                        failureList.add(rSp2Si)
                    }
                }
                if (successList.size > 0) {
                    val jsonObject = JSONObject()
                    jsonObject.put("type", "13")
                    jsonObject.put("projectId", projectId)
                    jsonObject.put("id", rSpInSiBaseList.ispaceId)
                    jsonObject.put("obj_type", "ElementSptoSpace")
                    jsonObject.put("linkId",successList.toJson())

                    /** 发送消息 */
//                    messageHandler.sendMessage(jsonObject.toString())

                    val sCreateResponse = SCreateResponse<RSiInSpBase>(SResponseType.success)
                    if (failureList.size > 0) {
                        sCreateResponse.message = "失败对象：" + failureList.toJson()
                    }
                    sCreateResponse
                } else {
                    SCreateResponse(SResponseType.failure)
                }
            }else{
                SCreateResponse(SResponseType.success)
            }
        } catch (e: Exception) {
            e.printStackTrace()
            SCreateResponse(SResponseType.failure,e.message.toString())
        }
    } // Function createSp()

    /**
     * 创建业务空间内的元空间关系
     *
     * @param    rSiInSpBaseList    业务空间内的元空间关系对象列表
     */
    fun createsSi(rSiInSpBaseList: RSiInSpBaseList): SCreateResponse<RSiInSpBase> {
        return try {
            /** 成功集合 */
            val successList = ArrayList<Any>()
            /** 失败集合 */
            val failureList = ArrayList<Any>()
//            delete("spaceID = '${rSiInSpBaseList.spaceId}'")
            RSp2SiService.delete("Id1 = '${rSiInSpBaseList.spaceId}'")

            val projectId = SPageContext.getHeader("projectId")
            if(!rSiInSpBaseList.ispaceIdList.isNullOrEmpty()) {
                for (ispaceId in rSiInSpBaseList.ispaceIdList!!) {
                    val rSp2Si = RSp2Si()
                    rSp2Si.id1 = rSiInSpBaseList.spaceId
                    rSp2Si.id2 = ispaceId
                    rSp2Si.projectId = projectId
                    rSp2Si.zoneType = rSiInSpBaseList.zoneType
                    rSp2Si.sign = 1
                    rSp2Si.type = "si2sp"
                    val replace = RSp2SiService.replace(rSp2Si)
                    if (replace) {
                        successList.add(rSp2Si)
                    } else {
                        failureList.add(rSp2Si)
                    }
                }
                if (successList.size > 0) {
                    val jsonObject = JSONObject()
                    jsonObject.put("type", "13")
                    jsonObject.put("projectId", projectId)
                    jsonObject.put("id", rSiInSpBaseList.spaceId)
                    jsonObject.put("obj_type", "ElementSptoSpace")
                    jsonObject.put("linkId",successList.toJson())

                    /** 发送消息 */
//                    messageHandler.sendMessage(jsonObject.toString())
                    if (failureList.size > 0) {
                        val sCreateResponse = SCreateResponse<RSiInSpBase>(SResponseType.success)
                        sCreateResponse.message = "失败对象：" + failureList.toJson()
                        sCreateResponse
                    } else {
                        val sCreateResponse = SCreateResponse<RSiInSpBase>(SResponseType.success)
                        sCreateResponse
                    }
                } else {
                    SCreateResponse(SResponseType.failure)
                }
            }else{
                SCreateResponse(SResponseType.success)
            }
        } catch (e:Exception){
            e.printStackTrace()
            SCreateResponse(SResponseType.failure,e.message.toString())
        }
    } // Function createsSi()

    /**
     * 创建业务空间内的元空间关系
     *
     * @param    rSiInSpBaseList    元空间和业务空间关系的对象列表
     */
    fun createsSpSi(type: String,rSiInSpBaseList: RSiInSpBaseList): SCreateResponse<RSiInSpBaseList> {
        return try {
            /** 成功集合 */
            val successList = ArrayList<Any>()
            /** 失败集合 */
            val failureList = ArrayList<Any>()
            val projectId = SPageContext.getHeader("projectId")
            if(!rSiInSpBaseList.ispaceIdList.isNullOrEmpty()) {
                for (ispaceId in rSiInSpBaseList.ispaceIdList!!) {
                    val rSp2Si = RSp2Si()
                    rSp2Si.id1 = rSiInSpBaseList.spaceId
                    rSp2Si.id2 = ispaceId
                    rSp2Si.projectId = projectId
                    rSp2Si.zoneType = type
                    rSp2Si.sign = 1
                    rSp2Si.type = "si2sp"
                    val replace = RSp2SiService.insert(rSp2Si)
                    if (replace) {
                        successList.add(rSp2Si)
                    } else {
                        failureList.add(rSp2Si)
                    }
                }
                if (successList.size > 0) {
                    val jsonObject = JSONObject()
                    jsonObject.put("type", "14")
                    jsonObject.put("projectId", projectId)
                    jsonObject.put("id", rSiInSpBaseList.spaceId)
                    jsonObject.put("obj_type", "ElementSptoSpace")
                    jsonObject.put("linkId",successList.toJson())

                    /** 发送消息 */
//                    messageHandler.sendMessage(jsonObject.toString())
//                    kafkaService.sendNotice("datacenterrel", RelationNotice(projectId!!, "ElementSptoSpace", "14", IdUtils.uuidCreate(),  rSiInSpZoneHeatingList.spaceId!!, null),false)
                    if (failureList.size > 0) {
                        val sCreateResponse = SCreateResponse<RSiInSpBaseList>(SResponseType.success)
                        sCreateResponse.message = "失败对象：" + failureList.toJson()
                        sCreateResponse
                    } else {
                        val sCreateResponse = SCreateResponse<RSiInSpBaseList>(SResponseType.success)
                        sCreateResponse
                    }
                } else {
                    SCreateResponse(SResponseType.failure)
                }
            }else{
                SCreateResponse(SResponseType.failure)
            }

//            if (type == "HeatingZone"){
//                RSpInSiZoneHeatingService.createsSiSp(rSiInSpBaseList)
//            }else if (type == "CleanZone"){
//                RSpInSiZoneCleanService.createsSiSp(rSiInSpBaseList)
//            }else if (type == "DomesticWaterSupplyZone"){
//                RSpInSiZoneDomesticWaterSupplyService.createsSiSp(rSiInSpBaseList)
//            }else if (type == "NetworkZone" ){
//                RSpInSiZoneNetworkService.createsSiSp(rSiInSpBaseList)
//            }else if (type == "TenantZone"){
//                RSpInSiZoneTenantService.createsSiSp(rSiInSpBaseList)
//            }else if (type == "AirConditioningZone"){
//                RSpInSiZoneAirConditioningService.createsSiSp(rSiInSpBaseList)
//            }else if (type == "FunctionZone"){
//                RSpInSiZoneFunctionService.createsSiSp(rSiInSpBaseList)
//            }else if (type == "FireZone"){
//                RSpInSiZoneFireService.createsSiSp(rSiInSpBaseList)
//            }else if (type == "SecurityZone"){
//                RSpInSiZoneSecurityService.createsSiSp(rSiInSpBaseList)
//            }else if (type == "GeneralZone"){
//                RSpInSiZoneGeneralService.createsSiSp(rSiInSpBaseList)
//            }else if (type == "PowerSupplyZone"){
//                RSpInSiZonePowerSupplyService.createsSiSp(rSiInSpBaseList)
//            }else if (type == "LightingZone"){
//                RSpInSiZoneLightingService.createsSiSp(rSiInSpBaseList)
//            }else{
//                val sCreateResponse = SCreateResponse<RSiInSpBaseList>(SResponseType.failure)
//                sCreateResponse.message = "空间分区类型不存在：$type"
//                sCreateResponse
//            }
        } catch (e:Exception){
            e.printStackTrace()
            SCreateResponse(SResponseType.failure,e.message.toString())
        }
    } // Function createsSpSi()

    /**
     * 创建业务空间内的元空间关系
     *
     * @param    rSiInSpBaseList    元空间和业务空间关系的对象列表
     */
    fun createsSp(type: String,rSiInSpBaseList: RSiInSpBaseList): SCreateResponse<RSiInSpBaseList> {
        return try {
            val projectId = SPageContext.getHeader("projectId")
            /** 成功集合 */
            val successList = ArrayList<Any>()
            /** 失败集合 */
            val failureList = ArrayList<Any>()
            RSp2SiService.delete(SFilter.eq("projectId",projectId!!),SFilter.eq("id1",rSiInSpBaseList.spaceId!!),SFilter.eq("type","si2sp"))
//            RSp2SiService.delete("id1 = '${rSiInSpBaseList.spaceId}'")

            if(!rSiInSpBaseList.ispaceIdList.isNullOrEmpty()) {
                for (ispaceId in rSiInSpBaseList.ispaceIdList!!) {
                    val rSp2Si = RSp2Si()
                    rSp2Si.id1 = rSiInSpBaseList.spaceId
                    rSp2Si.id2 = ispaceId
                    rSp2Si.projectId = projectId
                    rSp2Si.sign = 1
                    rSp2Si.zoneType = type
                    rSp2Si.type = "si2sp"
                    val replace = RSp2SiService.replace(rSp2Si)
                    if (replace) {
                        successList.add(rSp2Si)
                    } else {
                        failureList.add(rSp2Si)
                    }
                }
                if (successList.size > 0) {
                    val jsonObject = JSONObject()
                    jsonObject.put("type", "13")
                    jsonObject.put("projectId", projectId)
                    jsonObject.put("id", rSiInSpBaseList.spaceId)
                    jsonObject.put("obj_type", "ElementSptoSpace")
                    jsonObject.put("linkId",successList.toJson())

                    /** 发送消息 */
//                    messageHandler.sendMessage(jsonObject.toString())
//                    kafkaService.sendNotice("datacenterrel", RelationNotice(projectId!!, "ElementSptoSpace", "13", IdUtils.uuidCreate(),  rSiInSpZoneHeatingList.spaceId!!, null),false)
                    if (failureList.size > 0) {
                        val sCreateResponse = SCreateResponse<RSiInSpBaseList>(SResponseType.success)
                        sCreateResponse.message = "失败对象：" + failureList.toJson()
                        sCreateResponse
                    } else {
                        val sCreateResponse = SCreateResponse<RSiInSpBaseList>(SResponseType.success)
                        sCreateResponse
                    }
                } else {
                    SCreateResponse(SResponseType.failure)
                }
            }else{
                SCreateResponse(SResponseType.success)
            }

//            if (type == "HeatingZone"){
//                RSpInSiZoneHeatingService.createsSi(rSiInSpBaseList)
//            }else if (type == "CleanZone"){
//                RSpInSiZoneCleanService.createsSi(rSiInSpBaseList)
//            }else if (type == "DomesticWaterSupplyZone"){
//                RSpInSiZoneDomesticWaterSupplyService.createsSi(rSiInSpBaseList)
//            }else if (type == "NetworkZone" ){
//                RSpInSiZoneNetworkService.createsSi(rSiInSpBaseList)
//            }else if (type == "TenantZone"){
//                RSpInSiZoneTenantService.createsSi(rSiInSpBaseList)
//            }else if (type == "AirConditioningZone"){
//                RSpInSiZoneAirConditioningService.createsSi(rSiInSpBaseList)
//            }else if (type == "FunctionZone"){
//                RSpInSiZoneFunctionService.createsSi(rSiInSpBaseList)
//            }else if (type == "FireZone"){
//                RSpInSiZoneFireService.createsSi(rSiInSpBaseList)
//            }else if (type == "SecurityZone"){
//                RSpInSiZoneSecurityService.createsSi(rSiInSpBaseList)
//            }else if (type == "GeneralZone"){
//                RSpInSiZoneGeneralService.createsSi(rSiInSpBaseList)
//            }else if (type == "PowerSupplyZone"){
//                RSpInSiZonePowerSupplyService.createsSi(rSiInSpBaseList)
//            }else if (type == "LightingZone"){
//                RSpInSiZoneLightingService.createsSi(rSiInSpBaseList)
//            }else{
//                val sCreateResponse = SCreateResponse<RSiInSpBaseList>(SResponseType.failure)
//                sCreateResponse.message = "空间分区类型不存在：$type"
//                sCreateResponse
//            }
        } catch (e:Exception){
            e.printStackTrace()
            SCreateResponse(SResponseType.failure,e.message.toString())
        }
    } // Function createsSpSi()

} // Object RSpInSiBaseService