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

import RelBuilding
import com.persagy.base.extensions.toJson
import com.persagy.database.SFilter
import com.persagy.mybatis.SMybatisDao
import com.persagy.server.Opt
import com.persagy.server.datacenter.datacenter.dao.mappers.SchemeMapper
import com.persagy.server.datacenter.datacenter.models.entities.dictnew.DefClass
import com.persagy.server.datacenter.models.entities.graphtype.RelationTypeProject
import com.persagy.server.datacenter.datacenter.models.entities.graphtype.rel.RelZoneSpace
import com.persagy.server.datacenter.wanda.rel.REq2Sy
import com.persagy.server.datacenter.models.entities.graphtype.rel.*
import com.persagy.server.datacenter.models.entities.rel.*
import com.persagy.server.datacenter.models.requests.ManualRelationCalcDel
import com.persagy.server.datacenter.datacenter.models.requests.ManualRelationCalcRequest
import com.persagy.server.datacenter.models.entities.graphtype.rel.RelFloor
import com.persagy.server.datacenter.services.objects.EquipmentService
import com.persagy.server.datacenter.wanda.obj.Building
import com.persagy.server.datacenter.wanda.obj.Equipment
import com.persagy.server.datacenter.wanda.obj.Floor
import com.persagy.server.datacenter.wanda.rel.*
import com.persagy.server.datacenter.wanda.rel.RelGeneralSystem
import com.persagy.server.datacenter.wanda.response.ManualRelationCalcResponse
import com.persagy.server.jms.RabbitMqService
import com.persagy.service.SObjectService
import com.persagy.service.SPageContext
import com.persagy.service.models.SCascadeQuery
import com.persagy.service.models.enums.SResponseType
import com.persagy.service.models.requests.SQueryRequest
import com.persagy.service.models.responses.SBaseResponse
import com.persagy.service.utils.SSpringContextUtil
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service
import java.text.SimpleDateFormat
import java.util.*
import kotlin.collections.ArrayList
import kotlin.collections.HashSet

/**
 * 手动计算
 *
 * @author 张维新
 */
@Service
open class ManualRelationCalcService {
    companion object {
        /** 日志 */
        private val logger = LoggerFactory.getLogger(ManualRelationCalcService::class.java)
        /** RabbitMQ消息对象 */
        val rabbitMqService by lazy {
            SSpringContextUtil.getBean(RabbitMqService::class.java) as RabbitMqService
        }

        val schemeMapper by lazy {
            SSpringContextUtil.getBean(SchemeMapper::class.java) as SchemeMapper
        }

    } // Companion object
//    /** 资产服务 */
//    private val relPropertyService = SObjectService(SMybatisDao(RelProperty::class.java))


    /** 设备建筑 设备 */
    private val relEquipBuildingService = SObjectService(SMybatisDao(RelEq2BdEquip::class.java))
    /** 设备级联楼层 */
    private val relEquipFloorService = SObjectService(SMybatisDao(RelEq2FlEquip::class.java))
    /** 关系建筑 */
    private val relBuildingService = SObjectService(SMybatisDao(RelBuilding::class.java))
    /** 关系楼层 */
    private val relFloorService = SObjectService(SMybatisDao(RelFloor::class.java))
    /** 建筑和空间的关系-建筑 */
    private val rBd2SpBuilidingService = SObjectService(SMybatisDao(RBd2SpBuiliding::class.java))
    /** 建筑和空间的关系-楼层 */
    private val rFl2SpFloorService = SObjectService(SMybatisDao(RFl2SpFloor::class.java))
    /** 空间 */
    private val spaceBaseZoneService = SObjectService(SMybatisDao(SpaceBaseZone::class.java))
    /** 空间带有建筑和楼层名 */
    private val spaceBaseBuildingFloorService = SObjectService(SMybatisDao(RelZoneSpaceBd::class.java))
    /** 空间和空间的关系 */
    private val rSp2SpService = SObjectService(SMybatisDao(RSp2Sp::class.java))
    /** 空间和空间 - 空间 */
    private val relSp2SpSpaceService = SObjectService(SMybatisDao(RelSp2SpSpace::class.java))
    /** 关系和对象数据结合 */
    private val rSp2SpTwoService = SObjectService(SMybatisDao(RSp2SpTwo::class.java))
    /** 系统和设备-系统 */
    private val relSy2EqSystemService = SObjectService(SMybatisDao(RelSy2EqSystem::class.java))
    /**系统下的设备 */
    private val rEq2SyTwoService = SObjectService(SMybatisDao(REq2SyTwo::class.java))
    /** 设备和空间 */
    private val rEq2SpTwoService = SObjectService(SMybatisDao(REq2SpTwo::class.java))
    /** 设备所在的空间-设备 */
    private val relEq2SpEquipService = SObjectService(SMybatisDao(RelEq2SpEquip::class.java))
    /** 设备和空间关系 */
    private val rEq2SpService = SObjectService(SMybatisDao(REq2Sp::class.java))


    /** 资产所在的空间 *******************************************************/
    private val rPeInSpBaseService = SObjectService(SMybatisDao(RPe2Sp::class.java))
    /** 设备服务于竖井 */
    private val rEq2ShService = SObjectService(SMybatisDao(REq2Sh::class.java))
    /** 设备服务于楼层 */
    private val rEq2FlService = SObjectService(SMybatisDao(REq2Fl::class.java))
    /** 系统和空间关系 */
    private val rSy2SpService = SObjectService(SMybatisDao(RSy2Sp::class.java))
    /** 系统和竖井关系 */
    private val rSy2ShService = SObjectService(SMybatisDao(RSy2Sh::class.java))
    /** 系统和楼层关系 */
    private val rSy2FlService = SObjectService(SMybatisDao(RSy2Fl::class.java))
    /** 系统和建筑关系 */
    private val rSy2BdService = SObjectService(SMybatisDao(RSy2Bd::class.java))
    /** 设备和系统关系 */
    private val rEq2SyService = SObjectService(SMybatisDao(REq2Sy::class.java))

    /** 设备和设备关系 */
    private val rEq2EqService = SObjectService(SMybatisDao(REq2Eq::class.java))



    /** 空间 */
    private val relZoneSpaceService = SObjectService(SMybatisDao(RelZoneSpace::class.java))
    /** 类型查询 */
    private val dClassDefService = SObjectService(SMybatisDao(DefClass::class.java))
    /** 资产所在的竖井 */
    private val rPeInShService = SObjectService(SMybatisDao(RPeInSh::class.java))
    /** 资产和竖井的资产实例 */
    private val relPropertyShaftService = SObjectService(SMybatisDao(RelPropertyShaft::class.java))
    /** 竖井 */
    private val relShaftService = SObjectService(SMybatisDao(RelShaft::class.java))
    /** 设备和竖井的设备 */
    private val relEquipmentShaftService = SObjectService(SMybatisDao(RelEquipmentShaft::class.java))
    /** 设备和楼层的设备 */
    private val  relEquipmentFloorService = SObjectService(SMybatisDao(RelEquipmentFloor::class.java))
    /** 设备服务于建筑 设备 */
    private val relEquipmentBuildingService = SObjectService(SMybatisDao(RelEquipmentBuilding::class.java))
    /** 系统服务于业务空间 */
    private val relGeneralSystemService = SObjectService(SMybatisDao(RelGeneralSystem::class.java))
    /** 系统服务于竖井 */
    private val relGeneralSystemShaftService = SObjectService(SMybatisDao(RelGeneralSystemShaft::class.java))
    /** 系统服务于楼层 */
    private val relGeneralSystemFloorService = SObjectService(SMybatisDao(RelGeneralSystemFloor::class.java))
    /** 系统服务于建筑 */
    private val relGeneralSystemBuildingService = SObjectService(SMybatisDao(RelGeneralSystemBuilding::class.java))


