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

package com.persagy.server.service.objects

import com.persagy.database.SFilter
import com.persagy.mybatis.SMybatisDao
import com.persagy.server.models.objects.Building
import com.persagy.server.service.base.Service
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


/**
 * 建筑信息服务
 *
 * @author 张维新
 */
object BuildingService : Service<Building>(SMybatisDao(Building::class.java)) {

    /**
     * 查询建筑数据 以及楼层重复使用模型文件的数量
     */
    fun queryBuilding(request: SQueryRequest) : SQueryResponse<Building> {

        return try {
            val projectId = SPageContext.getHeader("ProjectId")
            val pageQuery = pageQuery(request, arrayListOf(SFilter.eq("projectId", projectId!!)))
            if (!pageQuery.content.isNullOrEmpty()){
                for ( building in  pageQuery.content!!){
                    val floorList = FloorService.select(SFilter.eq("projectId", projectId!!), SFilter.eq("BuildID", building.id!!)).exec()

                    var count = 0
                    for (floor in floorList){

                        if (floor.outline == null&&!floor.modelId.isNullOrEmpty()){
//                            val floorList = FloorService.select(SFilter.eq("projectId", SPageContext.getHeader("ProjectId").toString()), SFilter.not(SFilter.eq("FloorID", floor.id!!)),
//                                    SFilter.isNull("Outline"), SFilter.eq("ModelId", floor.modelId!!)).exec()
                            val floorList = FloorService.select(SFilter.eq("projectId", SPageContext.getHeader("ProjectId").toString()),
                                     SFilter.eq("ModelId", floor.modelId!!)).exec()
                            if (floorList.size>1){
                                floor.sign = 1
                                count++
                            }
                        }
                    }
                    building.count = count
                }
            }
            pageQuery
        } catch (e: Exception) {
            e.printStackTrace()
            SQueryResponse(SResponseType.failure, e.message.toString())
        }
    } // // Function queryBuilding（）

} // Object BuildingService