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

package com.persagy.server.service.objects

import com.persagy.base.extensions.toJson
import com.persagy.database.SFilter
import com.persagy.mybatis.SMybatisDao
import com.persagy.server.dao.mappers.ZoneSpaceMapper
import com.persagy.server.datacenter.models.entities.objects.ZoneSpace
import com.persagy.server.datacenter.models.requests.UpZoneSpaceBaseRequest
import com.persagy.service.SObjectService
import com.persagy.service.SPageContext
import com.persagy.service.models.enums.SResponseType
import com.persagy.service.models.requests.SQueryRequest
import com.persagy.service.models.requests.SUpdateRequest
import com.persagy.service.models.responses.SBaseResponse
import com.persagy.service.models.responses.SCreateResponse
import com.persagy.service.models.responses.SQueryResponse
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import org.springframework.web.bind.annotation.RequestBody


/**
 * 业务空间
 *
 * @author 邓婷婷
 */
@Service
open class ZoneSpaceService {

    @Autowired
    lateinit var mapper: ZoneSpaceMapper
    /**
     * 查询未关联业务空间的元空间
     *
     * @param   objectType      分区类型
     * @param   request         查询条件
     */
    fun inBdflZoneSpace(x: String, y: String,  request: SQueryRequest): SQueryResponse<ZoneSpace> {
        return try {
            val projectId = SPageContext.getHeader("ProjectId")

            val idList = ArrayList<String>()
            var list = ArrayList<SFilter>()
            val zoneSpaceBaseTableService = SObjectService(SMybatisDao(ZoneSpace::class.java))
            val spaceList = zoneSpaceBaseTableService.select(request.filters).filter(SFilter.eq("projectId",projectId!!)).exec()
            for(space in spaceList){
                if(!space.outline.isNullOrEmpty()){
                    val result = mapper.inBdflZoneSpace(x,y,space.outline!!.toJson())
                    if (result){
                        idList.add(space.id!!)
                    }
                }
            }
            if(idList.size>0){
                list.add(SFilter.inList("id",idList))
                return zoneSpaceBaseTableService.pageQuery(request,list)
            }else{
                val sQueryResponse = SQueryResponse<ZoneSpace>(SResponseType.success)
                sQueryResponse.content = ArrayList<ZoneSpace>()
                sQueryResponse
            }

        } catch (e: Throwable) {
            e.printStackTrace()
            SQueryResponse(SResponseType.failure, e.message!!)
        }
    } // Function inBdflZoneSpace()

    /**
     * 修改空间图片信息
     * @param request 空间信息
     */
    fun updateSpacePicInfo(@RequestBody request: SUpdateRequest<UpZoneSpaceBaseRequest>): SBaseResponse {
        return try {
            val list = request.content
            val listStr = ArrayList<String>()
            listStr.add("pic")
            if (list != null) {
                for (space in list) {
                    if(space.pic.isNullOrEmpty()){
                        continue
                    }

                    val zoneSpace = ZoneSpace()
                    zoneSpace.id = space.id
                    zoneSpace.pic = space.pic
                    ZoneSpaceBaseService.update(zoneSpace,listStr)

//                    if (space.objectType == "HeatingZone") {
//                        val zoneHeating = ZoneHeating()
//                        zoneHeating.id = space.id
//                        zoneHeating.pic = space.pic
//                        ZoneHeatingService.update(zoneHeating, listStr)
//                    } else if (space.objectType == "CleanZone") {
//                        val zoneClean = ZoneClean()
//                        zoneClean.id = space.id
//                        zoneClean.pic = space.pic
//                        ZoneCleanService.update(zoneClean, listStr)
//                    } else if (space.objectType == "DomesticWaterSupplyZone") {
//                        val zoneDomesticWaterSupply = ZoneDomesticWaterSupply()
//                        zoneDomesticWaterSupply.id = space.id
//                        zoneDomesticWaterSupply.pic = space.pic
//                        ZoneDomesticWaterSupplyService.update(zoneDomesticWaterSupply, listStr)
//                    } else if (space.objectType == "NetworkZone") {
//                        val zoneNetwork = ZoneNetwork()
//                        zoneNetwork.id = space.id
//                        zoneNetwork.pic = space.pic
//                        ZoneNetworkService.update(zoneNetwork, listStr)
//                    } else if (space.objectType == "TenantZone") {
//                        val zoneTenant = ZoneTenant()
//                        zoneTenant.id = space.id
//                        zoneTenant.pic = space.pic
//                        ZoneTenantService.update(zoneTenant, listStr)
//                    } else if (space.objectType == "AirConditioningZone") {
//                        val zoneAirConditioning = ZoneAirConditioning()
//                        zoneAirConditioning.id = space.id
//                        zoneAirConditioning.pic = space.pic
//                        ZoneAirConditioningService.update(zoneAirConditioning, listStr)
//                    } else if (space.objectType == "FunctionZone") {
//                        val zoneFunction = ZoneFunction()
//                        zoneFunction.id = space.id
//                        zoneFunction.pic = space.pic
//                        ZoneFunctionService.update(zoneFunction, listStr)
//                    } else if (space.objectType == "FireZone") {
//                        val zoneFire = ZoneFire()
//                        zoneFire.id = space.id
//                        zoneFire.pic = space.pic
//                        ZoneFireService.update(zoneFire, listStr)
//                    } else if (space.objectType == "SecurityZone") {
//                        val zoneSecurity = ZoneSecurity()
//                        zoneSecurity.id = space.id
//                        zoneSecurity.pic = space.pic
//                        ZoneSecurityService.update(zoneSecurity, listStr)
//                    } else if (space.objectType == "GeneralZone") {
//                        val zoneGeneral = ZoneGeneral()
//                        zoneGeneral.id = space.id
//                        zoneGeneral.pic = space.pic
//                        ZoneGeneralService.update(zoneGeneral, listStr)
//                    } else if (space.objectType == "PowerSupplyZone") {
//                        val zonePowerSupply = ZonePowerSupply()
//                        zonePowerSupply.id = space.id
//                        zonePowerSupply.pic = space.pic
//                        ZonePowerSupplyService.update(zonePowerSupply, listStr)
//                    } else if (space.objectType == "LightingZone") {
//                        val zoneLighting = ZoneLighting()
//                        zoneLighting.id = space.id
//                        zoneLighting.pic = space.pic
//                        ZoneLightingService.update(zoneLighting, listStr)
//                    } else {
//                        val sCreateResponse = SCreateResponse<UpZoneSpaceBaseRequest>(SResponseType.failure)
//                        sCreateResponse.message = "空间分区类型不存在：" + space.objectType
//                        sCreateResponse
//                    }
                }
            }
            val sCreateResponse = SCreateResponse<UpZoneSpaceBaseRequest>(SResponseType.success)
            sCreateResponse
        }catch (e: Exception){
            e.printStackTrace()
            return SBaseResponse(SResponseType.failure, e.message!!)
        }
    }

} // Object ZoneSpaceService