/*
 * *********************************************************************************************************************
 *
 *          !!
 *        .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.service.objects

import com.persagy.server.models.requests.ShaftSpaceVertical
import com.persagy.server.utils.SpaceRunnables
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.persagy.database.SFilter
import com.persagy.mybatis.SMybatisDao
import com.persagy.server.dao.mappers.CustomSql
import com.persagy.server.dao.mappers.EquipmentZoneMapper
import com.persagy.server.datacenter.models.entities.objects.Shaft
import com.persagy.server.datacenter.models.entities.rel.RSh2Sp
import com.persagy.server.datacenter.models.entities.rel.RSpVerticalSp
import com.persagy.server.datacenter.models.entities.rel.RSpinBd
import com.persagy.server.datacenter.models.entities.rel.RSpinFl
import com.persagy.server.models.SimilarityDegree
import com.persagy.server.models.objects.FloorCompon
import com.persagy.server.models.objects.ZoneSpace
import com.persagy.server.service.RShaftThroughShaftService
import com.persagy.server.service.base.Service
import com.persagy.service.SObjectService
import com.persagy.service.SPageContext
import com.persagy.service.models.enums.SResponseType
import com.persagy.service.models.requests.SQueryRequest
import com.persagy.service.models.responses.SQueryResponse
import com.persagy.service.utils.SSpringContextUtil
import org.slf4j.LoggerFactory
import org.springframework.web.bind.annotation.RequestBody
import java.lang.reflect.Type
import java.util.concurrent.Executors

/**
 * 竖井信息服务
 *
 * @author 张维新
 */
object ShaftService : Service<Shaft>(SMybatisDao(Shaft::class.java)) {

    /** 日志 */
    private val logger = LoggerFactory.getLogger(Service::class.java)

    /** 竖井和空间的关系 */
    val rShContainSpBaseService = SObjectService(SMybatisDao(RSh2Sp::class.java))
    /** 空间建筑 */
    val rSpinBdService = SObjectService(SMybatisDao(RSpinBd::class.java))
    /** 空间楼层 */
    val rSpinFlService = SObjectService(SMybatisDao(RSpinFl::class.java))
    /** 空间实例 */
    val zoneSpaceService = SObjectService(SMybatisDao(ZoneSpace::class.java))
    /** 空间垂直关系 */
    val rSpVerticalSpService = SObjectService(SMybatisDao(RSpVerticalSp::class.java))

    /** 自定义查询 */
    private val equipZoneMapper by lazy {
        SSpringContextUtil.getBean(EquipmentZoneMapper::class.java) as EquipmentZoneMapper
    }

    /** 自定义查询 */
    private val mapperCustomSql by lazy {
        SSpringContextUtil.getBean(CustomSql::class.java) as CustomSql
    }

//    @Autowired
//    lateinit var equipZoneMapper : EquipmentZoneMapper

//    @Autowired
//    lateinit var mapper: CustomSql


    /**
     * 查询没有和当前竖井贯通的竖井
     *
     * @param   shaftId     竖井id
     * @param   request     条件
     */
    fun unShaftThroughShaft(shaftId: String,@RequestBody request: SQueryRequest): SQueryResponse<Shaft> {
        return try {
            val projectId = SPageContext.getHeader("projectId")
            val execList = RShaftThroughShaftService.select(SFilter.eq("shaftId", shaftId), SFilter.eq("projectId", projectId!!)).exec()
            val execOtherList = RShaftThroughShaftService.select(SFilter.eq("shaftOtherId", shaftId), SFilter.eq("projectId", projectId!!)).exec()

            val list = ArrayList<String>()
            val listf = ArrayList<SFilter>()
            for (exec in execList){
                list.add(exec.shaftOtherId!!)
            }
            for (exec in execOtherList){
                list.add(exec.shaftId!!)
            }

            if (list.size>0){
                listf.add(SFilter.eq("projectId",projectId!!))
                listf.add(SFilter.not(SFilter.inList("ShaftID",list)))
            }else{

                listf.add(SFilter.eq("projectId",projectId!!))
            }

            val pageQuery = pageQuery(request, listf)
            pageQuery
        } catch (e: Exception) {
            e.printStackTrace()
            SQueryResponse(SResponseType.failure, e.message!!)
        }

    } // Function unShaftThroughShaft()

