/*
 * ********************************************************************************************************************
 *
 *                     :*$@@%$*:                         ;:                ;;    ;;
 *                   :@@%!  :!@@%:                       %!             ;%%@@%$ =@@@@@@@%;     @%@@@%%%%@@@@@
 *                  :@%;       :$=                       %%$$$%$$         ;$$  ;$@=   !@$
 *                  =@!                                  %!              @ $=;%   !@@@%:      !$$$$$$$$$$$$$$=
 *                  =@*                                  %!              @ $= % %@=   =%@!      %=
 *             *$%%! @@=        ;=$%%%$*:                %!              @ $= % =%%%%%%@$      *%:         =%
 *           %@@!:    !@@@%=$@@@@%!  :*@@$:              %!              @ $= % $*     ;@      @*          :%*
 *         ;@@!          ;!!!;:         ;@%:      =======@%========*     @ $$ % $%*****$@     :@$=*********=@$
 *         $@*   ;@@@%=!:                *@*
 *         =@$    ;;;!=%@@@@=!           =@!
 *          %@$:      =@%: :*@@@*       %@=                    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.ComponentService
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.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 = ["005、部件"])
@RestController
@RequestMapping("/object/component")
open class ComponentController {
    companion object {
        /** 日志 */
        private val logger = LoggerFactory.getLogger(ComponentController::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 ComponentService.createList(request)
    } // Function create()

    /**
     * 根据id删除部件
     *
     * @param   idList      id数组
     * @return              删除的结果信息
     */
    @ApiOperation(value = "根据id删除部件信息", notes = "")
    @PostMapping(value = ["/delete"])
    fun delete(@RequestBody idList: ArrayList<Equipment>): SBaseResponse {
        return ComponentService.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 ComponentService.updateList(request)
    } // Function update()

    /**
     * 查询部件信息
     *
     * @param   request     查询信息条件
     * @return              查询结果
     */
    @ApiOperation(value = "查询部件信息", notes = "")
    @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 ComponentService.pageQuery(request,withFilterList)
    } // Function query()

    /**
     * 根据条件查询统计数量
     */
    @ApiOperation(value = "根据条件查询统计数量", notes = "例子： 已经关联岗位的资产数量（\"Filters\": \" not EquipId isNull\"），未关联岗位的资产数量（\"Filters\": \" EquipId isNull\"）")
    @PostMapping(value = ["/count"])
    fun count(@RequestBody request: SCountRequest): SCountResponse {
        val withFilterList = ArrayList<SFilter>()
        withFilterList.add(SFilter.eq("projectId", SPageContext.getHeader("ProjectId").toString()))
        return ComponentService.count(request,withFilterList)
    } // Function count()

} // Class EquipController