-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
759 additions
and
227 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
musicBackend/src/main/java/com/cph/musicbackend/controller/MessageController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
musicBackend/src/main/java/com/cph/musicbackend/entity/Message.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
musicBackend/src/main/java/com/cph/musicbackend/mapper/MessageMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> { | ||
} |
Oops, something went wrong.