/*
 * *********************************************************************************************************************
 *
 *          !!
 *        .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.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.datacenter.models.entities.dictnew.DefClass
import com.persagy.server.datacenter.models.requests.UpZoneSpaceBaseRequest
import com.persagy.server.datacenter.wanda.obj.ZoneSpace
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 {

    /** 平台集团对象类型 */
    private val defClassService = SObjectService(SMybatisDao(DefClass::class.java))
    @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))

                val pageQuery = zoneSpaceBaseTableService.pageQuery(request, list)
                if (!pageQuery.content.isNullOrEmpty()) {
                    for (content in pageQuery.content!!) {
                        val entity = defClassService.select(
                            SFilter.eq("projectId", projectId),
                            SFilter.eq("code", content.classCode!!)
                        ).entity()
                        if (entity!=null){
                            content.zoneName = entity
                        }
                    }
                }

                return pageQuery
            }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