Skip to content

Commit

Permalink
chat ui
Browse files Browse the repository at this point in the history
  • Loading branch information
cph999 committed Oct 14, 2024
1 parent 8d1a1db commit 478a2ac
Show file tree
Hide file tree
Showing 17 changed files with 759 additions and 227 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,10 @@ public CommonResult(Integer code, String message, T data) {
this.message = message;
this.data = data;
}

public CommonResult(Integer code, String message, List<T> datas) {
this.code = code;
this.message = message;
this.datas = datas;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.cph.musicbackend.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cph.musicbackend.aspect.RecognizeAddress;
import com.cph.musicbackend.aspect.UserContext;
import com.cph.musicbackend.common.CommonResult;
import com.cph.musicbackend.entity.Message;
import com.cph.musicbackend.entity.User;
import com.cph.musicbackend.mapper.MessageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

@RestController
public class MessageController {

@Autowired
MessageMapper messagesMapper;

@PostMapping("/api/messages")
@RecognizeAddress
public CommonResult getAllMessages() {
User currentUser = UserContext.getCurrentUser();
List<Message> messages = messagesMapper.selectList(
new QueryWrapper<Message>()
.eq("from_id", currentUser.getId())
.or()
.eq("to_id", currentUser.getId())
);

// 将对话按照无序的 from_id 和 to_id 进行分组
Map<String, List<Message>> collect = messages.stream()
.collect(Collectors.groupingBy(m -> {
Long minId = (long) Math.min(m.getFromId(), m.getToId());
Long maxId = (long) Math.max(m.getFromId(), m.getToId());
return minId + " " + maxId;
}));

ArrayList<List<Message>> lists = new ArrayList<>();
SimpleDateFormat sdf = new SimpleDateFormat("MM月dd日");

collect.values().forEach(temp -> {
if (!CollectionUtils.isEmpty(temp)) {
lists.add(temp.stream()
.sorted((a, b) -> b.getCreatedTime().compareTo(a.getCreatedTime())) // 按照创建时间排序
.map(m -> {
String formattedDate = sdf.format(m.getCreatedTime());
m.setShowTime(formattedDate);
return m;
})
.collect(Collectors.toList()));
}
});

// 最终按照最新消息的创建时间对对话框进行排序
List<List<Message>> messageList = lists.stream()
.sorted((a, b) -> b.get(0).getCreatedTime().compareTo(a.get(0).getCreatedTime()))
.collect(Collectors.toList());

return new CommonResult(200, "success", messageList);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.cph.musicbackend.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.experimental.Accessors;

import java.util.Date;

@Data
@Accessors(chain =true)
@TableName("user_message")
public class Message {

@TableId(type = IdType.AUTO, value = "id")
private Integer id;

private Integer fromId;
private String fromNickname;
private String fromIcon;
private Integer toId;
private String toNickname;
private String toIcon;

private String message;
private Date createdTime;

@TableField(exist = false)
private String showTime;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.cph.musicbackend.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.cph.musicbackend.entity.Message;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface MessageMapper extends BaseMapper<Message> {
}
Loading

0 comments on commit 478a2ac

Please sign in to comment.