    fun  queryRel(manualRelationCalcRequest: ManualRelationCalcRequest): ManualRelationCalcResponse {

        if (manualRelationCalcRequest.graphicType == "ArchSubset" && manualRelationCalcRequest.relType == "Bd2Sp") {
            return queryBd2Sp(manualRelationCalcRequest)
            } else if (manualRelationCalcRequest.graphicType == "ArchSubset" && manualRelationCalcRequest.relType == "Fl2Sp") {
            return queryFl2Sp(manualRelationCalcRequest)
            } else if (manualRelationCalcRequest.graphicType == "MechInArch" && manualRelationCalcRequest.relType == "Eq2Bd") {
                return  queryEq2Bd(manualRelationCalcRequest)
            } else if (manualRelationCalcRequest.graphicType == "MechInArch" && manualRelationCalcRequest.relType == "Eq2Fl") {
                return queryEq2Fl(manualRelationCalcRequest)
            } else if (manualRelationCalcRequest.graphicType == "ArchForArch" && manualRelationCalcRequest.relType == "Sp2Sp") {
            return querySp2Sp(manualRelationCalcRequest)
            } else if (manualRelationCalcRequest.graphicType == "MechSubset" && manualRelationCalcRequest.relType == "Sy2Eq") {
            return querySy2Eq(manualRelationCalcRequest)
            } else if (manualRelationCalcRequest.graphicType == "MechInArch" && manualRelationCalcRequest.relType == "Eq2Sp") {
            return queryEq2Sp(manualRelationCalcRequest)
            }else if (manualRelationCalcRequest.graphicType == "MechForArch" && manualRelationCalcRequest.relType == "Eq2Sp"){
            return queryEq2Sp(manualRelationCalcRequest)
            }
        return ManualRelationCalcResponse()

    }

    /**
     * 设备所在建筑查询
     */
    fun queryEq2Bd(manualRelationCalcRequest: ManualRelationCalcRequest): ManualRelationCalcResponse {
        val sQueryRequest = SQueryRequest()
        if(manualRelationCalcRequest.pageNumber>0&&manualRelationCalcRequest.pageSize>0){
            SQueryRequest.maxRow = 400000
            sQueryRequest.pageSize= manualRelationCalcRequest.pageSize
            sQueryRequest.pageNumber = manualRelationCalcRequest.pageNumber
        }else{
            /** 控制条数 */
            SQueryRequest.maxRow = 100000
            sQueryRequest.pageSize= 100000
        }

        val sCascadeQueryList = ArrayList<SCascadeQuery>()
        /** 级联资产 */
        val sCascadeQuery = SCascadeQuery()
        sCascadeQuery.name = "objectInfo"
        sCascadeQueryList.add(sCascadeQuery)
        sQueryRequest.cascade = sCascadeQueryList

        val manualRelationCalcResponse = ManualRelationCalcResponse()
        manualRelationCalcResponse.direction = "Left"
        val list = ArrayList<SFilter>()
        list.add(SFilter.eq("projectId",Opt.projectId!!))
        if (manualRelationCalcRequest.objectType == "1") {

            list.add(SFilter.not(SFilter.isNull("buildingId")))
            /** 模糊查询 */
            if(!manualRelationCalcRequest.vagueFrom.isNullOrEmpty()){
                list.add( SFilter.or(SFilter.contain("id",manualRelationCalcRequest.vagueFrom!!),SFilter.contain("name",manualRelationCalcRequest.vagueFrom!!)
                        ,SFilter.contain("localName",manualRelationCalcRequest.vagueFrom!!),SFilter.contain("localId",manualRelationCalcRequest.vagueFrom!!)))
            }
            if (!manualRelationCalcRequest.categoryFrom.isNullOrEmpty()) {
                list.add(SFilter.eq("classCode",manualRelationCalcRequest.categoryFrom!!))
            }
            val pageQuery = relEquipBuildingService.pageQuery(sQueryRequest,list)
            manualRelationCalcResponse.content = pageQuery.content
            manualRelationCalcResponse.message = pageQuery.message
            manualRelationCalcResponse.pageNumber = pageQuery.pageNumber
            manualRelationCalcResponse.pageSize = pageQuery.pageSize
            manualRelationCalcResponse.result = pageQuery.result
            manualRelationCalcResponse.total = pageQuery.total

            return manualRelationCalcResponse
        }else if (manualRelationCalcRequest.objectType == "2"){

            if (!manualRelationCalcRequest.vagueFrom.isNullOrEmpty()) {
                val relEquipBuildingList = relEquipBuildingService.select(SFilter.eq("projectId", Opt.projectId!!), SFilter.not(SFilter.isNull("buildingId"))).exec()
                if (relEquipBuildingList.size>0) {

                    val buildingIdList = schemeMapper.buildingId(Opt.projectId!!)
                    val listId = ArrayList<String>()
                    for (buildingId in buildingIdList) {
                        listId.add(buildingId.id!!)
                    }

                    val relBuildinglist = relBuildingService.select(SFilter.inList("id", listId), SFilter.contain("localName", manualRelationCalcRequest.vagueTo!!)).exec()
                    if (relBuildinglist.size>0) {
                        val listIds = ArrayList<String>()
                        for (building in relBuildinglist) {
                            listIds.add(building.id!!)
                        }
                        list.add(SFilter.inList("buildingId",listIds))
                        val pageQuery = relEquipBuildingService.pageQuery(sQueryRequest,list)
                        manualRelationCalcResponse.content = pageQuery.content
                        manualRelationCalcResponse.message = pageQuery.message
                        manualRelationCalcResponse.pageNumber = pageQuery.pageNumber
                        manualRelationCalcResponse.pageSize = pageQuery.pageSize
                        manualRelationCalcResponse.result = pageQuery.result
                        manualRelationCalcResponse.total = pageQuery.total

                        return manualRelationCalcResponse
                    }else {
                        manualRelationCalcResponse.content = ArrayList()
                        manualRelationCalcResponse.result = SResponseType.success
                    }
                }else {
                    manualRelationCalcResponse.content = ArrayList()
                    manualRelationCalcResponse.result = SResponseType.success
                }
            }else {
                manualRelationCalcResponse.content = ArrayList()
                manualRelationCalcResponse.result = SResponseType.success
            }

        }else if (manualRelationCalcRequest.objectType == "3") {

            val listType = ArrayList<SFilter>()
            /** 模糊查询 */
            if(!manualRelationCalcRequest.vagueFrom.isNullOrEmpty()){
                listType.add( SFilter.or(SFilter.contain("id",manualRelationCalcRequest.vagueFrom!!),SFilter.contain("name",manualRelationCalcRequest.vagueFrom!!)
                        ,SFilter.contain("localName",manualRelationCalcRequest.vagueFrom!!),SFilter.contain("localId",manualRelationCalcRequest.vagueFrom!!)))
            }
            if (!manualRelationCalcRequest.categoryFrom.isNullOrEmpty()) {
                listType.add(SFilter.eq("classCode",manualRelationCalcRequest.categoryFrom!!))
            }
            listType.add(SFilter.not(SFilter.isNull("buildingId")))
            val relEquipBuildingList = relEquipBuildingService.select(listType).exec()
            if (relEquipBuildingList.size>0) {

                val relBuildinglist = relBuildingService.select(SFilter.eq("projectId", Opt.projectId!!), SFilter.contain("localName", manualRelationCalcRequest.vagueTo!!)).exec()
                if (relBuildinglist.size>0) {
                    val listIds = ArrayList<String>()
                    for (building in relBuildinglist) {
                        listIds.add(building.id!!)
                    }
                    val listEquipIds = ArrayList<String>()
                    for (relEquipBuilding in relEquipBuildingList) {
                        listEquipIds.add(relEquipBuilding.id!!)
                    }

                    list.add(SFilter.inList("buildingId",listIds))
                    list.add(SFilter.inList("id",listEquipIds))
                    val pageQuery = relEquipBuildingService.pageQuery(sQueryRequest,list)
                    manualRelationCalcResponse.content = pageQuery.content
                    manualRelationCalcResponse.message = pageQuery.message
                    manualRelationCalcResponse.pageNumber = pageQuery.pageNumber
                    manualRelationCalcResponse.pageSize = pageQuery.pageSize
                    manualRelationCalcResponse.result = pageQuery.result
                    manualRelationCalcResponse.total = pageQuery.total
                    return manualRelationCalcResponse
                }else {
                    manualRelationCalcResponse.content = ArrayList()
                    manualRelationCalcResponse.result = SResponseType.success
                }


            }else {
                manualRelationCalcResponse.content = ArrayList()
                manualRelationCalcResponse.result = SResponseType.success
            }

        }
        return  manualRelationCalcResponse

    }

