Browse Source

添加用户查询站内信分页列表数据,未读站内信数量。

xrcoder 3 years ago
parent
commit
af4290784e

+ 1 - 0
yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/enums/ErrorCodeConstants.java

@@ -145,6 +145,7 @@ public interface ErrorCodeConstants {
     // ========== 站内信模版 1002023000 ==========
     ErrorCode NOTIFY_TEMPLATE_NOT_EXISTS = new ErrorCode(1002023000, "站内信模版不存在");
     ErrorCode NOTIFY_TEMPLATE_CODE_DUPLICATE = new ErrorCode(1002023001, "已经存在编码为【{}】的站内信模板");
+    ErrorCode NOTIFY_TEMPLATE_PARAM_MISS = new ErrorCode(1002023002, "模板参数({})缺失");
 
     // ========== 站内信 1002024000 ==========
     ErrorCode NOTIFY_MESSAGE_NOT_EXISTS = new ErrorCode(1002024000, "站内信不存在");

+ 15 - 1
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/notify/UserNotifyMessageController.java

@@ -1,5 +1,7 @@
 package cn.iocoder.yudao.module.system.controller.admin.notify;
 
+import cn.hutool.core.collection.CollUtil;
+import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
 import cn.iocoder.yudao.framework.common.pojo.CommonResult;
 import cn.iocoder.yudao.framework.common.pojo.PageResult;
 import cn.iocoder.yudao.module.system.controller.admin.notify.vo.message.NotifyMessagePageReqVO;
@@ -23,6 +25,7 @@ import java.util.Collections;
 import java.util.List;
 
 import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
+import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
 
 /**
  * 管理后台 - 站内信-消息中心
@@ -42,6 +45,8 @@ public class UserNotifyMessageController {
     @GetMapping("/page")
     @ApiOperation("获得站内信分页")
     public CommonResult<PageResult<NotifyMessageRespVO>> getNotifyMessagePage(@Valid NotifyMessagePageReqVO pageVO) {
+        pageVO.setUserId(getLoginUserId());
+        pageVO.setUserType(UserTypeEnum.ADMIN.getValue());
         PageResult<NotifyMessageDO> pageResult = notifyMessageService.getNotifyMessagePage(pageVO);
         return success(NotifyMessageConvert.INSTANCE.convertPage(pageResult));
     }
@@ -49,13 +54,22 @@ public class UserNotifyMessageController {
     @GetMapping("/latest/list")
     @ApiOperation("获得最新10站内信列表")
     public CommonResult<List<NotifyMessageRespVO>> getNotifyLatestMessageList() {
+        NotifyMessagePageReqVO reqVO = new NotifyMessagePageReqVO();
+        reqVO.setUserId(getLoginUserId());
+        reqVO.setUserType(UserTypeEnum.ADMIN.getValue());
+        reqVO.setPageNo(1);
+        reqVO.setPageSize(10);
+        PageResult<NotifyMessageDO> pageResult = notifyMessageService.getNotifyMessagePage(reqVO);
+        if (CollUtil.isNotEmpty(pageResult.getList())) {
+            return success(NotifyMessageConvert.INSTANCE.convertList(pageResult.getList()));
+        }
         return success(Collections.emptyList());
     }
 
     @GetMapping("/unread/count")
     @ApiOperation("获得未读站内信数量")
     public CommonResult<Long> getUnreadNotifyMessageCount() {
-        return success(1L);
+        return success(notifyMessageService.getUnreadNotifyMessageCount(getLoginUserId(), UserTypeEnum.ADMIN.getValue()));
     }
 
     @GetMapping("/read")

+ 6 - 0
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/controller/admin/notify/vo/message/NotifyMessagePageReqVO.java

@@ -24,4 +24,10 @@ public class NotifyMessagePageReqVO extends PageParam {
     @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
     private Date[] createTime;
 
+
+    @ApiModelProperty(value = "用户编号", hidden = true)
+    private Long userId;
+
+    @ApiModelProperty(value = "用户类型", hidden = true)
+    private Integer userType;
 }

+ 13 - 1
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/notify/NotifyMessageMapper.java

@@ -1,12 +1,15 @@
 package cn.iocoder.yudao.module.system.dal.mysql.notify;
 
 import cn.iocoder.yudao.framework.common.pojo.PageResult;
-import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
 import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
+import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
 import cn.iocoder.yudao.module.system.controller.admin.notify.vo.message.NotifyMessagePageReqVO;
 import cn.iocoder.yudao.module.system.dal.dataobject.notify.NotifyMessageDO;
+import cn.iocoder.yudao.module.system.enums.notify.NotifyReadStatusEnum;
 import org.apache.ibatis.annotations.Mapper;
 
+import java.util.List;
+
 /**
  * 站内信 Mapper
  *
@@ -20,6 +23,15 @@ public interface NotifyMessageMapper extends BaseMapperX<NotifyMessageDO> {
                 .likeIfPresent(NotifyMessageDO::getTitle, reqVO.getTitle())
                 .eqIfPresent(NotifyMessageDO::getReadStatus, reqVO.getReadStatus())
                 .betweenIfPresent(NotifyMessageDO::getCreateTime, reqVO.getCreateTime())
+                .eqIfPresent(NotifyMessageDO::getUserId, reqVO.getUserId())
+                .eqIfPresent(NotifyMessageDO::getUserType, reqVO.getUserType())
                 .orderByDesc(NotifyMessageDO::getId));
     }
+
+    default Long selectUnreadCountByUserIdAndUserType(Long userId, Integer userType) {
+        return selectCount(new LambdaQueryWrapperX<NotifyMessageDO>()
+                .eq(NotifyMessageDO::getReadStatus, NotifyReadStatusEnum.UNREAD.getStatus())
+                .eq(NotifyMessageDO::getUserId, userId)
+                .eq(NotifyMessageDO::getUserType, userType));
+    }
 }

+ 13 - 4
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/notify/NotifyMessageService.java

@@ -1,13 +1,14 @@
 package cn.iocoder.yudao.module.system.service.notify;
 
-import java.util.*;
-import javax.validation.*;
-
+import cn.iocoder.yudao.framework.common.pojo.PageResult;
 import cn.iocoder.yudao.module.system.controller.admin.notify.vo.message.NotifyMessageCreateReqVO;
 import cn.iocoder.yudao.module.system.controller.admin.notify.vo.message.NotifyMessagePageReqVO;
 import cn.iocoder.yudao.module.system.controller.admin.notify.vo.message.NotifyMessageUpdateReqVO;
 import cn.iocoder.yudao.module.system.dal.dataobject.notify.NotifyMessageDO;
-import cn.iocoder.yudao.framework.common.pojo.PageResult;
+
+import javax.validation.Valid;
+import java.util.Collection;
+import java.util.List;
 
 /**
  * 站内信 Service 接口
@@ -62,4 +63,12 @@ public interface NotifyMessageService {
      */
     PageResult<NotifyMessageDO> getNotifyMessagePage(NotifyMessagePageReqVO pageReqVO);
 
+    /**
+     * 统计用户未读站内信条数
+     *
+     * @param userId 用户ID
+     * @param userType 用户类型
+     * @return 返回未读站内信条数
+     */
+    Long getUnreadNotifyMessageCount(Long userId, Integer userType);
 }

