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


import com.persagy.database.SFilter
import com.persagy.mybatis.SMybatisDao
import com.persagy.server.Opt
import com.persagy.server.datacenter.models.entities.assistant.DictSpaceProject
import com.persagy.server.datacenter.datacenter.models.entities.dictnew.DefClass
import com.persagy.server.datacenter.datacenter.models.response.SpaceSQueryResponse
import com.persagy.server.datacenter.models.entities.graphtype.RelationTypeProject
import com.persagy.server.datacenter.models.entities.objects.SpaceOutline
import com.persagy.server.datacenter.models.entities.rel.RSpinBd
import com.persagy.server.datacenter.models.entities.rel.RSpinFl
import com.persagy.server.datacenter.models.requests.DCSQueryRequest
import com.persagy.server.datacenter.models.requests.UpDataFloorAndBuildingRequests
import com.persagy.server.datacenter.wanda.obj.Building
import com.persagy.server.datacenter.wanda.obj.ZoneSpace
import com.persagy.server.services.assistant.ObjInfoService
import com.persagy.server.services.base.ServiceSpace
import com.persagy.server.services.log.DataCenterLogsService
import com.persagy.server.services.objects.FloorService
import com.persagy.server.services.rel.RSpInBdService
import com.persagy.server.services.rel.RSpInFlService
import com.persagy.server.utils.IdUtils
import com.persagy.server.utils.ShaftNameUtil
import com.persagy.service.SBaseService
import com.persagy.service.SObjectService
import com.persagy.service.SPageContext
import com.persagy.service.models.enums.SResponseType
import com.persagy.service.models.requests.SUpdateRequest
import com.persagy.service.models.responses.SBaseResponse
import com.persagy.service.models.responses.SQueryResponse
import org.slf4j.LoggerFactory

/**
 * 空间父类
 *
 * @author 邓婷婷
 */
object ZoneSpaceBaseService : ServiceSpace<ZoneSpace>(SMybatisDao(ZoneSpace::class.java)) {

    /** 日志 */
    private val logger = LoggerFactory.getLogger(EquipmentService::class.java)
    /** 关系总览服务 */
    val relationTypeProjectservice = SBaseService(SMybatisDao(RelationTypeProject::class.java))
    /** 空间轮廓线 */
    val spaceOutlineService = SObjectService(SMybatisDao(SpaceOutline::class.java))

    /** 空间轮廓线 */
    val defClassService = SObjectService(SMybatisDao(DefClass::class.java))


