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

package com.persagy.server.syn.controllers.obj

import com.persagy.base.extensions.toJson
import com.persagy.database.SFilter
import com.persagy.database.functions.SLength
import com.persagy.mybatis.SMybatisDao
import com.persagy.server.datacenter.models.entities.dictnew.DictProject
import com.persagy.server.datacenter.models.entities.objects.Equipment
import com.persagy.server.datacenter.models.entities.objects.Floor
import com.persagy.server.datacenter.models.entities.rel.*
import com.persagy.server.syn.Configure
import com.persagy.server.syn.models.api.obj.EquipApi
import com.persagy.service.SBaseService
import com.persagy.service.SObjectService
import com.persagy.service.models.enums.SResponseType
import com.persagy.service.models.responses.SBaseResponse
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import org.slf4j.LoggerFactory
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

/**
 * 关系数据-数据中心同步到数据中台
 *
 * @author wx  <zhangweixin@sagacloud.com>
 * @date  2020/11/26 14:52
 */
@Tag(name = "0004、关系-数据中心到中台")
@RestController
@RequestMapping("/datacenter/object/sync")
class RelToFromController {

    companion object {
        /** 日志 */
        private val logger = LoggerFactory.getLogger(ObjectToFromController::class.java)
    } // Companion object

    /** 设备服务楼层 */
    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 equipmentService = SObjectService(SMybatisDao(Equipment::class.java))
    /** 数据中心楼层服务 */
    private val floorService = SObjectService(SMybatisDao(Floor::class.java))
    /** 字典项目服务 */
    private val dictProjectService = SBaseService(SMybatisDao(DictProject::class.java))

    /**
     * 设备和楼层关系-同步到中台
     *
     * @param projectId     项目id
     * @param graphCode     图类型
     * @param type          边类型
     * @param isDel         是否删除以前的关系
     * @return 返回是否成功结果
     */
    @Operation(summary= "设备和楼层关系-同步到中台", description = "设备所在的楼层(图类型 MechInArch 边类型 Eq2Fl )，设备服务的楼层（图类型 MechForArch 边类型 Eq2Fl）, ")
    @PostMapping("/eq2fl")
    fun eq2Fl(projectId: String,graphCode: String,type: String,isDel: Boolean = false): SBaseResponse {
        try {
            val dictProject = dictProjectService.select(SFilter.eq("id", projectId)).entity()
            if (graphCode == "MechInArch") {
                val equipmentList = equipmentService.select(SFilter.eq("projectId", projectId)).exec()
                if (equipmentList.size>0) {
                    for (equipment in equipmentList) {
                        val toJsonObjArray =
                                Configure.toJsonObjArray(graphCode, type, equipment.id!!, equipment.floorId!!)
                        if (isDel) {
                            val toJsonObj = Configure.toJsonObj(graphCode, type, equipment.id!!, null)
                            EquipApi.delRel(dictProject!!.groupCode!!, projectId,toJsonObj)
                        }
                        val createRel = EquipApi.createRel(dictProject!!.groupCode!!, projectId, toJsonObjArray)
                        if (!createRel){
                            logger.debug("设备所在楼层的关系失败：${toJsonObjArray.toJson()}")
                        }
                    }
                }
            } else if (graphCode == "MechForArch") {
                val rEqForFlList = rEqForFlService.select(SFilter.eq("projectId", projectId)).exec()
                if (rEqForFlList.size>0) {
                    for (rEqForFl in rEqForFlList) {
                        if (isDel) {
                            val toJsonObj = Configure.toJsonObj(graphCode, type, rEqForFl.equipId!!, null)
                            EquipApi.delRel(dictProject!!.groupCode!!, projectId,toJsonObj)
                        }
                        val toJsonObjArray =
                                Configure.toJsonObjArray(graphCode, type, rEqForFl.equipId!!, rEqForFl.floorId!!)
                        val createRel = EquipApi.createRel(dictProject!!.groupCode!!, projectId, toJsonObjArray)
                        if (!createRel){
                            logger.debug("设备服务楼层的关系失败：${toJsonObjArray.toJson()}")
                        }
                    }
                }
            }
            return SBaseResponse(SResponseType.success)
        } catch (e: Exception) {
            e.printStackTrace()
            return SBaseResponse(SResponseType.failure,e.message!!)
        }
    }

