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

package com.persagy.server.controllers.objects

import com.persagy.server.datacenter.models.entities.objects.Equipment
import com.persagy.server.services.objects.EquipmentService
import com.persagy.base.extensions.toJson
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.slf4j.LoggerFactory
import org.springframework.web.bind.annotation.*

/**
 * 设备接口
 *
 * @author 张维新
 */
@Api(tags = ["004、设备"])
@RestController
@RequestMapping("/object/equip")
open class EquipController {

    companion object {
        /** 日志 */
        private val logger = LoggerFactory.getLogger(EquipController::class.java)
    } // Companion object

    /**
     * 创建设备
     *
     * @param   request         设备对象
     * @return                  创建结果信息
     */
    @ApiOperation(value = "创建设备信息", notes = "")
    @PostMapping(value = ["/create"])
    fun create(@RequestBody request: SCreateRequest<Equipment>): SCreateResponse<Equipment> {
        logger.debug("equip =${request.toJson()}")
        return EquipmentService.createList(request)
    } // Function create()

    /**
     * 根据id删除设备
     *
     * @param   idList      id数组
     * @return              删除的结果信息
     */
    @ApiOperation(value = "根据id删除设备信息", notes = "")
    @PostMapping(value = ["/delete"])
    fun delete(@RequestBody idList: ArrayList<Equipment>): SBaseResponse {
        return EquipmentService.deleteByKeysList(idList)
    } // Function delete()

    /**
     * 更新设备信息
     *
     * @param   request        更新的内容对象
     * @return                  更新的结果
     */
    @ApiOperation(value = "更新设备信息", notes = "")
    @PostMapping(value = ["/update"])
    fun update(@RequestBody request: SUpdateRequest<Equipment>): SBaseResponse {
        logger.debug("equipUpdate =${request.toJson()}")
        return EquipmentService.updateList(request)
    } // Function update()

    /**
     * 查询设备信息
     *
     * @param   request     查询信息条件
     * @return              查询结果
     */
    @ApiOperation(value = "查询设备信息", notes = " 可以级联的对象：资产（property）、楼层（floor）、建筑（building）、项目（project）、设备类（equipCategory）、设备服务的业务空间（zoneSpaceForBase）、设备所在的业务空间（zoneSpaceInBase）、设备所在的竖井（shaftList）  级联查询时子对象过滤时必须指定id否则无法定位对象，级联查询例子：{\n" +
            "  \"Cascade\": [\n" +
            "    {\n" +
            "      \"Name\": \"property\",\n" +
            "      \"Projection\": [\n" +
            "        \"Family\",\"EquipLocalName\",\"EquipLocalID\",\"EquipID\",\"CodeType\"\n" +
            "      ]\n" +
            "    },\n" +
            "    {\n" +
            "      \"Name\": \"building\",\n" +
            "      \"Projection\": [\n" +
            "        \"BuildLocalName\",\"BuildName\",\"BuildID\"\n" +
            "      ]\n" +
            "    },\n" +
            " {\n" +
            "      \"Name\": \"floor\",\n" +
            "      \"Projection\": [\n" +
            "        \"FloorLocalName\",\"FloorName\",\"FloorID\"\n" +
            "      ]\n" +
            "    }\n" +
            "  ],\n" +
            "  \"Filters\": \" not propertyId isnull \",\n" +
            "  \"PageNumber\": 1,\n" +
            "  \"PageSize\": 50,\n" +
            "  \"Projection\": [\n" +
            "    \"BuildingId\",\"FloorId\",\"Category\",\"EquipLocalName\",\"EquipLocalID\",\"EquipID\",\"BIMID\"\n" +
            "  ]\n" +
            "}")
    @PostMapping(value = ["/query"])
    fun query(@RequestBody request: SQueryRequest): SQueryResponse<Equipment> {
        logger.debug("equipQuery =${request.toJson()}")
        val withFilterList = ArrayList<SFilter>()
        withFilterList.add(SFilter.eq("projectId", SPageContext.getHeader("ProjectId").toString()))
        return EquipmentService.pageQuery(request, withFilterList)
    } // Function query()

    /**
     * 根据条件查询统计数量
     */
    @ApiOperation(value = "根据条件查询统计数量", notes = "例子： 根据设备类型查询（\"Filters\": \"category ='SISU'\"）," +
            "根据设备类型和建筑查询（\"Filters\": \"category ='FASE';buildingId = '建筑id'\"），" +
            "根据设备类型和建筑楼层查询（\"Filters\": \"category ='FASE';buildingId = '建筑id';floorId = '楼层id'\"）")
    @PostMapping(value = ["/count"])
    fun count(@RequestBody request: SCountRequest): SCountResponse {
        val withFilterList = ArrayList<SFilter>()
        withFilterList.add(SFilter.eq("projectId", SPageContext.getHeader("ProjectId").toString()))
        return EquipmentService.count(request, withFilterList)
    } // Function count()

    /**
     * 根据项目id查询所有的设备和部件
     */
    @ApiOperation(value = "根据项目id查询所有的设备和部件", notes = "")
    @GetMapping(value = ["/query-equip"])
    fun queryEquipCom(): SQueryResponse<Equipment>  {
        return EquipmentService.queryEquipCom()
    } // Function count()


} // Class EquipController