|
|
@@ -1,5 +1,8 @@
|
|
|
package cn.vber.system.service.impl;
|
|
|
|
|
|
+import cn.vber.common.core.service.NoticeService;
|
|
|
+import cn.vber.common.notice.dto.NoticeMessageDetailDto;
|
|
|
+import cn.vber.common.satoken.utils.LoginHelper;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
@@ -26,6 +29,7 @@ import cn.vber.system.service.ISysNoticeService;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
@@ -36,9 +40,8 @@ import java.util.*;
|
|
|
*/
|
|
|
@RequiredArgsConstructor
|
|
|
@Service
|
|
|
-public class SysNoticeServiceImpl implements ISysNoticeService {
|
|
|
+public class SysNoticeServiceImpl implements ISysNoticeService, NoticeService {
|
|
|
|
|
|
- private final static String MESSAGE_TYPE = "NOTICE";
|
|
|
private final SysNoticeMapper baseMapper;
|
|
|
private final SysNoticeStatusMapper noticeStatusMapper;
|
|
|
private final SysUserMapper userMapper;
|
|
|
@@ -105,7 +108,7 @@ public class SysNoticeServiceImpl implements ISysNoticeService {
|
|
|
int rows = baseMapper.insert(notice);
|
|
|
if (rows > 0) {
|
|
|
// 发送消息
|
|
|
- SysMessageVo sysMessageVo = getSysMessageVo(notice);
|
|
|
+ SysMessageVo sysMessageVo = buildSysMessageVo(notice);
|
|
|
String msg = JsonUtils.toJsonString(sysMessageVo);
|
|
|
SseMessageUtils.publishAll(msg);
|
|
|
}
|
|
|
@@ -114,6 +117,22 @@ public class SysNoticeServiceImpl implements ISysNoticeService {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 构建系统消息
|
|
|
+ *
|
|
|
+ * @param notice 公告
|
|
|
+ * @return 系统消息
|
|
|
+ */
|
|
|
+ private SysMessageVo buildSysMessageVo(SysNotice notice) {
|
|
|
+ SysMessageVo sysMessageVo = new SysMessageVo();
|
|
|
+ sysMessageVo.setMessageId(UUID.randomUUID().toString());
|
|
|
+ sysMessageVo.setNoticeType(notice.getNoticeType());
|
|
|
+ sysMessageVo.setAlertType("success");
|
|
|
+ sysMessageVo.setMessageTitle(notice.getNoticeTitle());
|
|
|
+ sysMessageVo.setMessageContent(notice.getNoticeContent());
|
|
|
+ sysMessageVo.setMessageTime(notice.getCreateTime());
|
|
|
+ return sysMessageVo;
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* 修改公告
|
|
|
@@ -160,8 +179,7 @@ public class SysNoticeServiceImpl implements ISysNoticeService {
|
|
|
public int read(Long userId, String messageId, String messageType) {
|
|
|
SysNoticeStatus sysNoticeStatus = noticeStatusMapper.selectOne(new LambdaQueryWrapper<SysNoticeStatus>()
|
|
|
.eq(SysNoticeStatus::getUserId, userId)
|
|
|
- .eq(SysNoticeStatus::getMessageId, messageId)
|
|
|
- .eq(SysNoticeStatus::getMessageType, messageType));
|
|
|
+ .eq(SysNoticeStatus::getMessageId, messageId));
|
|
|
if (sysNoticeStatus == null) {
|
|
|
sysNoticeStatus = new SysNoticeStatus();
|
|
|
sysNoticeStatus.setMessageId(messageId);
|
|
|
@@ -190,7 +208,7 @@ public class SysNoticeServiceImpl implements ISysNoticeService {
|
|
|
|
|
|
List<SysNotice> list = baseMapper.selectList(new LambdaQueryWrapper<SysNotice>()
|
|
|
.eq(SysNotice::getStatus, "0")
|
|
|
- .apply("notice_id NOT IN (SELECT message_id FROM sys_notice_status WHERE message_type='" + MESSAGE_TYPE + "' AND user_id=" + userId + " AND status='1')")
|
|
|
+ .apply("notice_id NOT IN (SELECT message_id FROM sys_notice_status WHERE user_id=" + userId + " AND status='1')")
|
|
|
.orderByDesc(SysNotice::getCreateTime));
|
|
|
List<SysMessageVo> msgList = new ArrayList<>();
|
|
|
for (SysNotice notice : list) {
|
|
|
@@ -200,19 +218,128 @@ public class SysNoticeServiceImpl implements ISysNoticeService {
|
|
|
return msgList;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 查询我的公告列表
|
|
|
+ *
|
|
|
+ * @param notice 查询条件
|
|
|
+ * @param pageQuery 分页参数
|
|
|
+ * @return 公告分页列表
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public TableDataInfo<SysNoticeVo> selectPageMyNoticeList(SysNoticeBo notice, PageQuery pageQuery) {
|
|
|
+ Long userId = LoginHelper.getUserId();
|
|
|
+ LambdaQueryWrapper<SysNotice> lqw = new LambdaQueryWrapper<SysNotice>()
|
|
|
+ .eq(SysNotice::getStatus, "0")
|
|
|
+ .orderByDesc(SysNotice::getCreateTime);
|
|
|
+ Page<SysNoticeVo> page = baseMapper.selectMyNoticePage(pageQuery.build(), lqw, userId);
|
|
|
+
|
|
|
+ // 检查类型为2的公告是否都有对应的sys_notice_status记录,没有则插入默认记录
|
|
|
+ List<SysNotice> type2Notices = baseMapper.selectList(new LambdaQueryWrapper<SysNotice>()
|
|
|
+ .eq(SysNotice::getNoticeType, "2")
|
|
|
+ .eq(SysNotice::getStatus, "0"));
|
|
|
+
|
|
|
+ if (!type2Notices.isEmpty()) {
|
|
|
+ for (SysNotice sysNotice : type2Notices) {
|
|
|
+ SysNoticeStatus status = noticeStatusMapper.selectOne(new LambdaQueryWrapper<SysNoticeStatus>()
|
|
|
+ .eq(SysNoticeStatus::getMessageId, sysNotice.getNoticeId().toString())
|
|
|
+ .eq(SysNoticeStatus::getUserId, userId));
|
|
|
+
|
|
|
+ if (status == null) {
|
|
|
+ // 插入默认记录
|
|
|
+ SysNoticeStatus newStatus = new SysNoticeStatus();
|
|
|
+ newStatus.setUserId(userId);
|
|
|
+ newStatus.setMessageId(sysNotice.getNoticeId().toString());
|
|
|
+ newStatus.setMessageType("2");
|
|
|
+ newStatus.setStatus("0");
|
|
|
+ noticeStatusMapper.insert(newStatus);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return TableDataInfo.build(page);
|
|
|
+ }
|
|
|
+
|
|
|
private @NotNull SysMessageVo getSysMessageVo(SysNotice notice) {
|
|
|
SysMessageVo msg = new SysMessageVo();
|
|
|
msg.setMessageId(notice.getNoticeId().toString());
|
|
|
msg.setMessageTitle(notice.getNoticeTitle());
|
|
|
- msg.setMessageContent(notice.getNoticeContent());
|
|
|
- msg.setAlertType("success");
|
|
|
- msg.setMessageType(MESSAGE_TYPE);
|
|
|
- msg.setRouterPath(sysMessageConfig.getNoticeRouterPath());
|
|
|
+ msg.setNoticeType(notice.getNoticeType());
|
|
|
msg.setMessageTime(notice.getCreateTime());
|
|
|
- Map<String, String> params = new HashMap<>();
|
|
|
- params.put("id", notice.getNoticeId().toString());
|
|
|
- params.put("title", notice.getNoticeTitle());
|
|
|
- msg.setParams(params);
|
|
|
+ NoticeMessageDetailDto detail = JsonUtils.parseObject(notice.getNoticeContent(), NoticeMessageDetailDto.class);
|
|
|
+ if(detail != null){
|
|
|
+ msg.setBusinessType(detail.getBusinessType());
|
|
|
+ msg.setData(detail.getData());
|
|
|
+ msg.setMessageContent(detail.getContent());
|
|
|
+ msg.setAlertType(detail.getAlertType());
|
|
|
+ }
|
|
|
return msg;
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 阅读所有公告
|
|
|
+ *
|
|
|
+ * @param userId 用户Id
|
|
|
+ * @return 结果
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ @Transactional
|
|
|
+ public int readAll(Long userId) {
|
|
|
+ int count = 0;
|
|
|
+ count += noticeStatusMapper.update(new LambdaUpdateWrapper<SysNoticeStatus>()
|
|
|
+ .eq(SysNoticeStatus::getUserId, userId)
|
|
|
+ .eq(SysNoticeStatus::getMessageType, "1")
|
|
|
+ .set(SysNoticeStatus::getStatus, "1"));
|
|
|
+
|
|
|
+ // 查询所有的公告(这些在sys_notice_status中可能没有记录)
|
|
|
+ List<SysNotice> type2Notices = baseMapper.selectList(new LambdaQueryWrapper<SysNotice>()
|
|
|
+ .eq(SysNotice::getNoticeType, "2")
|
|
|
+ .eq(SysNotice::getStatus, "0"));
|
|
|
+
|
|
|
+ // 为公告创建阅读记录或更新现有记录
|
|
|
+ for (SysNotice notice : type2Notices) {
|
|
|
+ SysNoticeStatus status = noticeStatusMapper.selectOne(new LambdaQueryWrapper<SysNoticeStatus>()
|
|
|
+ .eq(SysNoticeStatus::getUserId, userId)
|
|
|
+ .eq(SysNoticeStatus::getMessageId, notice.getNoticeId().toString()));
|
|
|
+
|
|
|
+ if (status == null) {
|
|
|
+ // 如果没有记录,则插入新记录并设置为已读
|
|
|
+ SysNoticeStatus newStatus = new SysNoticeStatus();
|
|
|
+ newStatus.setUserId(userId);
|
|
|
+ newStatus.setMessageId(notice.getNoticeId().toString());
|
|
|
+ newStatus.setMessageType("2");
|
|
|
+ newStatus.setStatus("1"); // 设置为已读
|
|
|
+ noticeStatusMapper.insert(newStatus);
|
|
|
+ count++;
|
|
|
+ } else if (!"1".equals(status.getStatus())) {
|
|
|
+ // 如果有记录但未读,则更新为已读
|
|
|
+ count += noticeStatusMapper.update(new LambdaUpdateWrapper<SysNoticeStatus>()
|
|
|
+ .eq(SysNoticeStatus::getUserId, userId)
|
|
|
+ .eq(SysNoticeStatus::getMessageId, notice.getNoticeId().toString())
|
|
|
+ .set(SysNoticeStatus::getStatus, "1"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return count;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional
|
|
|
+ public Long savePushNotice(String noticeTitle, String noticeType, String noticeContent, List<Long> userIds) {
|
|
|
+ SysNotice notice = new SysNotice();
|
|
|
+ notice.setNoticeTitle(noticeTitle);
|
|
|
+ notice.setNoticeType(noticeType );
|
|
|
+ notice.setNoticeContent(noticeContent);
|
|
|
+ notice.setStatus("0");
|
|
|
+ if( baseMapper.insert(notice)>0){
|
|
|
+ for (Long userId : userIds) {
|
|
|
+ SysNoticeStatus sysNoticeStatus = new SysNoticeStatus();
|
|
|
+ sysNoticeStatus.setMessageId(notice.getNoticeId().toString());
|
|
|
+ sysNoticeStatus.setMessageType(noticeType);
|
|
|
+ sysNoticeStatus.setUserId(userId);
|
|
|
+ sysNoticeStatus.setStatus("0");
|
|
|
+ noticeStatusMapper.insert(sysNoticeStatus);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return notice.getNoticeId();
|
|
|
+ }
|
|
|
}
|