/*
 * *********************************************************************************************************************
 *
 *          !!
 *        .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.datacenter.syn.services.jms

import com.persagy.database.SFilter
import com.persagy.mybatis.SMybatisDao
import com.persagy.server.datacenter.models.entities.dictnew.DictProject
import com.persagy.server.datacenter.wanda.obj.Equipment
import com.persagy.server.datacenter.models.entities.rel.*
import com.persagy.server.datacenter.wanda.obj.Floor
import com.persagy.server.datacenter.wanda.obj.ZoneSpace
import com.persagy.server.datacenter.wanda.rel.*
import com.persagy.server.syn.jms.bean.SyncRwdDatacenterMessage
import com.persagy.service.SBaseService
import com.persagy.service.SObjectService
import org.springframework.stereotype.Service

/**
 *
 * @author wx  <zhangweixin@sagacloud.com>
 * @date  2020/12/25 17:33
 */
@Service
class RwdRelationAdd {

    /** 业务空间服务 */
    private val zoneSpaceService = SBaseService(SMybatisDao(ZoneSpace::class.java))
    /** 设备服务楼层 */
    private val rEqForFlService = SBaseService(SMybatisDao(REqForFl::class.java))
    /** 设备服务建筑 */
    private val rEqForBdService = SBaseService(SMybatisDao(REqForBd::class.java))
    /** 系统下的设备 */
    private val rSyEqService = SBaseService(SMybatisDao(RSyEq::class.java))
    /** 设备服务于竖井 */
    private val rEqForShService = SBaseService(SMybatisDao(REqForSh::class.java))
    /** 设备所在竖井 */
    private val rEqInShService = SBaseService(SMybatisDao(REqInSh::class.java))
    /** 数据中心设备和空间关系 */
    private val rEq2SpService = SObjectService(SMybatisDao(REq2Sp::class.java))
    /** 数据中心系统所在竖井关系 */
    private val syInShService = SObjectService(SMybatisDao(RSyInSh::class.java))
    /** 数据中心系统服务于竖井关系 */
    private val rSyForShService = SObjectService(SMybatisDao(RSyForSh::class.java))
    /** 数据中心竖井下的业务空间关系 */
    private val rSh2SpService = SObjectService(SMybatisDao(RSh2Sp::class.java))
    /** 数据中心建筑下的竖井关系 */
    private val rShInBdService = SObjectService(SMybatisDao(RShInBd::class.java))
    /** 数据中心系统所在楼层关系 */
    private val rSyInFlService = SObjectService(SMybatisDao(RSyInFl::class.java))
    /** 数据中心系统服务于楼层关系 */
    private val rSyForFlService = SObjectService(SMybatisDao(RSyForFl::class.java))
    /** 数据中心系统所在的建筑体关系 */
    private val rSyInBdService = SObjectService(SMybatisDao(RSyInBd::class.java))
    /** 数据中心系统服务于楼层关系 */
    private val rSyForBdService = SObjectService(SMybatisDao(RSyForBd::class.java))
    /** 数据中心楼层下的业务空间关系 */
    private val rSpinFlService = SObjectService(SMybatisDao(RSpinFl::class.java))
    /** 数据中心建筑体下的业务空间关系 */
    private val rSpinBdService = SObjectService(SMybatisDao(RSpinBd::class.java))
    /** 数据中心系统和空间关系 */
    private val rSy2SpService = SObjectService(SMybatisDao(RSy2Sp::class.java))
    /** 数据中心竖井贯通关系 */
    private val rShaftThroughShaftService = SObjectService(SMybatisDao(RShaftThroughShaft::class.java))
    /** 数据中心楼层贯通关系 */
    private val rFlThroughFlService = SObjectService(SMybatisDao(RFlThroughFl::class.java))
    /** 空间和空间的关系 */
    private val rSp2SpService = SObjectService(SMybatisDao(RSp2Sp::class.java))
    /** 空间和空间的关系 */
    private val rEq2EqService = SObjectService(SMybatisDao(REq2Eq::class.java))
    /** 系统控制关系服务 */
    private val rEq2SyService = SObjectService(SMybatisDao(REq2Sy::class.java))