    /**
     * 设备和建筑关系-同步到中台
     *
     * @param projectId     项目id
     * @param graphCode     图类型
     * @param type          边类型
     * @param isDel         是否删除以前的关系
     * @return 返回是否成功结果
     */
    @Operation(summary= "设备和建筑关系-同步到中台", description = "设备所在的建筑(图类型 MechInArch 边类型 Eq2Bd )，设备服务的建筑（图类型 MechForArch 边类型 Eq2Bd）, ")
    @PostMapping("/eq2bd")
    fun eq2Bd(projectId: String,graphCode: String,type: String,isDel: Boolean = false): SBaseResponse {
        try {
            val dictProject = dictProjectService.select(SFilter.eq("id", projectId)).entity()
            if (graphCode == "MechInArch") {
                val equipmentList = equipmentService.select(SFilter.eq("projectId", projectId)).exec()
                if (equipmentList.size>0) {
                    for (equipment in equipmentList) {
                        val toJsonObjArray =
                                Configure.toJsonObjArray(graphCode, type, equipment.id!!, equipment.buildingId!!)
                        if (isDel) {
                            val toJsonObj = Configure.toJsonObj(graphCode, type, equipment.id!!, null)
                            EquipApi.delRel(dictProject!!.groupCode!!, projectId,toJsonObj)
                        }
                        val createRel = EquipApi.createRel(dictProject!!.groupCode!!, projectId, toJsonObjArray)
                        if (!createRel){
                            logger.debug("设备所在建筑的关系失败：${toJsonObjArray.toJson()}")
                        }
                    }
                }
            } else if (graphCode == "MechForArch") {
                val rEqForFlList = rEqForBdService.select(SFilter.eq("projectId", projectId)).exec()
                if (rEqForFlList.size>0) {
                    for (rEqForFl in rEqForFlList) {
                        if (isDel) {
                            val toJsonObj = Configure.toJsonObj(graphCode, type, rEqForFl.equipId!!, null)
                            EquipApi.delRel(dictProject!!.groupCode!!, projectId,toJsonObj)
                        }
                        val toJsonObjArray =
                                Configure.toJsonObjArray(graphCode, type, rEqForFl.equipId!!, rEqForFl.buildingId!!)
                        val createRel = EquipApi.createRel(dictProject!!.groupCode!!, projectId, toJsonObjArray)
                        if (!createRel){
                            logger.debug("设备服务建筑的关系失败：${toJsonObjArray.toJson()}")
                        }
                    }
                }
            }
            return SBaseResponse(SResponseType.success)
        } catch (e: Exception) {
            e.printStackTrace()
            return SBaseResponse(SResponseType.failure,e.message!!)
        }
    }


    /**
     * 设备和系统关系
     */
    @Operation(summary= "设备和系统关系-同步到中台", description = "系统下设备(图类型 MechSubset 边类型 Sy2Eq )")
    @PostMapping("/sy2eq")
    fun sy2Eq(projectId: String,graphCode: String,type: String,isDel: Boolean = false): SBaseResponse {
        try {
            val dictProject = dictProjectService.select(SFilter.eq("id", projectId)).entity()
            val rSyEqList = rSyEqService.select(
                    SFilter.eq("projectId", projectId),
                    SFilter.eq("graphCode", graphCode),
                    SFilter.eq("type", type)
            ).exec()
            if (rSyEqList.size>0){
                for (rSyEq in rSyEqList){
                    if (isDel) {
                        val toJsonObj = Configure.toJsonObj(graphCode, type, rSyEq.sysId!!, null)
                        EquipApi.delRel(dictProject!!.groupCode!!, projectId,toJsonObj)
                    }
                    val toJsonObjArray =
                            Configure.toJsonObjArray(graphCode, type, rSyEq.sysId!!, rSyEq.equipId!!)
                    val createRel = EquipApi.createRel(dictProject!!.groupCode!!, projectId, toJsonObjArray)
                    if (!createRel){
                        logger.debug("系统下的设备关系失败：${toJsonObjArray.toJson()}")
                    }
                }
            }
            return SBaseResponse(SResponseType.success)
        } catch (e: Exception) {
            e.printStackTrace()
            return SBaseResponse(SResponseType.failure,e.message!!)
        }

    }

