mirror of
https://github.com/zongzibinbin/MallChat.git
synced 2026-03-16 07:43:42 +08:00
feat:消息标记重构
This commit is contained in:
@@ -64,7 +64,7 @@ public class ChatController {
|
||||
return userCache.getBlackMap().getOrDefault(BlackTypeEnum.UID.getType(), new HashSet<>());
|
||||
}
|
||||
|
||||
@GetMapping("public/member/statistic/")
|
||||
@GetMapping("public/member/statistic")
|
||||
@ApiOperation("群成员人数统计")
|
||||
public ApiResult<ChatMemberStatisticResp> getMemberStatistic() {
|
||||
return ApiResult.success(chatService.getMemberStatistic());
|
||||
|
||||
@@ -20,5 +20,6 @@ public class ChatMemberStatisticResp {
|
||||
@ApiModelProperty("在线人数")
|
||||
private Long onlineNum;//在线人数
|
||||
@ApiModelProperty("总人数")
|
||||
@Deprecated
|
||||
private Long totalNum;//总人数
|
||||
}
|
||||
|
||||
@@ -6,15 +6,14 @@ import cn.hutool.core.lang.Pair;
|
||||
import com.abin.mallchat.common.chat.dao.MessageDao;
|
||||
import com.abin.mallchat.common.chat.dao.MessageMarkDao;
|
||||
import com.abin.mallchat.common.chat.dao.RoomDao;
|
||||
import com.abin.mallchat.common.chat.domain.dto.ChatMessageMarkDTO;
|
||||
import com.abin.mallchat.common.chat.domain.entity.Message;
|
||||
import com.abin.mallchat.common.chat.domain.entity.MessageMark;
|
||||
import com.abin.mallchat.common.chat.domain.entity.Room;
|
||||
import com.abin.mallchat.common.chat.domain.enums.MessageMarkActTypeEnum;
|
||||
import com.abin.mallchat.common.common.annotation.RedissonLock;
|
||||
import com.abin.mallchat.common.common.domain.enums.YesOrNoEnum;
|
||||
import com.abin.mallchat.common.common.domain.vo.request.CursorPageBaseReq;
|
||||
import com.abin.mallchat.common.common.domain.vo.response.CursorPageBaseResp;
|
||||
import com.abin.mallchat.common.common.event.MessageMarkEvent;
|
||||
import com.abin.mallchat.common.common.event.MessageSendEvent;
|
||||
import com.abin.mallchat.common.common.exception.BusinessException;
|
||||
import com.abin.mallchat.common.common.utils.AssertUtil;
|
||||
@@ -36,8 +35,9 @@ import com.abin.mallchat.custom.chat.service.adapter.MemberAdapter;
|
||||
import com.abin.mallchat.custom.chat.service.adapter.MessageAdapter;
|
||||
import com.abin.mallchat.custom.chat.service.adapter.RoomAdapter;
|
||||
import com.abin.mallchat.custom.chat.service.helper.ChatMemberHelper;
|
||||
import com.abin.mallchat.custom.chat.service.strategy.mark.AbstractMsgMarkStrategy;
|
||||
import com.abin.mallchat.custom.chat.service.strategy.mark.MsgMarkFactory;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -165,38 +165,25 @@ public class ChatServiceImpl implements ChatService {
|
||||
public ChatMemberStatisticResp getMemberStatistic() {
|
||||
System.out.println(Thread.currentThread().getName());
|
||||
Long onlineNum = userCache.getOnlineNum();
|
||||
Long offlineNum = userCache.getOfflineNum();
|
||||
// Long offlineNum = userCache.getOfflineNum();不展示总人数
|
||||
ChatMemberStatisticResp resp = new ChatMemberStatisticResp();
|
||||
resp.setOnlineNum(onlineNum);
|
||||
resp.setTotalNum(onlineNum + offlineNum);
|
||||
// resp.setTotalNum(onlineNum + offlineNum);
|
||||
return resp;
|
||||
}
|
||||
|
||||
@Override
|
||||
@RedissonLock(key = "#uid")
|
||||
public void setMsgMark(Long uid, ChatMessageMarkReq request) {
|
||||
//用户对该消息的标记
|
||||
MessageMark messageMark = messageMarkDao.get(uid, request.getMsgId(), request.getMarkType());
|
||||
if (Objects.nonNull(messageMark)) {//有标记过消息修改一下就好
|
||||
MessageMark update = MessageMark.builder()
|
||||
.id(messageMark.getId())
|
||||
.status(transformAct(request.getActType()))
|
||||
.build();
|
||||
messageMarkDao.updateById(update);
|
||||
return;
|
||||
AbstractMsgMarkStrategy strategy = MsgMarkFactory.getStrategyNoNull(request.getMarkType());
|
||||
switch (MessageMarkActTypeEnum.of(request.getActType())) {
|
||||
case MARK:
|
||||
strategy.mark(uid, request.getMsgId());
|
||||
break;
|
||||
case UN_MARK:
|
||||
strategy.unMark(uid, request.getMsgId());
|
||||
break;
|
||||
}
|
||||
//没标记过消息,插入一条新消息
|
||||
MessageMark insert = MessageMark.builder()
|
||||
.uid(uid)
|
||||
.msgId(request.getMsgId())
|
||||
.type(request.getMarkType())
|
||||
.status(transformAct(request.getActType()))
|
||||
.build();
|
||||
messageMarkDao.save(insert);
|
||||
//发布消息标记事件
|
||||
ChatMessageMarkDTO dto = new ChatMessageMarkDTO();
|
||||
BeanUtils.copyProperties(request, dto);
|
||||
applicationEventPublisher.publishEvent(new MessageMarkEvent(this, dto));
|
||||
}
|
||||
|
||||
private Integer transformAct(Integer actType) {
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
package com.abin.mallchat.custom.chat.service.strategy.mark;
|
||||
|
||||
import com.abin.mallchat.common.chat.dao.MessageMarkDao;
|
||||
import com.abin.mallchat.common.chat.domain.dto.ChatMessageMarkDTO;
|
||||
import com.abin.mallchat.common.chat.domain.entity.MessageMark;
|
||||
import com.abin.mallchat.common.chat.domain.enums.MessageMarkActTypeEnum;
|
||||
import com.abin.mallchat.common.chat.domain.enums.MessageMarkTypeEnum;
|
||||
import com.abin.mallchat.common.common.domain.enums.YesOrNoEnum;
|
||||
import com.abin.mallchat.common.common.event.MessageMarkEvent;
|
||||
import com.abin.mallchat.common.common.exception.BusinessException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* Description: 消息标记抽象类
|
||||
* Author: <a href="https://github.com/zongzibinbin">abin</a>
|
||||
* Date: 2023-05-30
|
||||
*/
|
||||
public abstract class AbstractMsgMarkStrategy {
|
||||
@Autowired
|
||||
private MessageMarkDao messageMarkDao;
|
||||
@Autowired
|
||||
private ApplicationEventPublisher applicationEventPublisher;
|
||||
|
||||
protected abstract MessageMarkTypeEnum getTypeEnum();
|
||||
|
||||
@Transactional
|
||||
public void mark(Long uid, Long msgId) {
|
||||
exec(uid, msgId, MessageMarkActTypeEnum.MARK);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void unMark(Long uid, Long msgId) {
|
||||
exec(uid, msgId, MessageMarkActTypeEnum.UN_MARK);
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
private void init() {
|
||||
MsgMarkFactory.register(getTypeEnum().getType(), this);
|
||||
}
|
||||
|
||||
protected void exec(Long uid, Long msgId, MessageMarkActTypeEnum actTypeEnum) {
|
||||
Integer markType = getTypeEnum().getType();
|
||||
Integer actType = actTypeEnum.getType();
|
||||
MessageMark oldMark = messageMarkDao.get(uid, msgId, markType);
|
||||
if (Objects.isNull(oldMark) && actTypeEnum == MessageMarkActTypeEnum.UN_MARK) {
|
||||
//取消的类型,数据库一定有记录,没有就直接跳过操作
|
||||
return;
|
||||
}
|
||||
//插入一条新消息,或者修改一条消息
|
||||
MessageMark insertOrUpdate = MessageMark.builder()
|
||||
.id(Optional.ofNullable(oldMark).map(MessageMark::getId).orElse(null))
|
||||
.uid(uid)
|
||||
.msgId(msgId)
|
||||
.type(markType)
|
||||
.status(transformAct(actType))
|
||||
.build();
|
||||
boolean modify = messageMarkDao.saveOrUpdate(insertOrUpdate);
|
||||
if (modify) {
|
||||
//修改成功才发布消息标记事件
|
||||
ChatMessageMarkDTO dto = new ChatMessageMarkDTO(uid, msgId, markType, actType);
|
||||
applicationEventPublisher.publishEvent(new MessageMarkEvent(this, dto));
|
||||
}
|
||||
}
|
||||
|
||||
private Integer transformAct(Integer actType) {
|
||||
if (actType == 1) {
|
||||
return YesOrNoEnum.NO.getStatus();
|
||||
} else if (actType == 2) {
|
||||
return YesOrNoEnum.YES.getStatus();
|
||||
}
|
||||
throw new BusinessException("动作类型 1确认 2取消");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.abin.mallchat.custom.chat.service.strategy.mark;
|
||||
|
||||
import com.abin.mallchat.common.chat.domain.enums.MessageMarkTypeEnum;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* Description: 点踩标记策略类
|
||||
* Author: <a href="https://github.com/zongzibinbin">abin</a>
|
||||
* Date: 2023-05-30
|
||||
*/
|
||||
@Component
|
||||
public class DisLikeStrategy extends AbstractMsgMarkStrategy {
|
||||
@Autowired
|
||||
@Lazy
|
||||
private LikeStrategy likeStrategy;
|
||||
|
||||
@Override
|
||||
protected MessageMarkTypeEnum getTypeEnum() {
|
||||
return MessageMarkTypeEnum.DISLIKE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mark(Long uid, Long msgId) {
|
||||
super.mark(uid, msgId);
|
||||
//同时取消点赞的动作
|
||||
MsgMarkFactory.getStrategyNoNull(MessageMarkTypeEnum.LIKE.getType()).unMark(uid, msgId);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.abin.mallchat.custom.chat.service.strategy.mark;
|
||||
|
||||
import com.abin.mallchat.common.chat.domain.enums.MessageMarkTypeEnum;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* Description: 点赞标记策略类
|
||||
* Author: <a href="https://github.com/zongzibinbin">abin</a>
|
||||
* Date: 2023-05-30
|
||||
*/
|
||||
@Component
|
||||
public class LikeStrategy extends AbstractMsgMarkStrategy {
|
||||
|
||||
@Override
|
||||
protected MessageMarkTypeEnum getTypeEnum() {
|
||||
return MessageMarkTypeEnum.LIKE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void mark(Long uid, Long msgId) {
|
||||
super.mark(uid, msgId);
|
||||
//同时取消点踩的动作
|
||||
MsgMarkFactory.getStrategyNoNull(MessageMarkTypeEnum.DISLIKE.getType()).unMark(uid, msgId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.abin.mallchat.custom.chat.service.strategy.mark;
|
||||
|
||||
import com.abin.mallchat.common.common.exception.CommonErrorEnum;
|
||||
import com.abin.mallchat.common.common.utils.AssertUtil;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Description: 消息标记策略工厂
|
||||
* Author: <a href="https://github.com/zongzibinbin">abin</a>
|
||||
* Date: 2023-05-30
|
||||
*/
|
||||
public class MsgMarkFactory {
|
||||
private static final Map<Integer, AbstractMsgMarkStrategy> STRATEGY_MAP = new HashMap<>();
|
||||
|
||||
public static void register(Integer markType, AbstractMsgMarkStrategy strategy) {
|
||||
STRATEGY_MAP.put(markType, strategy);
|
||||
}
|
||||
|
||||
public static AbstractMsgMarkStrategy getStrategyNoNull(Integer markType) {
|
||||
AbstractMsgMarkStrategy strategy = STRATEGY_MAP.get(markType);
|
||||
AssertUtil.isNotEmpty(strategy, CommonErrorEnum.PARAM_VALID);
|
||||
return strategy;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user