当前位置:网站首页>7. List of private messages
7. List of private messages
2022-07-31 02:39:00 【A snicker】
1、创建Message实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Message {
private int id;
private int fromId;
private int toId;
private String conversationId;
private String content;
private int status;
private Date createTime;
}
2、数据访问层MessageMapper
@Mapper
public interface MessageMapper {
// 查询当前用户的会话列表,针对每个会话只返回一条最新的私信.
List<Message> selectConversations(int userId, int offset, int limit);
// 查询当前用户的会话数量.
int selectConversationCount(int userId);
// 查询某个会话所包含的私信列表.
List<Message> selectLetters(String conversationId, int offset, int limit);
// 查询某个会话所包含的私信数量.
int selectLetterCount(String conversationId);
// 查询未读私信的数量
int selectLetterUnreadCount(int userId, String conversationId);
}
message-mapper.xml
<mapper namespace="com.nowcoder.community.dao.MessageMapper">
<sql id="selectFields">
id, from_id, to_id, conversation_id, content, status, create_time
</sql>
<!-- // 查询当前用户的会话列表,针对每个会话只返回一条最新的私信.-->
<!-- List<Message> selectConversations(int userId, int offset, int limit);-->
<select id="selectConversations" resultType="Message">
select <include refid="selectFields"></include>
from message
where id in(
select max(id) from message
where status != 2
and from_id != 1
and (from_id = #{
userId} or to_id = #{
userId})
group by conversation_id
)
order by id desc
limit #{
offset}, #{
limit}
</select>
<!-- // 查询当前用户的会话数量.-->
<!-- int selectConversationCount(int userId);-->
<select id="selectConversationCount" resultType="int">
select count(m.maxid) from(
select max(id) as maxid from message
where status != 2
and from_id != 1
and (from_id = #{
userId} or to_id = #{
userId})
group by conversation_id
) as m
</select>
<!-- // 查询某个会话所包含的私信列表.-->
<!-- List<Message> selectLetters(String conversationId, int offset, int limit);-->
<select id="selectLetters" resultType="Message">
select <include refid="selectFields"></include>
from message
where status != 2
and from_id != 1
and conversation_id = #{
conversationId}
order by id desc
limit #{
offset}, #{
limit}
</select>
<!-- // 查询某个会话所包含的私信数量.-->
<!-- int selectLetterCount(String conversationId);-->
<select id="selectLetterCount" resultType="int">
select count(id)
from message
where status != 2
and from_id != 1
and conversation_id = #{
conversationId}
</select>
<!-- // 查询未读私信的数量-->
<!-- int selectLetterUnreadCount(int userId, String conversationId);-->
<select id="selectLetterUnreadCount" resultType="int">
select count(id)
from message
where status = 0
and from_id != 1
and to_id = #{
userId}
<if test="conversationId != null">
and conversation_id = #{
conversationId}
</if>
</select>
</mapper>
3、业务层
@Service
public class MessageService {
@Autowired
private MessageMapper messageMapper;
public List<Message> findConversations(int userId, int offset, int limit) {
return messageMapper.selectConversations(userId, offset, limit);
}
public int findConversationCount(int userId) {
return messageMapper.selectConversationCount(userId);
}
public List<Message> findLetters(String conversationId, int offset, int limit) {
return messageMapper.selectLetters(conversationId, offset, limit);
}
public int findLetterCount(String conversationId) {
return messageMapper.selectLetterCount(conversationId);
}
public int findLetterUnreadCount(int userId, String conversationId) {
return messageMapper.selectLetterUnreadCount(userId, conversationId);
}
}
4、表现层
@Controller
public class MessageController implements CommunityConstant {
@Autowired
private MessageService messageService;
@Autowired
private HostHolder hostHolder;
@Autowired
private UserService userService;
// 私信列表
@GetMapping("/letter/list")
public String getLetterList(Model model, Page page) {
User user = hostHolder.getUser();
// 分页信息
page.setLimit(5);
page.setPath("/letter/list");
page.setRows(messageService.findConversationCount(user.getId()));
// 会话列表
List<Message> conversationList =
messageService.findConversations(user.getId(), page.getOffset(), page.getLimit());
List<Map<String, Object>> conversations = new ArrayList<>();
if (conversationList != null) {
for (Message message : conversationList) {
Map<String, Object> map = new HashMap<>(16);
map.put("conversation", message);
map.put("letterCount", messageService.findLetterCount(message.getConversationId()));
map.put(
"unreadCount",
messageService.findLetterUnreadCount(
user.getId(), message.getConversationId()));
int targetId =
user.getId() == message.getFromId()
? message.getToId()
: message.getFromId();
map.put("target", userService.findUserById(targetId));
conversations.add(map);
}
}
model.addAttribute("conversations", conversations);
// 查询未读消息数量
int letterUnreadCount = messageService.findLetterUnreadCount(user.getId(), null);
model.addAttribute("letterUnreadCount", letterUnreadCount);
int noticeUnreadCount =
messageService.findNoticeUnreadCount(hostHolder.getUser().getId(), null);
model.addAttribute("noticeUnreadCount", noticeUnreadCount);
return "site/letter";
}
// specific personal message
@GetMapping("/letter/detail/{conversationId}")
public String getLetterDetail(
@PathVariable("conversationId") String conversationId, Page page, Model model) {
// 分页信息
page.setLimit(5);
page.setPath("/letter/detail/" + conversationId);
page.setRows(messageService.findLetterCount(conversationId));
// 私信列表
List<Message> letterList =
messageService.findLetters(conversationId, page.getOffset(), page.getLimit());
List<Map<String, Object>> letters = new ArrayList<>();
if (letterList != null) {
for (Message message : letterList) {
Map<String, Object> map = new HashMap<>(16);
map.put("letter", message);
map.put("fromUser", userService.findUserById(message.getFromId()));
letters.add(map);
}
}
model.addAttribute("letters", letters);
model.addAttribute("letters", letters);
// 私信目标
model.addAttribute("target", getLetterTarget(conversationId));
// 设置已读
List<Integer> ids = getLetterIds(letterList);
if (!ids.isEmpty()) {
messageService.readMessage(ids);
}
return "site/letter-detail";
}
/** * Get all unread messagesid * * @param letterList * @return */
private List<Integer> getLetterIds(List<Message> letterList) {
List<Integer> ids = new ArrayList<>();
if (letterList != null) {
for (Message message : letterList) {
if (hostHolder.getUser().getId() == message.getToId() && message.getStatus() == 0) {
ids.add(message.getId());
}
}
}
return ids;
}
}
边栏推荐
- Pythagorean tuple od js
- Introduction to flask series 】 【 flask - using SQLAlchemy
- 12 磁盘相关命令
- 12 pictures take you to fully understand service current limit, circuit breaker, downgrade, and avalanche
- Introduction and use of Drools WorkBench
- Observer mode (1)
- AI中的数学思想
- Classic linked list OJ strong training problem - fast and slow double pointer efficient solution
- TCP/IP四层模型
- CorelDRAW2022精简亚太新增功能详细介绍
猜你喜欢
力扣刷题之爬楼梯(7/30)
StringJoiner详解
mysql 视图
Drools WorkBench的简介与使用
Pythagorean tuple od js
Classic linked list OJ strong training problem - fast and slow double pointer efficient solution
Problems that need to be solved by the tcp framework
STM32CUBEMX开发GD32F303(11)----ADC在DMA模式下扫描多个通道
ShardingJDBC使用总结
String为什么不可变?
随机推荐
项目开发软件目录结构规范
【C语言基础】解决C语言error: expected ‘;‘, ‘,‘ or ‘)‘ before ‘&‘ token
AtCoder Beginner Contest 261 Partial Solution
LeetCode Daily Question 2022/7/25-2022/7/31
如何搭建私有yum源
经典链表OJ强训题——快慢双指针高效解法
15、网站统计数据
FPGA-based vending machine
Fiddler captures packets to simulate weak network environment testing
golang GUI for nuxui — HelloWorld
基于FPGA的图像实时采集
Maximum area of solar panel od js
Introduction to flask series 】 【 flask - using SQLAlchemy
Layer 2 broadcast storm (cause + judgment + solution)
YOLOV5学习笔记(二)——环境安装+运行+训练
【shell基础】判断目录是否为空
The effective square of the test (one question of the day 7/29)
Pythagorean tuple od js
Verify the integer input
ShardingJDBC基本介绍