    /**
     * 设备和楼层
     */
    fun queryEq2Fl(manualRelationCalcRequest: ManualRelationCalcRequest): ManualRelationCalcResponse {
        val sQueryRequest = SQueryRequest()
        if(manualRelationCalcRequest.pageNumber>0&&manualRelationCalcRequest.pageSize>0){
            SQueryRequest.maxRow = 400000
            sQueryRequest.pageSize= manualRelationCalcRequest.pageSize
            sQueryRequest.pageNumber = manualRelationCalcRequest.pageNumber
        }else{
            /** 控制条数 */
            SQueryRequest.maxRow = 100000
            sQueryRequest.pageSize= 100000
        }

        val sCascadeQueryList = ArrayList<SCascadeQuery>()
        /** 级联资产 */
        val sCascadeQuery = SCascadeQuery()
        sCascadeQuery.name = "objectInfo"
        sCascadeQueryList.add(sCascadeQuery)
        sQueryRequest.cascade = sCascadeQueryList

        val manualRelationCalcResponse = ManualRelationCalcResponse()
        manualRelationCalcResponse.direction = "Left"
        val list = ArrayList<SFilter>()
        list.add(SFilter.eq("projectId",Opt.projectId!!))
        if (manualRelationCalcRequest.objectType == "1") {

            list.add(SFilter.not(SFilter.isNull("floorId")))
            /** 模糊查询 */
            if(!manualRelationCalcRequest.vagueFrom.isNullOrEmpty()){
                list.add( SFilter.or(SFilter.contain("id",manualRelationCalcRequest.vagueFrom!!),SFilter.contain("name",manualRelationCalcRequest.vagueFrom!!)
                        ,SFilter.contain("localName",manualRelationCalcRequest.vagueFrom!!),SFilter.contain("localId",manualRelationCalcRequest.vagueFrom!!)))
            }
            if (!manualRelationCalcRequest.categoryFrom.isNullOrEmpty()) {
                list.add(SFilter.eq("classCode",manualRelationCalcRequest.categoryFrom!!))
            }
            val pageQuery = relEquipFloorService.pageQuery(sQueryRequest,list)
            manualRelationCalcResponse.content = pageQuery.content
            manualRelationCalcResponse.message = pageQuery.message
            manualRelationCalcResponse.pageNumber = pageQuery.pageNumber
            manualRelationCalcResponse.pageSize = pageQuery.pageSize
            manualRelationCalcResponse.result = pageQuery.result
            manualRelationCalcResponse.total = pageQuery.total

            return manualRelationCalcResponse
        }else if (manualRelationCalcRequest.objectType == "2"){

            if (!manualRelationCalcRequest.vagueFrom.isNullOrEmpty()) {
                val relEquipBuildingList = relEquipFloorService.select(SFilter.eq("projectId", Opt.projectId!!), SFilter.not(SFilter.isNull("floorId"))).exec()
                if (relEquipBuildingList.size>0) {

                    val buildingIdList = schemeMapper.floorId(Opt.projectId!!)
                    val listId = ArrayList<String>()
                    for (buildingId in buildingIdList) {
                        listId.add(buildingId.id!!)
                    }

                    val relBuildinglist = relFloorService.select(SFilter.inList("id", listId), SFilter.contain("localName", manualRelationCalcRequest.vagueTo!!)).exec()
                    if (relBuildinglist.size>0) {
                        val listIds = ArrayList<String>()
                        for (building in relBuildinglist) {
                            listIds.add(building.id!!)
                        }
                        list.add(SFilter.inList("floorId",listIds))
                        val pageQuery = relEquipFloorService.pageQuery(sQueryRequest,list)
                        manualRelationCalcResponse.content = pageQuery.content
                        manualRelationCalcResponse.message = pageQuery.message
                        manualRelationCalcResponse.pageNumber = pageQuery.pageNumber
                        manualRelationCalcResponse.pageSize = pageQuery.pageSize
                        manualRelationCalcResponse.result = pageQuery.result
                        manualRelationCalcResponse.total = pageQuery.total

                        return manualRelationCalcResponse
                    }else {
                        manualRelationCalcResponse.content = ArrayList()
                        manualRelationCalcResponse.result = SResponseType.success
                    }
                }else {
                    manualRelationCalcResponse.content = ArrayList()
                    manualRelationCalcResponse.result = SResponseType.success
                }
            }else {
                manualRelationCalcResponse.content = ArrayList()
                manualRelationCalcResponse.result = SResponseType.success
            }

        }else if (manualRelationCalcRequest.objectType == "3") {

            val listType = ArrayList<SFilter>()
            /** 模糊查询 */
            if(!manualRelationCalcRequest.vagueFrom.isNullOrEmpty()){
                listType.add( SFilter.or(SFilter.contain("id",manualRelationCalcRequest.vagueFrom!!),SFilter.contain("name",manualRelationCalcRequest.vagueFrom!!)
                        ,SFilter.contain("localName",manualRelationCalcRequest.vagueFrom!!),SFilter.contain("localId",manualRelationCalcRequest.vagueFrom!!)))
            }
            if (!manualRelationCalcRequest.categoryFrom.isNullOrEmpty()) {
                listType.add(SFilter.eq("classCode",manualRelationCalcRequest.categoryFrom!!))
            }
            listType.add(SFilter.not(SFilter.isNull("floorId")))
            val relEquipBuildingList = relEquipFloorService.select(listType).exec()
            if (relEquipBuildingList.size>0) {

                val relBuildinglist = relFloorService.select(SFilter.eq("projectId", Opt.projectId!!), SFilter.contain("localName", manualRelationCalcRequest.vagueTo!!)).exec()
                if (relBuildinglist.size>0) {
                    val listIds = ArrayList<String>()
                    for (building in relBuildinglist) {
                        listIds.add(building.id!!)
                    }
                    val listEquipIds = ArrayList<String>()
                    for (relEquipBuilding in relEquipBuildingList) {
                        listEquipIds.add(relEquipBuilding.id!!)
                    }

                    list.add(SFilter.inList("floorId",listIds))
                    list.add(SFilter.inList("id",listEquipIds))
                    val pageQuery = relEquipFloorService.pageQuery(sQueryRequest,list)
                    manualRelationCalcResponse.content = pageQuery.content
                    manualRelationCalcResponse.message = pageQuery.message
                    manualRelationCalcResponse.pageNumber = pageQuery.pageNumber
                    manualRelationCalcResponse.pageSize = pageQuery.pageSize
                    manualRelationCalcResponse.result = pageQuery.result
                    manualRelationCalcResponse.total = pageQuery.total
                    return manualRelationCalcResponse
                }else {
                    manualRelationCalcResponse.content = ArrayList()
                    manualRelationCalcResponse.result = SResponseType.success
                }


            }else {
                manualRelationCalcResponse.content = ArrayList()
                manualRelationCalcResponse.result = SResponseType.success
            }

        }
        return  manualRelationCalcResponse
    }

