package com.persagy.server.controllers

import com.persagy.database.SFilter
import com.persagy.server.models.TenantCascade
import com.persagy.server.models.objects.Building
import com.persagy.server.models.requests.ZoneEquipQueryRequest
import com.persagy.server.service.TenantZoneService
import com.persagy.server.service.base.BaseDataService
import com.persagy.server.service.objects.TenantService
import com.persagy.service.SPageContext
import com.persagy.service.models.responses.SQueryResponse
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

/**
 * 租户相关接口
 *
 * @author 张维新
 */
@Api(tags = ["租户相关接口"])
@RestController
@RequestMapping("/tenant")
class TenantController {

    /**
     * 查询租户的建筑楼层
     */
    @ApiOperation(value=" 查询租户的建筑楼层 ", notes="")
    @PostMapping(value = ["/tenant-building-floor"])
    fun tenantBuildingFloor(tenantId: String,objectType: String): SQueryResponse<Building> {
        return TenantService.tenantBuildingFloors(tenantId,objectType)
    } //


    @Autowired
    lateinit var tenantZoneService : TenantZoneService


    /**
     * 根据业务空间查询租户
     */
    @ApiOperation(value=" 查询租户的建筑楼层 ", notes="")
    @PostMapping(value = ["/zone-query"])
    fun queryTenantBySpace(@RequestBody request: ZoneEquipQueryRequest): SQueryResponse<TenantCascade> {
        val withFilterList = ArrayList<SFilter>()
        val projectId = SPageContext.getHeader("ProjectId").toString()
        withFilterList.add(SFilter.eq("projectId", projectId))
        if(request.zoneId.isNullOrEmpty() && request.zoneType.isNullOrEmpty()) {
            return BaseDataService.tenantService.pageQuery(request, withFilterList)
        }
        if(request.notInZone == true) {
            val tenantIdsUnderSpace = tenantZoneService.getTenantIdUnderSpace(projectId)
            if(!tenantIdsUnderSpace.isNullOrEmpty())
                withFilterList.add(SFilter.not(SFilter.inList("id", tenantIdsUnderSpace)))
            return BaseDataService.tenantService.pageQuery(request, withFilterList)
        }
        val tenantIdUnderSpace = tenantZoneService.getTenantIdUnderSpace(projectId, request.zoneType, request.zoneId)
        if(tenantIdUnderSpace.isNullOrEmpty()){
            return SQueryResponse()
        }
        withFilterList.add(SFilter.inList("id", tenantIdUnderSpace))
        return BaseDataService.tenantService.pageQuery(request, withFilterList)
    } //


} // Class TenantController