    /**
     * 设备和竖井关系
     */
    @Operation(summary= "设备和竖井关系-同步到中台", description = "设备所在的竖井(图类型 MechInArch 边类型 Eq2Sh )，设备服务的竖井（图类型 MechForArch 边类型 Eq2Sh）, ")
    @PostMapping("/eq2sh")
    fun eq2Sh(projectId: String,graphCode: String,type: String,isDel: Boolean = false): SBaseResponse {
        try {
            val dictProject = dictProjectService.select(SFilter.eq("id", projectId)).entity()
            if (graphCode == "MechInArch") {
                val rEqInShList = rEqInShService.select(SFilter.eq("projectId", projectId)).exec()
                if (rEqInShList.size>0) {
                    for (rEqInSh in rEqInShList) {
                        if (isDel) {
                            val toJsonObj = Configure.toJsonObj(graphCode, type, rEqInSh.equipId!!, null)
                            EquipApi.delRel(dictProject!!.groupCode!!, projectId,toJsonObj)
                        }
                        val toJsonObjArray =
                                Configure.toJsonObjArray(graphCode, type, rEqInSh.equipId!!, rEqInSh.shaftId!!)
                        val createRel = EquipApi.createRel(dictProject!!.groupCode!!, projectId, toJsonObjArray)
                        if (!createRel){
                            logger.debug("设备所在竖井关系失败：${toJsonObjArray.toJson()}")
                        }
                    }
                }
            } else if (graphCode == "MechForArch") {
                val rEqForShList = rEqForShService.select(SFilter.eq("projectId", projectId)).exec()
                if (rEqForShList.size>0 ) {
                    for (rEqForSh in rEqForShList) {
                        if (isDel) {
                            val toJsonObj = Configure.toJsonObj(graphCode, type, rEqForSh.equipId!!, null)
                            EquipApi.delRel(dictProject!!.groupCode!!, projectId,toJsonObj)
                        }
                        val toJsonObjArray =
                                Configure.toJsonObjArray(graphCode, type, rEqForSh.equipId!!, rEqForSh.shaftId!!)
                        val createRel = EquipApi.createRel(dictProject!!.groupCode!!, projectId, toJsonObjArray)
                        if (!createRel){
                            logger.debug("设备服务的竖井关系失败：${toJsonObjArray.toJson()}")
                        }
                    }
                }
            }
            return SBaseResponse(SResponseType.success)
        } catch (e: Exception) {
            e.printStackTrace()
            return SBaseResponse(SResponseType.failure,e.message!!)
        }
    }

    /**
     * 设备和空间关系
     */
    @Operation(summary= "设备和空间关系-同步到中台", description = "设备所在的空间(图类型 MechInArch 边类型 Eq2Sp )，设备服务的空间（图类型 MechForArch 边类型 Eq2Sp）, ")
    @PostMapping("/eq2sp")
    fun eq2Sp(projectId: String,graphCode: String,type: String,isDel: Boolean = false): SBaseResponse {
        try {
            val dictProject = dictProjectService.select(SFilter.eq("id", projectId)).entity()
            val rEq2SpList = rEq2SpService.select(
                    SFilter.eq("projectId", projectId),
                    SFilter.eq("graphCode", graphCode),
                    SFilter.eq("type", type)
            ).exec()
            if (rEq2SpList.size>0) {
                for (rEq2Sp in rEq2SpList) {
                    if (isDel) {
                        val toJsonObj = Configure.toJsonObj(graphCode, type, rEq2Sp.id1!!, null)
                        EquipApi.delRel(dictProject!!.groupCode!!, projectId,toJsonObj)
                    }
                    val toJsonObjArray =
                            Configure.toJsonObjArray(graphCode, type, rEq2Sp.id1!!, rEq2Sp.id2!!)
                    val createRel = EquipApi.createRel(dictProject!!.groupCode!!, projectId, toJsonObjArray)
                    if (!createRel){
                        logger.debug("设备和空间关系失败：${toJsonObjArray.toJson()}")
                    }
                }
            }
            return SBaseResponse(SResponseType.success)
        } catch (e: Exception) {
            e.printStackTrace()
            return SBaseResponse(SResponseType.failure,e.message!!)
        }
    }

    /**
     * 设备和部件关系
     */
    @Operation(summary= "设备和部件关系-同步到中台", description = "设备下部件(图类型 MechSubset 边类型 Eq2Ec )")
    @PostMapping("/eq2ec")
    fun eq2Ec(projectId: String,graphCode: String,type: String,isDel: Boolean = false): SBaseResponse {
        try {
            val dictProject = dictProjectService.select(SFilter.eq("id", projectId)).entity()
            val equipmentList =
                    equipmentService.select(SFilter.eq("projectId", projectId), SFilter.lte(SLength("classCode"), 8)).exec()
            if (equipmentList.size>0) {
                for (equipment in equipmentList) {
                    if (!equipment.parentId.isNullOrEmpty()){
                        if (isDel) {
                            val toJsonObj = Configure.toJsonObj(graphCode, type, equipment.parentId!!, null)
                            EquipApi.delRel(dictProject!!.groupCode!!, projectId,toJsonObj)
                        }
                        val toJsonObjArray =
                                Configure.toJsonObjArray(graphCode, type, equipment.parentId!!, equipment.id!!)
                        val createRel = EquipApi.createRel(dictProject!!.groupCode!!, projectId, toJsonObjArray)
                        if (!createRel){
                            logger.debug("设备和空间关系失败：${toJsonObjArray.toJson()}")
                        }
                    }
                }
            }

            return SBaseResponse(SResponseType.success)
        } catch (e: Exception) {
            e.printStackTrace()
            return SBaseResponse(SResponseType.failure,e.message!!)
        }
    }

