当前位置:网站首页>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;
}
}
边栏推荐
- Coldfusion file read holes (CVE - 2010-2861)
- 10 权限介绍
- Face detection based on opencv
- Brute Force/Adjacency List Breadth First Directed Weighted Graph Undirected Weighted Graph
- TCP/IP四层模型
- Unity界面总体介绍
- Software testing basic interface testing - getting started with Jmeter, you should pay attention to these things
- The real CTO is a technical person who understands products
- 静态路由+PAT+静态NAT(讲解+实验)
- mysql 索引
猜你喜欢

Shell script to loop through values in log file to sum and calculate average, max and min

Intranet Infiltration - Privilege Escalation

静态路由+PAT+静态NAT(讲解+实验)

Basic learning about Redis related content

Inter-vlan routing + static routing + NAT (PAT + static NAT) comprehensive experiment

f.grid_sample

What level of software testing does it take to get a 9K job?

你们程序员为什么不靠自己的项目谋生?而必须为其他人打工?

完整复制虚拟机原理(云计算)

Manchester City confuses fans with smart scarf that detects emotions
随机推荐
The application of AI in the whole process of medical imaging equipment
开题报告之论文框架
The effective square of the test (one question of the day 7/29)
【shell基础】判断目录是否为空
Teach you how to configure Jenkins automated email notifications
mysql 索引
12 磁盘相关命令
Basic introduction to ShardingJDBC
Drools WorkBench的简介与使用
Unity界面总体介绍
Inter-vlan routing + static routing + NAT (PAT + static NAT) comprehensive experiment
二层广播风暴(产生原因+判断+解决)
怎样做好一个创业公司CTO?
Go 项目实战-获取多级分类下的全部商品
mysql 视图
自动化办公案例:如何自动生成期数据?
经典链表OJ强训题——快慢双指针高效解法
SQL注入 Less47(报错注入) 和Less49(时间盲注)
16. Registration Center-consul
Intel's software and hardware optimization empowers Neusoft to accelerate the arrival of the era of smart medical care