当前位置:网站首页>7、私信列表
7、私信列表
2022-07-31 02:36: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";
}
// 具体个人私信
@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";
}
/** * 获取所有未读消息id * * @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;
}
}
边栏推荐
- Calculate S=a+aa+…+aa…a
- Installation, start and stop of redis7 under Linux
- 开题报告之论文框架
- Hanyuan Hi-Tech 8-channel HDMI integrated multi-service high-definition video optical transceiver 8-channel HDMI video + 8-channel two-way audio + 8-channel 485 data + 8-channel E1 + 32-channel teleph
- Android's webview cache related knowledge collection
- 图像处理技术的心酸史
- 修改未正确放入沙盒造成苹果兼容性问题
- Coldfusion file read holes (CVE - 2010-2861)
- execsnoop 工具
- 221. Largest Square
猜你喜欢
全流程调度——MySQL与Sqoop
php 网站的多语言设置(IP地址区分国内国外)
f.grid_sample
How to do a startup CTO?
The application of AI in the whole process of medical imaging equipment
自动化办公案例:如何自动生成期数据?
CMOS和TTL的区别?
MPPT太阳能充放电控制器数据采集-通过网关采集电池电压容量电量SOC,wifi传输
JS 函数 this上下文 运行时点语法 圆括号 数组 IIFE 定时器 延时器 self.备份上下文 call apply
Hanyuan Hi-Tech 8-channel HDMI integrated multi-service high-definition video optical transceiver 8-channel HDMI video + 8-channel two-way audio + 8-channel 485 data + 8-channel E1 + 32-channel teleph
随机推荐
BAT can't sell "Medical Cloud": Hospitals flee, mountains stand, and there are rules
医疗影像领域AI软件开发流程
Detailed explanation of STP election (step + case)
mysql 索引
php 网站的多语言设置(IP地址区分国内国外)
开题报告之论文框架
mysql 视图
The real CTO is a technical person who understands products
Clustering index, and what is the difference between a clustering index
二层广播风暴(产生原因+判断+解决)
Observer mode (1)
leetcode-399: division evaluation
SQL注入 Less47(报错注入) 和Less49(时间盲注)
Classic linked list OJ strong training problem - fast and slow double pointer efficient solution
公司官网建站笔记(六):域名进行公安备案并将备案号显示在网页底部
Drools Rule Properties, Advanced Syntax
【CV项目调试】CUDNN_CONVOLUTION_FWD_SPECIFY_WORKSPACE_LIMIT问题
Verify the integer input
你们程序员为什么不靠自己的项目谋生?而必须为其他人打工?
To write good test cases, you must first learn test design