    /**
     * 系统所在的竖井，系统服务的竖井
     */
    @Operation(summary= "系统和竖井关系-同步到中台", description = "系统所在的竖井(图类型 MechInArch 边类型 Sy2Sh )，系统服务于竖井（图类型MechForArch  边类型 Sy2Sh ）")
    @PostMapping("/sy2sh")
    fun sy2Sh(projectId: String,graphCode: String,type: String,isDel: Boolean = false): SBaseResponse{
        try {
            val dictProject = dictProjectService.select(SFilter.eq("id", projectId)).entity()
            if (graphCode == "MechInArch") {
                val syInShList = syInShService.select(SFilter.eq("projectId", projectId)).exec()
                if (syInShList.size>0) {

                    for (syInSh in syInShList) {
                        if (isDel) {
                            val toJsonObj = Configure.toJsonObj(graphCode, type, syInSh.sysId!!, null)
                            EquipApi.delRel(dictProject!!.groupCode!!, projectId,toJsonObj)
                        }
                        val toJsonObjArray =
                                Configure.toJsonObjArray(graphCode, type, syInSh.sysId!!, syInSh.shaftId!!)
                        val createRel = EquipApi.createRel(dictProject!!.groupCode!!, projectId, toJsonObjArray)
                        if (!createRel){
                            logger.debug("系统所在竖井失败：${toJsonObjArray.toJson()}")
                        }
                    }
                }

            } else if (graphCode == "MechForArch"){
                val rSyForShList = rSyForShService.select(SFilter.eq("projectId", projectId)).exec()
                if (rSyForShList.size>0) {
                    for (rSyForSh in rSyForShList ) {
                        if (isDel) {
                            val toJsonObj = Configure.toJsonObj(graphCode, type, rSyForSh.sysId!!, null)
                            EquipApi.delRel(dictProject!!.groupCode!!, projectId,toJsonObj)
                        }
                        val toJsonObjArray =
                                Configure.toJsonObjArray(graphCode, type, rSyForSh.sysId!!, rSyForSh.shaftId!!)
                        val createRel = EquipApi.createRel(dictProject!!.groupCode!!, projectId, toJsonObjArray)
                        if (!createRel){
                            logger.debug("系统服务于竖井失败：${toJsonObjArray.toJson()}")
                        }
                    }
                }
            }
            return SBaseResponse(SResponseType.success)
        } catch (e: Exception) {
            e.printStackTrace()
            return SBaseResponse(SResponseType.failure,e.message!!)
        }

    }

    /**
     * 竖井下的业务空间
     */
    @Operation(summary= "竖井下的业务空间关系-同步到中台", description = "竖井下的业务空间(图类型 ArchSubset 边类型 Sh2Sp )")
    @PostMapping("/sh2sp")
    fun sh2Sp(projectId: String,graphCode: String,type: String,isDel: Boolean = false): SBaseResponse {
        try {
            val dictProject = dictProjectService.select(SFilter.eq("id", projectId)).entity()
            val rSh2SpList = rSh2SpService.select(SFilter.eq("projectId", projectId)).exec()
            if (rSh2SpList.size>0) {
                for (rSh2Sp in rSh2SpList) {
                    if (isDel) {
                        val toJsonObj = Configure.toJsonObj(graphCode, type, rSh2Sp.id1!!, null)
                        EquipApi.delRel(dictProject!!.groupCode!!, projectId,toJsonObj)
                    }
                    val toJsonObjArray =
                            Configure.toJsonObjArray(graphCode, type, rSh2Sp.id1!!, rSh2Sp.id2!!)
                    val createRel = EquipApi.createRel(dictProject!!.groupCode!!, projectId, toJsonObjArray)
                    if (!createRel){
                        logger.debug("竖井下的业务空间：${toJsonObjArray.toJson()}")
                    }
                }
            }

            return SBaseResponse(SResponseType.success)
        } catch (e: Exception) {
            e.printStackTrace()
            return SBaseResponse(SResponseType.failure,e.message!!)
        }
    }
    /**
     * 建筑下的竖井
     */
    @Operation(summary= "建筑体下的竖井关系-同步到中台", description = "建筑体下的竖井(图类型 ArchSubset 边类型 Bd2Sh )")
    @PostMapping("/bd2sh")
    fun bd2sh(projectId: String,graphCode: String,type: String,isDel: Boolean = false): SBaseResponse {
        try {
            val dictProject = dictProjectService.select(SFilter.eq("id", projectId)).entity()
            val rShInBdList = rShInBdService.select(SFilter.eq("projectId", projectId)).exec()
            if (rShInBdList.size>0){
                for (rShInBd in rShInBdList) {
                    if (isDel) {
                        val toJsonObj = Configure.toJsonObj(graphCode, type, rShInBd.buildingId!!, null)
                        EquipApi.delRel(dictProject!!.groupCode!!, projectId,toJsonObj)
                    }
                    val toJsonObjArray =
                            Configure.toJsonObjArray(graphCode, type, rShInBd.buildingId!!, rShInBd.shaftId!!)
                    val createRel = EquipApi.createRel(dictProject!!.groupCode!!, projectId, toJsonObjArray)
                    if (!createRel){
                        logger.debug("建筑体下的竖井：${toJsonObjArray.toJson()}")
                    }

                }
            }

            return SBaseResponse(SResponseType.success)
        } catch (e: Exception) {
            e.printStackTrace()
            return SBaseResponse(SResponseType.failure,e.message!!)
        }
    }