    /**
     * 查询业务空间
     */
    fun querySpaces (request: DCSQueryRequest): SpaceSQueryResponse {
        return try {

            val projectId = SPageContext.getHeader("projectId")
            var spaceSQueryResponse = SpaceSQueryResponse()
            spaceSQueryResponse.zoneType = request.zoneType
            val listId = ArrayList<String>()
            if(!request.buildingId.isNullOrEmpty()&&!request.floorId.isNullOrEmpty()&&request.floorId != "isnull"){
                var rSpInFlList: ArrayList<RSpinFl>? = null
                if (!request.zoneType.isNullOrEmpty()){
                     rSpInFlList =  RSpInFlService.select(SFilter.eq("projectId",projectId!!),SFilter.eq("floorId",request.floorId!!),SFilter.eq("objectType",request.zoneType!!)).exec()
                }else if (!request.zoneTypeList.isNullOrEmpty()){
                     rSpInFlList =  RSpInFlService.select(SFilter.eq("projectId",projectId!!),SFilter.eq("floorId",request.floorId!!),SFilter.inList("objectType",request.zoneTypeList!!)).exec()
                }else{
                    rSpInFlList =  RSpInFlService.select(SFilter.eq("projectId",projectId!!),SFilter.eq("floorId",request.floorId!!)).exec()
                }
                if (rSpInFlList!=null&&rSpInFlList.size>0){

                    for (rSpInFl in rSpInFlList){
                        listId.add(rSpInFl.spaceId!!)
                    }
                }
                if (listId.size>0){
                    var listSFilter = ArrayList<SFilter>()
                    listSFilter.add(SFilter.eq("projectId",projectId))
                    listSFilter.add(SFilter.inList("id",listId))
                    if (!request.zoneType.isNullOrEmpty()){
                        listSFilter.add(SFilter.eq("classCode",request.zoneType!!))
                    }else if (!request.zoneTypeList.isNullOrEmpty()){
                        listSFilter.add(SFilter.inList("classCode",request.zoneTypeList!!))
                    }
                  val  pageQuery = pageQuery(request, listSFilter)
                    spaceSQueryResponse.message = pageQuery.message
                    spaceSQueryResponse.total = pageQuery.total
                    spaceSQueryResponse.result = pageQuery.result
                    spaceSQueryResponse.pageNumber = pageQuery.pageNumber
                    spaceSQueryResponse.pageSize = pageQuery.pageSize
                    spaceSQueryResponse.content = pageQuery.content
                    spaceSQueryResponse
                }

            }else if(!request.buildingId.isNullOrEmpty()&&!request.floorId.isNullOrEmpty()&&request.floorId == "isnull"){

               val floorList = FloorService.select(SFilter.eq("projectId",projectId!!),SFilter.eq("buildingId",request.buildingId!!)).exec()
               if (floorList.size>0){
                   val listFloorId = ArrayList<String>()
                   for (floor in floorList){
                       listFloorId.add(floor.id!!)
                   }
                   if (!request.zoneType.isNullOrEmpty()){
                       val listSpId = ArrayList<String>()
                       val rSpInFlList = RSpInFlService.select(SFilter.eq("projectId", projectId), SFilter.inList("floorId", listFloorId),SFilter.eq("objectType",request.zoneType!!)).exec()
                       if (rSpInFlList.size>0){

                           for (rSpInFl in rSpInFlList){
                               listSpId.add(rSpInFl.spaceId!!)
                           }

                           val rSpInBdList = RSpInBdService.select(SFilter.eq("projectId", projectId), SFilter.eq("buildingId", request.buildingId!!), SFilter.eq("objectType", request.zoneType!!), SFilter.not(SFilter.inList("spaceId", listSpId))).exec()
                           if (rSpInBdList.size>0){
                               for (rSpInBd in rSpInBdList){
                                   listId.add(rSpInBd.spaceId!!)
                               }
                           }

                       }else{
                           val rSpInBdList = RSpInBdService.select(SFilter.eq("projectId", projectId), SFilter.eq("buildingId", request.buildingId!!), SFilter.eq("objectType", request.zoneType!!)).exec()

                           if (rSpInBdList.size>0){
                               for (rSpInBd in rSpInBdList){
                                   listId.add(rSpInBd.spaceId!!)
                               }
                           }
                       }

                       if (listId.size>0){
                           var listSFilter = ArrayList<SFilter>()
                           listSFilter.add(SFilter.eq("projectId",projectId))
                           listSFilter.add(SFilter.inList("id",listId))
                           listSFilter.add(SFilter.eq("objectType",request.zoneType!!))
                          val pageQuery = pageQuery(request, listSFilter)
                           spaceSQueryResponse.message = pageQuery.message
                           spaceSQueryResponse.total = pageQuery.total
                           spaceSQueryResponse.result = pageQuery.result
                           spaceSQueryResponse.pageNumber = pageQuery.pageNumber
                           spaceSQueryResponse.pageSize = pageQuery.pageSize
                           spaceSQueryResponse.content = pageQuery.content
                       }
                   }else {
                       val listSpId = ArrayList<String>()
                       var rSpInFlList: ArrayList<RSpinFl>? = null
                       if (!request.zoneTypeList.isNullOrEmpty()){
                            rSpInFlList = RSpInFlService.select(SFilter.eq("projectId", projectId), SFilter.inList("floorId", listFloorId),SFilter.inList("objectType",request.zoneTypeList!!)).exec()
                       }else{
                            rSpInFlList = RSpInFlService.select(SFilter.eq("projectId", projectId), SFilter.inList("floorId", listFloorId)).exec()
                       }

                       if (rSpInFlList.size>0){

                           for (rSpInFl in rSpInFlList){
                               listSpId.add(rSpInFl.spaceId!!)
                           }

                           val rSpInBdList = RSpInBdService.select(SFilter.eq("projectId", projectId), SFilter.eq("buildingId", request.buildingId!!), SFilter.eq("objectType", request.zoneType!!), SFilter.not(SFilter.inList("spaceId", listSpId))).exec()
                           if (rSpInBdList.size>0){
                               for (rSpInBd in rSpInBdList){
                                   listId.add(rSpInBd.spaceId!!)
                               }
                           }

                       }else{
                           var rSpInBdList: ArrayList<RSpinBd>
                           if (!request.zoneTypeList.isNullOrEmpty()){
                               rSpInBdList = RSpInBdService.select(SFilter.eq("projectId", projectId), SFilter.eq("buildingId", request.buildingId!!), SFilter.inList("objectType", request.zoneTypeList!!)).exec()
                           }else{
                               rSpInBdList = RSpInBdService.select(SFilter.eq("projectId", projectId), SFilter.eq("buildingId", request.buildingId!!)).exec()
                           }

                           if (rSpInBdList.size>0){
                               for (rSpInBd in rSpInBdList){
                                   listId.add(rSpInBd.spaceId!!)
                               }
                           }
                       }

                       if (listId.size>0){
                           var listSFilter = ArrayList<SFilter>()
                           listSFilter.add(SFilter.eq("projectId",projectId))
                           listSFilter.add(SFilter.inList("id",listId))
                           if (!request.zoneTypeList.isNullOrEmpty()){
                               listSFilter.add(SFilter.inList("classCode",request.zoneTypeList!!))
                           }
                         val   pageQuery = pageQuery(request, listSFilter)
                           spaceSQueryResponse.message = pageQuery.message
                           spaceSQueryResponse.total = pageQuery.total
                           spaceSQueryResponse.result = pageQuery.result
                           spaceSQueryResponse.pageNumber = pageQuery.pageNumber
                           spaceSQueryResponse.pageSize = pageQuery.pageSize
                           spaceSQueryResponse.content = pageQuery.content
                       }
                   }
               }else{
                   var rSpInFlList: ArrayList<RSpinBd>
                   if (!request.zoneType.isNullOrEmpty()){
                        rSpInFlList = RSpInBdService.select(SFilter.eq("projectId", projectId), SFilter.eq("buildingId", request.buildingId!!),SFilter.eq("objectType",request.zoneType!!)).exec()
                   }else if (!request.zoneTypeList.isNullOrEmpty()){
                        rSpInFlList = RSpInBdService.select(SFilter.eq("projectId", projectId), SFilter.eq("buildingId", request.buildingId!!),SFilter.inList("objectType",request.zoneTypeList!!)).exec()
                   }else{
                        rSpInFlList = RSpInBdService.select(SFilter.eq("projectId", projectId), SFilter.eq("buildingId", request.buildingId!!)).exec()
                   }

                   if (rSpInFlList.size>0){
                       for (rSpInFl in rSpInFlList){
                           listId.add(rSpInFl.spaceId!!)
                       }
                   }

                   if (listId.size>0){
                       var listSFilter = ArrayList<SFilter>()
                       listSFilter.add(SFilter.eq("projectId",projectId))
                       listSFilter.add(SFilter.inList("id",listId))
                       if (!request.zoneType.isNullOrEmpty()){
                           listSFilter.add(SFilter.eq("classCode",request.zoneType!!))
                       }else  if (!request.zoneTypeList.isNullOrEmpty()){
                           listSFilter.add(SFilter.inList("classCode",request.zoneTypeList!!))
                       }

                     val  pageQuery = pageQuery(request, listSFilter)
                       spaceSQueryResponse.message = pageQuery.message
                       spaceSQueryResponse.total = pageQuery.total
                       spaceSQueryResponse.result = pageQuery.result
                       spaceSQueryResponse.pageNumber = pageQuery.pageNumber
                       spaceSQueryResponse.pageSize = pageQuery.pageSize
                       spaceSQueryResponse.content = pageQuery.content
                   }

               }
            }else if (!request.buildingId.isNullOrEmpty()&&request.floorId.isNullOrEmpty()&&request.buildingId != "isnull"){
                var rSpInFlList: ArrayList<RSpinBd>
                if (!request.zoneType.isNullOrEmpty()){
                    rSpInFlList = RSpInBdService.select(SFilter.eq("projectId", projectId!!), SFilter.eq("buildingId", request.buildingId!!),SFilter.eq("objectType",request.zoneType!!)).exec()
                }else if (!request.zoneTypeList.isNullOrEmpty()){
                    rSpInFlList = RSpInBdService.select(SFilter.eq("projectId", projectId!!), SFilter.eq("buildingId", request.buildingId!!),SFilter.inList("objectType",request.zoneTypeList!!)).exec()
                }else{
                    rSpInFlList = RSpInBdService.select(SFilter.eq("projectId", projectId!!), SFilter.eq("buildingId", request.buildingId!!)).exec()
                }

                if (rSpInFlList.size>0){
                    for (rSpInFl in rSpInFlList){
                        listId.add(rSpInFl.spaceId!!)
                    }
                }
                if (listId.size>0){
                    var listSFilter = ArrayList<SFilter>()
                    listSFilter.add(SFilter.eq("projectId",projectId))
                    listSFilter.add(SFilter.inList("id",listId))
                    if (!request.zoneType.isNullOrEmpty()){
                        listSFilter.add(SFilter.eq("classCode",request.zoneType!!))
                    }else if (!request.zoneTypeList.isNullOrEmpty()){
                        listSFilter.add(SFilter.inList("classCode",request.zoneTypeList!!))
                    }
                   val pageQuery = pageQuery(request, listSFilter)
                    spaceSQueryResponse.message = pageQuery.message
                    spaceSQueryResponse.total = pageQuery.total
                    spaceSQueryResponse.result = pageQuery.result
                    spaceSQueryResponse.pageNumber = pageQuery.pageNumber
                    spaceSQueryResponse.pageSize = pageQuery.pageSize
                    spaceSQueryResponse.content = pageQuery.content
                }

            }else if (!request.buildingId.isNullOrEmpty()&&request.floorId.isNullOrEmpty()&&request.buildingId == "isnull"){
                logger.debug("未明确建筑****************************************************************************")
                val buildingList = BuildingService.select(SFilter.eq("projectId", projectId!!)).exec()

                if (buildingList.size>0){
                    logger.debug("未明确建筑4****************************************************************************")
                    for (building in buildingList){
                        val rSpInBdList : ArrayList<RSpinBd>
                        if (!request.zoneType.isNullOrEmpty()){
                            rSpInBdList = RSpInBdService.select(SFilter.eq("projectId", projectId), SFilter.eq("buildingId", building.id!!), SFilter.eq("objectType", request.zoneType!!)).exec()
                        }else if (!request.zoneTypeList.isNullOrEmpty()){
                            rSpInBdList = RSpInBdService.select(SFilter.eq("projectId", projectId), SFilter.eq("buildingId", building.id!!), SFilter.inList("objectType", request.zoneTypeList!!)).exec()
                        }else{
                            rSpInBdList =RSpInBdService.select(SFilter.eq("projectId", projectId), SFilter.eq("buildingId", building.id!!)).exec()
                        }

                        logger.debug("未明确建筑长度：${rSpInBdList.size}")
                            for (rSpInBd in rSpInBdList){
                                /** 需要被排除的空间id */
                                listId.add(rSpInBd.spaceId!!)
                            }
                    }

                            logger.debug("未明确建筑2****************************************************************************")
                            var listSFilter = ArrayList<SFilter>()
                            listSFilter.add(SFilter.eq("projectId",projectId))
                            if (listId.size>0) {
                                listSFilter.add(SFilter.not(SFilter.inList("id", listId)))
                            }
                            if (!request.zoneType.isNullOrEmpty()){
                                listSFilter.add(SFilter.eq("classCode",request.zoneType!!))
                            }else if (!request.zoneTypeList.isNullOrEmpty()){
                                listSFilter.add(SFilter.inList("classCode",request.zoneTypeList!!))
                            }
                           val pageQuery = pageQuery(request, listSFilter)
                    spaceSQueryResponse.message = pageQuery.message
                    spaceSQueryResponse.total = pageQuery.total
                    spaceSQueryResponse.result = pageQuery.result
                    spaceSQueryResponse.pageNumber = pageQuery.pageNumber
                    spaceSQueryResponse.pageSize = pageQuery.pageSize
                    spaceSQueryResponse.content = pageQuery.content
                    spaceSQueryResponse
                }else{
                    logger.debug("未明确建筑3****************************************************************************")
                        var listSFilter = ArrayList<SFilter>()
                        listSFilter.add(SFilter.eq("projectId",projectId))
                    if (!request.zoneType.isNullOrEmpty()){
                        listSFilter.add(SFilter.eq("classCode",request.zoneType!!))
                    }else if (!request.zoneTypeList.isNullOrEmpty()){
                        listSFilter.add(SFilter.inList("classCode",request.zoneTypeList!!))
                    }
                      val  pageQuery = pageQuery(request, listSFilter)
                    spaceSQueryResponse.message = pageQuery.message
                    spaceSQueryResponse.total = pageQuery.total
                    spaceSQueryResponse.result = pageQuery.result
                    spaceSQueryResponse.pageNumber = pageQuery.pageNumber
                    spaceSQueryResponse.pageSize = pageQuery.pageSize
                    spaceSQueryResponse.content = pageQuery.content
                }

            }else if (request.buildingId.isNullOrEmpty()&&request.floorId.isNullOrEmpty()){
                var listSFilter = ArrayList<SFilter>()
                listSFilter.add(SFilter.eq("projectId",projectId!!))
                if (!request.zoneType.isNullOrEmpty()){
                    listSFilter.add(SFilter.eq("classCode",request.zoneType!!))
                }else if (!request.zoneTypeList.isNullOrEmpty()){
                    listSFilter.add(SFilter.inList("classCode",request.zoneTypeList!!))
                }
              val   pageQuery = pageQuery(request, listSFilter)
                spaceSQueryResponse.message = pageQuery.message
                spaceSQueryResponse.total = pageQuery.total
                spaceSQueryResponse.result = pageQuery.result
                spaceSQueryResponse.pageNumber = pageQuery.pageNumber
                spaceSQueryResponse.pageSize = pageQuery.pageSize
                spaceSQueryResponse.content = pageQuery.content
            }

            if (spaceSQueryResponse.content.isNullOrEmpty()){
                spaceSQueryResponse.content = ArrayList<ZoneSpace>()
            }

            spaceSQueryResponse.zoneType = request.zoneType
            spaceSQueryResponse.floorId = request.floorId
            return spaceSQueryResponse
        } catch (e: Exception) {
            e.printStackTrace()
            val spaceSQueryResponse = SpaceSQueryResponse()
            spaceSQueryResponse.result = SResponseType.failure
            spaceSQueryResponse.message = e.message.toString()
            return spaceSQueryResponse
        }
    }


