/*
 * *********************************************************************************************************************
 *
 *          !!
 *        .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.database.SFilter
import com.persagy.database.SQueryBuilder
import com.persagy.mybatis.SMybatisDao
import com.persagy.server.Opt
import com.persagy.server.datacenter.wanda.obj.ZoneSpace
import com.persagy.server.datacenter.models.entities.rel.RSpatialConnection
import com.persagy.server.datacenter.models.entities.rel.RSpinFl
import com.persagy.server.datacenter.wanda.obj.ZoneIspace
import com.persagy.server.models.planar.PlanarSpace
import com.persagy.server.models.requests.PlanarEquipRequests
import com.persagy.server.models.requests.SpatialRelationship
import com.persagy.server.service.CustomSqlService
import com.persagy.service.SObjectService
import com.persagy.service.SPageContext
import com.persagy.service.models.enums.SResponseType
import com.persagy.service.models.responses.SCountResponse
import com.persagy.service.models.responses.SQueryResponse
import com.persagy.service.utils.SSpringContextUtil
import org.slf4j.LoggerFactory


/**
 * 空间父类
 *
 * @author
 */
object ZoneSpaceBaseService : SObjectService<ZoneSpace>(SMybatisDao(ZoneSpace::class.java)) {
    /** 日志 */
    private val logger = LoggerFactory.getLogger(ZoneSpaceBaseService::class.java)
    /** 邻接关系服务 */
    private val rSpatialConnectionService = SObjectService(SMybatisDao(RSpatialConnection::class.java))
    /** 元空间 */
    private val zoneIspaceService = SObjectService(SMybatisDao(ZoneIspace::class.java))
    /** 平面图空间服务 */
    private val planarSpaceService = SObjectService(SMybatisDao(PlanarSpace::class.java))
    /** 空间建筑楼层 */
    val spInFlService = SObjectService(SMybatisDao(RSpinFl::class.java))

    /** 自定义sql */
    val customSqlService by lazy {
        SSpringContextUtil.getBean(CustomSqlService::class.java) as CustomSqlService
    }

    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 spaceBdFls(): SCountResponse {
        val projectId = SPageContext.getHeader("projectId")
        val spaceBdFls = customSqlService.spaceBdFls(projectId!!)
        return SCountResponse(spaceBdFls.toLong(),SResponseType.success)
    } //

    /**
     * 平面图空间查询
     *
     * @param request   条件查询
     * @return 返回结果
     */
    fun spaceQuery(request: PlanarEquipRequests): SQueryResponse<PlanarSpace> {
        try {
            val sQueryResponse = SQueryResponse<PlanarSpace>(SResponseType.success)

            if (!request.zoneTypes.isNullOrEmpty()&&!request.funcTypes.isNullOrEmpty()){
                if (request.spaceIds.isNullOrEmpty()){
                    request.spaceIds = ArrayList()

                    val spInFlList = spInFlService.select(SFilter.eq("projectId", Opt.projectId!!), SFilter.inList("objectType", request.zoneTypes!!),
                        SFilter.eq("buildingId", request.buildingId!!), SFilter.eq("floorId", request.floorId!!)).exec()
                    if (!spInFlList.isNullOrEmpty()) {
                        val setIds = HashSet<String>()
                        for (spInFl in spInFlList){
                            request.spaceIds!!.add(spInFl.spaceId!!)
                        }
                    }
                }
                if (!request.spaceIds.isNullOrEmpty()) {
                    val planarSpaceList = planarSpaceService.select(
                        SFilter.eq("projectId", Opt.projectId!!), SFilter.inList("id", request.spaceIds!!),
                        SFilter.inList("roomFuncType", request.funcTypes!!)
                    ).exec()
                    sQueryResponse.content = planarSpaceList
                    sQueryResponse.total = planarSpaceList.size.toLong()
                }else {
                    sQueryResponse.content = ArrayList()
                }

            } else if (!request.zoneTypes.isNullOrEmpty()){
                if (request.spaceIds.isNullOrEmpty()){
                    val spInFlList = spInFlService.select(SFilter.eq("projectId", Opt.projectId!!), SFilter.inList("objectType", request.zoneTypes!!),
                        SFilter.eq("buildingId", request.buildingId!!), SFilter.eq("floorId", request.floorId!!)).exec()
                    if (!spInFlList.isNullOrEmpty()) {
                        val setIds = HashSet<String>()
                        for (spInFl in spInFlList){
                            request.spaceIds!!.add(spInFl.spaceId!!)
                        }
                    }
                }
                if (!request.spaceIds.isNullOrEmpty()) {
                    logger.debug("长度= ${request.spaceIds?.size}")
                    val planarSpaceList = planarSpaceService.select(
                        SFilter.eq("projectId", Opt.projectId!!), SFilter.inList("id", request.spaceIds!!)
                    ).exec()
                    sQueryResponse.content = planarSpaceList
                    sQueryResponse.total = planarSpaceList.size.toLong()
                }else {
                    sQueryResponse.content = ArrayList()
                }

            } else {
                sQueryResponse.content = ArrayList()
            }
            return sQueryResponse
        } catch (e: Exception) {
            e.printStackTrace()
            return  SQueryResponse(SResponseType.success,e.message!!)
        }
    }

}