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

package com.persagy.server.messagecenter.controllers

import com.persagy.base.extensions.toJson
import com.persagy.server.messagecenter.models.entities.Message
import com.persagy.server.messagecenter.models.request.MessageRequest
import com.persagy.server.messagecenter.services.MessageService
import com.persagy.server.messagecenter.services.activemq.JMSProducer
import com.persagy.service.models.requests.SCountRequest
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.apache.activemq.command.ActiveMQTopic
import org.slf4j.LoggerFactory
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("/message")
open class MessageController {

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

    @Autowired
    lateinit var producer: JMSProducer

    val destination = ActiveMQTopic("message.manage")

    /**
     * 查询消息列表
     */
    @ApiOperation(value = "查询消息列表", notes = "级联： 项目(project)")
    @PostMapping(value = ["/page-query"])
    fun pageQuery(@RequestBody request: SQueryRequest): SQueryResponse<Message> {
        logger.debug("MessageQuery =${request.toJson()}")
//        val projectId = SPageContext.getHeader("ProjectId");
//
//        if(projectId != null){
//            val withFilterList = ArrayList<SFilter>()
//            withFilterList.add(SFilter.eq("projectId",projectId ))
//
//            return MessageService.pageQuery(request,withFilterList)
//        }
        return MessageService.pageQuery(request)

    } // Function pageQuery()

    /**
     * 创建消息
     *
                {
                    "Content": {
                        "ButtonList": [
                            {
                                "FileName": "ca7de892f14411e9b20d17b4c79481be.rvt",
                                "Name": "下载报告",
                                "Url": "/image-service/common/file_get?systemId=revit&key=model/ca7de892f14411e9b20d17b4c79481be.rvt"
                            }
                        ],
                        "Message": "3楼模型损坏，请重新上传"
                    },
                    "Module": "模型文件管理",
                    "ProjectId": "Pj4201050001",
                    "Title": "3楼模型损坏，请重新上传",
                    "Type": "Error",
                    "UserList": [
                    "234567"
                    ]
                }

     */
    @ApiOperation(value = "创建消息", notes = "消息类型：提示(Info)、警告(Warning)、错误(Error)、成功(Success)；")
    @PostMapping(value = ["/create"])
    fun create(@RequestBody request: MessageRequest): SCreateResponse<Message> {
        logger.debug("message =${request.toJson()}")
//        println("content: "+ request.toJson())
        producer.sendMessage(destination,request.toJson())
        return MessageService.createList(MessageService.createBefore(request))
    } // Function create()

    /**
     * 更新消息状态
     */
    @ApiOperation(value = "更新消息状态", notes = "是否读过：未读（false）、已读（true）")
    @PostMapping(value = ["/update"])
    fun update(@RequestBody request: SUpdateRequest<Message>): SBaseResponse {
        return MessageService.updateList(request)
    } // Function update()

    /**
     * 删除消息
     */
    @ApiOperation(value = "删除消息", notes = "")
    @PostMapping(value = ["/delete"])
    fun delete(@RequestBody idList: ArrayList<Message>): SBaseResponse {
        return MessageService.deleteByKeysList(idList)
    } // Function delete()

    /**
    * 统计数量
    */
    @ApiOperation(value = "统计数量", notes = "查询总数量不传参数，查询 已读Read = true，未读 Read = false")
    @PostMapping(value = ["/count"])
    fun count(@RequestBody request: SCountRequest): SCountResponse {
//        val projectId = SPageContext.getHeader("ProjectId");
//        if(projectId != null){
//            val withFilterList = ArrayList<SFilter>()
//            withFilterList.add(SFilter.eq("projectId",projectId ))
//
//            return MessageService.count(request,withFilterList)
//        }
        return MessageService.count(request)
    } // Function count()

    /**
     * 根据接收人将所有消息置为已读
     */
    @ApiOperation(value = "根据接收人将消息置为已读", notes = "已读Read = true")
    @PostMapping(value = ["/update-state"])
    fun updateState(@RequestBody request: Message): SBaseResponse {
        return MessageService.updateMessageState(request)
    } // Fun updateState

} // Class ScanTaskBaseController