    /**
     * 建筑下的空间
     */
    fun queryBd2Sp(manualRelationCalcRequest: ManualRelationCalcRequest): ManualRelationCalcResponse{
        val sQueryRequest = SQueryRequest()
        if(manualRelationCalcRequest.pageNumber>0&&manualRelationCalcRequest.pageSize>0){
            SQueryRequest.maxRow = 400000
            sQueryRequest.pageSize= manualRelationCalcRequest.pageSize
            sQueryRequest.pageNumber = manualRelationCalcRequest.pageNumber
        }else{
            /** 控制条数 */
            SQueryRequest.maxRow = 100000
            sQueryRequest.pageSize= 100000
        }

        val sCascadeQueryList = ArrayList<SCascadeQuery>()
        /** 级联资产 */
        val sCascadeQuery = SCascadeQuery()
        sCascadeQuery.name = "objectInfo"
        sCascadeQueryList.add(sCascadeQuery)
        sQueryRequest.cascade = sCascadeQueryList

        val manualRelationCalcResponse = ManualRelationCalcResponse()
        manualRelationCalcResponse.direction = "Left"
        val list = ArrayList<SFilter>()
        list.add(SFilter.eq("projectId",Opt.projectId!!))
        if (manualRelationCalcRequest.objectType == "1") {
           if (!manualRelationCalcRequest.vagueFrom.isNullOrEmpty()) {
               list.add(SFilter.contain("localName",manualRelationCalcRequest.vagueFrom!!))
           }
//            val spaceBaseList = spaceBaseZoneService.select(SFilter.eq("projectId", Opt.projectId!!), SFilter.not(SFilter.isNull("buildingId"))).exec()
            val spBuildingIdList = schemeMapper.spBuildingId(Opt.projectId!!)
            if (spBuildingIdList.size>0) {


                val listBuildingIds = ArrayList<String>()
                for (spBuildingId in spBuildingIdList) {
                    listBuildingIds.add(spBuildingId.id!!)
                }
                list.add(SFilter.inList("id",listBuildingIds))
                val pageQuery = rBd2SpBuilidingService.pageQuery(sQueryRequest, list)
                manualRelationCalcResponse.content = pageQuery.content
                manualRelationCalcResponse.message = pageQuery.message
                manualRelationCalcResponse.pageNumber = pageQuery.pageNumber
                manualRelationCalcResponse.pageSize = pageQuery.pageSize
                manualRelationCalcResponse.result = pageQuery.result
                manualRelationCalcResponse.total = pageQuery.total
            }else {
                manualRelationCalcResponse.content = ArrayList()
                manualRelationCalcResponse.result = SResponseType.success
            }
            return manualRelationCalcResponse

        }else if (manualRelationCalcRequest.objectType == "2"){
            val listSpace = ArrayList<SFilter>()
            listSpace.add(SFilter.eq("projectId",Opt.projectId!!))
            /** 筛选空间 */
            if (!manualRelationCalcRequest.zoneType.isNullOrEmpty()) {
                listSpace.add(SFilter.eq("classCode",manualRelationCalcRequest.zoneType!!))
            }
            if (!manualRelationCalcRequest.vagueTo.isNullOrEmpty()) {
                listSpace.add(SFilter.or(SFilter.contain("localName",manualRelationCalcRequest.vagueTo!!),SFilter.contain("localId",manualRelationCalcRequest.vagueTo!!)))
            }
            listSpace.add(SFilter.not(SFilter.isNull("buildingId")))
            val spaceBaseList = spaceBaseBuildingFloorService.select(listSpace).exec()
            if (spaceBaseList.size>0) {
                var spbuildingIdVagueToZoneType =  ArrayList<Building>()
                if (!manualRelationCalcRequest.zoneType.isNullOrEmpty()&&!manualRelationCalcRequest.vagueTo.isNullOrEmpty()){
                    spbuildingIdVagueToZoneType = schemeMapper.spbuildingIdVagueToZoneType(Opt.projectId!!, manualRelationCalcRequest.zoneType!!, manualRelationCalcRequest.vagueTo!!)
                }else if (!manualRelationCalcRequest.zoneType.isNullOrEmpty()) {
                    spbuildingIdVagueToZoneType = schemeMapper.spbuildingIdZoneType(Opt.projectId!!, manualRelationCalcRequest.zoneType!!)
                } else if (!manualRelationCalcRequest.vagueTo.isNullOrEmpty()){
                    spbuildingIdVagueToZoneType = schemeMapper.spbuildingIdVague(Opt.projectId!!, manualRelationCalcRequest.vagueTo!!)
                }
                var listBuildingId = ArrayList<String>()
                for (spbuilding in spbuildingIdVagueToZoneType) {
                    listBuildingId.add(spbuilding.id!!)
                }
                var rBd2SpBuilidingList: ArrayList<RBd2SpBuiliding>
                if(spbuildingIdVagueToZoneType.size > 0){
                    rBd2SpBuilidingList = rBd2SpBuilidingService.select(SFilter.eq("projectId", Opt.projectId!!), SFilter.inList("id", listBuildingId)).exec()
                }else{
                    rBd2SpBuilidingList = rBd2SpBuilidingService.select(SFilter.eq("projectId", Opt.projectId!!)).exec()
                }
                for (spbuilding in rBd2SpBuilidingList) {
                    for (spaceBase in spaceBaseList) {
                        if (spaceBase.buildingId == spbuilding.id) {
                            if (spbuilding.objectInfo.isNullOrEmpty()) {
                                spbuilding.objectInfo = ArrayList()
                            }
                            spbuilding.objectInfo!!.add(spaceBase)
                        }
                    }
                }
                manualRelationCalcResponse.content = rBd2SpBuilidingList
                manualRelationCalcResponse.message = ""
                manualRelationCalcResponse.pageNumber = 1
                manualRelationCalcResponse.pageSize = 50
                manualRelationCalcResponse.result = SResponseType.success
                manualRelationCalcResponse.total = rBd2SpBuilidingList.size.toLong()
            }else {
                manualRelationCalcResponse.content = ArrayList()
                manualRelationCalcResponse.result = SResponseType.success
            }
            return manualRelationCalcResponse
        }else if (manualRelationCalcRequest.objectType == "3") {
            val spBuildingIdList = schemeMapper.spBuildingId(Opt.projectId!!)
            if (spBuildingIdList.size>0) {

                val buildingList = ArrayList<SFilter>()
                buildingList.add(SFilter.eq("projectId",Opt.projectId!!))
                if (!manualRelationCalcRequest.vagueFrom.isNullOrEmpty()) {
                    buildingList.add(SFilter.contain("localName",manualRelationCalcRequest.vagueFrom!!))
                }
                val listBuildingIds = ArrayList<String>()
                for (spBuildingId in spBuildingIdList) {
                    listBuildingIds.add(spBuildingId.id!!)
                }
                list.add(SFilter.inList("id",listBuildingIds))
                val rBd2SpBuilidingList = rBd2SpBuilidingService.select(list).exec()
                if (rBd2SpBuilidingList.size>0) {

                    val listSpace = ArrayList<SFilter>()
                    listSpace.add(SFilter.eq("projectId",Opt.projectId!!))
                    /** 筛选空间 */
                    if (!manualRelationCalcRequest.zoneType.isNullOrEmpty()) {
                        listSpace.add(SFilter.eq("classCode",manualRelationCalcRequest.zoneType!!))
                    }
                    if (!manualRelationCalcRequest.vagueTo.isNullOrEmpty()) {
                        listSpace.add(SFilter.or(SFilter.contain("localName",manualRelationCalcRequest.vagueTo!!),SFilter.contain("localId",manualRelationCalcRequest.vagueTo!!)))
                    }
                    var listBuildingTwo = ArrayList<String>()
                    for (rBd2SpBuiliding in rBd2SpBuilidingList) {
                        listBuildingTwo.add(rBd2SpBuiliding.id!!)
                    }

                    listSpace.add(SFilter.inList("buildingId",listBuildingTwo))
                    val spaceBaseList = spaceBaseBuildingFloorService.select(listSpace).exec()
                    if (spaceBaseList.size>0) {

                        for (rBd2SpBuiliding in rBd2SpBuilidingList) {
                            for (spaceBase in spaceBaseList) {
                                if (spaceBase.buildingId == rBd2SpBuiliding.id) {
                                    if (rBd2SpBuiliding.objectInfo.isNullOrEmpty()) {
                                        rBd2SpBuiliding.objectInfo = ArrayList()
                                    }
                                    rBd2SpBuiliding.objectInfo!!.add(spaceBase)
                                }
                            }
                        }

                        manualRelationCalcResponse.content = rBd2SpBuilidingList
                        manualRelationCalcResponse.message = ""
                        manualRelationCalcResponse.pageNumber = 1
                        manualRelationCalcResponse.pageSize = 50
                        manualRelationCalcResponse.result = SResponseType.success
                        manualRelationCalcResponse.total = rBd2SpBuilidingList.size.toLong()

                    }else {
                        manualRelationCalcResponse.content = ArrayList()
                        manualRelationCalcResponse.result = SResponseType.success
                    }
                }else {
                    manualRelationCalcResponse.content = ArrayList()
                    manualRelationCalcResponse.result = SResponseType.success
                }
            }else {
                manualRelationCalcResponse.content = ArrayList()
                manualRelationCalcResponse.result = SResponseType.success
            }

            return manualRelationCalcResponse
        }
        return manualRelationCalcResponse
    }

