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

package com.persagy.server.service.objects

import com.persagy.mybatis.SMybatisDao
import com.persagy.server.datacenter.models.entities.objects.ZoneIspace
import com.persagy.server.datacenter.models.response.SpaceSQueryResponse
import com.persagy.server.models.requests.ZoneISpaceQueryRequest
import com.persagy.server.service.base.Service
import com.persagy.service.SPageContext
import com.persagy.service.models.enums.SResponseType
import com.persagy.service.models.responses.SQueryResponse


/**
 * 空间信息服务
 *
 * @author 张维新
 */
object ISpaceService : Service<ZoneIspace>(SMybatisDao(ZoneIspace::class.java)) {

    /**
     * 查询未关联业务空间的元空间
     *
     * @param   objectType      分区类型
     * @param   request         查询条件
     */
    fun unSpaceIspace(objectType: String, request: ZoneISpaceQueryRequest): SpaceSQueryResponse {
        return try {
            val projectId = SPageContext.getHeader("ProjectId")

            val queryBuilder = ISpaceService.select(request.filters)
//            var floorId: String? = null
////            val filterList = queryBuilder.filterList
////            for (filter in filterList){
////                 if (filter.first.toString() == "FloorId"){
////                    floorId = filter.second.toString()
////                }
////            }
            queryBuilder.tableName = "f_unspace_ispace('$projectId','$objectType','${request.floorId}')"
            if (null != request.projection) {
                queryBuilder.projection(request.projection!!)
            }
            if (null != request.orders) {
                queryBuilder.order(request.orders!!)
            }

            val sPageQueryInfo = queryBuilder.pageQuery(request.pageNumber, request.pageSize)

           val sQueryResponse =  SQueryResponse(sPageQueryInfo)
            val spaceSQueryResponse = SpaceSQueryResponse()
            spaceSQueryResponse.floorId = request.floorId
            spaceSQueryResponse.zoneType = objectType
            spaceSQueryResponse.result = sQueryResponse.result
            spaceSQueryResponse.content = sQueryResponse.content
            spaceSQueryResponse.total = sQueryResponse.total
            spaceSQueryResponse.pageSize = sQueryResponse.pageSize
            spaceSQueryResponse.pageSize = sQueryResponse.pageSize
            spaceSQueryResponse
        } catch (e: Throwable) {
            e.printStackTrace()
            val spaceSQueryResponse = SpaceSQueryResponse()
            spaceSQueryResponse.floorId = request.floorId
            spaceSQueryResponse.zoneType = objectType
            spaceSQueryResponse.result = SResponseType.failure
            spaceSQueryResponse.message = e.message!!
            spaceSQueryResponse
        }
    } // Function unSpaceIspace()

} // Object SpaceService