/*
 * ********************************************************************************************************************
 *
 *                     :*$@@%$*:                         ;:                ;;    ;;
 *                   :@@%!  :!@@%:                       %!             ;%%@@%$ =@@@@@@@%;     @%@@@%%%%@@@@@
 *                  :@%;       :$=                       %%$$$%$$         ;$$  ;$@=   !@$
 *                  =@!                                  %!              @ $=;%   !@@@%:      !$$$$$$$$$$$$$$=
 *                  =@*                                  %!              @ $= % %@=   =%@!      %=
 *             *$%%! @@=        ;=$%%%$*:                %!              @ $= % =%%%%%%@$      *%:         =%
 *           %@@!:    !@@@%=$@@@@%!  :*@@$:              %!              @ $= % $*     ;@      @*          :%*
 *         ;@@!          ;!!!;:         ;@%:      =======@%========*     @ $$ % $%*****$@     :@$=*********=@$
 *         $@*   ;@@@%=!:                *@*
 *         =@$    ;;;!=%@@@@=!           =@!
 *          %@$:      =@%: :*@@@*       %@=                    Copyright (c) 2016-2019.  北京上格云技术有限公司
 *           ;%@@$=$@@%*       *@@@$=%@@%;
 *              ::;::             ::;::                                              All rights reserved.
 *
 * ********************************************************************************************************************
 */

package com.persagy.server.service.objects

import com.persagy.server.models.requests.SpatialRelationship
import com.persagy.database.SFilter
import com.persagy.database.SQueryBuilder
import com.persagy.mybatis.SMybatisDao
import com.persagy.server.datacenter.models.entities.objects.ZoneIspace
import com.persagy.server.datacenter.models.entities.objects.ZoneSpace
import com.persagy.server.datacenter.models.entities.rel.RSpatialConnection
import com.persagy.service.SObjectService
import com.persagy.service.SPageContext
import com.persagy.service.models.enums.SResponseType
import com.persagy.service.models.responses.SQueryResponse


/**
 * 空间父类
 *
 * @author
 */
object ZoneSpaceBaseService : SObjectService<ZoneSpace>(SMybatisDao(ZoneSpace::class.java)) {
    /** 邻接关系服务 */
    private val rSpatialConnectionService = SObjectService(SMybatisDao(RSpatialConnection::class.java))
    /** 元空间 */
    private val zoneIspaceService = SObjectService(SMybatisDao(ZoneIspace::class.java))

    override fun onQueryBefore(queryBuilder: SQueryBuilder<ZoneSpace>) {
        val projectId = SPageContext.getHeader("ProjectId")
//        val spaceBdFlObjs = ZoneAirConditioningService.spaceBdFlObjs(projectId!!)
//        if (spaceBdFlObjs.size>0){
//            queryBuilder.filter(SFilter.inList("id",spaceBdFlObjs.toList()))
//        }
    }

    /**
     * 查询元空间关系 邻接关系、建筑交通网络 、 空气流通网络、 光照辐射网络
     */
    fun spatialRelationship(spatialRelationship: SpatialRelationship): SQueryResponse<RSpatialConnection> {
        return try {
            val projectId = SPageContext.getHeader("ProjectId")
            if (!spatialRelationship.buildingId.isNullOrEmpty()&&!spatialRelationship.floorId.isNullOrEmpty()&&!spatialRelationship.graphType.isNullOrEmpty()){

                val zoneIspaceList = zoneIspaceService.select(SFilter.eq("projectId", projectId!!),SFilter.eq("buildingId", spatialRelationship.buildingId!!), SFilter.eq("floorId", spatialRelationship.floorId!!)).exec()

                var list = ArrayList<RSpatialConnection>()
                if (zoneIspaceList.size>0){
                    val listId = ArrayList<String>()
                    for (zoneIspace in zoneIspaceList){
                        listId.add(zoneIspace.id!!)
                    }


                    if (!spatialRelationship.relType.isNullOrEmpty()){
                        list = rSpatialConnectionService.select(SFilter.eq("projectId", projectId!!), SFilter.eq("graphType", spatialRelationship.graphType!!), SFilter.eq("relType", spatialRelationship.relType!!), SFilter.inList("spaceId", listId)).exec()
                    }else{
                        list = rSpatialConnectionService.select(SFilter.eq("projectId", projectId!!),SFilter.eq("graphType", spatialRelationship.graphType!!),SFilter.inList("spaceId",listId)).exec()
                    }
                }

                val sQueryResponse = SQueryResponse<RSpatialConnection>(SResponseType.success)
                sQueryResponse.content = list
                sQueryResponse.total = list.size.toLong()
                sQueryResponse
            }else{
                val sQueryResponse = SQueryResponse<RSpatialConnection>(SResponseType.failure)
                sQueryResponse
            }
        } catch (e: Exception) {
            e.printStackTrace()
            SQueryResponse(SResponseType.success)
        }


    } // Fun spatialRelationship()


} // Object ZoneSpaceBaseService