    /**
     * 竖井贯通关系
     */
    @Operation(summary= "竖井贯通关系-同步到中台", description = "竖井贯通关系(图类型 ThroughRelationship 边类型 Sh2Sh )")
    @PostMapping("/sh2sh")
    fun sh2sh(projectId: String,graphCode: String,type: String,isDel: Boolean = false): SBaseResponse {
        try {
            val dictProject = dictProjectService.select(SFilter.eq("id", projectId)).entity()
            val rShaftThroughShaftList = rShaftThroughShaftService.select(SFilter.eq("projectId", projectId)).exec()
            if (rShaftThroughShaftList.size>0){
                for (rShaftThroughShaft in rShaftThroughShaftList) {
                    if (isDel) {
                        val toJsonObj = Configure.toJsonObj(graphCode, type, rShaftThroughShaft.shaftId!!, null)
                        EquipApi.delRel(dictProject!!.groupCode!!, projectId,toJsonObj)
                    }
                    val toJsonObjArray =
                            Configure.toJsonObjArray(graphCode, type, rShaftThroughShaft.shaftId!!, rShaftThroughShaft.shaftOtherId!!)
                    val createRel = EquipApi.createRel(dictProject!!.groupCode!!, projectId, toJsonObjArray)
                    if (!createRel){
                        logger.debug("竖井贯通关系：${toJsonObjArray.toJson()}")
                    }
                }
            }
            return SBaseResponse(SResponseType.success)
        } catch (e: Exception) {
            e.printStackTrace()
            return SBaseResponse(SResponseType.failure,e.message!!)
        }
    }

    /**
     * 系统所在的楼层，系统服务于楼层
     */
    @Operation(summary= "系统和楼层关系-同步到中台", description = "系统所在的楼层(图类型 MechInArch 边类型 Sy2Fl )，系统服务于楼层（图类型MechForArch  边类型 Sy2Fl ）")
    @PostMapping("/sy2fl")
    fun sy2fl(projectId: String,graphCode: String,type: String,isDel: Boolean = false): SBaseResponse{
        try {
            val dictProject = dictProjectService.select(SFilter.eq("id", projectId)).entity()
            if (graphCode == "MechInArch") {
                val syInShList = rSyInFlService.select(SFilter.eq("projectId", projectId)).exec()
                if (syInShList.size>0) {

                    for (syInSh in syInShList) {
                        if (isDel) {
                            val toJsonObj = Configure.toJsonObj(graphCode, type, syInSh.sysId!!, null)
                            EquipApi.delRel(dictProject!!.groupCode!!, projectId,toJsonObj)
                        }
                        val toJsonObjArray =
                                Configure.toJsonObjArray(graphCode, type, syInSh.sysId!!, syInSh.floorId!!)
                        val createRel = EquipApi.createRel(dictProject!!.groupCode!!, projectId, toJsonObjArray)
                        if (!createRel){
                            logger.debug("系统所在的楼层失败：${toJsonObjArray.toJson()}")
                        }
                    }
                }

            } else if (graphCode == "MechForArch"){
                val rSyForShList = rSyForFlService.select(SFilter.eq("projectId", projectId)).exec()
                if (rSyForShList.size>0) {
                    for (rSyForSh in rSyForShList ) {
                        if (isDel) {
                            val toJsonObj = Configure.toJsonObj(graphCode, type, rSyForSh.sysId!!, null)
                            EquipApi.delRel(dictProject!!.groupCode!!, projectId,toJsonObj)
                        }
                        val toJsonObjArray =
                                Configure.toJsonObjArray(graphCode, type, rSyForSh.sysId!!, rSyForSh.floorId!!)
                        val createRel = EquipApi.createRel(dictProject!!.groupCode!!, projectId, toJsonObjArray)
                        if (!createRel){
                            logger.debug("系统服务于楼层失败：${toJsonObjArray.toJson()}")
                        }
                    }
                }
            }
            return SBaseResponse(SResponseType.success)
        } catch (e: Exception) {
            e.printStackTrace()
            return SBaseResponse(SResponseType.failure,e.message!!)
        }
    }