    /**
     * 查询业务空间的建筑楼层数量
     */
    fun spBuildingFloor(): SQueryResponse<Building>{
       return try {
            /** 项目id */
            val projectId = SPageContext.getHeader("projectId")

            val buildingList = BuildingService.select(SFilter.eq("projectId", projectId!!)).order("localName").exec()
            if (buildingList.size>0){
                for (building in buildingList){

                   val floorList = FloorService.select(SFilter.eq("projectId", projectId),SFilter.eq("buildingId", building.id!!)).order("floorSequenceId desc").exec()
                    if (floorList.size>0){
                        building .floor = floorList
                    }
                }

                for (building in buildingList){

                   if (building.floor!=null&&building.floor!!.size>0){
                       for (floor in building.floor!!){

                           val rSpInFlList = RSpInFlService.select(SFilter.eq("projectId", projectId),SFilter.eq("floorId", floor.id!!)).exec()
                           if (rSpInFlList.size>0){
                               val setId = HashSet<String>()
                               for (rSpInFl in rSpInFlList){
                                   setId.add(rSpInFl.spaceId!!)
                               }
                               val spaceList = select(SFilter.eq("projectId", projectId), SFilter.inList("id", setId.toList()), SFilter.eq("state", 1)).exec()
                               floor.count = spaceList.size
                           }
                       }
                   }
                }

            }
            val sQueryResponse = SQueryResponse<Building>(SResponseType.success)
            sQueryResponse.content=buildingList
            sQueryResponse.total = buildingList.size.toLong()
           sQueryResponse
        } catch (e: Exception) {
            e.printStackTrace()
           SQueryResponse<Building>(SResponseType.failure,e.message.toString())
        }
    }

