package com.zcxk.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zcxk.Dto.MessageStatic;
import com.zcxk.Dto.RequsetMessageQuery;
import com.zcxk.entity.Message;
import com.zcxk.model.AjaxMessage;
import com.zcxk.model.Pagination;
import com.zcxk.model.ResultStatus;
import com.zcxk.service.MessageService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * (Message)控制层
 *
 * @author makejava
 * @since 2020-11-12 10:53:19
 */
@RestController
@RequestMapping("/message")
@Api(tags = "消息服务")
public class MessageController {
    /**
     * 服务对象
     */
    @Autowired
    private MessageService messageService;

    @Autowired
    private AmqpTemplate rabbitTemplate;
    @Value("${receive.exchange.name}")
    private  String receiveExchangeName;
    @Value("${dispath.routing.key}")
    private  String dispathRoutingKey;
   /**
     * 分页查询
     *
     * @param pageNum  偏移
     * @param pageSize 条数
     * @return Response对象
     */
    @RequestMapping(value = "selectPage", method = RequestMethod.POST)
    @ApiOperation(value = "查询消息列表(分页)",notes = "全部的时候除分页参数都不传，messageStatus：0 未读，1已读")
    public AjaxMessage<Pagination<Message>> selectPage(Integer pageNum, Integer pageSize,Integer typeId,
                                                       Integer messageStatus) {
        Message message = new Message();
        message.setMessageType(typeId);
        message.setMessageStatus(messageStatus);
        IPage<Message> iPage = new Page<>(pageNum, pageSize);
        iPage = messageService.selectPage(message, iPage);
        Pagination<Message> pages = new Pagination<>(iPage);
        return new AjaxMessage<>(ResultStatus.OK, pages);
    }
    /**
     * 分页查询
     *
     * @param num  偏移
     * @param num 条数
     * @return Response对象
     */
    @RequestMapping(value = "queryLastMessage", method = RequestMethod.POST)
    @ApiOperation(value = "查询最新消息")
    public AjaxMessage<List<Message>> queryLastMessage(int num,Integer type) {

       return new AjaxMessage<>(ResultStatus.OK,messageService.queryLastMessage(num,type));
    }
    /**
     * 分页查询
     *
     *
     *
     * @return Response对象
     */
    @RequestMapping(value = "queryUnreadMessageStatic", method = RequestMethod.POST)
    @ApiOperation(value = "统计未读消息数量")
    public AjaxMessage<List<MessageStatic>> queryUnreadMessageStatic() {
        ;
        return new AjaxMessage<>(ResultStatus.OK,messageService.queryUnreadMessageStatic());
    }
    @ResponseBody
    @RequestMapping(value="/sendToOne", produces = {"application/json; charset=utf-8"},method= RequestMethod.POST)
    public String sendToOne( @RequestBody Message message){
        rabbitTemplate.convertAndSend(receiveExchangeName,dispathRoutingKey, JSONObject.toJSONString(message));
        return "成功";
    }
    /**
     * 修改一条数据
     *
     * @param message 实体类
     * @return Response对象
     */
    @RequestMapping(value = "updateMultiple", method = RequestMethod.POST)
    @ApiOperation(value = "更改多个消息状态")
    public AjaxMessage<Integer> updateMultiple(@ApiParam(value = "设置配置", required = true) @RequestBody RequsetMessageQuery message) {
        int result = messageService.updateMultiple(message);
        return new AjaxMessage<>(ResultStatus.OK, result);

    }
    /**
     * judgingTheMessageJumpPermission
     *
     * @param url
     * @return Response对象
     */
    @RequestMapping(value = "judgingTheMessageJumpPermission", method = RequestMethod.POST)
    @ApiOperation(value = "更改多个消息状态")
    public AjaxMessage<Integer> judgingTheMessageJumpPermission(@ApiParam(value = "跳转url", required = true) @RequestParam String url,
                                                                @ApiParam(value = "应用id", required = true) @RequestParam String appId) {

        int result = messageService.judgingTheMessageJumpPermission(url,appId);
        return new AjaxMessage<>(ResultStatus.OK, result);

    }
}