    /**
     * 系统所在的建筑体，系统服务于建筑体
     */
    @Operation(summary= "系统和楼层关系-同步到中台", description = "系统所在的建筑体(图类型 MechInArch 边类型 Sy2Bd )，系统服务于建筑体（图类型MechForArch  边类型 Sy2Bd ）")
    @PostMapping("/sy2bd")
    fun sy2bd(projectId: String,graphCode: String,type: String,isDel: Boolean = false): SBaseResponse{
        try {
            val dictProject = dictProjectService.select(SFilter.eq("id", projectId)).entity()
            if (graphCode == "MechInArch") {
                val rSyInBdList = rSyInBdService.select(SFilter.eq("projectId", projectId)).exec()
                if (rSyInBdList.size>0) {

                    for (rSyInBd in rSyInBdList) {
                        if (isDel) {
                            val toJsonObj = Configure.toJsonObj(graphCode, type, rSyInBd.sysId!!, null)
                            EquipApi.delRel(dictProject!!.groupCode!!, projectId,toJsonObj)
                        }
                        val toJsonObjArray =
                                Configure.toJsonObjArray(graphCode, type, rSyInBd.sysId!!, rSyInBd.buildingId!!)
                        val createRel = EquipApi.createRel(dictProject!!.groupCode!!, projectId, toJsonObjArray)
                        if (!createRel){
                            logger.debug("系统所在的建筑体失败：${toJsonObjArray.toJson()}")
                        }
                    }
                }

            } else if (graphCode == "MechForArch"){
                val rSyForBdList = rSyForBdService.select(SFilter.eq("projectId", projectId)).exec()
                if (rSyForBdList.size>0) {
                    for (rSyForBd in rSyForBdList ) {
                        if (isDel) {
                            val toJsonObj = Configure.toJsonObj(graphCode, type, rSyForBd.sysId!!, null)
                            EquipApi.delRel(dictProject!!.groupCode!!, projectId,toJsonObj)
                        }
                        val toJsonObjArray =
                                Configure.toJsonObjArray(graphCode, type, rSyForBd.sysId!!, rSyForBd.buildingId!!)
                        val createRel = EquipApi.createRel(dictProject!!.groupCode!!, projectId, toJsonObjArray)
                        if (!createRel){
                            logger.debug("系统服务于建筑体失败：${toJsonObjArray.toJson()}")
                        }
                    }
                }
            }
            return SBaseResponse(SResponseType.success)
        } catch (e: Exception) {
            e.printStackTrace()
            return SBaseResponse(SResponseType.failure,e.message!!)
        }
    }


    /**
     * 建筑下的楼层
     */
    @Operation(summary= "建筑体下的楼层-同步到中台", description = "建筑体下的楼层(图类型 ArchSubset 边类型 Bd2Fl )")
    @PostMapping("/bd2fl")
    fun bd2fl(projectId: String,graphCode: String,type: String,isDel: Boolean = false): SBaseResponse {
        try {
            val dictProject = dictProjectService.select(SFilter.eq("id", projectId)).entity()
            val floorList = floorService.select(SFilter.eq("projectId", projectId)).exec()
            if (floorList.size>0){
                for (floor in floorList) {
                    if (isDel) {
                        val toJsonObj = Configure.toJsonObj(graphCode, type, floor.buildingId!!, null)
                        EquipApi.delRel(dictProject!!.groupCode!!, projectId,toJsonObj)
                    }
                    val toJsonObjArray =
                            Configure.toJsonObjArray(graphCode, type, floor.buildingId!!, floor.id!!)
                    val createRel = EquipApi.createRel(dictProject!!.groupCode!!, projectId, toJsonObjArray)
                    if (!createRel){
                        logger.debug("建筑体下的楼层关系：${toJsonObjArray.toJson()}")
                    }
                }
            }
            return SBaseResponse(SResponseType.success)
        } catch (e: Exception) {
            e.printStackTrace()
            return SBaseResponse(SResponseType.failure,e.message!!)
        }
    }


    /**
     * 楼层下的业务空间
     */
    @Operation(summary= "楼层下的业务空间-同步到中台", description = "楼层下的业务空间(图类型 ArchSubset 边类型 Fl2Sp )")
    @PostMapping("/fl2sp")
    fun fl2sp(projectId: String,graphCode: String,type: String,isDel: Boolean = false): SBaseResponse {
        try {
            val dictProject = dictProjectService.select(SFilter.eq("id", projectId)).entity()
            val rSpinFlList = rSpinFlService.select(SFilter.eq("projectId", projectId)).exec()
            if (rSpinFlList.size>0){
                for (rSpinFl in rSpinFlList) {
                    if (isDel) {
                        val toJsonObj = Configure.toJsonObj(graphCode, type, rSpinFl.floorId!!, null)
                        EquipApi.delRel(dictProject!!.groupCode!!, projectId,toJsonObj)
                    }
                    val toJsonObjArray =
                            Configure.toJsonObjArray(graphCode, type, rSpinFl.floorId!!, rSpinFl.spaceId!!)
                    val createRel = EquipApi.createRel(dictProject!!.groupCode!!, projectId, toJsonObjArray)
                    if (!createRel){
                        logger.debug("楼层下的业务空间关系：${toJsonObjArray.toJson()}")
                    }
                }
            }
            return SBaseResponse(SResponseType.success)
        } catch (e: Exception) {
            e.printStackTrace()
            return SBaseResponse(SResponseType.failure,e.message!!)
        }
    }

