当前位置:网站首页>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;
}
}
边栏推荐
- vlan间路由+静态路由+NAT(PAT+静态NAT)综合实验
- Static route analysis (the longest mask matching principle + active and standby routes)
- 10 权限介绍
- Installation, start and stop of redis7 under Linux
- Classic linked list OJ strong training problem - fast and slow double pointer efficient solution
- 基于FPGA的售货机
- BAT can't sell "Medical Cloud": Hospitals flee, mountains stand, and there are rules
- Drools规则属性,高级语法
- AI在医疗影像设备全流程应用
- Draw Your Cards
猜你喜欢

coldfusion8 background scheduled tasks take shell

Intel's software and hardware optimization empowers Neusoft to accelerate the arrival of the era of smart medical care

STP选举(步骤+案列)详解

The application of AI in the whole process of medical imaging equipment

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

STM32CUBEMX开发GD32F303(11)----ADC在DMA模式下扫描多个通道

【银行系列第一期】中国人民银行

Manchester City confuses fans with smart scarf that detects emotions

To write good test cases, you must first learn test design

MPPT太阳能充放电控制器数据采集-通过网关采集电池电压容量电量SOC,wifi传输
随机推荐
Intel's software and hardware optimization empowers Neusoft to accelerate the arrival of the era of smart medical care
ShardingJDBC usage summary
vlan间路由+静态路由+NAT(PAT+静态NAT)综合实验
基于FPGA的图像实时采集
【Android】Room —— SQLite的替代品
LeetCode 每日一题 2022/7/25-2022/7/31
BAT can't sell "Medical Cloud": Hospitals flee, mountains stand, and there are rules
Problems that need to be solved by the tcp framework
字体压缩神器font-spider的使用
The Sad History of Image Processing Technology
11、Redis实现关注、取消关注以及关注和粉丝列表
Word/Excel fixed table size, when filling in the content, the table does not change with the cell content
User interaction + formatted output
软件积累 -- 截图软件ScreenToGif
完整复制虚拟机原理(云计算)
力扣刷题之有效的正方形(每日一题7/29)
How to do a startup CTO?
基于opencv实现人脸检测
The difference between link and @import
汉源高科8路HDMI综合多业务高清视频光端机8路HDMI视频+8路双向音频+8路485数据+8路E1+32路电话+4路千兆物理隔离网络