package com.ruoyi.generator.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.domain.CommunityChatMsg;
import com.ruoyi.system.domain.vo.CommunityChatMsgVo;
import com.ruoyi.system.domain.vo.SysUserVo;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.ICommunityChatMsgService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 聊天记录
 *
 * @author fangzhen
 */
@Api(tags = "聊天记录")
@RestController
@RequestMapping("/community/chat")
public class CommunityChatMsgController extends BaseController {

    @Resource
    private ICommunityChatMsgService communityChatMsgService;

    @Resource
    private SysUserMapper sysUserMapper;

    /**
     * 获取当前登录用户聊天记录
     */
    @ApiOperation("获取当前登录用户聊天记录")
    @GetMapping()
    public AjaxResult chatRecord(Long otherUserId, String type) {
        Long userId = SecurityUtils.getUserId();
        List<CommunityChatMsg> chatMsgList = communityChatMsgService.list(new QueryWrapper<CommunityChatMsg>()
                .eq("sender_id", userId)
                .eq("receiver_id", otherUserId)
                .or()
                .eq("sender_id", otherUserId)
                .eq("receiver_id", userId)
                .eq("type", type)
                .orderByDesc("create_time"));
        //更新该消息标记为已读
        List<Long> msgId = chatMsgList.stream()
                .filter(item -> !item.isRead())
                .map(CommunityChatMsg::getId)
                .collect(Collectors.toList());
        if (!msgId.isEmpty()) {
            CommunityChatMsg chatMsg = new CommunityChatMsg();
            chatMsg.setRead(true);
            communityChatMsgService.update(chatMsg, new UpdateWrapper<CommunityChatMsg>().in("id", msgId));
        }

        List<CommunityChatMsgVo> chatMsgVos = new ArrayList<>();
        CommunityChatMsgVo chatMsgVo = null;
        for (CommunityChatMsg chatMsg : chatMsgList) {
            chatMsgVo = new CommunityChatMsgVo();
            BeanUtils.copyProperties(chatMsg, chatMsgVo);
            SysUser senderUser = sysUserMapper.selectUserById(chatMsgVo.getSenderId());
            SysUser receiveUser = sysUserMapper.selectUserById(chatMsgVo.getReceiverId());
            chatMsgVo.setSenderNickName(senderUser.getNickName());
            chatMsgVo.setSenderAvatar(senderUser.getAvatar());
            chatMsgVo.setReceiverNickName(receiveUser.getNickName());
            chatMsgVo.setReceiverAvatar(receiveUser.getAvatar());
            chatMsgVos.add(chatMsgVo);
        }
        return AjaxResult.success(chatMsgVos);
    }

    /**
     * 获取当前登录用户聊天记录
     */
    @ApiOperation("获取当前登录用户聊天列表")
    @GetMapping("/list")
    public AjaxResult chatList(String type) {
        Long userId = SecurityUtils.getUserId();
        List<SysUserVo> sysUserChatVos = communityChatMsgService.getChatListWithLatestMessage(userId, type);
        return AjaxResult.success(sysUserChatVos);
    }
}