    /**
     * 楼层下的空间
     */
    fun queryFl2Sp(manualRelationCalcRequest: ManualRelationCalcRequest): ManualRelationCalcResponse{
        val sQueryRequest = SQueryRequest()
        if(manualRelationCalcRequest.pageNumber>0&&manualRelationCalcRequest.pageSize>0){
            SQueryRequest.maxRow = 400000
            sQueryRequest.pageSize= manualRelationCalcRequest.pageSize
            sQueryRequest.pageNumber = manualRelationCalcRequest.pageNumber
        }else{
            /** 控制条数 */
            SQueryRequest.maxRow = 100000
            sQueryRequest.pageSize= 100000
        }

        val sCascadeQueryList = ArrayList<SCascadeQuery>()
        /** 级联资产 */
        val sCascadeQuery = SCascadeQuery()
        sCascadeQuery.name = "objectInfo"
        sCascadeQueryList.add(sCascadeQuery)
        sQueryRequest.cascade = sCascadeQueryList

        val manualRelationCalcResponse = ManualRelationCalcResponse()
        manualRelationCalcResponse.direction = "Left"
        val list = ArrayList<SFilter>()
        list.add(SFilter.eq("projectId",Opt.projectId!!))
        if (manualRelationCalcRequest.objectType == "1") {
            if (!manualRelationCalcRequest.vagueFrom.isNullOrEmpty()) {
                list.add(SFilter.contain("localName",manualRelationCalcRequest.vagueFrom!!))
            }
//            val spaceBaseList = spaceBaseZoneService.select(SFilter.eq("projectId", Opt.projectId!!), SFilter.not(SFilter.isNull("buildingId"))).exec()
            val spBuildingIdList = schemeMapper.spFloorId(Opt.projectId!!)
            if (spBuildingIdList.size>0) {

                val listBuildingIds = ArrayList<String>()
                for (spBuildingId in spBuildingIdList) {
                    listBuildingIds.add(spBuildingId.id!!)
                }
                list.add(SFilter.inList("id",listBuildingIds))
                val pageQuery = rFl2SpFloorService.pageQuery(sQueryRequest, list)
                manualRelationCalcResponse.content = pageQuery.content
                manualRelationCalcResponse.message = pageQuery.message
                manualRelationCalcResponse.pageNumber = pageQuery.pageNumber
                manualRelationCalcResponse.pageSize = pageQuery.pageSize
                manualRelationCalcResponse.result = pageQuery.result
                manualRelationCalcResponse.total = pageQuery.total
            }else {
                manualRelationCalcResponse.content = ArrayList()
                manualRelationCalcResponse.result = SResponseType.success
            }
            return manualRelationCalcResponse

        }else if (manualRelationCalcRequest.objectType == "2"){
            val listSpace = ArrayList<SFilter>()
            listSpace.add(SFilter.eq("projectId",Opt.projectId!!))
            /** 筛选空间 */
            if (!manualRelationCalcRequest.zoneType.isNullOrEmpty()) {
                listSpace.add(SFilter.eq("classCode",manualRelationCalcRequest.zoneType!!))
            }
            if (!manualRelationCalcRequest.vagueTo.isNullOrEmpty()) {
                listSpace.add(SFilter.or(SFilter.contain("localName",manualRelationCalcRequest.vagueTo!!),SFilter.contain("localId",manualRelationCalcRequest.vagueTo!!)))
            }
            listSpace.add(SFilter.not(SFilter.isNull("floorId")))
            val spaceBaseList = spaceBaseBuildingFloorService.select(listSpace).exec()
            if (spaceBaseList.size>0) {
                var spbuildingIdVagueToZoneType =  ArrayList<Floor>()
                if (!manualRelationCalcRequest.zoneType.isNullOrEmpty()&&!manualRelationCalcRequest.vagueTo.isNullOrEmpty()){
                    spbuildingIdVagueToZoneType = schemeMapper.spfloorIdVagueToZoneType(Opt.projectId!!, manualRelationCalcRequest.zoneType!!, manualRelationCalcRequest.vagueTo!!)
                }else if (!manualRelationCalcRequest.zoneType.isNullOrEmpty()) {
                    spbuildingIdVagueToZoneType = schemeMapper.spfloorIdZoneType(Opt.projectId!!, manualRelationCalcRequest.zoneType!!)
                } else if (!manualRelationCalcRequest.vagueTo.isNullOrEmpty()){
                    spbuildingIdVagueToZoneType = schemeMapper.spfloorIdVague(Opt.projectId!!, manualRelationCalcRequest.vagueTo!!)
                }
                var listBuildingId = ArrayList<String>()
                for (spbuilding in spbuildingIdVagueToZoneType) {
                    listBuildingId.add(spbuilding.id!!)
                }
                val rBd2SpBuilidingList = rFl2SpFloorService.select(SFilter.eq("projectId", Opt.projectId!!), SFilter.inList("id", listBuildingId)).exec()
                for (spbuilding in rBd2SpBuilidingList) {
                    for (spaceBase in spaceBaseList) {
                        if (spaceBase.floorId == spbuilding.id) {
                            if (spbuilding.objectInfo.isNullOrEmpty()) {
                                spbuilding.objectInfo = ArrayList()
                            }
                            spbuilding.objectInfo!!.add(spaceBase)
                        }
                    }
                }
                manualRelationCalcResponse.content = rBd2SpBuilidingList
                manualRelationCalcResponse.message = ""
                manualRelationCalcResponse.pageNumber = 1
                manualRelationCalcResponse.pageSize = 50
                manualRelationCalcResponse.result = SResponseType.success
                manualRelationCalcResponse.total = rBd2SpBuilidingList.size.toLong()
            }else {
                manualRelationCalcResponse.content = ArrayList()
                manualRelationCalcResponse.result = SResponseType.success
            }
            return manualRelationCalcResponse
        }else if (manualRelationCalcRequest.objectType == "3") {
            val spBuildingIdList = schemeMapper.spFloorId(Opt.projectId!!)
            if (spBuildingIdList.size>0) {

                val buildingList = ArrayList<SFilter>()
                buildingList.add(SFilter.eq("projectId",Opt.projectId!!))
                if (!manualRelationCalcRequest.vagueFrom.isNullOrEmpty()) {
                    buildingList.add(SFilter.contain("localName",manualRelationCalcRequest.vagueFrom!!))
                }
                val listBuildingIds = ArrayList<String>()
                for (spBuildingId in spBuildingIdList) {
                    listBuildingIds.add(spBuildingId.id!!)
                }
                list.add(SFilter.inList("id",listBuildingIds))
                val rBd2SpBuilidingList = rFl2SpFloorService.select(list).exec()
                if (rBd2SpBuilidingList.size>0) {

                    val listSpace = ArrayList<SFilter>()
                    listSpace.add(SFilter.eq("projectId",Opt.projectId!!))
                    /** 筛选空间 */
                    if (!manualRelationCalcRequest.zoneType.isNullOrEmpty()) {
                        listSpace.add(SFilter.eq("classCode",manualRelationCalcRequest.zoneType!!))
                    }
                    if (!manualRelationCalcRequest.vagueTo.isNullOrEmpty()) {
                        listSpace.add(SFilter.or(SFilter.contain("localName",manualRelationCalcRequest.vagueTo!!),SFilter.contain("localId",manualRelationCalcRequest.vagueTo!!)))
                    }
                    var listBuildingTwo = ArrayList<String>()
                    for (rBd2SpBuiliding in rBd2SpBuilidingList) {
                        listBuildingTwo.add(rBd2SpBuiliding.id!!)
                    }

                    listSpace.add(SFilter.inList("floorId",listBuildingTwo))
                    val spaceBaseList = spaceBaseBuildingFloorService.select(listSpace).exec()
                    if (spaceBaseList.size>0) {

                        for (rBd2SpBuiliding in rBd2SpBuilidingList) {
                            for (spaceBase in spaceBaseList) {
                                if (spaceBase.buildingId == rBd2SpBuiliding.id) {
                                    if (rBd2SpBuiliding.objectInfo.isNullOrEmpty()) {
                                        rBd2SpBuiliding.objectInfo = ArrayList()
                                    }
                                    rBd2SpBuiliding.objectInfo!!.add(spaceBase)
                                }
                            }
                        }

                        manualRelationCalcResponse.content = rBd2SpBuilidingList
                        manualRelationCalcResponse.message = ""
                        manualRelationCalcResponse.pageNumber = 1
                        manualRelationCalcResponse.pageSize = 50
                        manualRelationCalcResponse.result = SResponseType.success
                        manualRelationCalcResponse.total = rBd2SpBuilidingList.size.toLong()

                    }else {
                        manualRelationCalcResponse.content = ArrayList()
                        manualRelationCalcResponse.result = SResponseType.success
                    }
                }else {
                    manualRelationCalcResponse.content = ArrayList()
                    manualRelationCalcResponse.result = SResponseType.success
                }
            }else {
                manualRelationCalcResponse.content = ArrayList()
                manualRelationCalcResponse.result = SResponseType.success
            }

            return manualRelationCalcResponse
        }
        return manualRelationCalcResponse
    }

