package com.mmc.iuav.user.controller;

import com.mmc.iuav.response.ResultBody;
import com.mmc.iuav.user.model.qo.MessageQO;
import com.mmc.iuav.user.model.qo.MessageUserQO;
import com.mmc.iuav.user.model.vo.SystemMessageVO;
import com.mmc.iuav.user.model.vo.UserMessageVO;
import com.mmc.iuav.user.service.MessageService;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Api(tags = "消息管理")
@RequestMapping("/message/")
@RestController
public class MessageController  extends BaseController{
    @Resource
    private MessageService messageService;

    @ApiOperation(value = "后台发布消息——新增系统消息")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = ResultBody.class)})
    @PostMapping("/insertSystemMessage")
    public ResultBody insertSystemMessage(@RequestBody SystemMessageVO messageVO){
        return messageService.insertSystemMessage(messageVO);
    }

    @ApiOperation(value = "后台——根据ID查询系统消息")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = ResultBody.class)})
    @GetMapping("/selectSystemMessageById")
    public ResultBody selectSystemMessageById(@ApiParam(value = "消息ID") @RequestParam Integer id){
        return messageService.selectSystemMessageById(id);
    }
    @ApiOperation(value = "后台——系统消息列表")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = ResultBody.class)})
    @PostMapping("/selectAllSystemMessage")
    public ResultBody selectAllSystemMessage(@RequestBody MessageQO messageQO){
        return ResultBody.success(messageService.selectAllSystemMessage(messageQO));
    }

    @ApiOperation(value = "后台——删除消息")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = ResultBody.class)})
    @GetMapping("/removeMessage")
    public ResultBody removeMessage(@ApiParam(value = "消息ID") @RequestParam Integer id){
        return messageService.removeMessage(id);
    }

    @ApiOperation(value = "用户——系统消息列表")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = ResultBody.class)})
    @PostMapping("/systemMessageListByUser")
    public ResultBody selectAllSystemMessageByUser(HttpServletRequest request,@RequestBody MessageQO messageQO){
        Integer userId = this.getUserLoginInfoFromRedis(request).getUserAccountId();
        return ResultBody.success(messageService.selectAllSystemMessageByReceive(userId,messageQO));
    }
    @ApiOperation(value = "用户查看——系统消息详情")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = ResultBody.class)})
    @GetMapping("/showSystemMessageByUser")
    public ResultBody showSystemMessageByUser(HttpServletRequest request,@ApiParam(value = "消息ID") @RequestParam Integer messageId){
        Integer userId = this.getUserLoginInfoFromRedis(request).getUserAccountId();
        return messageService.showSystemMessageByUser(userId,messageId);
    }

    @ApiOperation(value = "用户——删除接收的消息")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = ResultBody.class)})
    @PostMapping("/removeMessageByUser")
    public ResultBody removeMessageByUser(HttpServletRequest request,@ApiParam(value = "消息ID") @RequestParam Integer messageId){
        Integer userId = this.getUserLoginInfoFromRedis(request).getUserAccountId();
        return messageService.removeMessageByUser(userId,messageId);
    }

    @ApiOperation(value = "用户发送消息")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = ResultBody.class)})
    @PostMapping("/insertUserMessage")
    public ResultBody insertUserMessage(HttpServletRequest request,@RequestBody UserMessageVO messageVO){
        return messageService.insertUserMessage(messageVO,this.getUserLoginInfoFromRedis(request).getUserAccountId());
    }

    @ApiOperation(value = "用户聊天详细信息")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = ResultBody.class)})
    @PostMapping("/selectUserMessage")
    public ResultBody selectUserMessage(HttpServletRequest request,@RequestBody MessageUserQO messageQO){
        return ResultBody.success(messageService.selectUserMessage(messageQO,this.getUserLoginInfoFromRedis(request).getUserAccountId()));
    }

    @ApiOperation(value = "用户列表")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = ResultBody.class)})
    @PostMapping("/selectUserList")
    public ResultBody selectUserList(HttpServletRequest request){
        return messageService.selectUserList(this.getUserLoginInfoFromRedis(request).getUserAccountId());
    }
    @ApiOperation(value = "用户列表——用户移除")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = ResultBody.class)})
    @GetMapping("/removeUser")
    public ResultBody removeUser(HttpServletRequest request,@ApiParam(value = "接收人ID") @RequestParam Integer receiverUserId){
        return messageService.removeUser(this.getUserLoginInfoFromRedis(request).getUserAccountId(),receiverUserId);
    }
    @ApiOperation(value = "用户撤回消息（三分钟之内可以撤回）")
    @ApiResponses({@ApiResponse(code = 200, message = "OK", response = ResultBody.class)})
    @GetMapping("/removeUserMessage")
    public ResultBody removeUserMessage(@ApiParam(value = "消息ID") @RequestParam Integer id){
        return messageService.removeUserMessage(id);
    }

}