    /**
     * 查询和竖井关联的空间 垂直交通关系
     */
    fun shaftSpace(request: ShaftSpaceVertical): SQueryResponse<FloorCompon>{
       return try {

           if (request.buildingId.isNullOrEmpty()||request.objectType.isNullOrEmpty()||request.shaftId.isNullOrEmpty()){
               val sQueryResponse = SQueryResponse<FloorCompon>(SResponseType.failure)
               sQueryResponse.message = "入参不能为空"
               return  sQueryResponse

           }
         val buildingId =  request.buildingId!!
          val shaftId =  request.shaftId!!
           val objectType = request.objectType!!
           val projectId = SPageContext.getHeader("projectId")
//           if (request.aiSwitch!!){
//               val spVertically = mapperCustomSql.spVertically(projectId!!)
//               if (spVertically){
//                   logger.debug("计算成功***************")
//               }else{
//                   logger.debug("计算失败***************")
//               }
//           }

            /** 获取建筑下的楼层 */
            val floorList = FloorService.select(SFilter.eq("projectId",projectId!!),SFilter.eq("buildId", buildingId)).order("FloorSequenceID desc").exec()
            /** 项目下的竖井关联的空间  对应空间类型*/
            val rShContainSpBaseList = rShContainSpBaseService.select(SFilter.eq("projectId",projectId!!),SFilter.eq("Id1", shaftId),SFilter.eq("ZoneType", objectType), SFilter.eq("type","sh2sp")).exec()
            if (rShContainSpBaseList.isNotEmpty()&&rShContainSpBaseList.size>0){

                /** 获取空间对象实例 */
                val listId = ArrayList<String>()
                for (rShContainSpBase in rShContainSpBaseList){
                    listId.add(rShContainSpBase.id2!!)
                }
                /** 空间实例 */
                val  zoneSpaceListBase = zoneSpaceService.select(SFilter.inList("id", listId)).exec()
                if (zoneSpaceListBase.isNotEmpty()&&zoneSpaceListBase.size>0){
                    /** 处理空间的建筑和楼层 */
                    for (zoneSpace in zoneSpaceListBase){
                        try {
                            /** 空间建筑 */
                            val rSpinBd = rSpinBdService.select(SFilter.eq("spaceId", zoneSpace.id!!), SFilter.eq("projectId", projectId!!)).entity()
                            if (rSpinBd!=null){
                                zoneSpace.buildingId =  rSpinBd.buildingId
                            }
                            /** 空间楼层*/
                            val rSpinFl = rSpinFlService.select(SFilter.eq("spaceId",zoneSpace.id!!), SFilter.eq("projectId", projectId!!)).entity()
                            if (rSpinFl!=null){
                                zoneSpace.floorId = rSpinFl.floorId
                            }
                        } catch (e: Exception) {
                            e.printStackTrace()
                        }
                    }
                    /** 排除当前建筑以外的空间 */
                    val iterator = zoneSpaceListBase.iterator()
                    while (iterator.hasNext()){
                        val next = iterator.next()
                        if (next.buildingId.isNullOrEmpty()||next.buildingId != buildingId ){
                            iterator.remove()
                        }
                    }
                    if (zoneSpaceListBase.size>0){
                        /** 处理楼层下的空间 */
                        for (floor in floorList){
                            val list = ArrayList<ZoneSpace>()
                            for (zoneSpace in zoneSpaceListBase){
                                if (zoneSpace.floorId == floor.id){
                                    list.add(zoneSpace)
                                }
                            }
                            if (list.size>0){
                                floor.zoneSpaceList=list
                            }
                        }
                        /** 处理垂直交通关系 */
                        for (floor in floorList){
                            if (floor.zoneSpaceList!=null&&floor.zoneSpaceList!!.size>0){
                                val zoneSpaceFloorList = floor.zoneSpaceList
                                for (zoneSpace in zoneSpaceFloorList!!){
                                    /** 查询空间的垂直交通关系 */
                                    val rSpVerticalSpList = rSpVerticalSpService.select(SFilter.eq("projectId",projectId!!),SFilter.eq("spaceId", zoneSpace.id!!),SFilter.eq("objectType", zoneSpace.objectType!!)).exec()
                                    if (rSpVerticalSpList.size>0){
                                        val listId = ArrayList<String>()
                                        /** 排除和当前竖井空间没有关系的空间 */
                                        for (zoneSpaceFloor in zoneSpaceListBase){
                                            val iterator1 = rSpVerticalSpList.iterator()
                                            while (iterator1.hasNext()){
                                                val next = iterator1.next()
                                                if (next.spaceOtherId!=zoneSpace.id&&next.spaceOtherId == zoneSpaceFloor.id ){
                                                    listId.add(next.spaceOtherId!!)
                                                }
                                            }
                                        }
                                        logger.debug("1匹配长度=${listId.size} ***********************************")
                                        zoneSpace.spaceIdList = listId

                                    }

                                    /** 查询空间的垂直交通关系 其它 */
                                    val rSpVerticalSpOtherList = rSpVerticalSpService.select(SFilter.eq("projectId",projectId!!),SFilter.eq("spaceOtherId", zoneSpace.id!!),SFilter.eq("objectType", zoneSpace.objectType!!)).exec()
                                    if (rSpVerticalSpOtherList.size>0){
                                        /** 排除和当前竖井空间没有关系的空间 */
                                        val listId = ArrayList<String>()
                                        /** 排除和当前竖井空间没有关系的空间 */
                                        for (zoneSpaceFloor in zoneSpaceListBase){
                                            val iterator1 = rSpVerticalSpOtherList.iterator()
                                            while (iterator1.hasNext()){
                                                val next = iterator1.next()
                                                if (next.spaceId!=zoneSpace.id&&next.spaceId == zoneSpaceFloor.id ){
                                                    listId.add(next.spaceId!!)
                                                }
                                            }
                                        }
                                        logger.debug("2匹配长度=${listId.size} ***********************************")
                                        zoneSpace.spaceIdList = listId
                                    }
                                }
                            }
                        }
                    }

                    /** 处理AI */
                    if (request.aiSwitch!!){
                        if (floorList.size>0){

                            /** 处理垂直交通关系 */
                            val listSpace = ArrayList<ZoneSpace>()
                            for (floor in floorList){
                            /** 处理有业务空间的空间 */
                            if (floor.zoneSpaceList!=null&&floor.zoneSpaceList!!.size>0){
                                val listid = ArrayList<String>()
                                for (zoneSpace in floor.zoneSpaceList!!){
                                    /** 获取和当前业务空间名称相似度为90%的 */
                                    val setName = HashSet<String>()
                                    if (!zoneSpace.localName.isNullOrEmpty()){
                                        val listPr = ArrayList<String>()
                                        listPr.add("id")
                                        listPr.add("localName")
                                        val zoneSpaceLists = ZoneSpaceBaseService.select(SFilter.eq("projectId", projectId),
                                                SFilter.eq("buildingId", zoneSpace.buildingId!!), SFilter.not(SFilter.isNull("localName"))
                                                ,SFilter.eq("objectType", zoneSpace.objectType!!), SFilter.not(SFilter.eq("id",zoneSpace.id!!)))
                                                .projection(listPr).exec()
                                      if (zoneSpaceLists!=null&&zoneSpaceLists.size>0){
                                          for (zoneSp in zoneSpaceLists){
                                              if (!zoneSp.localName.isNullOrEmpty()){
                                                  setName.add(zoneSp.localName!!)
                                              }
                                          }
                                          /** 获取推荐出来的业务空间 */
//                                          logger.debug("1匹配本地名称=${zoneSpace.localName} ***********************************")
//                                          logger.debug("2匹配本地名称=${setName.toList().toJson()} ***********************************")
                                          val similarList = equipZoneMapper.similarTwo(zoneSpace.localName!!, setName.toList(),setName.toList().size)
//                                          logger.debug("4匹配长度=${similarList} ***********************************")
                                          val gson = Gson()
                                          val type: Type = object : TypeToken<List<SimilarityDegree>>() {}.getType()
                                          val lists: List<SimilarityDegree> = gson.fromJson(similarList, type)

//                                          logger.debug("3匹配长度=${lists.size} ***********************************")
                                          if (lists.size>0){
                                                val setName = HashSet<String>()
                                              for (similarit in lists){
                                                  if (similarit.value!! >0.9){
                                                      setName.add(similarit.key!!)
                                                  }
                                              }
                                              if (setName.size>0){
                                                  var zoneSpaceList:ArrayList<ZoneSpace>
                                                  if (listid.size>0){
                                                      zoneSpaceList = zoneSpaceService.select(SFilter.eq("projectId", projectId), SFilter.inList("localName", setName.toList()),
                                                              SFilter.eq("objectType", objectType!!),SFilter.not(SFilter.inList("id",listid))).exec()
                                                  }else{
                                                      zoneSpaceList = zoneSpaceService.select(SFilter.eq("projectId", projectId), SFilter.inList("localName", setName.toList()),
                                                              SFilter.eq("objectType", objectType!!)).exec()
                                                  }

                                                  if (zoneSpaceList.size>0){
                                                      for (zone in zoneSpaceList){
                                                          listid.add(zone.id!!)
                                                          zone.isAi = true
                                                          listSpace.add(zone)
                                                      }
                                                  }
                                              }
                                          }
                                      }

                                    } else if (!zoneSpace.localId.isNullOrEmpty()){
                                        val listPr = ArrayList<String>()
                                        listPr.add("id")
                                        listPr.add("localId")
                                        val zoneSpaceLists = ZoneSpaceBaseService.select(SFilter.eq("projectId", projectId),
                                                SFilter.eq("buildingId", zoneSpace.buildingId!!),SFilter.eq("objectType", zoneSpace.objectType!!)
                                                , SFilter.not(SFilter.eq("id",zoneSpace.id!!)), SFilter.not(SFilter.isNull("localId")))
                                                .projection(listPr).exec()

                                            if (zoneSpaceLists.size>0){
                                                for (zoneSp in zoneSpaceLists){
                                                    setName.add(zoneSp.localId!!)
                                                }
                                            }
                                        val similarList = equipZoneMapper.similarTwo(zoneSpace.localId!!, setName.toList(),setName.toList().size)
                                        val gson = Gson()
                                        val type: Type = object : TypeToken<List<SimilarityDegree>>() {}.getType()
                                        val lists: List<SimilarityDegree> = gson.fromJson(similarList, type)
                                        if (lists.size>0){
                                            val setName = HashSet<String>()
                                            for (similarit in lists){
                                                if (similarit.value!! >0.9){
                                                    setName.add(similarit.key!!)
                                                }
                                            }
                                            if (setName.size>0){

                                                var zoneSpaceList:ArrayList<ZoneSpace>
                                                if (listid.size>0){
                                                    zoneSpaceList = zoneSpaceService.select(SFilter.eq("projectId", projectId), SFilter.inList("localId", setName.toList()), SFilter.eq("objectType", objectType!!),SFilter.not(SFilter.inList("id",listid))).exec()
                                                }else{
                                                    zoneSpaceList = zoneSpaceService.select(SFilter.eq("projectId", projectId), SFilter.inList("localId", setName.toList()), SFilter.eq("objectType", objectType!!)).exec()
                                                }


                                                if (zoneSpaceList.size>0){
                                                    for (zone in zoneSpaceList){
                                                        listid.add(zone.id!!)
                                                        zone.isAi = true
                                                        listSpace.add(zone)
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                              }
                            }

                            /** 删除和已经有的数据重复 */
                            if (zoneSpaceListBase.size>0){
                                for (zoneSpace in zoneSpaceListBase){
                                    val iterator1 = listSpace.iterator()
                                    while (iterator1.hasNext()){
                                        val next = iterator1.next()
                                        if (next.id == zoneSpace.id){
                                            iterator1.remove()
                                        }
                                    }
                                }
                            }

                            /** 处理 相似度匹配的 的数据 **************************************************************/
                            if (listSpace.size>0){
                                for (floor in floorList){
                                    for (space in listSpace){
                                        if (floor.id ==space.floorId){
                                            if (floor.zoneSpaceList!=null&&floor.zoneSpaceList!!.size>0){
                                                floor.zoneSpaceList!!.add(space)
                                            }else{
                                                val list = ArrayList<ZoneSpace>()
                                                list.add(space)
                                                floor.zoneSpaceList=list
                                            }
                                        }
                                    }
                                }
                            }

                            /**  垂直关系匹配**********************************************************************************/
                            val listSp = ArrayList<ZoneSpace>()
                            /** 处理垂直关系 */
                            /** 处理垂直交通关系 */
                            for (floor in floorList){

                                /** 处理有业务空间的空间 */
                                if (floor.zoneSpaceList!=null&&floor.zoneSpaceList!!.size>0){
                                    val setIds = ArrayList<String>()
                                    for (zoneSpace in floor.zoneSpaceList!!){
                                        /** 排除AI识别的 */
                                        if (!zoneSpace.isAi){

                                            /** 查询空间的垂直交通关系 其它 */
                                            val rSpVerticalSpOtherList = rSpVerticalSpService.select(SFilter.eq("projectId", projectId),SFilter.eq("spaceOtherId", zoneSpace.id!!),SFilter.eq("objectType", zoneSpace.objectType!!)).exec()
                                            if (rSpVerticalSpOtherList.size>0){

                                                /** 排除已经关联的数据 */
                                                if (zoneSpace.spaceIdList.size>0){

                                                    for (spaceId in zoneSpace.spaceIdList){
                                                        val iterator1 = rSpVerticalSpOtherList.iterator()
                                                        while (iterator1.hasNext()){
                                                            val next = iterator1.next()
                                                            if (next.spaceId == spaceId||next.spaceId == zoneSpace.id){
                                                                /** 清除重复的id */
                                                                iterator1.remove()
                                                            }
                                                        }
                                                    }
                                                }else{
                                                    val iterator1 = rSpVerticalSpOtherList.iterator()
                                                    while (iterator1.hasNext()){
                                                        val next = iterator1.next()
                                                        if (next.spaceId == zoneSpace.id){
                                                            /** 清除重复的id */
                                                            iterator1.remove()
                                                        }
                                                    }
                                                }
                                                if (rSpVerticalSpOtherList.size>0){
                                                    /** 处理去重重复以后的id */
                                                    val setListId = HashSet<String>()
                                                    if (setIds.size>0){
                                                        val iterator1 = rSpVerticalSpOtherList.iterator()
                                                        while (iterator1.hasNext()) {
                                                            val next = iterator1.next()
                                                            for (ids in setIds){
                                                                if (next.spaceId == ids ){
                                                                    iterator1.remove()
                                                                }
                                                            }
                                                        }

                                                        if (rSpVerticalSpOtherList.size>0){
                                                            val iterator2 = rSpVerticalSpOtherList.iterator()
                                                            while (iterator2.hasNext()) {
                                                                val next = iterator2.next()
                                                                setListId.add(next.spaceId!!)
                                                                setIds.add(next.spaceId!!)
                                                            }
                                                        }

                                                    }else{
                                                        val iterator2 = rSpVerticalSpOtherList.iterator()
                                                        while (iterator2.hasNext()) {
                                                            val next = iterator2.next()
                                                            setListId.add(next.spaceId!!)
                                                            setIds.add(next.spaceId!!)
                                                        }
                                                    }

                                                    if (setListId.size>0){
                                                        for (id in setListId){
                                                            /** 处理垂直的数据 */
                                                        val entity = zoneSpaceService.select(SFilter.eq("projectId", projectId), SFilter.eq("id", id),SFilter.eq("objectType", objectType)).entity()
                                                            if (entity!=null){
                                                                entity.isAi = true
                                                                entity.spaceIdList.add(zoneSpace.id!!)
                                                                listSp.add(entity)
                                                            }
                                                        }
                                                    }

//                                                    val iterator1 = rSpVerticalSpOtherList.iterator()
//                                                    while (iterator1.hasNext()) {
//                                                        val next = iterator1.next()
//                                                        zoneSpace.spaceIdList.add(next.spaceId!!)
//
//                                                        /** 处理垂直的数据 */
//                                                        val entity = zoneSpaceService.select(SFilter.eq("projectId", projectId), SFilter.eq("id", next.spaceId!!),SFilter.eq("objectType", next.objectType!!)).entity()
//                                                            if (entity!=null){
//                                                                entity.isAi = true
//                                                                entity.spaceIdList.add(zoneSpace.id!!)
//                                                                listSp.add(entity)
//                                                            }
//                                                    }
                                                }
                                            }
                                            /** 查询空间的垂直交通关系 其它 */
                                            val rSpVerticalSpOtherListv = rSpVerticalSpService.select(SFilter.eq("projectId", projectId),SFilter.eq("spaceId", zoneSpace.id!!),SFilter.eq("objectType", zoneSpace.objectType!!)).exec()
                                            if (rSpVerticalSpOtherListv.size>0){
                                                /** 排除已经关联的数据 */
                                                if (zoneSpace.spaceIdList.size>0){

                                                    for (spaceId in zoneSpace.spaceIdList){

                                                        val iterator1 = rSpVerticalSpOtherListv.iterator()
                                                        while (iterator1.hasNext()){
                                                            val next = iterator1.next()
                                                            if (next.spaceOtherId == spaceId||next.spaceOtherId == zoneSpace.id){
                                                                /** 清除重复的id */
                                                                iterator1.remove()
                                                            }
                                                        }
                                                    }

                                                }else{
                                                    val iterator1 = rSpVerticalSpOtherListv.iterator()
                                                    while (iterator1.hasNext()){
                                                        val next = iterator1.next()
                                                        if (next.spaceOtherId == zoneSpace.id){
                                                            /** 清除重复的id */
                                                            iterator1.remove()
                                                        }

                                                    }
                                                }
                                                if (rSpVerticalSpOtherListv.size>0){
                                                    /** 处理去重重复以后的id */
                                                    /** 处理去重重复以后的id */
                                                    val setListId = HashSet<String>()
                                                    if (setIds.size>0){
                                                        val iterator1 = rSpVerticalSpOtherListv.iterator()
                                                        while (iterator1.hasNext()) {
                                                            val next = iterator1.next()
                                                            for (ids in setIds){
                                                                if (next.spaceId == ids ){
                                                                    iterator1.remove()
                                                                }
                                                            }
                                                        }

                                                        if (rSpVerticalSpOtherListv.size>0){
                                                            val iterator2 = rSpVerticalSpOtherListv.iterator()
                                                            while (iterator2.hasNext()) {
                                                                val next = iterator2.next()
                                                                setListId.add(next.spaceId!!)
                                                                setIds.add(next.spaceId!!)
                                                            }
                                                        }

                                                    }else{
                                                        val iterator2 = rSpVerticalSpOtherListv.iterator()
                                                        while (iterator2.hasNext()) {
                                                            val next = iterator2.next()
                                                            setListId.add(next.spaceId!!)
                                                            setIds.add(next.spaceId!!)
                                                        }
                                                    }

                                                    if (setListId.size>0){
                                                        for (id in setListId){
                                                            val entity = zoneSpaceService.select(SFilter.eq("projectId", projectId), SFilter.eq("id", id!!),SFilter.eq("objectType", objectType)).entity()
                                                            if (entity!=null){
                                                                entity.isAi = true
                                                                entity.spaceIdList.add(zoneSpace.id!!)
                                                                listSp.add(entity)
                                                            }
                                                        }
                                                    }

//                                                    val iterator1 = rSpVerticalSpOtherListv.iterator()
//                                                    while (iterator1.hasNext()) {
//                                                        val next = iterator1.next()
//                                                        zoneSpace.spaceIdList.add(next.spaceOtherId!!)
//
//                                                        /** 查询匹配到的对象 */
//                                                        /** 处理垂直的数据 */
//                                                        val entity = zoneSpaceService.select(SFilter.eq("projectId", projectId), SFilter.eq("id", next.spaceId!!),SFilter.eq("objectType", next.objectType!!)).entity()
//                                                        if (entity!=null){
//                                                            entity.isAi = true
//                                                            entity.spaceIdList.add(zoneSpace.id!!)
//                                                            listSp.add(entity)
//                                                        }
//                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }

                            /** 清除和已经关联的和  相似度匹配的 */
                            if (zoneSpaceListBase.size>0){
                                for (zoneSpace in zoneSpaceListBase){
                                    val iterator1 = listSp.iterator()
                                    while (iterator1.hasNext()){
                                        val next = iterator1.next()
                                        if (next.id == zoneSpace.id){
                                            iterator1.remove()
                                        }
                                    }
                                }
                            }

                            /** 相似度的 */
                            if (listSpace.size>0){
                                for (zoneSpace in listSpace){
                                    val iterator1 = listSp.iterator()
                                    while (iterator1.hasNext()){
                                        val next = iterator1.next()
                                        if (next.id == zoneSpace.id){
                                            iterator1.remove()
                                        }
                                    }
                                }
                            }

                            /** 处理垂直AI 的数据 **************************************************************/
                            if (listSp.size>0){
                                for (floor in floorList){
                                    for (space in listSp){
                                        if (floor.id ==space.floorId){
                                            if (floor.zoneSpaceList!=null&&floor.zoneSpaceList!!.size>0){
                                                floor.zoneSpaceList!!.add(space)
                                            }else{
                                                val list = ArrayList<ZoneSpace>()
                                                list.add(space)
                                                floor.zoneSpaceList=list
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            if (floorList.size>0){
                val sQueryResponse = SQueryResponse<FloorCompon>(SResponseType.success)
                sQueryResponse.content = floorList
                sQueryResponse
            }else{
                val sQueryResponse = SQueryResponse<FloorCompon>(SResponseType.failure)
                sQueryResponse.message = "暂无楼层数据"
                sQueryResponse
            }
        } catch (e: Exception) {
            e.printStackTrace()
           return  SQueryResponse<FloorCompon>(SResponseType.success,e.message.toString())
        }

    } // Function

    /**
     * 计算垂直交通关系
     */
    fun spVerticalSp(request: ShaftSpaceVertical): SQueryResponse<FloorCompon>{

        try {
            if (request.buildingId.isNullOrEmpty()||request.objectType.isNullOrEmpty()||request.shaftId.isNullOrEmpty()){
                val sQueryResponse = SQueryResponse<FloorCompon>(SResponseType.failure)
                sQueryResponse.message = "入参不能为空"
                return  sQueryResponse

            }
            val projectId = SPageContext.getHeader("projectId")

            /** 创建线程池  */
            val executorService = Executors.newFixedThreadPool(10)
            val buildingId =  request.buildingId!!
            val shaftId =  request.shaftId!!
            val objectType = request.objectType!!

            /** 项目下的竖井关联的空间  对应空间类型*/
            val rShContainSpBaseList = rShContainSpBaseService.select(SFilter.eq("projectId",projectId!!),SFilter.eq("Id1", shaftId),SFilter.eq("ZoneType", objectType), SFilter.eq("type","sh2sp")).exec()

            if (rShContainSpBaseList.size>0){
                val list = ArrayList<String>()
                for (rShContainSpBase in rShContainSpBaseList){
                    list.add(rShContainSpBase.id2!!)
                }
                executorService.execute(SpaceRunnables(projectId, buildingId, objectType, list))
//                val spVerticallySp = equipZoneMapper.spVerticallySp(projectId, buildingId, objectType, list)
//                logger.debug("计算结果=${spVerticallySp}")
            }


        } catch (e: Exception) {
            e.printStackTrace()
        }
        return SQueryResponse(SResponseType.success)

    } // Fun




} // Object ShaftService


