    /**
     * 建筑体下的业务空间
     */
    @Operation(summary= "建筑体下的业务空间-同步到中台", description = "建筑体下的业务空间(图类型 ArchSubset 边类型 Bd2Sp )")
    @PostMapping("/bd2sp")
    fun bd2sp(projectId: String,graphCode: String,type: String,isDel: Boolean = false): SBaseResponse {
        try {
            val dictProject = dictProjectService.select(SFilter.eq("id", projectId)).entity()
            val rSpinBdList = rSpinBdService.select(SFilter.eq("projectId", projectId)).exec()
            if (rSpinBdList.size>0){
                for (rSpinBd in rSpinBdList) {
                    if (isDel) {
                        val toJsonObj = Configure.toJsonObj(graphCode, type, rSpinBd.buildingId!!, null)
                        EquipApi.delRel(dictProject!!.groupCode!!, projectId,toJsonObj)
                    }
                    val toJsonObjArray =
                            Configure.toJsonObjArray(graphCode, type, rSpinBd.buildingId!!, rSpinBd.spaceId!!)
                    val createRel = EquipApi.createRel(dictProject!!.groupCode!!, projectId, toJsonObjArray)
                    if (!createRel){
                        logger.debug("建筑体下的业务空间关系：${toJsonObjArray.toJson()}")
                    }
                }
            }
            return SBaseResponse(SResponseType.success)
        } catch (e: Exception) {
            e.printStackTrace()
            return SBaseResponse(SResponseType.failure,e.message!!)
        }
    }

    /**
     * 楼层贯通关系
     */
    @Operation(summary= "楼层贯通关系-同步到中台", description = "楼层贯通关系(图类型 ThroughRelationship 边类型 Fl2Fl )")
    @PostMapping("/fl2fl")
    fun fl2fl(projectId: String,graphCode: String,type: String,isDel: Boolean = false): SBaseResponse {
        try {
            val dictProject = dictProjectService.select(SFilter.eq("id", projectId)).entity()
            val rFlThroughFlList = rFlThroughFlService.select(SFilter.eq("projectId", projectId)).exec()
            if (rFlThroughFlList.size>0){
                for (rFlThroughFl in rFlThroughFlList) {
                    if (isDel) {
                        val toJsonObj = Configure.toJsonObj(graphCode, type, rFlThroughFl.floorId!!, null)
                        EquipApi.delRel(dictProject!!.groupCode!!, projectId,toJsonObj)
                    }
                    val toJsonObjArray =
                            Configure.toJsonObjArray(graphCode, type, rFlThroughFl.floorId!!, rFlThroughFl.floorOtherId!!)
                    val createRel = EquipApi.createRel(dictProject!!.groupCode!!, projectId, toJsonObjArray)
                    if (!createRel){
                        logger.debug("楼层贯通关系关系：${toJsonObjArray.toJson()}")
                    }
                }
            }
            return SBaseResponse(SResponseType.success)
        } catch (e: Exception) {
            e.printStackTrace()
            return SBaseResponse(SResponseType.failure,e.message!!)
        }
    }

