package com.webchat.controller.client;

import com.webchat.common.bean.APIPageResponseBean;
import com.webchat.common.bean.APIResponseBean;
import com.webchat.common.bean.APIResponseBeanUtil;
import com.webchat.domain.vo.response.MessageResponseVO;
import com.webchat.domain.vo.response.UserUnReadMessageCountVO;
import com.webchat.service.UserService;
import com.webchat.service.message.MessageService;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author: 程序员七七 https://www.coderutil.com
 * @Description: 无描述信息
 */
@Api(tags = "【客户端】消息服务接口")
@RestController
@RequestMapping("/api/message")
public class MessageController {

    @Autowired
    private UserService userService;
    @Autowired
    private MessageService messageService;

    /***
     * 查询新消息数
     * @return
     */
    @GetMapping("/unreadCount")
    public APIResponseBean<UserUnReadMessageCountVO> unreadCount() {
        String userId = userService.getLoginCurrentUserId();
        UserUnReadMessageCountVO unReadMessageCountVO = new UserUnReadMessageCountVO();
        if (StringUtils.isNotBlank(userId)) {
            unReadMessageCountVO = messageService.getUserUnReadMessageCountFromCache(userId);
        }
        return APIResponseBeanUtil.success(unReadMessageCountVO);
    }

    /**
     * 查询消息列表
     *
     * @param category 分类
     * @param pageNo 页码
     * @param pageSize 页条数
     * @return
     */
    @GetMapping("/page")
    public APIPageResponseBean<List<MessageResponseVO>> messageList(@RequestParam("category") String category,
                                                                    @RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
                                                                    @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        // 查询当前登录账号
        String userId = userService.getLoginCurrentUserIdOrElseThrowUnLoginExp();
        // 查询消息列表
        return messageService.pageMessage(userId, category, pageNo, pageSize);
    }
}
