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

package com.persagy.server.controllers.objects

import com.persagy.server.datacenter.models.entities.objects.Tenant
import com.persagy.server.datacenter.models.entities.objects.TenantWithCount
import com.persagy.server.services.objects.TenantService
import com.persagy.database.SFilter
import com.persagy.service.SPageContext
import com.persagy.service.models.requests.SCountRequest
import com.persagy.service.models.requests.SCreateRequest
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.SCountResponse
import com.persagy.service.models.responses.SCreateResponse
import com.persagy.service.models.responses.SQueryResponse
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
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 = ["007、租户"])
@RestController
@RequestMapping("/object/tenant")
open class TenantController {

    /**
     * 创建租户
     *
     * @param   request     租户对象
     * @return
     */
    @ApiOperation(value = "创建租户", notes = "")
    @PostMapping(value = ["/create"])
    fun create(@RequestBody request: SCreateRequest<Tenant>): SCreateResponse<Tenant> {
        return TenantService.createList( request)
    } // Function create()

    /**
     * 根据id删除数据源
     */
    @ApiOperation(value = "根据id删除数据源", notes = "")
    @PostMapping(value = ["/delete"])
    fun delete(@RequestBody idList: ArrayList<Tenant>): SBaseResponse {
        return TenantService.deleteByKeysList( idList)
    } // Function deleteList()

    /**
     * 更新租户
     */
    @ApiOperation(value = "更新租户", notes = "")
    @PostMapping(value = ["/update"])
    fun update(@RequestBody request: SUpdateRequest<Tenant>): SBaseResponse {
        return TenantService.updateList(request)
    } // Function update()

    /**
     * 查询租户
     */
    @ApiOperation(value = "查询租户", notes = "级联-租赁租户分区（zoneTenantList）、租赁默认分区（zoneGeneraltList）")
    @PostMapping(value = ["/query"])
    fun query(@RequestBody request: SQueryRequest): SQueryResponse<Tenant> {
        return TenantService.pageQuery(request, arrayListOf(SFilter.eq("projectId", SPageContext.getHeader("ProjectId").toString())))
    } // Function query()

    /**
     * 查询租户-包含统计信息
     *
     * @param       request
     * @return      租户统计信息
     */
    @ApiOperation(value = "查询租户-包含统计信息", notes = "此方法可能废弃")
    @PostMapping(value = ["/query-with-count"])
    fun queryWithCount(@RequestBody request: SQueryRequest): SQueryResponse<TenantWithCount> {
        return TenantService.queryWithCount(request)
    } // Function queryWithCount()

    /**
     * 根据条件查询统计数量
     */
    @ApiOperation(value = "根据条件查询统计数量", notes = "")
    @PostMapping(value = ["/count"])
    fun count(@RequestBody request: SCountRequest): SCountResponse {
        val withFilterList = ArrayList<SFilter>()
        withFilterList.add(SFilter.eq("projectId", SPageContext.getHeader("ProjectId").toString()))
        return TenantService.count(request,withFilterList)
    } // Function count()

} // Class ZoneTenantController