    /**
     * 系统所在业务空间 ,系统服务于业务空间   Sy2Sp
     */
    @Operation(summary= "系统和业务空间关系-同步到中台", description = "系统所在业务空间(图类型 MechInArch 边类型 Sy2Bd )，系统服务于业务空间（图类型MechForArch  边类型 Sy2Bd ）")
    @PostMapping("/sy2sp")
    fun sy2sp(projectId: String,graphCode: String,type: String,isDel: Boolean = false): SBaseResponse{
        try {
            val dictProject = dictProjectService.select(SFilter.eq("id", projectId)).entity()
            if (graphCode == "MechInArch") {
                val rSy2SpList = rSy2SpService.select(SFilter.eq("projectId", projectId),
                        SFilter.eq("graphCode", graphCode),
                        SFilter.eq("type", type)).exec()
                if (rSy2SpList.size>0) {

                    for (rSy2Sp in rSy2SpList) {
                        if (isDel) {
                            val toJsonObj = Configure.toJsonObj(graphCode, type, rSy2Sp.id1!!, null)
                            EquipApi.delRel(dictProject!!.groupCode!!, projectId,toJsonObj)
                        }
                        val toJsonObjArray =
                                Configure.toJsonObjArray(graphCode, type, rSy2Sp.id1!!, rSy2Sp.id2!!)
                        val createRel = EquipApi.createRel(dictProject!!.groupCode!!, projectId, toJsonObjArray)
                        if (!createRel){
                            logger.debug("系统所在业务空间：${toJsonObjArray.toJson()}")
                        }
                    }
                }

            } else if (graphCode == "MechForArch"){
                val rSy2SpList = rSy2SpService.select(SFilter.eq("id", projectId),
                        SFilter.eq("graphCode", graphCode),
                        SFilter.eq("type", type)).exec()
                if (rSy2SpList.size>0) {
                    for (rSy2Sp in rSy2SpList ) {
                        if (isDel) {
                            val toJsonObj = Configure.toJsonObj(graphCode, type, rSy2Sp.id1!!, null)
                            EquipApi.delRel(dictProject!!.groupCode!!, projectId,toJsonObj)
                        }
                        val toJsonObjArray =
                                Configure.toJsonObjArray(graphCode, type, rSy2Sp.id1!!, rSy2Sp.id2!!)
                        val createRel = EquipApi.createRel(dictProject!!.groupCode!!, projectId, toJsonObjArray)
                        if (!createRel){
                            logger.debug("系统服务于业务空间：${toJsonObjArray.toJson()}")
                        }
                    }
                }
            }
            return SBaseResponse(SResponseType.success)
        } catch (e: Exception) {
            e.printStackTrace()
            return SBaseResponse(SResponseType.failure,e.message!!)
        }
    }

    /**
     * 空间和空间的关系
     */
    @Operation(summary= "空间和空间的关系-同步到中台", description = "")
    @PostMapping("/sp2sp")
    fun sp2sp(projectId: String,graphCode: String,type: String,isDel: Boolean = false): SBaseResponse {
        try {
            val dictProject = dictProjectService.select(SFilter.eq("id", projectId)).entity()
            val rSp2SpList = rSp2SpService.select(SFilter.eq("projectId", projectId),
                    SFilter.eq("graphCode", graphCode),
                    SFilter.eq("type", type)).exec()
            if (rSp2SpList.size>0) {
                for (rSp2Sp in rSp2SpList) {
                    if (isDel) {
                        val toJsonObj = Configure.toJsonObj(graphCode, type, rSp2Sp.id1!!, null)
                        EquipApi.delRel(dictProject!!.groupCode!!, projectId,toJsonObj)
                    }
                    val toJsonObjArray =
                            Configure.toJsonObjArray(graphCode, type, rSp2Sp.id1!!, rSp2Sp.id2!!)
                    val createRel = EquipApi.createRel(dictProject!!.groupCode!!, projectId, toJsonObjArray)
                    if (!createRel){
                        logger.debug("空间和空间的关系：${toJsonObjArray.toJson()}")
                    }
                }
            }
            return SBaseResponse(SResponseType.success)
        } catch (e: Exception) {
            e.printStackTrace()
            return SBaseResponse(SResponseType.failure,e.message!!)
        }
    }

    /**
     * 设备和设备的关系
     */
    @Operation(summary= "设备和设备的关系-同步到中台", description = "")
    @PostMapping("/eq2eq")
    fun eq2eq(projectId: String,graphCode: String,type: String,isDel: Boolean = false) : SBaseResponse {
        try {
            val dictProject = dictProjectService.select(SFilter.eq("id", projectId)).entity()
            val rEq2EqList = rEq2EqService.select(SFilter.eq("projectId", projectId),
                    SFilter.eq("graphCode", graphCode),
                    SFilter.eq("type", type)).exec()
            if (rEq2EqList.size>0) {
                for (rEq2Eq in rEq2EqList) {
                    if (isDel) {
                        val toJsonObj = Configure.toJsonObj(graphCode, type, rEq2Eq.id1!!, null)
                        EquipApi.delRel(dictProject!!.groupCode!!, projectId,toJsonObj)
                    }
                    val toJsonObjArray =
                            Configure.toJsonObjArray(graphCode, type, rEq2Eq.id1!!, rEq2Eq.id2!!)
                    val createRel = EquipApi.createRel(dictProject!!.groupCode!!, projectId, toJsonObjArray)
                    if (!createRel){
                        logger.debug("空间和空间的关系：${toJsonObjArray.toJson()}")
                    }
                }
            }
            return SBaseResponse(SResponseType.success)
        } catch (e: Exception) {
            e.printStackTrace()
            return SBaseResponse(SResponseType.failure,e.message!!)
        }
    }

    /**
     * 所有关系同步到数据中台
     */
    @Operation(summary= "所有关系-同步到中台", description = "")
    @PostMapping("/all/rel")
    fun allRel(projectId: String,graphCode: String,type: String,isDel: Boolean = false) {


    }

}