    /**
     * 查询业务空间所有类型
     */
    fun zoneTypequery(request: DCSQueryRequest):SQueryResponse<DictSpaceProject> {
        try {
            /** 项目id */
            val projectId = SPageContext.getHeader("projectId")

            if (request.floorId.isNullOrEmpty()) {
                val pageQuery = defClassService.pageQuery(request, arrayListOf(SFilter.eq("projectId", projectId!!), SFilter.eq("parentCode","space")))
                val list = ArrayList<DictSpaceProject>()
                if (!pageQuery.content.isNullOrEmpty()) {
                    for (content in pageQuery.content!!) {
                        val dictSpaceProject = DictSpaceProject()
                        dictSpaceProject.projectId = projectId
                        dictSpaceProject.id = content.code
                        dictSpaceProject.name = content.name
                        list.add(dictSpaceProject)
                    }
                }
                val sQueryResponse = SQueryResponse<DictSpaceProject>()
                sQueryResponse.content = list
                sQueryResponse.total = list.size.toLong()
                sQueryResponse.result = SResponseType.success
                return sQueryResponse
            } else {
                val pageQuery = defClassService.pageQuery(request, arrayListOf(SFilter.eq("projectId", projectId!!), SFilter.eq("parentCode","space")))
                val contentList = pageQuery.content
                val list = ArrayList<DictSpaceProject>()
                if (contentList != null && contentList.size > 0) {

                    for (content in contentList) {
                        val dictSpaceProject = DictSpaceProject()
                        dictSpaceProject.projectId = projectId
                        dictSpaceProject.id = content.code
                        dictSpaceProject.name = content.name

                            val rSpInFlList = RSpInFlService.select(SFilter.eq("projectId", projectId!!), SFilter.eq("floorId", request.floorId!!), SFilter.eq("objectType", content.id!!)).exec()
                            if (rSpInFlList.size > 0) {
                                val idSet = ArrayList<String>()
                                for (rSpInFl in rSpInFlList) {
                                    idSet.add(rSpInFl.spaceId!!)
                                }
                                val zoneSpaceBaseList = ZoneSpaceBaseService.select(SFilter.eq("projectId", projectId), SFilter.inList("id", idSet.toList()), SFilter.eq("state", 1)).exec()
                                if (zoneSpaceBaseList.size > 0) {
                                    dictSpaceProject.count = zoneSpaceBaseList.size
                                }
                            }
                        list.add(dictSpaceProject)
                    }
                }
                val sQueryResponse = SQueryResponse<DictSpaceProject>()
                sQueryResponse.content = list
                sQueryResponse.total = list.size.toLong()
                sQueryResponse.result = SResponseType.success
                return sQueryResponse
            }
        } catch (e: Exception) {
            e.printStackTrace()
            return SQueryResponse(SResponseType.failure,e.message.toString())
        }
    } // Fun