    /**
     * 空间对应空间查询
     */
    fun querySp2Sp(manualRelationCalcRequest: ManualRelationCalcRequest): ManualRelationCalcResponse {
        val sQueryRequest = SQueryRequest()
        if(manualRelationCalcRequest.pageNumber>0&&manualRelationCalcRequest.pageSize>0){
            SQueryRequest.maxRow = 400000
            sQueryRequest.pageSize= manualRelationCalcRequest.pageSize
            sQueryRequest.pageNumber = manualRelationCalcRequest.pageNumber
        }else{
            /** 控制条数 */
            SQueryRequest.maxRow = 100000
            sQueryRequest.pageSize= 100000
        }

        val sCascadeQueryList = ArrayList<SCascadeQuery>()
        /** 级联资产 */
        val sCascadeQuery = SCascadeQuery()
        sCascadeQuery.name = "objectInfo"
        sCascadeQueryList.add(sCascadeQuery)
        sQueryRequest.cascade = sCascadeQueryList

        val manualRelationCalcResponse = ManualRelationCalcResponse()
        manualRelationCalcResponse.direction = "Left"
        val list = ArrayList<SFilter>()
        list.add(SFilter.eq("projectId",Opt.projectId!!))

        val rSp2SpList = rSp2SpService.select(SFilter.eq("projectId", Opt.projectId!!), SFilter.eq("graphCode", "ArchForArch"), SFilter.eq("type", "Sp2Sp")).exec()
        if (rSp2SpList.size>0) {
            if (manualRelationCalcRequest.objectType == "1") {

                if (!manualRelationCalcRequest.vagueFrom.isNullOrEmpty()) {
                    list.add(SFilter.or(SFilter.contain("localName",manualRelationCalcRequest.vagueFrom!!),SFilter.contain("localId",manualRelationCalcRequest.vagueFrom!!)))
                }
                var listIds = ArrayList<String>()
                for (rSp2Sp in rSp2SpList) {
                    listIds.add(rSp2Sp.id1!!)
                }
                list.add(SFilter.inList("id",listIds))
                val pageQuery = relSp2SpSpaceService.pageQuery(sQueryRequest, list)
                manualRelationCalcResponse.content = pageQuery.content
                manualRelationCalcResponse.message = pageQuery.message
                manualRelationCalcResponse.pageNumber = pageQuery.pageNumber
                manualRelationCalcResponse.pageSize = pageQuery.pageSize
                manualRelationCalcResponse.result = pageQuery.result
                manualRelationCalcResponse.total = pageQuery.total

            } else if (manualRelationCalcRequest.objectType == "2") {
                val listSpace = ArrayList<SFilter>()
                if (!manualRelationCalcRequest.vagueTo.isNullOrEmpty()) {
                    listSpace.add(SFilter.or(SFilter.contain("localName",manualRelationCalcRequest.vagueTo!!),SFilter.contain("localId",manualRelationCalcRequest.vagueTo!!)))
                }
                var listIds = ArrayList<String>()
                for (rSp2Sp in rSp2SpList) {
                    listIds.add(rSp2Sp.id2!!)
                }
                listSpace.add(SFilter.inList("id",listIds))
                listSpace.add(SFilter.eq("projectId",Opt.projectId!!))
                val relSp2SpSpaceList = relSp2SpSpaceService.select(listSpace).exec()
                if (relSp2SpSpaceList.size>0) {

                    val listIdSp = ArrayList<String>()
                    for (relSpace in relSp2SpSpaceList) {
                        listIdSp.add(relSpace.id!!)
                    }
                    val rSp2SpLists = rSp2SpService.select(SFilter.inList("id2", listIdSp)).exec()
                    if (rSp2SpLists.size>0) {
                        val listIdSpaceIds = ArrayList<String>()
                        for (relSpace in rSp2SpLists) {
                            listIdSpaceIds.add(relSpace.id1!!)
                        }
                        list.add(SFilter.inList("id",listIdSpaceIds))
                        val pageQuery = relSp2SpSpaceService.pageQuery(sQueryRequest, list)
                        manualRelationCalcResponse.content = pageQuery.content
                        manualRelationCalcResponse.message = pageQuery.message
                        manualRelationCalcResponse.pageNumber = pageQuery.pageNumber
                        manualRelationCalcResponse.pageSize = pageQuery.pageSize
                        manualRelationCalcResponse.result = pageQuery.result
                        manualRelationCalcResponse.total = pageQuery.total
                    }else {
                        manualRelationCalcResponse.content = ArrayList()
                        manualRelationCalcResponse.result = SResponseType.success
                    }
                }else {
                    manualRelationCalcResponse.content = ArrayList()
                    manualRelationCalcResponse.result = SResponseType.success
                }
            } else if (manualRelationCalcRequest.objectType == "3") {

                val listSF = ArrayList<SFilter>()
                listSF.add(SFilter.eq("projectId",Opt.projectId!!))
                listSF.add(SFilter.or(SFilter.contain("localName1",manualRelationCalcRequest.vagueFrom!!),SFilter.contain("localId1",manualRelationCalcRequest.vagueFrom!!)))
                listSF.add(SFilter.or(SFilter.contain("localName2",manualRelationCalcRequest.vagueTo!!),SFilter.contain("localId2",manualRelationCalcRequest.vagueTo!!)))
                val rSp2SpTwoList = rSp2SpTwoService.select(SFilter.eq("projectId",Opt.projectId!!),SFilter.or(SFilter.contain("localName1",manualRelationCalcRequest.vagueFrom!!),SFilter.contain("localId1",manualRelationCalcRequest.vagueFrom!!))
                    ,SFilter.or(SFilter.contain("localName2",manualRelationCalcRequest.vagueTo!!),SFilter.contain("localId2",manualRelationCalcRequest.vagueTo!!)) ).exec()
                if (rSp2SpTwoList.size>0) {
                    val listId1 = HashSet<String>()
                    val listId2 = HashSet<String>()
                    for (rSp2SpTwo in rSp2SpTwoList) {
                        listId1.add(rSp2SpTwo.id1!!)
                        listId2.add(rSp2SpTwo.id2!!)
                    }
                    sCascadeQuery.filters = "id in ${listId2.toJson()}"
                    list.add(SFilter.inList("id",listId1.toList()))
                    val pageQuery = relSp2SpSpaceService.pageQuery(sQueryRequest, list)
                    manualRelationCalcResponse.content = pageQuery.content
                    manualRelationCalcResponse.message = pageQuery.message
                    manualRelationCalcResponse.pageNumber = pageQuery.pageNumber
                    manualRelationCalcResponse.pageSize = pageQuery.pageSize
                    manualRelationCalcResponse.result = pageQuery.result
                    manualRelationCalcResponse.total = pageQuery.total
                }else {
                    manualRelationCalcResponse.content = ArrayList()
                    manualRelationCalcResponse.result = SResponseType.success
                }
            }
        }else {
            manualRelationCalcResponse.content = ArrayList()
            manualRelationCalcResponse.result = SResponseType.success
        }

        return manualRelationCalcResponse
    }