    /** 数据中心设备服务 */
    private val equipmentService = SObjectService(SMybatisDao(Equipment::class.java))
    /** 数据中心楼层服务 */
    private val floorService = SObjectService(SMybatisDao(Floor::class.java))
    /** 字典项目服务 */
    private val dictProjectService = SBaseService(SMybatisDao(DictProject::class.java))
    /** 缓存对象数据 - 数据中台到数据中心 */
    var syncRwdMessageService = SObjectService(SMybatisDao(SyncRwdDatacenterMessage::class.java))
    /**
     * 添加关系-中台
     */
    fun addRelation(message: SyncRwdDatacenterMessage){

        try {
            if (!message.exts.isNullOrEmpty()){
                val graphCodeContains = message.exts!!.contains("graphCode")
                val relCodeContains = message.exts!!.contains("relCode")
                message.state = 0
                if (graphCodeContains&&relCodeContains){
                    val graphCode = message.exts!!["graphCode"]
                    val relCode = message.exts!!["relCode"]
                    val objFrom = message.exts!!["objFrom"]
                    val objTo = message.exts!!["relCode"]
                    try {

                        when (graphCode) {
                            "ArchSubset" -> {
                                /** 建筑从属关系 */
                                when (relCode) {
                                    "Bd2Fl" -> {
                                        /** 建筑体下的楼层 */

                                    }
                                    "Bd2Sh" -> {
                                        /** 建筑体下的竖井 */
                                        val rShInBd = RShInBd()
                                        rShInBd.graphCode = graphCode.toString()
                                        rShInBd.type = relCode.toString()
                                        rShInBd.buildingId = objFrom.toString()
                                        rShInBd.shaftId = objTo.toString()
                                        rShInBd.projectId = message.projectId
                                        rShInBdService.replace(rShInBd)
                                    }
                                    "Bd2Sp" -> {
                                        /** 建筑体下的业务空间 */
                                        val rSpinBd = RSpinBd()
                                        val containsKey = message.exts!!.containsKey("relValue")
                                        if (containsKey) {
                                            val objTo = message.exts!!["relValue"]
                                            rSpinBd.objectType = objTo.toString()
                                        }
                                        if (rSpinBd.objectType.isNullOrEmpty()){
                                            val entity = zoneSpaceService.select(SFilter.eq("id", objTo.toString())).entity()
                                            if (entity!=null) {
                                                rSpinBd.objectType = entity.classCode
                                            }
                                        }
                                        rSpinBd.buildingId = objFrom.toString()
                                        rSpinBd.spaceId = objTo.toString()
                                        rSpinBd.projectId  = message.projectId
                                        rSpinBdService.replace(rSpinBd)
                                    }
                                    "Fl2Sp" -> {
                                        /** 楼层下的业务空间 */
                                        val rSpinFl = RSpinFl()
                                        val containsKey = message.exts!!.containsKey("relValue")
                                        if (containsKey) {
                                            val objTo = message.exts!!["relValue"]
                                            rSpinFl.objectType = objTo.toString()
                                        }
                                        if (rSpinFl.objectType.isNullOrEmpty()){
                                            val entity = zoneSpaceService.select(SFilter.eq("id", objTo.toString())).entity()
                                            if (entity!=null) {
                                                rSpinFl.objectType = entity.classCode
                                            }
                                        }
                                        val entity = floorService.select(SFilter.eq("id", objFrom.toString())).entity()
                                        if (entity!=null) {
                                            rSpinFl.buildingId = entity.buildingId
                                        }
                                        rSpinFl.floorId = objFrom.toString()
                                        rSpinFl.spaceId = objTo.toString()
                                        rSpinFl.projectId  = message.projectId
                                        rSpinFlService.replace(rSpinFl)
                                    }
                                    "Sh2Sp" -> {
                                        /** 竖井下的业务空间 */
                                        val rSh2Sp = RSh2Sp()
                                        val containsKey = message.exts!!.containsKey("relValue")
                                        if (containsKey) {
                                            val objTo = message.exts!!["relValue"]
                                            rSh2Sp.zoneType = objTo.toString()
                                        }
                                        if (rSh2Sp.zoneType.isNullOrEmpty()){
                                            val entity = zoneSpaceService.select(SFilter.eq("id", objTo.toString())).entity()
                                            if (entity!=null) {
                                                rSh2Sp.zoneType = entity.classCode
                                            }
                                        }
                                        rSh2Sp.graphCode = graphCode.toString()
                                        rSh2Sp.type = relCode.toString()
                                        rSh2Sp.id1 = objFrom.toString()
                                        rSh2Sp.id2 = objTo.toString()
                                        rSh2Sp.projectId  = message.projectId
                                        rSh2SpService.replace(rSh2Sp)
                                    }
                                }

                            }

                            "MechSubset" -> {
                                /** 机电从属关系 */
                                if (relCode == "Sy2Eq"){
                                    val rSyEq = RSyEq()
                                    rSyEq.equipId = objTo.toString()
                                    rSyEq.sysId = objFrom.toString()
                                    rSyEq.graphCode = graphCode.toString()
                                    rSyEq.type = relCode.toString()
                                    rSyEq.projectId = message.projectId
                                    rSyEqService.replace(rSyEq)
                                }else if (relCode == "Eq2Ec") {
                                    val equipment = Equipment()
                                    equipment.id = objTo.toString()
                                    equipment.parentId = objFrom.toString()
                                    equipmentService.update(equipment, arrayListOf("parentId"))
                                }
                            }
                            "ThroughRelationship" -> {
                                /** 贯通关系 */
                                if (relCode == "Fl2Fl"){
                                /** 楼层贯通关系 */
                                    val rFlThroughFl = RFlThroughFl()
                                    rFlThroughFl.floorId = objFrom.toString()
                                    rFlThroughFl.floorOtherId = objTo.toString()
                                    rFlThroughFl.projectId = message.projectId
                                    rFlThroughFlService.replace(rFlThroughFl)
                                }else if (relCode == "Sh2Sh") {
                                /** 竖井贯通关系 */
                                    val rShaftThroughShaft = RShaftThroughShaft()
                                    rShaftThroughShaft.graphCode = graphCode.toString()
                                    rShaftThroughShaft.projectId = message.projectId
                                    rShaftThroughShaft.type = relCode.toString()
                                    rShaftThroughShaft.shaftId = objFrom.toString()
                                    rShaftThroughShaft.shaftOtherId = objTo.toString()
                                    rShaftThroughShaftService.replace(rShaftThroughShaft)
                                }

                            }
                            "MechInArch" -> {
                                /** 机电类所在区域关系 */
                                when (relCode) {
                                    "Sy2Bd" -> {
                                        /** 系统所在建筑 */
                                        val rSyInBd = RSyInBd()
                                        rSyInBd.projectId = message.projectId
                                        rSyInBd.buildingId = objTo.toString()
                                        rSyInBd.sysId = objFrom.toString()
                                        rSyInBdService.replace(rSyInBd)
                                    }
                                    "Sy2Fl" -> {
                                        /** 系统所在楼层 */
                                        val rSyInFl = RSyInFl()
                                        rSyInFl.sysId = objFrom.toString()
                                        rSyInFl.floorId = objTo.toString()
                                        rSyInFl.projectId = message.projectId
                                        val entity = floorService.select(SFilter.eq("id", objTo.toString())).entity()
                                        if (entity!=null) {
                                            rSyInFl.buildingId = entity.buildingId
                                        }
                                        rSyInFl.graphCode = graphCode.toString()
                                        rSyInFl.type = relCode.toString()
                                        rSyInFlService.replace(rSyInFl)
                                    }
                                    "Sy2Sh" -> {
                                        /** 系统所在竖井 */
                                        val rSyInSh = RSyInSh()
                                        rSyInSh.graphCode = graphCode.toString()
                                        rSyInSh.type = relCode.toString()
                                        rSyInSh.projectId = message.projectId
                                        rSyInSh.sysId = objFrom.toString()
                                        rSyInSh.shaftId = objTo.toString()
                                        syInShService.replace(rSyInSh)
                                    }
                                    "Sy2Sp" -> {
                                        /** 系统所在空间 */
                                        val rSy2Sp = RSy2Sp()
                                        rSy2Sp.projectId = message.projectId
                                        rSy2Sp.id1 = objFrom.toString()
                                        rSy2Sp.id2 = objTo.toString()
                                        rSy2Sp.type = "sy2sp"
                                        val containsKey = message.exts!!.containsKey("relValue")
                                        if (containsKey) {
                                            val relValue = message.exts!!["relValue"]
                                            rSy2Sp.zoneType  = relValue.toString()
                                        }
                                        if (rSy2Sp.zoneType.isNullOrEmpty()) {
                                            val entity = zoneSpaceService.select(SFilter.eq("id", objTo.toString())).entity()
                                            if (entity!=null) {
                                                rSy2Sp.zoneType = entity.classCode
                                            }
                                        }
                                        rSy2SpService.replace(rSy2Sp)
                                    }
                                    "Eq2Bd" -> {
                                        /** 设备设施所在建筑 */
                                        val equipment = Equipment()
                                        equipment.id = objFrom.toString()
                                        equipment.buildingId = objTo.toString()
                                        equipment.projectId = message.projectId
                                        equipmentService.update(equipment, arrayListOf("buildingId"))
                                    }
                                    "Eq2Fl" -> {
                                        /** 设备设施所在楼层 */
                                        val equipment = Equipment()
                                        equipment.id = objFrom.toString()
                                        equipment.floorId = objTo.toString()
                                        equipment.projectId = message.projectId
                                        val entity = floorService.select(SFilter.eq("id", objTo.toString())).entity()
                                        if (entity!=null) {
                                            equipment.buildingId = entity.buildingId
                                        }
                                        equipmentService.update(equipment, arrayListOf("buildingId","floorId"))
                                    }
                                    "Eq2Sh" -> {
                                        /** 设备设施所在竖井 */
                                        val rEqInSh = REqInSh()
                                        rEqInSh.equipId = objFrom.toString()
                                        rEqInSh.shaftId = objTo.toString()
                                        rEqInSh.graphCode = graphCode.toString()
                                        rEqInSh.type = relCode.toString()
                                        rEqInSh.projectId = message.projectId
                                        rEqInShService.replace(rEqInSh)
                                    }
                                    "Eq2Sp" -> {
                                        /** 设备设施所在空间 */
                                      val rEq2Sp = REq2Sp()
                                        rEq2Sp.id1 = objFrom.toString()
                                        rEq2Sp.id2 = objTo.toString()
                                        rEq2Sp.projectId = message.projectId
                                        rEq2Sp.graphCode = graphCode.toString()
                                        val containsKey = message.exts!!.containsKey("relValue")
                                        if (containsKey) {
                                            val relValue = message.exts!!["relValue"]
                                            rEq2Sp.zoneType  = relValue.toString()
                                        }
                                        if (rEq2Sp.zoneType.isNullOrEmpty()){
                                            val entity = zoneSpaceService.select(SFilter.eq("id", objTo.toString())).entity()
                                            if (entity!=null) {
                                                rEq2Sp.zoneType = entity.classCode
                                            }
                                        }
                                        rEq2Sp.type = "eq2sp_in"
                                        rEq2SpService.replace(rEq2Sp)
                                    }
                                }

                            }
                            "MechForArch" -> {
                                /** 机电类服务区域关系 */
                                when (relCode) {
                                    "Sy2Bd" -> {
                                        /** 系统服务于建筑 */
                                        val rSyForBd = RSyForBd()
                                        rSyForBd.sysId = objFrom.toString()
                                        rSyForBd.buildingId = objTo.toString()
                                        rSyForBd.projectId = message.projectId
                                        rSyForBdService.replace(rSyForBd)
                                    }
                                    "Sy2Fl" -> {
                                        /** 系统服务于楼层 */
                                        val rSyForFl = RSyForFl()
                                        rSyForFl.sysId = objFrom.toString()
                                        rSyForFl.floorId = objTo.toString()
                                        rSyForFl.projectId = message.projectId
                                        rSyForFlService.replace(rSyForFl)
                                    }
                                    "Sy2Sh" -> {
                                        /** 系统服务于竖井 */
                                        val rSyForSh = RSyForSh()
                                        rSyForSh.graphCode = graphCode.toString()
                                        rSyForSh.sysId = objFrom.toString()
                                        rSyForSh.shaftId = objTo.toString()
                                        rSyForSh.type = relCode.toString()
                                        rSyForSh.projectId = message.projectId
                                        rSyForShService.replace(rSyForSh)
                                    }
                                    "Sy2Sp" -> {
                                        /** 系统服务于空间 */
                                        val rSy2Sp = RSy2Sp()
                                        rSy2Sp.type = "sy2sp_for"
                                        rSy2Sp.id1 = objFrom.toString()
                                        rSy2Sp.id2 = objTo.toString()
                                        rSy2Sp.projectId = message.projectId
                                        val containsKey = message.exts!!.containsKey("relValue")
                                        if (containsKey) {
                                            val relValue = message.exts!!["relValue"]
                                            rSy2Sp.zoneType  = relValue.toString()
                                        }
                                        if (rSy2Sp.zoneType.isNullOrEmpty()){
                                            val entity = zoneSpaceService.select(SFilter.eq("id", objTo.toString())).entity()
                                            if (entity!=null) {
                                                rSy2Sp.zoneType = entity.classCode
                                            }
                                        }
                                        rSy2SpService.replace(rSy2Sp)
                                    }
                                    "Eq2Bd" -> {
                                        /** 设备设施服务于建筑 */
                                        val rEqForBd = REqForBd()
                                        rEqForBd.projectId = message.projectId
                                        rEqForBd.equipId = objFrom.toString()
                                        rEqForBd.buildingId = objTo.toString()
                                        rEqForBd.graphCode = graphCode.toString()
                                        rEqForBd.type = relCode.toString()
                                        rEqForBdService.replace(rEqForBd)
                                    }
                                    "Eq2Fl" -> {
                                        /** 设备设施服务于楼层 */
                                        val rEqForFl = REqForFl()
                                        rEqForFl.equipId = objFrom.toString()
                                        rEqForFl.floorId = objTo.toString()
                                        rEqForFl.projectId = message.projectId
                                        rEqForFl.graphCode = graphCode.toString()
                                        rEqForFl.type = relCode.toString()
                                        rEqForFlService.replace(rEqForFl)
                                    }
                                    "Eq2Sh" -> {
                                        /** 设备设施服务于竖井 */
                                        val rEqForSh = REqForSh()
                                        rEqForSh.equipId = objFrom.toString()
                                        rEqForSh.shaftId = objTo.toString()
                                        rEqForSh.graphCode = graphCode.toString()
                                        rEqForSh.type = relCode.toString()
                                        rEqForSh.projectId = message.projectId
                                        rEqForShService.replace(rEqForSh)
                                    }
                                    "Eq2Sp" -> {
                                        /** 设备设施服务于空间 */
                                       val rEq2Sp = REq2Sp()
                                        rEq2Sp.graphCode = graphCode.toString()
                                        rEq2Sp.type = "eq2sp_for"
                                        rEq2Sp.id1 = objFrom.toString()
                                        rEq2Sp.id2 = objTo.toString()
                                        rEq2Sp.projectId = message.projectId
                                        val containsKey = message.exts!!.containsKey("relValue")
                                        if (containsKey) {
                                            val relValue = message.exts!!["relValue"]
                                            rEq2Sp.zoneType  = relValue.toString()
                                        }
                                        if (rEq2Sp.zoneType.isNullOrEmpty()){
                                            val entity = zoneSpaceService.select(SFilter.eq("id", objTo.toString())).entity()
                                            if (entity!=null) {
                                                rEq2Sp.zoneType = entity.classCode
                                            }
                                        }
                                        rEq2SpService.replace(rEq2Sp)
                                    }
                                }
                            }
                            "SpaceNeighborhood" -> {
                                /** 业务空间（同类）邻接关系 */
                                var type = ""
                                when (relCode) {
                                    "None" -> {
                                        /** 空间隔断无门窗 */
                                        type = "sp2sp_SpaceNeighborhood_1"
                                    }
                                    "Door" -> {
                                        /** 空间隔断有门 */
                                        type = "sp2sp_SpaceNeighborhood_2"
                                    }
                                    "Window" -> {
                                        /** 空间隔断有窗 */
                                        type = "sp2sp_SpaceNeighborhood_3"
                                    }
                                    "DoorWindow" -> {
                                        /** 空间隔断有门窗 */
                                        type = "sp2sp_SpaceNeighborhood_4"
                                    }
                                    "Connect" -> {
                                        /** 空间连通 */
                                        type = "sp2sp_SpaceNeighborhood_5"
                                    }
                                }
                                if (!type.isNullOrEmpty()) {
                                    val rSp2Sp = RSp2Sp()
                                    rSp2Sp.id1 = objFrom.toString()
                                    rSp2Sp.id2 = objTo.toString()
                                    rSp2Sp.projectId = message.projectId
                                    rSp2Sp.type = type
                                    val containsKey = message.exts!!.containsKey("relValue")
                                    if (containsKey) {
                                        val relValue = message.exts!!["relValue"]
                                        rSp2Sp.zoneType  = relValue.toString()
                                    }
                                    if (rSp2Sp.zoneType.isNullOrEmpty()){
                                        val entity = zoneSpaceService.select(SFilter.eq("id", objTo.toString())).entity()
                                        if (entity!=null) {
                                            rSp2Sp.zoneType = entity.classCode
                                        }
                                    }
                                    rSp2SpService.replace(rSp2Sp)
                                } else {
                                    /** 不做处理 */
                                }

                            }
                            "TrafficNetwork" -> {
                                /** 建筑交通网络（同类） */
                                var type = ""
                                if (relCode == "Normal"){
                                    /** 普通交通 */
                                    type = "sp2sp_TrafficNetwork_1"
                                }else if (relCode == "FFOpen") {
                                    /** 消防常开交通 */
                                    type = "sp2sp_TrafficNetwork_2"
                                }else if (relCode == "FFClose") {
                                    /** 消防常关交通 */
                                    type = "sp2sp_TrafficNetwork_3"
                                }

                                if (!type.isNullOrEmpty()) {
                                    val rSp2Sp = RSp2Sp()
                                    rSp2Sp.id1 = objFrom.toString()
                                    rSp2Sp.id2 = objTo.toString()
                                    rSp2Sp.projectId = message.projectId
                                    rSp2Sp.type = type
                                    val containsKey = message.exts!!.containsKey("relValue")
                                    if (containsKey) {
                                        val relValue = message.exts!!["relValue"]
                                        rSp2Sp.zoneType  = relValue.toString()
                                    }
                                    if (rSp2Sp.zoneType.isNullOrEmpty()){
                                        val entity = zoneSpaceService.select(SFilter.eq("id", objTo.toString())).entity()
                                        if (entity!=null) {
                                            rSp2Sp.zoneType = entity.classCode
                                        }
                                    }
                                    rSp2SpService.replace(rSp2Sp)
                                } else {
                                    /** 不做处理 */
                                }
                            }
                            "ConvectionNetwork" -> {
                                /** 空气流通网络（同类） */
                                var type = ""
                                if (relCode == "Natural"){
                                    /** 空气自然对流 */
                                    type = "sp2sp_ConvectionNetwork_1"
                                }else if (relCode == "OnewayMech") {
                                    /** 空气单向机械通风 */
                                    type = "sp2sp_ConvectionNetwork_3"
                                }else if (relCode == "MixMech") {
                                    /** 空气混合机械通风 */
                                    type = "sp2sp_ConvectionNetwork_2"
                                }
                                if (!type.isNullOrEmpty()) {
                                    val rSp2Sp = RSp2Sp()
                                    rSp2Sp.id1 = objFrom.toString()
                                    rSp2Sp.id2 = objTo.toString()
                                    rSp2Sp.projectId = message.projectId
                                    rSp2Sp.type = type
                                    val containsKey = message.exts!!.containsKey("relValue")
                                    if (containsKey) {
                                        val relValue = message.exts!!["relValue"]
                                        rSp2Sp.zoneType  = relValue.toString()
                                    }
                                    if (rSp2Sp.zoneType.isNullOrEmpty()){
                                        val entity = zoneSpaceService.select(SFilter.eq("id", objTo.toString())).entity()
                                        if (entity!=null) {
                                            rSp2Sp.zoneType = entity.classCode
                                        }
                                    }
                                    rSp2SpService.replace(rSp2Sp)
                                } else {
                                    /** 不做处理 */
                                }
                            }
                            "RadiationNetwork" -> {
                                /** 光照辐射网络（同类） */
                                var type = ""
                                if (relCode == "Connect"){
                                /** 光照连通 */
                                    type = "sp2sp_RadiationNetwork_1"
                                }else if (relCode == "Transparent"){
                                /** 光照透明隔断 */
                                    type = "sp2sp_RadiationNetwork_2"
                                }
                                if (!type.isNullOrEmpty()) {
                                    val rSp2Sp = RSp2Sp()
                                    rSp2Sp.id1 = objFrom.toString()
                                    rSp2Sp.id2 = objTo.toString()
                                    rSp2Sp.projectId = message.projectId
                                    rSp2Sp.type = type
                                    val containsKey = message.exts!!.containsKey("relValue")
                                    if (containsKey) {
                                        val relValue = message.exts!!["relValue"]
                                        rSp2Sp.zoneType  = relValue.toString()
                                    }
                                    if (rSp2Sp.zoneType.isNullOrEmpty()){
                                        val entity = zoneSpaceService.select(SFilter.eq("id", objTo.toString())).entity()
                                        if (entity!=null) {
                                            rSp2Sp.zoneType = entity.classCode
                                        }
                                    }
                                    rSp2SpService.replace(rSp2Sp)
                                } else {
                                    /** 不做处理 */
                                }
                            }
                            "LUDistribution" -> {
                                /** 低压配电网络 */
                                var type = ""
                                if (relCode == "Normal"){
                                    /** 低压配电普通连接 */
                                    type = "eq2eq_LUDistribution_1"
                                }else if (relCode == "Backup"){
                                    /** 低压配电互备连接 */
                                    type = "eq2eq_LUDistribution_2"
                                }
                                if (!type.isNullOrEmpty()) {
                                    val rEq2Eq = REq2Eq()
                                    rEq2Eq.id1 = objFrom.toString()
                                    rEq2Eq.id2 = objTo.toString()
                                    rEq2Eq.projectId = message.projectId
                                    rEq2Eq.type = type

                                    rEq2EqService.replace(rEq2Eq)
                                } else {
                                    /** 不做处理 */
                                }
                            }
                            "HUDistribution" -> {
                                /** 高压变配电网络 */
                                var type = ""
                                if (relCode == "Normal"){
                                    /** 高压变配电普通连接 */
                                    type = "eq2eq_HUDistribution_1"
                                }else if (relCode == "Backup"){
                                    /** 高压变配电互备连接 */
                                    type = "eq2eq_HUDistribution_2"
                                }
                                if (!type.isNullOrEmpty()) {
                                    val rEq2Eq = REq2Eq()
                                    rEq2Eq.id1 = objFrom.toString()
                                    rEq2Eq.id2 = objTo.toString()
                                    rEq2Eq.projectId = message.projectId
                                    rEq2Eq.type = type

                                    rEq2EqService.replace(rEq2Eq)
                                } else {
                                    /** 不做处理 */
                                }
                            }
                            "ChillWaterLoop" -> {
                                /** 空调冷冻水管网 */
                                var type = ""
                                if (relCode == "Supply"){
                                    /** 空调冷冻水供水 */
                                    type = "eq2eq_ChillWaterLoop_1"
                                }else if (relCode == "Return"){
                                    /** 空调冷冻水回水 */
                                    type = "eq2eq_ChillWaterLoop_2"
                                }else if (relCode == "Bypass"){
                                    /** 空调冷冻水旁通 */
                                    type = "eq2eq_ChillWaterLoop_3"
                                }else if (relCode == "Fill"){
                                    /** 空调冷冻水补水 */
                                    type = "eq2eq_ChillWaterLoop_4"
                                }
                                if (!type.isNullOrEmpty()) {
                                    val rEq2Eq = REq2Eq()
                                    rEq2Eq.id1 = objFrom.toString()
                                    rEq2Eq.id2 = objTo.toString()
                                    rEq2Eq.projectId = message.projectId
                                    rEq2Eq.type = type

                                    rEq2EqService.replace(rEq2Eq)
                                } else {
                                    /** 不做处理 */
                                }
                            }
                            "CoolingWaterLoop" -> {
                                /** 空调冷却水管网 */
                                var type = ""
                                if (relCode == "Supply"){
                                    /** 空调冷却水供水 */
                                    type = "eq2eq_CoolingWaterLoop_1"
                                }else if (relCode == "Return"){
                                    /** 空调冷却水回水 */
                                    type = "eq2eq_CoolingWaterLoop_2"
                                }else if (relCode == "Bypass"){
                                    /** 空调冷却水旁通 */
                                    type = "eq2eq_CoolingWaterLoop_3"
                                }else if (relCode == "Fill"){
                                    /** 空调冷却水补水 */
                                    type = "eq2eq_CoolingWaterLoop_4"
                                }
                                if (!type.isNullOrEmpty()) {
                                    val rEq2Eq = REq2Eq()
                                    rEq2Eq.id1 = objFrom.toString()
                                    rEq2Eq.id2 = objTo.toString()
                                    rEq2Eq.projectId = message.projectId
                                    rEq2Eq.type = type

                                    rEq2EqService.replace(rEq2Eq)
                                } else {
                                    /** 不做处理 */
                                }
                            }
                            "HeatWaterLoop" -> {
                                /** 采暖水管网 */
                                var type = ""
                                if (relCode == "Supply"){
                                    /** 采暖水供水 */
                                    type = "eq2eq_HeatWaterLoop_1"
                                }else if (relCode == "Return"){
                                    /** 采暖水回水 */
                                    type = "eq2eq_HeatWaterLoop_2"
                                }else if (relCode == "Bypass"){
                                    /** 采暖水旁通 */
                                    type = "eq2eq_HeatWaterLoop_3"
                                }else if (relCode == "Fill"){
                                    /** 采暖水补水 */
                                    type = "eq2eq_HeatWaterLoop_4"
                                }
                                if (!type.isNullOrEmpty()) {
                                    val rEq2Eq = REq2Eq()
                                    rEq2Eq.id1 = objFrom.toString()
                                    rEq2Eq.id2 = objTo.toString()
                                    rEq2Eq.projectId = message.projectId
                                    rEq2Eq.type = type

                                    rEq2EqService.replace(rEq2Eq)
                                } else {
                                    /** 不做处理 */
                                }
                            }
                            "CondWaterNetwork" -> {
                                /** 空调冷凝水管网 */
                                if (relCode == "Discharge"){
                                    /** 空调冷凝水排水 */
                                    val rEq2Eq = REq2Eq()
                                    rEq2Eq.id1 = objFrom.toString()
                                    rEq2Eq.id2 = objTo.toString()
                                    rEq2Eq.projectId = message.projectId
                                    rEq2Eq.type = "eq2eq_CondWaterNetwork_1"
                                    rEq2EqService.replace(rEq2Eq)
                                }
                            }
                            "ACAirNetwork" -> {
                                /** 空调风管网 */
                                var type = ""
                                if (relCode == "Supply"){
                                    /** 空调送风 */
                                    type = "eq2eq_ACAirNetwork_1"
                                }else if (relCode == "Return"){
                                    /** 空调回风 */
                                    type = "eq2eq_ACAirNetwork_2"
                                }else if (relCode == "Discharge"){
                                    /** 空调排风 */
                                    type = "eq2eq_ACAirNetwork_3"
                                }
                                if (!type.isNullOrEmpty()) {
                                    val rEq2Eq = REq2Eq()
                                    rEq2Eq.id1 = objFrom.toString()
                                    rEq2Eq.id2 = objTo.toString()
                                    rEq2Eq.projectId = message.projectId
                                    rEq2Eq.type = type

                                    rEq2EqService.replace(rEq2Eq)
                                } else {
                                    /** 不做处理 */
                                }
                            }
                            "FreshAirNetwork" -> {
                                /** 新风管网 */
                                if (relCode == "Fresh"){
                                    /** 新风送风 */
                                    val rEq2Eq = REq2Eq()
                                    rEq2Eq.id1 = objFrom.toString()
                                    rEq2Eq.id2 = objTo.toString()
                                    rEq2Eq.projectId = message.projectId
                                    rEq2Eq.type = "eq2eqFreshAirNetwork_1"
                                    rEq2EqService.replace(rEq2Eq)
                                }
                            }
                            "VentNetwork" -> {
                                /** 通风管网 */
                                var type = ""
                                if (relCode == "Supply"){
                                    /** 通风送风 */
                                    type = "eq2eq_VentNetwork_1"
                                }else if (relCode == "Discharge"){
                                    /** 通风排风 */
                                    type = "eq2eq_VentNetwork_2"
                                }else if (relCode == "Uncertain"){
                                    /** 通风不确定 */
                                    type = "eq2eq_VentNetwork_3"
                                }
                                if (!type.isNullOrEmpty()) {
                                    val rEq2Eq = REq2Eq()
                                    rEq2Eq.id1 = objFrom.toString()
                                    rEq2Eq.id2 = objTo.toString()
                                    rEq2Eq.projectId = message.projectId
                                    rEq2Eq.type = type

                                    rEq2EqService.replace(rEq2Eq)
                                } else {
                                    /** 不做处理 */
                                }
                            }
                            "VRFNetwork" -> {
                                /** 多联机管网 */
                                if (relCode == "SupplyReturn"){
                                    /** 多联机供回制冷剂 */
                                    val rEq2Eq = REq2Eq()
                                    rEq2Eq.id1 = objFrom.toString()
                                    rEq2Eq.id2 = objTo.toString()
                                    rEq2Eq.projectId = message.projectId
                                    rEq2Eq.type = "eq2eq_VRFNetwork_1"
                                    rEq2EqService.replace(rEq2Eq)
                                }
                            }
                            "DomesticWaterNetwork" -> {
                                /** 生活给水管网 */
                                var type = ""
                                if (relCode == "Supply"){
                                    /** 生活给水供水 */
                                    type = "eq2eq_SupplyWaterNetwork_1"
                                }else if (relCode == "Bypass"){
                                    /** 生活给水旁通 */
                                    type = "eq2eq_SupplyWaterNetwork_2"
                                }
                                if (!type.isNullOrEmpty()) {
                                    val rEq2Eq = REq2Eq()
                                    rEq2Eq.id1 = objFrom.toString()
                                    rEq2Eq.id2 = objTo.toString()
                                    rEq2Eq.projectId = message.projectId
                                    rEq2Eq.type = type

                                    rEq2EqService.replace(rEq2Eq)
                                } else {
                                    /** 不做处理 */
                                }
                            }
                            "DrainingWaterNetwork" -> {
                                /** 污水排水管网 */
                                var type = ""
                                if (relCode == "Discharge"){
                                    /** 污水排水 */
                                    type = "eq2eq_DrainingWaterNetwork_1"
                                }else if (relCode == "Bypass"){
                                    /** 污水旁通 */
                                    type = "eq2eq_DrainingWaterNetwork_2"
                                }
                                if (!type.isNullOrEmpty()) {
                                    val rEq2Eq = REq2Eq()
                                    rEq2Eq.id1 = objFrom.toString()
                                    rEq2Eq.id2 = objTo.toString()
                                    rEq2Eq.projectId = message.projectId
                                    rEq2Eq.type = type
                                    rEq2EqService.replace(rEq2Eq)
                                } else {
                                    /** 不做处理 */
                                }
                            }
                            "RecycleWaterNetwork" -> {
                                /** 中水管网 */
                                var type = ""
                                if (relCode == "Supply"){
                                    /** 中水供水 */
                                    type = "eq2eq_RecycleWaterNetwork_1"
                                }else if (relCode == "Bypass"){
                                    /** 中水旁通 */
                                    type = "eq2eq_RecycleWaterNetwork_2"
                                }
                                if (!type.isNullOrEmpty()) {
                                    val rEq2Eq = REq2Eq()
                                    rEq2Eq.id1 = objFrom.toString()
                                    rEq2Eq.id2 = objTo.toString()
                                    rEq2Eq.projectId = message.projectId
                                    rEq2Eq.type = type

                                    rEq2EqService.replace(rEq2Eq)
                                } else {
                                    /** 不做处理 */
                                }
                            }
                            "DrinkingWaterNetwork" -> {
                                /** 直饮水管网 */
                                var type = ""
                                if (relCode == "Supply"){
                                    /** 直饮水供水 */
                                    type = "eq2eq_DrinkingWaterNetwork_1"
                                }else if (relCode == "Bypass"){
                                    /** 直饮水旁通 */
                                    type = "eq2eq_DrinkingWaterNetwork_2"
                                }
                                if (!type.isNullOrEmpty()) {
                                    val rEq2Eq = REq2Eq()
                                    rEq2Eq.id1 = objFrom.toString()
                                    rEq2Eq.id2 = objTo.toString()
                                    rEq2Eq.projectId = message.projectId
                                    rEq2Eq.type = type

                                    rEq2EqService.replace(rEq2Eq)
                                } else {
                                    /** 不做处理 */
                                }
                            }
                            "DHWNetwork" -> {
                                /** 生活热水管网 */
                                var type = ""
                                if (relCode == "Supply"){
                                    /** 生活热水供水 */
                                    type = "eq2eq_DHWNetwork_1"
                                }else if (relCode == "Bypass"){
                                    /** 生活热水旁通 */
                                    type = "eq2eq_DHWNetwork_2"
                                }
                                if (!type.isNullOrEmpty()) {
                                    val rEq2Eq = REq2Eq()
                                    rEq2Eq.id1 = objFrom.toString()
                                    rEq2Eq.id2 = objTo.toString()
                                    rEq2Eq.projectId = message.projectId
                                    rEq2Eq.type = type
                                    rEq2EqService.replace(rEq2Eq)
                                } else {
                                    /** 不做处理 */
                                }
                            }
                            "GasNetwork" -> {
                                /** 燃气管网 */
                                var type = ""
                                if (relCode == "Supply"){
                                    /** 燃气供气 */
                                    type = "eq2eq_GasNetwork_1"
                                }else if (relCode == "Bypass"){
                                    /** 燃气旁通 */
                                    type = "eq2eq_GasNetwork_2"
                                }
                                if (!type.isNullOrEmpty()) {
                                    val rEq2Eq = REq2Eq()
                                    rEq2Eq.id1 = objFrom.toString()
                                    rEq2Eq.id2 = objTo.toString()
                                    rEq2Eq.projectId = message.projectId
                                    rEq2Eq.type = type
                                    rEq2EqService.replace(rEq2Eq)
                                } else {
                                    /** 不做处理 */
                                }
                            }
                            "FireWaterNetwork" -> {
                                /** 消防给水管网 */
                                var type = ""
                                if (relCode == "Supply"){
                                    /** 消防给水供水 */
                                    type = "eq2eq_FireWaterNetwork_1"
                                }else if (relCode == "Bypass"){
                                    /** 消防给水旁通 */
                                    type = "eq2eq_FireWaterNetwork_2"
                                }
                                if (!type.isNullOrEmpty()) {
                                    val rEq2Eq = REq2Eq()
                                    rEq2Eq.id1 = objFrom.toString()
                                    rEq2Eq.id2 = objTo.toString()
                                    rEq2Eq.projectId = message.projectId
                                    rEq2Eq.type = type
                                    rEq2EqService.replace(rEq2Eq)
                                } else {
                                    /** 不做处理 */
                                }
                            }
                            "FireVentNetwork" -> {
                                /** 消防排烟管网 */
                                if (relCode == "Discharge"){
                                    /** 消防排烟 */
                                    val rEq2Eq = REq2Eq()
                                    rEq2Eq.id1 = objFrom.toString()
                                    rEq2Eq.id2 = objTo.toString()
                                    rEq2Eq.projectId = message.projectId
                                    rEq2Eq.type = "eq2eq_FireVentNetwork_1"
                                    rEq2EqService.replace(rEq2Eq)
                                }
                            }
                            "WENetwork" -> {
                                /** 弱电通信网络 */
                                if (relCode == "Normal"){
                                    /** 弱电通信连接 */
                                    val rEq2Eq = REq2Eq()
                                    rEq2Eq.id1 = objFrom.toString()
                                    rEq2Eq.id2 = objTo.toString()
                                    rEq2Eq.projectId = message.projectId
                                    rEq2Eq.type = "eq2eq_SensorNetwork_1"
                                    rEq2EqService.replace(rEq2Eq)
                                }
                            }
                            "MechCtrl" -> {
                                /** 设备控制关系 */
                                var type = ""
                                if (relCode == "SyCtrl"){
                                    /** 系统控制关系 */
                                    type = "eq2eq_ControlRelation_2"
                                    val rEq2Sy = REq2Sy()
                                    rEq2Sy.id1 = objFrom.toString()
                                    rEq2Sy.projectId = message.projectId
                                    rEq2Sy.sign = 1
                                    rEq2Sy.id2 = objTo.toString()
                                    rEq2Sy.type = "eq2eq_ControlRelation_2"
                                    rEq2SyService.replace(rEq2Sy)
                                }else if (relCode == "EqCtrl"){
                                    /** 设备控制关系 */
                                    type = "eq2eq_ControlRelation_1"
                                    val rEq2Eq = REq2Eq()
                                    rEq2Eq.id1 = objFrom.toString()
                                    rEq2Eq.id2 = objTo.toString()
                                    rEq2Eq.projectId = message.projectId
                                    rEq2Eq.type = "eq2eq_ControlRelation_1"
                                    rEq2EqService.replace(rEq2Eq)
                                }
                            }
                            "MechPower" -> {
                                /** 设备供电关系 */
                                if (relCode == "SyNormal"){
                                    /** 系统常规电源 */
                                    val rEq2Sy = REq2Sy()
                                    rEq2Sy.id1 = objFrom.toString()
                                    rEq2Sy.projectId = message.projectId
                                    rEq2Sy.sign = 1
                                    rEq2Sy.id2 = objTo.toString()
                                    rEq2Sy.type = "eq2eq_EquipPower_3"
                                    rEq2SyService.replace(rEq2Sy)
                                }else if (relCode == "SyBackup"){
                                    /** 系统备用电源 */
                                    val rEq2Sy = REq2Sy()
                                    rEq2Sy.id1 = objFrom.toString()
                                    rEq2Sy.projectId = message.projectId
                                    rEq2Sy.sign = 1
                                    rEq2Sy.id2 = objTo.toString()
                                    rEq2Sy.type = "eq2eq_EquipPower_4"
                                    rEq2SyService.replace(rEq2Sy)
                                }else if (relCode == "EqNormal"){
                                    /** 设备常规电源 */
                                    val rEq2Eq = REq2Eq()
                                    rEq2Eq.id1 = objFrom.toString()
                                    rEq2Eq.id2 = objTo.toString()
                                    rEq2Eq.projectId = message.projectId
                                    rEq2Eq.type = "eq2eq_EquipPower_1"
                                    rEq2EqService.replace(rEq2Eq)
                                }else if (relCode == "EqBackup"){
                                    /** 设备备用电源  */
                                    val rEq2Eq = REq2Eq()
                                    rEq2Eq.id1 = objFrom.toString()
                                    rEq2Eq.id2 = objTo.toString()
                                    rEq2Eq.projectId = message.projectId
                                    rEq2Eq.type = "eq2eq_EquipPower_2"
                                    rEq2EqService.replace(rEq2Eq)
                                }
                            }
                            "SensorRelationship" -> {
                                /** 传感器关系 */
                                if (relCode == "Ss2Eq"){
                                    /** 传感器测量设备关系 */
                                    val rEq2Eq = REq2Eq()
                                    rEq2Eq.id1 = objFrom.toString()
                                    rEq2Eq.id2 = objTo.toString()
                                    rEq2Eq.projectId = message.projectId
                                    rEq2Eq.type = "eq2eq_SensorRelationship_ss2eq"
                                    rEq2EqService.replace(rEq2Eq)
                                }else if (relCode == "Ss2Sy"){
                                    /** 传感器测量系统关系 */
                                    val rEq2Sy = REq2Sy()
                                    rEq2Sy.id1 = objFrom.toString()
                                    rEq2Sy.projectId = message.projectId
                                    rEq2Sy.sign = 1
                                    rEq2Sy.id2 = objTo.toString()
                                    rEq2Sy.type = "eq2sy_SensorRelationship_ss2sy"
                                    rEq2SyService.replace(rEq2Sy)
                                }else if (relCode == "Ss2Sp"){
                                    /** 传感器测量空间关系 */
                                    val rEq2Sp = REq2Sp()
                                    rEq2Sp.graphCode = graphCode.toString()
                                    rEq2Sp.type = "eq2sp_SensorRelationship_ss2sp"
                                    rEq2Sp.id1 = objFrom.toString()
                                    rEq2Sp.id2 = objTo.toString()
                                    rEq2Sp.projectId = message.projectId
                                    val containsKey = message.exts!!.containsKey("relValue")
                                    if (containsKey) {
                                        val relValue = message.exts!!["relValue"]
                                        rEq2Sp.zoneType  = relValue.toString()
                                    }
                                    if (rEq2Sp.zoneType.isNullOrEmpty()){
                                        val entity = zoneSpaceService.select(SFilter.eq("id", objTo.toString())).entity()
                                        if (entity!=null) {
                                            rEq2Sp.zoneType = entity.classCode
                                        }
                                    }
                                    rEq2SpService.replace(rEq2Sp)
                                }

                            }
                            "ValveRelationship" -> {
                                /** 阀门关系 */
                                if (relCode == "Vv2Eq"){
                                    /** 阀门限制设备关系 */
                                    val rEq2Eq = REq2Eq()
                                    rEq2Eq.id1 = objFrom.toString()
                                    rEq2Eq.id2 = objTo.toString()
                                    rEq2Eq.projectId = message.projectId
                                    rEq2Eq.type = "eq2eq_ValveRelationship_vv2eq"
                                    rEq2EqService.replace(rEq2Eq)
                                }else if (relCode == "Vv2Sy"){
                                    /** 阀门限制系统关系 */
                                    val rEq2Sy = REq2Sy()
                                    rEq2Sy.id1 = objFrom.toString()
                                    rEq2Sy.projectId = message.projectId
                                    rEq2Sy.sign = 1
                                    rEq2Sy.id2 = objTo.toString()
                                    rEq2Sy.type = "eq2sy_ValveRelationship_vv2sy"
                                    rEq2SyService.replace(rEq2Sy)
                                }
                            }
                        }
                    } catch (e: Exception) {
                        e.printStackTrace()
                        message.state = 2
                        message.count = message.count +1
                    }

                }else{
                    message.state = 2
                    message.count = message.count +1
                }

            }else{
                message.state = 2
                message.count = message.count +1
            }
            syncRwdMessageService.update(message, arrayListOf("state","count"))
        } catch (e: Exception) {
            e.printStackTrace()
            message.state = 2
            message.count = message.count +1
            syncRwdMessageService.update(message, arrayListOf("state","count"))
        }
    }


}