+ 51 - 1
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/notify/NotifyMessageServiceImpl.java

@@ -1,21 +1,26 @@
 package cn.iocoder.yudao.module.system.service.notify;
 
+import cn.iocoder.yudao.framework.common.core.KeyValue;
 import cn.iocoder.yudao.framework.common.pojo.PageResult;
 import cn.iocoder.yudao.module.system.controller.admin.notify.vo.message.NotifyMessageCreateReqVO;
 import cn.iocoder.yudao.module.system.controller.admin.notify.vo.message.NotifyMessagePageReqVO;
 import cn.iocoder.yudao.module.system.controller.admin.notify.vo.message.NotifyMessageUpdateReqVO;
 import cn.iocoder.yudao.module.system.convert.notify.NotifyMessageConvert;
 import cn.iocoder.yudao.module.system.dal.dataobject.notify.NotifyMessageDO;
+import cn.iocoder.yudao.module.system.dal.dataobject.notify.NotifyTemplateDO;
 import cn.iocoder.yudao.module.system.dal.mysql.notify.NotifyMessageMapper;
+import com.google.common.annotations.VisibleForTesting;
 import org.springframework.stereotype.Service;
 import org.springframework.validation.annotation.Validated;
 
 import javax.annotation.Resource;
 import java.util.Collection;
 import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
 
 import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
-import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.NOTIFY_MESSAGE_NOT_EXISTS;
+import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;
 
 /**
  * 站内信 Service 实现类
@@ -29,6 +34,39 @@ public class NotifyMessageServiceImpl implements NotifyMessageService {
     @Resource
     private NotifyMessageMapper notifyMessageMapper;
 
+    @Resource
+    private NotifyTemplateService notifyTemplateService;
+
+
+    @VisibleForTesting
+    public NotifyTemplateDO checkNotifyTemplateValid(String templateCode) {
+        // 获得站内信模板。考虑到效率,从缓存中获取
+        NotifyTemplateDO template = notifyTemplateService.getNotifyTemplateByCodeFromCache(templateCode);
+        // 站内信模板不存在
+        if (template == null) {
+            throw exception(NOTIFY_TEMPLATE_NOT_EXISTS);
+        }
+        return template;
+    }
+
+    /**
+     * 将参数模板,处理成有序的 KeyValue 数组
+     *
+     * @param template       站内信模板
+     * @param templateParams 原始参数
+     * @return 处理后的参数
+     */
+    @VisibleForTesting
+    public List<KeyValue<String, Object>> buildTemplateParams(NotifyTemplateDO template, Map<String, Object> templateParams) {
+        return template.getParams().stream().map(key -> {
+            Object value = templateParams.get(key);
+            if (value == null) {
+                throw exception(NOTIFY_TEMPLATE_PARAM_MISS, key);
+            }
+            return new KeyValue<>(key, value);
+        }).collect(Collectors.toList());
+    }
+
     @Override
     public Long createNotifyMessage(NotifyMessageCreateReqVO createReqVO) {
         // 插入
@@ -75,4 +113,16 @@ public class NotifyMessageServiceImpl implements NotifyMessageService {
     public PageResult<NotifyMessageDO> getNotifyMessagePage(NotifyMessagePageReqVO pageReqVO) {
         return notifyMessageMapper.selectPage(pageReqVO);
     }
+
+    /**
+     * 统计用户未读站内信条数
+     *
+     * @param userId   用户ID
+     * @param userType 用户类型
+     * @return 返回未读站内信条数
+     */
+    @Override
+    public Long getUnreadNotifyMessageCount(Long userId, Integer userType) {
+        return notifyMessageMapper.selectUnreadCountByUserIdAndUserType(userId, userType);
+    }
 }