    override fun onCreateBefore(entity: ZoneSpace): Boolean {
        if (entity.classCode.isNullOrEmpty()){
            return false
        }

        val projectId = SPageContext.getHeader("projectId")
        /** id */
        if (!entity.id.isNullOrEmpty()) {
            val isWith = entity.id!!.startsWith("Sp")
            if (!isWith) {
                entity.id = IdUtils.spaceIdCreate(Opt.projectId!!)
            }
        } else {
            entity.id = IdUtils.spaceIdCreate(Opt.projectId!!)
        }
        entity.projectId = projectId
        /** 空间名字 */
        if (entity.name.isNullOrEmpty()) {
            entity.name = ShaftNameUtil.shaftName("空间-")
        }
        try {
            if (entity.height==null||entity.height ==0.0){
                if (!entity.floorId.isNullOrEmpty()){
                    val entity1 = FloorService.select(SFilter.eq("id", entity.floorId!!)).entity()
                    if (entity1!=null){
                        if (!entity1.infos.isNullOrEmpty()){
                            val containsKey = entity1.infos!!.containsKey(Opt.HEIGHt)
                            if (containsKey){
                                entity.height = entity1.infos!![Opt.HEIGHt].toString().toDoubleOrNull()
                            }
                        }
                    }
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }

        try {
//            if (!entity.outline.isNullOrEmpty()) {
//                val spaceOutline = SpaceOutline()
//                spaceOutline.id = entity.id
//                spaceOutline.outline = entity.outline
//                spaceOutlineService.replace(spaceOutline)
//            }
        } catch (e: Exception) {
            e.printStackTrace()
        }

        return true
    }

    override fun onCreateSuccess(entityList: ArrayList<ZoneSpace>) {
        super.onCreateSuccess(entityList)
        for (zoneSpace in entityList) {
            try {
               rabbitMqService.sendObjects(Opt.projectId!!, Opt.CREATE,zoneSpace.id!!)
            } catch (e: Exception) {
                e.printStackTrace()
            }
            try {
                /** 日志 */
                DataCenterLogsService.addLogs(
                    Opt.projectId!!,"create",zoneSpace.classCode!!,
                    zoneSpace.id!!,zoneSpace, Opt.account, Opt.coming)
            } catch (e: Exception) {
                e.printStackTrace()
            }
//            ObjInfoService.createInfo(zoneSpace,true,"Sp",zoneSpace.classCode!!)
        }
//        val projectId = SPageContext.getHeader("projectId")
//        try {
//            for (space in entityList){
//                if (!space.buildingId.isNullOrEmpty()&&!space.floorId.isNullOrEmpty()){
//                    val rSpinBd = RSpinBd()
//                    rSpinBd.buildingId = space.buildingId
//                    rSpinBd.objectType = space.classCode
//                    rSpinBd.projectId = space.projectId
//                    rSpinBd.spaceId = space.id
//                    rSpinBd.sign = 1
//                    RSpInBdService.insert(rSpinBd)
//                    val rSpinFl = RSpinFl()
//                    rSpinFl.buildingId = space.buildingId
//                    rSpinFl.floorId = space.floorId
//                    rSpinFl.projectId = space.projectId
//                    rSpinFl.objectType = space.classCode
//                    rSpinFl.spaceId = space.id
//                    rSpinFl.sign = 1
//                    RSpInFlService.insert(rSpinFl)
//                }else if (!space.buildingId.isNullOrEmpty()&&space.floorId.isNullOrEmpty()){
//                    val rSpinBd = RSpinBd()
//                    rSpinBd.buildingId = space.buildingId
//                    rSpinBd.objectType = space.classCode
//                    rSpinBd.projectId = space.projectId
//                    rSpinBd.spaceId = space.id
//                    rSpinBd.sign = 1
//                    RSpInBdService.insert(rSpinBd)
//                }
//            }
//        } catch (e: Exception) {
//            e.printStackTrace()
//        }
//
//        try {
//            /** 处理关系标记 */
//            val listtype = ArrayList<String>()
//            listtype.add("sp2sp_SpaceNeighborhood_5")
//            listtype.add("sp2sp_TrafficNetwork_1")
//            listtype.add("sp2sp_ConvectionNetwork_1")
//            listtype.add("sp2sp_ConvectionNetwork_2")
//            listtype.add("sp2sp_ConvectionNetwork_3")
//            listtype.add("sp2sp_RadiationNetwork_1")
//            listtype.add("sp2sp_RadiationNetwork_2")
//            for (type in listtype){
//                val entity = relationTypeProjectservice.select(SFilter.eq("projectId", projectId!!), SFilter.eq("relationType", type)).entity()
//                if (entity!=null){
//                    entity.computationalState = 2
//                    relationTypeProjectservice.update(entity)
//                }
//            }
//            val zoneSpace = entityList[0]
//
//            val relationType = relationTypeProjectservice.select(SFilter.eq("projectId", projectId!!), SFilter.eq("relationType", "eq2sp_in"),SFilter.eq("zoneType", zoneSpace.classCode!!)).entity()
//            if (relationType!=null){
//                    relationType.computationalState = 2
//                    relationTypeProjectservice.update(relationType)
//            }
//        } catch (e: Exception) {
//            e.printStackTrace()
//        }
    }

    override fun onUpdateBefore(entity: ZoneSpace): Boolean {

//        val list = ArrayList<String>()
//        list.add("id")
//        list.add("name")
//        ZoneSpaceBaseService.update(entity,list)

        return !entity.classCode.isNullOrEmpty()
    }

    override fun onUpdateSuccess(entityList: ArrayList<ZoneSpace>) {
        super.onUpdateSuccess(entityList)
        for (zoneSpace in entityList) {
            try {
                rabbitMqService.sendObjects(Opt.projectId!!, Opt.UPDATE,zoneSpace.id!!)
            } catch (e: Exception) {
                e.printStackTrace()
            }
            try {
                /** 日志 */
                DataCenterLogsService.addLogs(
                    Opt.projectId!!,"update",zoneSpace.classCode!!,
                    zoneSpace.id!!,zoneSpace, Opt.account, Opt.coming)
                ObjInfoService.createInfo(zoneSpace,false,"Sp",zoneSpace.classCode!!)
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }

//        try {
//            for (entity in entityList){
//                try {
//                    if (!Opt.coming.isNullOrEmpty()){
//                        if (Opt.coming == "app"){
//                            val user = LocationPointServices.userService.select(SFilter.eq("projectId", Opt.projectId!!), SFilter.eq("phone", Opt.account!!)).entity()
//                            val userLog = UserLog()
//                            userLog.comming = "app"
//                            userLog.action = "更新空间"
//                            userLog.id = IdUtils.uuidCreate()
//                            userLog.note = "项目ID: "+Opt.projectId+" 用户ID："+user!!.id+" 手机号："+ Opt.account+" 请求来源："+Opt.coming+ " "+entity.toJson()
//                            userLog.phone = Opt.account
//                            userLog.projectId = Opt.projectId
//                            userLog.userId = user.id
//                            userLog.name = user.name
//                            userLog.objectId = entity.id
//                            LocationPointServices.userLogService.insert(userLog)
//                        }
//                    }
//                } catch (e: Exception) {
//                    e.printStackTrace()
//                }
//            }
//        } catch (e: Exception) {
//            e.printStackTrace()
//        }
    }


    /**
     * 修改空间对象的楼层建筑信息
     *
     * @param   upDataFloorAndBuildingRequestsList
     *
     */
    fun upDataFloorORBuilding(upDataFloorAndBuildingRequestsList :ArrayList<UpDataFloorAndBuildingRequests>): SBaseResponse {
        try {
            val projectId = SPageContext.getHeader("projectId")

            if (upDataFloorAndBuildingRequestsList.isNullOrEmpty()) {
                val sBaseResponse = SBaseResponse(SResponseType.failure)
                sBaseResponse.message = "不可以传入空数据"
                return sBaseResponse
            }
            for (upDataFloorAndBuildingRequests in upDataFloorAndBuildingRequestsList) {

                val spaceId = upDataFloorAndBuildingRequests.spaceId
                val id = upDataFloorAndBuildingRequests.id
                val type = upDataFloorAndBuildingRequests.type

                try {
                    if (!id.isNullOrEmpty()) {
                        if (id.startsWith("Fl")) {
                            val floor = FloorService.select(SFilter.eq("projectId", projectId!!), SFilter.eq("id", id)).entity()

                            if (floor!=null){
                                /** 清除空间和建筑楼层的的数据 */
                                RSpInBdService.delete(SFilter.eq("projectId",projectId),SFilter.eq("spaceId",spaceId!!))
                                RSpInFlService.delete(SFilter.eq("projectId",projectId),SFilter.eq("spaceId",spaceId))
                                val rSpinFl = RSpinFl()
                                rSpinFl.floorId = floor.id
                                rSpinFl.projectId = projectId
                                rSpinFl.spaceId = spaceId
                                rSpinFl.objectType = type
                                rSpinFl.sign = 1
                                RSpInFlService.replace(rSpinFl)

                                val rSpinBd = RSpinBd()
                                rSpinBd.objectType = type
                                rSpinBd.buildingId = floor.buildingId
                                rSpinBd.spaceId = spaceId
                                rSpinBd.sign = 1
                                rSpinBd.projectId = projectId
                                RSpInBdService.replace(rSpinBd)
                            }

                        } else if (id.startsWith("Bd")) {
                            /** 清除空间和建筑楼层的的数据 */
                            RSpInBdService.delete(SFilter.eq("projectId",projectId!!),SFilter.eq("spaceId",spaceId!!))
                            RSpInFlService.delete(SFilter.eq("projectId",projectId),SFilter.eq("spaceId",spaceId))
                            val rSpinBd = RSpinBd()
                            rSpinBd.objectType = type
                            rSpinBd.buildingId = id
                            rSpinBd.spaceId = spaceId
                            rSpinBd.sign = 1
                            rSpinBd.projectId = projectId
                            RSpInBdService.replace(rSpinBd)
                        }
                    } else {
                        /** 清除空间和建筑楼层的的数据 */
                        RSpInBdService.delete(SFilter.eq("projectId",projectId!!),SFilter.eq("spaceId",spaceId!!))
                        RSpInFlService.delete(SFilter.eq("projectId",projectId),SFilter.eq("spaceId",spaceId))
                    }
                } catch (e: Exception) {
                    e.printStackTrace()
                }
                val zoneSpace = ZoneSpace()
                zoneSpace.id = spaceId
                if (!id.isNullOrEmpty()) {
                    if (id.startsWith("Fl")) {
                        zoneSpace.floorId = id
                        val floor = FloorService.select(SFilter.eq("projectId", projectId!!), SFilter.eq("id", id)).entity()
                        zoneSpace.buildingId = floor?.buildingId
                        val list = ArrayList<String>()
                        list.add("floorId")
                        list.add("buildingId")
                       update(zoneSpace, list)
                    } else if (id.startsWith("Bd")) {
                        val list = ArrayList<String>()
                        list.add("floorId")
                        list.add("buildingId")
                        zoneSpace.buildingId = id
                        zoneSpace.floorId = null
                        update(zoneSpace, list)
                    }
                } else {
                    /** 清空建筑楼层 */
                    val list = ArrayList<String>()
                    list.add("floorId")
                    list.add("buildingId")
                    zoneSpace.buildingId = null
                    zoneSpace.floorId = null
                    update(zoneSpace, list)
                }
                rabbitMqService.sendObjects(Opt.projectId!!, Opt.UPDATE,zoneSpace.id!!)
            }
            return SBaseResponse(SResponseType.success)
        } catch (e: Exception) {
            e.printStackTrace()
            return SBaseResponse(SResponseType.failure)
        }

    } // Function upDataFloorORBuilding

//    /**
//     * 发送消息
//     *
//     * @param   projectId   项目id
//     * @param   spaceId     业务空间id
//     * @param   id          楼层或建筑id
//     */
//    fun sendMessages(projectId: String, spaceId: String,id: String?){
//        try {
//            val jsonObject = JSONObject()
//            jsonObject.put("type", "20")
//            jsonObject.put("projectId", projectId)
//            jsonObject.put("id", spaceId)
//            jsonObject.put("linkId",id)
//            /** 发送消息 */
//            messageHandler.sendMessage(jsonObject.toString())
//        } catch (e: Exception) {
//            e.printStackTrace()
//        }
//    }

    /**
     * 修改空间轮廓线
     *
     * @param request   空间对象列表
     * @return 返回状态
     */
    fun upDateOutline(request: SUpdateRequest<ZoneSpace>): SBaseResponse{
        val projectId = SPageContext.getHeader("projectId")
        val zoneSpace = request.content!![0]
        val updateList = ZoneSpaceBaseService.updateList(request)

        rabbitMqService.sendObjects(Opt.projectId!!, Opt.UPDATE,zoneSpace.id!!)
        try {
            /** 处理关系标记 */
            val listtype = ArrayList<String>()
            listtype.add("sp2sp_SpaceNeighborhood_5")
            listtype.add("sp2sp_TrafficNetwork_3")
            listtype.add("sp2sp_ConvectionNetwork_1")
            listtype.add("sp2sp_ConvectionNetwork_2")
            listtype.add("sp2sp_ConvectionNetwork_3")
            listtype.add("sp2sp_RadiationNetwork_1")
            listtype.add("sp2sp_RadiationNetwork_2")
            for (type in listtype){
                val entity = relationTypeProjectservice.select(SFilter.eq("projectId", projectId!!), SFilter.eq("relationType", type)).entity()
                if (entity!=null){
                    entity.computationalState = 2
                    relationTypeProjectservice.update(entity)
                }
            }
            val relationType = relationTypeProjectservice.select(SFilter.eq("projectId", projectId!!), SFilter.eq("relationType", "eq2sp_in"),SFilter.eq("zoneType", zoneSpace.classCode!!)).entity()
            if (relationType!=null){
                relationType.computationalState = 2
                relationTypeProjectservice.update(relationType)
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return updateList
    }

    /**
     * @Description:删除成功之后
     */
    override fun onDeleteSuccess(entityList: ArrayList<ZoneSpace>) {
        super.onDeleteSuccess(entityList)
        try {
            val projectId = SPageContext.getHeader("projectId")
            for (zoneSpace in entityList) {
                try {
                    rabbitMqService.sendObjects(Opt.projectId!!, Opt.DELETE,zoneSpace.id!!)
                } catch (e: Exception) {
                    e.printStackTrace()
                }
                try {
                    /** 日志 */
                    DataCenterLogsService.addLogs(
                        Opt.projectId!!,"delete",zoneSpace.classCode!!,
                        zoneSpace.id!!,zoneSpace, Opt.account, Opt.coming)
                    ObjInfoService.delete(SFilter.eq("projectId",projectId!!), SFilter.eq("objId",zoneSpace.id!!), SFilter.eq("objType","Sp"))
                } catch (e: Exception) {
                    e.printStackTrace()
                }

            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

}