当前位置:网站首页>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;
}
}
边栏推荐
- coldfusion8 background scheduled tasks take shell
- 跨专业考研难度大?“上岸”成功率低?这份实用攻略请收下!
- LeetCode 1161 最大层内元素和[BFS 二叉树] HERODING的LeetCode之路
- 静态路由+PAT+静态NAT(讲解+实验)
- The final exam first year course
- Real-time image acquisition based on FPGA
- 修改未正确放入沙盒造成苹果兼容性问题
- BAT卖不动「医疗云」:医院逃离、山头林立、行有行规
- SQL注入 Less54(限制次数的SQL注入+union注入)
- 多线程下类对象的服务承诺探讨
猜你喜欢
Force buckled brush the stairs (7/30)
STM32CUBEMX develops GD32F303 (11) ---- ADC scans multiple channels in DMA mode
【Android】Room —— SQLite的替代品
Problems that need to be solved by the tcp framework
The real CTO is a technical person who understands products
mmdetection trains a model related command
Detailed explanation of STP election (step + case)
Between two orderly array of additive and Topk problem
The comprehensive result of the case statement, do you know it?[Verilog Advanced Tutorial]
MPPT solar charge controller data collection - through the gateway acquisition capacity battery SOC battery voltage, wi-fi
随机推荐
图像处理技术的心酸史
Pythagorean tuple od js
基于opencv实现人脸检测
Detailed explanation of STP election (step + case)
Intel's software and hardware optimization empowers Neusoft to accelerate the arrival of the era of smart medical care
Observer mode (1)
CorelDRAW2022精简亚太新增功能详细介绍
AtCoder Beginner Contest 261 部分题解
Introduction and use of Drools WorkBench
汉源高科8路HDMI综合多业务高清视频光端机8路HDMI视频+8路双向音频+8路485数据+8路E1+32路电话+4路千兆物理隔离网络
Brute Force/Adjacency Matrix Breadth First Directed Weighted Graph Undirected Weighted Graph
Android's webview cache related knowledge collection
MPPT solar charge controller data collection - through the gateway acquisition capacity battery SOC battery voltage, wi-fi
coldfusion8 background scheduled tasks take shell
Inter-vlan routing + static routing + NAT (PAT + static NAT) comprehensive experiment
BAT can't sell "Medical Cloud": Hospitals flee, mountains stand, and there are rules
tcp框架需要解决的问题
What level of software testing does it take to get a 9K job?
Maximum area of solar panel od js
SQL注入 Less46(order by后的注入+rand()布尔盲注)