    /**
     * 系统和设备
     */
    fun querySy2Eq(manualRelationCalcRequest: ManualRelationCalcRequest): ManualRelationCalcResponse{
        val sQueryRequest = SQueryRequest()
        if(manualRelationCalcRequest.pageNumber>0&&manualRelationCalcRequest.pageSize>0){
            SQueryRequest.maxRow = 400000
            sQueryRequest.pageSize= manualRelationCalcRequest.pageSize
            sQueryRequest.pageNumber = manualRelationCalcRequest.pageNumber
        }else{
            /** 控制条数 */
            SQueryRequest.maxRow = 100000
            sQueryRequest.pageSize= 100000
        }

        val sCascadeQueryList = ArrayList<SCascadeQuery>()
        /** 级联资产 */
        val sCascadeQuery = SCascadeQuery()
        sCascadeQuery.name = "objectInfo"
        sCascadeQueryList.add(sCascadeQuery)
        sQueryRequest.cascade = sCascadeQueryList

        val manualRelationCalcResponse = ManualRelationCalcResponse()
        manualRelationCalcResponse.direction = "Left"
        val list = ArrayList<SFilter>()
        list.add(SFilter.eq("projectId",Opt.projectId!!))

        val rEq2SyList = rEq2SyService.select(SFilter.eq("projectId", Opt.projectId!!)).exec()
        val syIds = LinkedHashSet<String>()
        for (rEq2Sy in rEq2SyList) {
            syIds.add(rEq2Sy.id1!!)
        }

        if (rEq2SyList.size>0) {
            if (manualRelationCalcRequest.objectType == "1") {
                if (!manualRelationCalcRequest.vagueFrom.isNullOrEmpty()) {
                    list.add(SFilter.or(SFilter.contain("localName",manualRelationCalcRequest.vagueFrom!!),SFilter.contain("localId",manualRelationCalcRequest.vagueFrom!!)))
                }
                if (!manualRelationCalcRequest.categoryFrom.isNullOrEmpty()) {
                    list.add(SFilter.eq("classCode",manualRelationCalcRequest.categoryFrom!!))
                }
                if(syIds.size > 0){
                    list.add(SFilter.inList("id",ArrayList(syIds)))
                }
//                list.add(SFilter.not(SFilter.isNull("objectInfo")))
                val pageQuery = relSy2EqSystemService.pageQuery(sQueryRequest, list)
                manualRelationCalcResponse.content = pageQuery.content!!.filter { r -> r.objectInfo != null }
                manualRelationCalcResponse.message = pageQuery.message
                manualRelationCalcResponse.pageNumber = pageQuery.pageNumber
                manualRelationCalcResponse.pageSize = pageQuery.pageSize
                manualRelationCalcResponse.result = pageQuery.result
                manualRelationCalcResponse.total = pageQuery.total
            }else if (manualRelationCalcRequest.objectType == "2" || manualRelationCalcRequest.objectType == "3" ) {
                if (!manualRelationCalcRequest.categoryFrom.isNullOrEmpty()) {
                    list.add(SFilter.eq("classCode1",manualRelationCalcRequest.categoryTo!!))
                }
                if (!manualRelationCalcRequest.vagueFrom.isNullOrEmpty()) {
                    list.add(SFilter.or(SFilter.contain("localName1",manualRelationCalcRequest.vagueTo!!),SFilter.contain("localId1",manualRelationCalcRequest.vagueTo!!)
                            ,SFilter.contain("codeName1",manualRelationCalcRequest.vagueTo!!)))
                }
                if (!manualRelationCalcRequest.categoryTo.isNullOrEmpty()) {
                    list.add(SFilter.eq("classCode2",manualRelationCalcRequest.categoryTo!!))
                }
                if (!manualRelationCalcRequest.vagueTo.isNullOrEmpty()) {
                    list.add(SFilter.or(SFilter.contain("codeName2",manualRelationCalcRequest.vagueTo!!),SFilter.contain("localId2",manualRelationCalcRequest.vagueTo!!)
                            ,SFilter.contain("classCode2",manualRelationCalcRequest.vagueTo!!),SFilter.contain("systemCategotry",manualRelationCalcRequest.vagueTo!!)))
                }
                val rEq2SyTwoList = rEq2SyTwoService.select(list).exec()
                if (rEq2SyTwoList.size>0) {
                    val listId1 = ArrayList<String>()
                    val listId2 = ArrayList<String>()
                    for (rEq2SyTwo in rEq2SyTwoList) {
                        listId1.add(rEq2SyTwo.id1!!)
                        listId2.add(rEq2SyTwo.id2!!)
                    }

                    val listSF = ArrayList<SFilter>()
                    sCascadeQuery.filters = "id in ${listId2.toJson()}"
                    listSF.add(SFilter.inList("id",listId1.toList()))
                    listSF.add(SFilter.eq("projectId",Opt.projectId!!))
                    val pageQuery = relSp2SpSpaceService.pageQuery(sQueryRequest, listSF)
                    manualRelationCalcResponse.content = pageQuery.content
                    manualRelationCalcResponse.message = pageQuery.message
                    manualRelationCalcResponse.pageNumber = pageQuery.pageNumber
                    manualRelationCalcResponse.pageSize = pageQuery.pageSize
                    manualRelationCalcResponse.result = pageQuery.result
                    manualRelationCalcResponse.total = pageQuery.total
                }else {
                    manualRelationCalcResponse.content = ArrayList()
                    manualRelationCalcResponse.result = SResponseType.success
                }
            }
        }else {
            manualRelationCalcResponse.content = ArrayList()
            manualRelationCalcResponse.result = SResponseType.success
        }
        return  manualRelationCalcResponse
    }

    /**
     * 空间和空间
     */
    fun queryEq2Sp(manualRelationCalcRequest: ManualRelationCalcRequest): ManualRelationCalcResponse{
        val sQueryRequest = SQueryRequest()
        if(manualRelationCalcRequest.pageNumber>0&&manualRelationCalcRequest.pageSize>0){
            SQueryRequest.maxRow = 400000
            sQueryRequest.pageSize= manualRelationCalcRequest.pageSize
            sQueryRequest.pageNumber = manualRelationCalcRequest.pageNumber
        }else{
            /** 控制条数 */
            SQueryRequest.maxRow = 100000
            sQueryRequest.pageSize= 100000
        }

        val sCascadeQueryList = ArrayList<SCascadeQuery>()
        /** 级联资产 */
        val sCascadeQuery = SCascadeQuery()
        sCascadeQuery.name = "objectInfo"
        sCascadeQueryList.add(sCascadeQuery)
        sQueryRequest.cascade = sCascadeQueryList

        val manualRelationCalcResponse = ManualRelationCalcResponse()
        manualRelationCalcResponse.direction = "Left"
        val list = ArrayList<SFilter>()
        list.add(SFilter.eq("projectId",Opt.projectId!!))
        val rEq2SpList = rEq2SpService.select(SFilter.eq("projectId", Opt.projectId!!), SFilter.eq("graphCode", manualRelationCalcRequest.graphicType!!), SFilter.eq("type", manualRelationCalcRequest.relType!!)
                , SFilter.eq("zoneType", manualRelationCalcRequest.zoneType!!)).exec()
        if (rEq2SpList.size>0) {
            if (manualRelationCalcRequest.objectType == "1") {
                if (!manualRelationCalcRequest.categoryFrom.isNullOrEmpty()){
                    list.add(SFilter.eq("classCode",manualRelationCalcRequest.categoryFrom!!))
                }
                    if (!manualRelationCalcRequest.vagueFrom.isNullOrEmpty()) {
                        list.add(SFilter.or(SFilter.contain("codeName",manualRelationCalcRequest.vagueFrom!!),SFilter.contain("localId1",manualRelationCalcRequest.vagueFrom!!)
                                ,SFilter.contain("bimTypeId",manualRelationCalcRequest.vagueFrom!!),SFilter.contain("systemCategotry",manualRelationCalcRequest.vagueFrom!!)))
                    }

                val pageQuery = relEq2SpEquipService.pageQuery(sQueryRequest, list)
                manualRelationCalcResponse.content = pageQuery.content
                manualRelationCalcResponse.message = pageQuery.message
                manualRelationCalcResponse.pageNumber = pageQuery.pageNumber
                manualRelationCalcResponse.pageSize = pageQuery.pageSize
                manualRelationCalcResponse.result = pageQuery.result
                manualRelationCalcResponse.total = pageQuery.total
            }else if (manualRelationCalcRequest.objectType == "2" ||manualRelationCalcRequest.objectType == "3"){
                if (!manualRelationCalcRequest.categoryFrom.isNullOrEmpty()){
                    list.add(SFilter.eq("classCode",manualRelationCalcRequest.categoryFrom!!))
                }
                if (!manualRelationCalcRequest.vagueFrom.isNullOrEmpty()) {
                    list.add(SFilter.or(SFilter.contain("codeName",manualRelationCalcRequest.vagueTo!!),SFilter.contain("localId1",manualRelationCalcRequest.vagueTo!!)
                            ,SFilter.contain("bimTypeId",manualRelationCalcRequest.vagueTo!!),SFilter.contain("systemCategotry",manualRelationCalcRequest.vagueTo!!)))
                }

                if (!manualRelationCalcRequest.vagueTo.isNullOrEmpty()){
                    list.add(SFilter.or(SFilter.contain("localName2",manualRelationCalcRequest.vagueTo!!),SFilter.contain("localId2",manualRelationCalcRequest.vagueTo!!)))
                }
                if (!manualRelationCalcRequest.zoneType.isNullOrEmpty()){
                    list.add(SFilter.eq("zoneType",manualRelationCalcRequest.zoneType!!))
                }
                val rEq2SpTwoList = rEq2SpTwoService.select(list).exec()
                if (rEq2SpTwoList.size>0){
                    for (rEq2SpTwo in rEq2SpTwoList){

                    }

                    val listId1 = ArrayList<String>()
                    val listId2 = ArrayList<String>()
                    for (rEq2SpTwo in rEq2SpTwoList){
                        listId1.add(rEq2SpTwo.id1!!)
                        listId2.add(rEq2SpTwo.id2!!)
                    }

                    val listSF = ArrayList<SFilter>()
                    sCascadeQuery.filters = "id in ${listId2.toJson()}"
                    listSF.add(SFilter.inList("id",listId1.toList()))
                    listSF.add(SFilter.eq("projectId",Opt.projectId!!))
                    val pageQuery = relEq2SpEquipService.pageQuery(sQueryRequest, listSF)
                    manualRelationCalcResponse.content = pageQuery.content
                    manualRelationCalcResponse.message = pageQuery.message
                    manualRelationCalcResponse.pageNumber = pageQuery.pageNumber
                    manualRelationCalcResponse.pageSize = pageQuery.pageSize
                    manualRelationCalcResponse.result = pageQuery.result
                    manualRelationCalcResponse.total = pageQuery.total
                }else {
                    manualRelationCalcResponse.content = ArrayList()
                    manualRelationCalcResponse.result = SResponseType.success
                }

            } else {
                manualRelationCalcResponse.content = ArrayList()
                manualRelationCalcResponse.result = SResponseType.success
            }

        }else {
            manualRelationCalcResponse.content = ArrayList()
            manualRelationCalcResponse.result = SResponseType.success
        }
        return  manualRelationCalcResponse
    }


    /**
     * 删除关系
     */
    fun delRel(manualRelationCalcDel: ManualRelationCalcDel):SBaseResponse{
        try {
            val projectId = SPageContext.getHeader("projectId")

            var delete: Boolean = false
            if(!manualRelationCalcDel.fromId.isNullOrEmpty()&&manualRelationCalcDel.toId.isNullOrEmpty()){

                try {
                    val relationTypeProjectService = SObjectService(SMybatisDao(RelationTypeProject::class.java))

                    val relationType = relationTypeProjectService.select(SFilter.eq("projectId", projectId!!),SFilter.eq("graphicType", manualRelationCalcDel.graphicType!!)
                            ,SFilter.eq("relationType", manualRelationCalcDel.relType!!)).entity()
                    if (relationType!=null){
                        relationType.computingTime = time()
                        relationTypeProjectService.update(relationType)
                    }
                } catch (e: Exception) {
                    e.printStackTrace()
                }

                if (manualRelationCalcDel.graphicType == "ArchSubset" && manualRelationCalcDel.relType == "Bd2Sp") {
                    val relZoneSpace = RelZoneSpace()
                    relZoneSpace.id = manualRelationCalcDel.fromId
                    delete = relZoneSpaceService.update(relZoneSpace, arrayListOf("buildingId", "floorId"))
                } else if (manualRelationCalcDel.graphicType == "ArchSubset" && manualRelationCalcDel.relType == "Fl2Sp") {
                    val relZoneSpace = RelZoneSpace()
                    relZoneSpace.id = manualRelationCalcDel.fromId
                    delete = relZoneSpaceService.update(relZoneSpace, arrayListOf( "floorId"))
                } else if (manualRelationCalcDel.graphicType == "MechInArch" && manualRelationCalcDel.relType == "Eq2Bd") {
                    val equipment = Equipment()
                    equipment.id = manualRelationCalcDel.fromId
                    delete = EquipmentService.update(equipment, arrayListOf("buildingId", "floorId"))
                } else if (manualRelationCalcDel.graphicType == "MechInArch" && manualRelationCalcDel.relType == "Eq2Fl") {
                    val equipment = Equipment()
                    equipment.id = manualRelationCalcDel.fromId
                    delete = EquipmentService.update(equipment, arrayListOf("floorId"))
                } else if (manualRelationCalcDel.graphicType == "ArchForArch" && manualRelationCalcDel.relType == "Sp2Sp") {
                    delete = rSp2SpService.delete(SFilter.eq("id1",manualRelationCalcDel.fromId!!),SFilter.eq("id2",manualRelationCalcDel.toId!!),
                            SFilter.eq("graphCode",manualRelationCalcDel.graphicType!!),SFilter.eq("type",manualRelationCalcDel.relType!!))
                } else if (manualRelationCalcDel.graphicType == "MechSubset" && manualRelationCalcDel.relType == "Sy2Eq") {
                    delete = rSp2SpService.delete(SFilter.eq("id1",manualRelationCalcDel.fromId!!),SFilter.eq("id2",manualRelationCalcDel.toId!!),
                            SFilter.eq("graphCode",manualRelationCalcDel.graphicType!!),SFilter.eq("type",manualRelationCalcDel.relType!!))
                } else if (manualRelationCalcDel.graphicType == "MechInArch" && manualRelationCalcDel.relType == "Eq2Sp") {
                    delete = rSp2SpService.delete(SFilter.eq("id1",manualRelationCalcDel.fromId!!),SFilter.eq("id2",manualRelationCalcDel.toId!!),
                            SFilter.eq("graphCode",manualRelationCalcDel.graphicType!!),SFilter.eq("type",manualRelationCalcDel.relType!!))
                } else if (manualRelationCalcDel.graphicType == "MechForArch" && manualRelationCalcDel.relType == "Eq2Sp") {
                    delete = rSp2SpService.delete(SFilter.eq("id1",manualRelationCalcDel.fromId!!),SFilter.eq("id2",manualRelationCalcDel.toId!!),
                            SFilter.eq("graphCode",manualRelationCalcDel.graphicType!!),SFilter.eq("type",manualRelationCalcDel.relType!!))
                }
                if (delete){
                    /** 发送消息 */
                    rabbitMqService.sendRel(Opt.projectId!!, manualRelationCalcDel.relType!!,"15",manualRelationCalcDel.fromId!!,"123456")
                    return SBaseResponse(SResponseType.success)
                }else{
                    return  SBaseResponse(SResponseType.failure)
                }
            }else{
                /** 主对象和从对象都不可以为空 */
                return  SBaseResponse(SResponseType.failure)
            }
        } catch (e: Exception) {
            e.printStackTrace()
            return  SBaseResponse(SResponseType.failure,e.message.toString())
        }
    }








    fun time():String {
        val df = SimpleDateFormat("yyyy-MM-dd HH:mm:ss") //设置日期格式
        println(df.format(Date())) // new Date()为获取当前系统时间
        return df.format(Date())
    }


}