diff --git a/cf-framework/cf-spring-boot-starter-web/src/main/java/com/cf/imes/framework/web/core/handler/GlobalExceptionHandler.java b/cf-framework/cf-spring-boot-starter-web/src/main/java/com/cf/imes/framework/web/core/handler/GlobalExceptionHandler.java index 1a5d710b8..9b9c2f7dd 100644 --- a/cf-framework/cf-spring-boot-starter-web/src/main/java/com/cf/imes/framework/web/core/handler/GlobalExceptionHandler.java +++ b/cf-framework/cf-spring-boot-starter-web/src/main/java/com/cf/imes/framework/web/core/handler/GlobalExceptionHandler.java @@ -134,7 +134,7 @@ public class GlobalExceptionHandler { log.warn("[handleBindException]", ex); FieldError fieldError = ex.getFieldError(); assert fieldError != null; // 断言,避免告警 - return CommonResult.error(BAD_REQUEST.getCode(), String.format("请求参数不正确:%s", fieldError.getDefaultMessage())); + return CommonResult.error(BAD_REQUEST.getCode(), String.format("请求参数不正确:%s", fieldError.getRejectedValue())); } /** diff --git a/cf-module-system/cf-module-system-biz/src/main/java/com/cf/imes/module/system/controller/admin/notify/NotifyMessageController.java b/cf-module-system/cf-module-system-biz/src/main/java/com/cf/imes/module/system/controller/admin/notify/NotifyMessageController.java index 3a6fcb0a2..c8354558d 100644 --- a/cf-module-system/cf-module-system-biz/src/main/java/com/cf/imes/module/system/controller/admin/notify/NotifyMessageController.java +++ b/cf-module-system/cf-module-system-biz/src/main/java/com/cf/imes/module/system/controller/admin/notify/NotifyMessageController.java @@ -4,7 +4,6 @@ import com.cf.imes.framework.common.enums.UserTypeEnum; import com.cf.imes.framework.common.pojo.CommonResult; import com.cf.imes.framework.common.pojo.PageResult; import com.cf.imes.framework.common.util.object.BeanUtils; -import com.cf.imes.module.system.controller.admin.notify.vo.message.NotifyMessageMyPageReqVO; import com.cf.imes.module.system.controller.admin.notify.vo.message.NotifyMessagePageReqVO; import com.cf.imes.module.system.controller.admin.notify.vo.message.NotifyMessageRespVO; import com.cf.imes.module.system.dal.dataobject.notify.NotifyMessageDO; @@ -19,10 +18,6 @@ import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import javax.validation.Valid; import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.Set; -import java.util.stream.Collectors; import static com.cf.imes.framework.common.pojo.CommonResult.success; import static com.cf.imes.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId; @@ -51,7 +46,7 @@ public class NotifyMessageController { @Operation(summary = "获得站内信分页") @PreAuthorize("@ss.hasPermission('system:notify-message:query')") public CommonResult> getNotifyMessagePage(@Valid NotifyMessagePageReqVO pageVO) { - PageResult pageResult = notifyMessageService.getNotifyMessagePage(pageVO); + PageResult pageResult = notifyMessageService.getMyMyNotifyMessagePage(pageVO); return success(BeanUtils.toBean(pageResult, NotifyMessageRespVO.class)); } @@ -59,18 +54,11 @@ public class NotifyMessageController { @GetMapping("/my-page") @Operation(summary = "获得我的站内信分页") - public CommonResult> getMyMyNotifyMessagePage(@Valid NotifyMessageMyPageReqVO pageVO) { - Long loginUserId = getLoginUserId(); - if(!Objects.isNull(pageVO.getReadStatus())) { - return success(notifyMessageService.getPageResultByRead(pageVO)); - } - PageResult pageResult = notifyMessageService.getMyMyNotifyMessagePage(pageVO, - loginUserId, UserTypeEnum.ADMIN.getValue()); + public CommonResult> getMyMyNotifyMessagePage(@Valid NotifyMessagePageReqVO pageVO) { + pageVO.setUserId(getLoginUserId()); + pageVO.setUserType(UserTypeEnum.ADMIN.getValue()); + PageResult pageResult = notifyMessageService.getMyMyNotifyMessagePage(pageVO); PageResult respVOPageResult = BeanUtils.toBean(pageResult, NotifyMessageRespVO.class); - Set messageRead = notifyMessageService.getMessageRead(pageResult.getList().stream().map(e -> String.valueOf(e.getId())).collect(Collectors.toSet()), loginUserId); - respVOPageResult.getList().forEach(e->{ - e.setReadStatus(messageRead.contains(e.getId().toString())); - }); return success(respVOPageResult); } @@ -89,19 +77,9 @@ public class NotifyMessageController { return success(Boolean.TRUE); } - @GetMapping("/get-unread-list") - @Operation(summary = "获取当前用户的最新站内信列表,默认 10 条") - @Parameter(name = "size", description = "10") - public CommonResult> getUnreadNotifyMessageList( - @RequestParam(name = "size", defaultValue = "10") Integer size) { - List list = notifyMessageService.getUnreadNotifyMessageList( - getLoginUserId(), UserTypeEnum.ADMIN.getValue(), size); - return success(BeanUtils.toBean(list, NotifyMessageRespVO.class)); - } - @GetMapping("/get-unread-count") @Operation(summary = "获得当前用户的未读站内信数量") - public CommonResult getUnreadNotifyMessageCount() { + public CommonResult getUnreadNotifyMessageCount() { return success(notifyMessageService.getUnreadNotifyMessageCount( getLoginUserId(), UserTypeEnum.ADMIN.getValue())); } diff --git a/cf-module-system/cf-module-system-biz/src/main/java/com/cf/imes/module/system/controller/admin/notify/vo/message/NotifyMessageMyPageReqVO.java b/cf-module-system/cf-module-system-biz/src/main/java/com/cf/imes/module/system/controller/admin/notify/vo/message/NotifyMessageMyPageReqVO.java deleted file mode 100644 index 79b854e63..000000000 --- a/cf-module-system/cf-module-system-biz/src/main/java/com/cf/imes/module/system/controller/admin/notify/vo/message/NotifyMessageMyPageReqVO.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.cf.imes.module.system.controller.admin.notify.vo.message; - -import com.cf.imes.framework.common.pojo.PageParam; -import io.swagger.v3.oas.annotations.media.Schema; -import lombok.Data; -import lombok.EqualsAndHashCode; -import lombok.ToString; -import org.springframework.format.annotation.DateTimeFormat; - -import java.time.LocalDateTime; - -import static com.cf.imes.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND; - -@Schema(description = "管理后台 - 站内信分页 Request VO") -@Data -@EqualsAndHashCode(callSuper = true) -@ToString(callSuper = true) -public class NotifyMessageMyPageReqVO extends PageParam { - - @Schema(description = "是否已读", example = "true") - private Boolean readStatus; - - @Schema(description = "创建时间") - @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND) - private LocalDateTime[] createTime; - -} diff --git a/cf-module-system/cf-module-system-biz/src/main/java/com/cf/imes/module/system/controller/admin/notify/vo/message/NotifyMessagePageReqVO.java b/cf-module-system/cf-module-system-biz/src/main/java/com/cf/imes/module/system/controller/admin/notify/vo/message/NotifyMessagePageReqVO.java index c10c68d54..e100fd55e 100644 --- a/cf-module-system/cf-module-system-biz/src/main/java/com/cf/imes/module/system/controller/admin/notify/vo/message/NotifyMessagePageReqVO.java +++ b/cf-module-system/cf-module-system-biz/src/main/java/com/cf/imes/module/system/controller/admin/notify/vo/message/NotifyMessagePageReqVO.java @@ -33,4 +33,6 @@ public class NotifyMessagePageReqVO extends PageParam { @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND) private LocalDateTime[] createTime; + @Schema(description = "是否已读", example = "true") + private Boolean readStatus; } diff --git a/cf-module-system/cf-module-system-biz/src/main/java/com/cf/imes/module/system/dal/dataobject/notify/NotifyMessageDO.java b/cf-module-system/cf-module-system-biz/src/main/java/com/cf/imes/module/system/dal/dataobject/notify/NotifyMessageDO.java index 7476e0ba5..3b3484890 100644 --- a/cf-module-system/cf-module-system-biz/src/main/java/com/cf/imes/module/system/dal/dataobject/notify/NotifyMessageDO.java +++ b/cf-module-system/cf-module-system-biz/src/main/java/com/cf/imes/module/system/dal/dataobject/notify/NotifyMessageDO.java @@ -8,6 +8,7 @@ import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler; import lombok.*; +import org.springframework.data.annotation.Transient; import java.time.LocalDateTime; import java.util.Map; @@ -88,12 +89,14 @@ public class NotifyMessageDO extends BaseDO { // ========= 读取相关字段 ========= /** - * 是否已读 现在这个状态存在redis中,所以先注释此字段 + * 是否已读 */ - /*private Boolean readStatus;*/ + @Transient + private Boolean readStatus; /** * 阅读时间 */ + @Transient private LocalDateTime readTime; } diff --git a/cf-module-system/cf-module-system-biz/src/main/java/com/cf/imes/module/system/dal/dataobject/notify/NotifyMessageReadInfoDO.java b/cf-module-system/cf-module-system-biz/src/main/java/com/cf/imes/module/system/dal/dataobject/notify/NotifyMessageReadInfoDO.java new file mode 100644 index 000000000..47d4b0099 --- /dev/null +++ b/cf-module-system/cf-module-system-biz/src/main/java/com/cf/imes/module/system/dal/dataobject/notify/NotifyMessageReadInfoDO.java @@ -0,0 +1,51 @@ +package com.cf.imes.module.system.dal.dataobject.notify; + +import com.baomidou.mybatisplus.annotation.KeySequence; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; +import lombok.ToString; + +import java.time.LocalDateTime; + +/** + * @author Gqr + * @since 2024/7/15 14:46 + */ +@TableName(value = "system_notify_message_readinfo") +@KeySequence("system_notify_message_readinfo_seq") // 用于 Oracle、PostgreSQL、Kingbase、DB2、H2 数据库的主键自增。如果是 MySQL 等数据库,可不写。 +@Data +@ToString(callSuper = true) +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class NotifyMessageReadInfoDO { + /** + * 主键,自增 + */ + @TableId + private Long id; + + /** + * 组织id + */ + private Long organId; + + /** + * 站内信消息id + */ + private Long messageId; + + /** + * 阅读时间 + */ + private LocalDateTime readTime; + + /** + * 用户id + */ + private Long userId; +} diff --git a/cf-module-system/cf-module-system-biz/src/main/java/com/cf/imes/module/system/dal/mysql/notify/NotifyMessageMapper.java b/cf-module-system/cf-module-system-biz/src/main/java/com/cf/imes/module/system/dal/mysql/notify/NotifyMessageMapper.java index 7a894c32f..fa0dc7591 100644 --- a/cf-module-system/cf-module-system-biz/src/main/java/com/cf/imes/module/system/dal/mysql/notify/NotifyMessageMapper.java +++ b/cf-module-system/cf-module-system-biz/src/main/java/com/cf/imes/module/system/dal/mysql/notify/NotifyMessageMapper.java @@ -1,71 +1,40 @@ package com.cf.imes.module.system.dal.mysql.notify; -import com.cf.imes.framework.common.pojo.PageResult; +import com.baomidou.mybatisplus.core.metadata.IPage; import com.cf.imes.framework.mybatis.core.mapper.BaseMapperX; -import com.cf.imes.framework.mybatis.core.query.LambdaQueryWrapperX; -import com.cf.imes.framework.mybatis.core.query.QueryWrapperX; -import com.cf.imes.module.system.controller.admin.notify.vo.message.NotifyMessageMyPageReqVO; import com.cf.imes.module.system.controller.admin.notify.vo.message.NotifyMessagePageReqVO; import com.cf.imes.module.system.dal.dataobject.notify.NotifyMessageDO; import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Param; -import java.time.LocalDateTime; -import java.util.Arrays; -import java.util.Collection; import java.util.List; @Mapper public interface NotifyMessageMapper extends BaseMapperX { + /** + * 分页查询我的站内信 + * + * @param page 分页信息 + * @param pageReqVO 请求参数 + * @return + */ + IPage getMyMyNotifyMessage(@Param("page") IPage page, @Param("reqVo") NotifyMessagePageReqVO pageReqVO); - default PageResult selectPage(NotifyMessagePageReqVO reqVO) { - return selectPage(reqVO, new LambdaQueryWrapperX() - .eqIfPresent(NotifyMessageDO::getUserId, reqVO.getUserId()) - .eqIfPresent(NotifyMessageDO::getUserType, reqVO.getUserType()) - .likeIfPresent(NotifyMessageDO::getTemplateCode, reqVO.getTemplateCode()) - .eqIfPresent(NotifyMessageDO::getTemplateType, reqVO.getTemplateType()) - .betweenIfPresent(NotifyMessageDO::getCreateTime, reqVO.getCreateTime()) - .orderByDesc(NotifyMessageDO::getId)); - } - - default PageResult selectPage(NotifyMessageMyPageReqVO reqVO, Long userId, Integer userType) { - return selectPage(reqVO, new LambdaQueryWrapperX() - //.eqIfPresent(NotifyMessageDO::getReadStatus, reqVO.getReadStatus()) - .betweenIfPresent(NotifyMessageDO::getCreateTime, reqVO.getCreateTime()) - .in(NotifyMessageDO::getUserId, Arrays.asList(userId, 0L)) - .eq(NotifyMessageDO::getUserType, userType) - .orderByDesc(NotifyMessageDO::getId)); - } - - default int updateListRead(Collection ids, Long userId, Integer userType) { - return update(new NotifyMessageDO().setReadTime(LocalDateTime.now()), - new LambdaQueryWrapperX() - .in(NotifyMessageDO::getId, ids) - .eq(NotifyMessageDO::getUserId, userId) - .eq(NotifyMessageDO::getUserType, userType)); - //.eq(NotifyMessageDO::getReadStatus, false)); - } - - default int updateListRead(Long userId, Integer userType) { - return update(new NotifyMessageDO().setReadTime(LocalDateTime.now()), - new LambdaQueryWrapperX() - .eq(NotifyMessageDO::getUserId, userId) - .eq(NotifyMessageDO::getUserType, userType)); - //.eq(NotifyMessageDO::getReadStatus, false)); - } - - default List selectUnreadListByUserIdAndUserType(Long userId, Integer userType, Integer size) { - return selectList(new QueryWrapperX() // 由于要使用 limitN 语句,所以只能用 QueryWrapperX - .in("user_id", Arrays.asList(userId, 0L)) - .eq("user_type", userType) - .eq("read_status", false) - .orderByDesc("id").limitN(size)); - } - - default Long selectUnreadCountByUserIdAndUserType(Long userId, Integer userType) { - return selectCount(new LambdaQueryWrapperX() - //.eq(NotifyMessageDO::getReadStatus, false) - .in(NotifyMessageDO::getUserId, Arrays.asList(userId, 0L)) - .eq(NotifyMessageDO::getUserType, userType)); - } + /** + * 统计未读消息数量 + * + * @param userId 用户id + * @param userType 接受用户类型 + * @return + */ + Integer getUnreadNotifyMessageCount(@Param("userId") Long userId, @Param("userType") Integer userType); + /** + * 获取未读消息id列表 + * + * @param userId + * @param userType + * @return + */ + List getUnreadNotifyMessageIds(@Param("userId") Long userId, @Param("userType") Integer userType); } diff --git a/cf-module-system/cf-module-system-biz/src/main/java/com/cf/imes/module/system/dal/mysql/notify/NotifyMessageReadInfoMapper.java b/cf-module-system/cf-module-system-biz/src/main/java/com/cf/imes/module/system/dal/mysql/notify/NotifyMessageReadInfoMapper.java new file mode 100644 index 000000000..1489d15bf --- /dev/null +++ b/cf-module-system/cf-module-system-biz/src/main/java/com/cf/imes/module/system/dal/mysql/notify/NotifyMessageReadInfoMapper.java @@ -0,0 +1,9 @@ +package com.cf.imes.module.system.dal.mysql.notify; + +import com.cf.imes.framework.mybatis.core.mapper.BaseMapperX; +import com.cf.imes.module.system.dal.dataobject.notify.NotifyMessageReadInfoDO; +import org.apache.ibatis.annotations.Mapper; + +@Mapper +public interface NotifyMessageReadInfoMapper extends BaseMapperX { +} diff --git a/cf-module-system/cf-module-system-biz/src/main/java/com/cf/imes/module/system/service/notify/NotifyMessageReadInfoService.java b/cf-module-system/cf-module-system-biz/src/main/java/com/cf/imes/module/system/service/notify/NotifyMessageReadInfoService.java new file mode 100644 index 000000000..ba236a2b3 --- /dev/null +++ b/cf-module-system/cf-module-system-biz/src/main/java/com/cf/imes/module/system/service/notify/NotifyMessageReadInfoService.java @@ -0,0 +1,20 @@ +package com.cf.imes.module.system.service.notify; + +import java.time.LocalDateTime; + +/** + * 站内信阅读信息 Service 接口 + * + * @author Gqr + * @since 2024/7/15 15:28 + */ +public interface NotifyMessageReadInfoService { + /** + * 获取任一站内信阅读信息 + * + * @param messageId 消息id + * @param userId 用户id + * @return + */ + LocalDateTime getAny(Long messageId, Long userId); +} diff --git a/cf-module-system/cf-module-system-biz/src/main/java/com/cf/imes/module/system/service/notify/NotifyMessageReadInfoServiceImpl.java b/cf-module-system/cf-module-system-biz/src/main/java/com/cf/imes/module/system/service/notify/NotifyMessageReadInfoServiceImpl.java new file mode 100644 index 000000000..fc4313768 --- /dev/null +++ b/cf-module-system/cf-module-system-biz/src/main/java/com/cf/imes/module/system/service/notify/NotifyMessageReadInfoServiceImpl.java @@ -0,0 +1,31 @@ +package com.cf.imes.module.system.service.notify; + +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.cf.imes.module.system.dal.dataobject.notify.NotifyMessageReadInfoDO; +import com.cf.imes.module.system.dal.mysql.notify.NotifyMessageReadInfoMapper; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import java.time.LocalDateTime; + +/** + * @author Gqr + * @since 2024/7/15 15:29 + */ +@Service +@Slf4j +public class NotifyMessageReadInfoServiceImpl implements NotifyMessageReadInfoService { + + @Resource + private NotifyMessageReadInfoMapper notifyMessageReadInfoMapper; + + @Override + public LocalDateTime getAny(Long messageId, Long userId) { + NotifyMessageReadInfoDO one = notifyMessageReadInfoMapper.selectOne(new LambdaQueryWrapper() + .eq(NotifyMessageReadInfoDO::getMessageId, messageId) + .eq(NotifyMessageReadInfoDO::getUserId, userId) + .select(NotifyMessageReadInfoDO::getReadTime)); + return one != null ? one.getReadTime() : null; + } +} diff --git a/cf-module-system/cf-module-system-biz/src/main/java/com/cf/imes/module/system/service/notify/NotifyMessageService.java b/cf-module-system/cf-module-system-biz/src/main/java/com/cf/imes/module/system/service/notify/NotifyMessageService.java index 1dfe7a98a..21666a8c1 100644 --- a/cf-module-system/cf-module-system-biz/src/main/java/com/cf/imes/module/system/service/notify/NotifyMessageService.java +++ b/cf-module-system/cf-module-system-biz/src/main/java/com/cf/imes/module/system/service/notify/NotifyMessageService.java @@ -1,16 +1,12 @@ package com.cf.imes.module.system.service.notify; import com.cf.imes.framework.common.pojo.PageResult; -import com.cf.imes.module.system.controller.admin.notify.vo.message.NotifyMessageMyPageReqVO; import com.cf.imes.module.system.controller.admin.notify.vo.message.NotifyMessagePageReqVO; -import com.cf.imes.module.system.controller.admin.notify.vo.message.NotifyMessageRespVO; import com.cf.imes.module.system.dal.dataobject.notify.NotifyMessageDO; import com.cf.imes.module.system.dal.dataobject.notify.NotifyTemplateDO; import java.util.Collection; -import java.util.List; import java.util.Map; -import java.util.Set; /** * 站内信 Service 接口 @@ -19,8 +15,6 @@ import java.util.Set; */ public interface NotifyMessageService { - String NOTIFY_MESSAGE_KEY = "notify-message:"; - /** * 创建站内信 * @@ -34,23 +28,13 @@ public interface NotifyMessageService { Long createNotifyMessage(Long userId, Integer userType, NotifyTemplateDO template, String templateContent, Map templateParams); - /** - * 获得站内信分页 - * - * @param pageReqVO 分页查询 - * @return 站内信分页 - */ - PageResult getNotifyMessagePage(NotifyMessagePageReqVO pageReqVO); - /** * 获得【我的】站内信分页 * * @param pageReqVO 分页查询 - * @param userId 用户编号 - * @param userType 用户类型 * @return 站内信分页 */ - PageResult getMyMyNotifyMessagePage(NotifyMessageMyPageReqVO pageReqVO, Long userId, Integer userType); + PageResult getMyMyNotifyMessagePage(NotifyMessagePageReqVO pageReqVO); /** * 获得站内信 @@ -60,16 +44,6 @@ public interface NotifyMessageService { */ NotifyMessageDO getNotifyMessage(Long id); - /** - * 获得【我的】未读站内信列表 - * - * @param userId 用户编号 - * @param userType 用户类型 - * @param size 数量 - * @return 站内信列表 - */ - List getUnreadNotifyMessageList(Long userId, Integer userType, Integer size); - /** * 统计用户未读站内信条数 * @@ -77,7 +51,7 @@ public interface NotifyMessageService { * @param userType 用户类型 * @return 返回未读站内信条数 */ - Long getUnreadNotifyMessageCount(Long userId, Integer userType); + Integer getUnreadNotifyMessageCount(Long userId, Integer userType); /** * 标记站内信为已读 @@ -97,19 +71,4 @@ public interface NotifyMessageService { * @return 更新到的条数 */ int updateAllNotifyMessageRead(Long userId, Integer userType); - - /** - * 获取用户的站内信以读 - * @param messageIds - * @param userId - * @return - */ - Set getMessageRead(Collection messageIds, Long userId); - - /** - * 查询已读或未读站内信分页 - * @param pageVO - * @return - */ - PageResult getPageResultByRead(NotifyMessageMyPageReqVO pageVO); } diff --git a/cf-module-system/cf-module-system-biz/src/main/java/com/cf/imes/module/system/service/notify/NotifyMessageServiceImpl.java b/cf-module-system/cf-module-system-biz/src/main/java/com/cf/imes/module/system/service/notify/NotifyMessageServiceImpl.java index ab444cda8..73d014fc4 100644 --- a/cf-module-system/cf-module-system-biz/src/main/java/com/cf/imes/module/system/service/notify/NotifyMessageServiceImpl.java +++ b/cf-module-system/cf-module-system-biz/src/main/java/com/cf/imes/module/system/service/notify/NotifyMessageServiceImpl.java @@ -1,27 +1,21 @@ package com.cf.imes.module.system.service.notify; import cn.hutool.core.collection.CollUtil; +import com.baomidou.mybatisplus.core.metadata.IPage; +import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO; import com.cf.imes.framework.common.pojo.PageResult; -import com.cf.imes.framework.common.util.object.BeanUtils; -import com.cf.imes.framework.mybatis.core.query.LambdaQueryWrapperX; -import com.cf.imes.module.system.controller.admin.notify.vo.message.NotifyMessageMyPageReqVO; import com.cf.imes.module.system.controller.admin.notify.vo.message.NotifyMessagePageReqVO; -import com.cf.imes.module.system.controller.admin.notify.vo.message.NotifyMessageRespVO; import com.cf.imes.module.system.dal.dataobject.notify.NotifyMessageDO; +import com.cf.imes.module.system.dal.dataobject.notify.NotifyMessageReadInfoDO; import com.cf.imes.module.system.dal.dataobject.notify.NotifyTemplateDO; import com.cf.imes.module.system.dal.mysql.notify.NotifyMessageMapper; -import org.springframework.data.redis.core.RedisCallback; -import org.springframework.data.redis.core.StringRedisTemplate; +import com.cf.imes.module.system.dal.mysql.notify.NotifyMessageReadInfoMapper; +import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.validation.annotation.Validated; import javax.annotation.Resource; -import java.nio.charset.StandardCharsets; import java.util.*; -import java.util.concurrent.atomic.AtomicReference; -import java.util.stream.Collectors; - -import static com.cf.imes.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId; /** * 站内信 Service 实现类 @@ -30,13 +24,14 @@ import static com.cf.imes.framework.security.core.util.SecurityFrameworkUtils.ge */ @Service @Validated +@Slf4j public class NotifyMessageServiceImpl implements NotifyMessageService { @Resource private NotifyMessageMapper notifyMessageMapper; @Resource - private StringRedisTemplate stringRedisTemplate; + private NotifyMessageReadInfoMapper notifyMessageReadInfoMapper; @Override public Long createNotifyMessage(Long userId, Integer userType, @@ -50,13 +45,10 @@ public class NotifyMessageServiceImpl implements NotifyMessageService { } @Override - public PageResult getNotifyMessagePage(NotifyMessagePageReqVO pageReqVO) { - return notifyMessageMapper.selectPage(pageReqVO); - } - - @Override - public PageResult getMyMyNotifyMessagePage(NotifyMessageMyPageReqVO pageReqVO, Long userId, Integer userType) { - return notifyMessageMapper.selectPage(pageReqVO, userId, userType); + public PageResult getMyMyNotifyMessagePage(NotifyMessagePageReqVO pageReqVO) { + PageDTO page = new PageDTO(pageReqVO.getPageNo(), pageReqVO.getPageSize()); + IPage myMyNotifyMessage = notifyMessageMapper.getMyMyNotifyMessage(page, pageReqVO); + return new PageResult<>(myMyNotifyMessage.getRecords(), myMyNotifyMessage.getTotal()); } @Override @@ -65,104 +57,32 @@ public class NotifyMessageServiceImpl implements NotifyMessageService { } @Override - public List getUnreadNotifyMessageList(Long userId, Integer userType, Integer size) { - return notifyMessageMapper.selectUnreadListByUserIdAndUserType(userId, userType, size); - } - - @Override - public Long getUnreadNotifyMessageCount(Long userId, Integer userType) { - List notifyMessageDOS = notifyMessageMapper.selectList(new LambdaQueryWrapperX() - .eq(NotifyMessageDO::getUserType, userType) - .in(NotifyMessageDO::getUserId, Arrays.asList(userId, 0L)) - .select(NotifyMessageDO::getId) - ); - if(CollUtil.isEmpty(notifyMessageDOS)) { - return 0L; - } - Set messageIds = notifyMessageDOS.stream().map(e->e.getId().toString()).collect(Collectors.toSet()); - Set set = bitGet(messageIds, userId); - messageIds.removeAll(set); - return (long) messageIds.size(); - //return notifyMessageMapper.selectUnreadCountByUserIdAndUserType(userId, userType); + public Integer getUnreadNotifyMessageCount(Long userId, Integer userType) { + return notifyMessageMapper.getUnreadNotifyMessageCount(userId, userType); } @Override public int updateNotifyMessageRead(Collection ids, Long userId, Integer userType) { - bitset(ids.stream().map(Object::toString).collect(Collectors.toSet()), userId); + List batchInsertReadInfoList = ids.stream().map(messageId -> NotifyMessageReadInfoDO.builder().messageId(messageId).userId(userId).build()).toList(); + Boolean insertBatch = notifyMessageReadInfoMapper.insertBatch(batchInsertReadInfoList); + if (!insertBatch) { + log.error("[updateNotifyMessageRead][新增站内信消息阅读信息失败]"); + return 0; + } return 1; - //return notifyMessageMapper.updateListRead(ids, userId, userType); } @Override public int updateAllNotifyMessageRead(Long userId, Integer userType) { - List notifyMessageDOS = notifyMessageMapper.selectList(new LambdaQueryWrapperX() - .in(NotifyMessageDO::getUserId, Arrays.asList(userId, 0)) - .eq(NotifyMessageDO::getUserType, 2) - .select(NotifyMessageDO::getId) - ); - if (CollUtil.isNotEmpty(notifyMessageDOS)) { - Set messageIds = notifyMessageDOS.stream().map(e -> String.valueOf(e.getId())).collect(Collectors.toSet()); - bitset(messageIds, userId); + List messageIds = notifyMessageMapper.getUnreadNotifyMessageIds(userId, userType); + if (CollUtil.isNotEmpty(messageIds)) { + List batchInsertReadInfoList = messageIds.stream().map(messageId -> NotifyMessageReadInfoDO.builder().messageId(messageId).userId(userId).build()).toList(); + Boolean insertBatch = notifyMessageReadInfoMapper.insertBatch(batchInsertReadInfoList); + if (!insertBatch) { + log.error("[updateAllNotifyMessageRead][新增站内信消息阅读信息失败]"); + return 0; + } } return 1; - //return notifyMessageMapper.updateListRead(userId, userType); } - - @Override - public Set getMessageRead(Collection messageIds, Long userId) { - return bitGet(messageIds,userId); - } - - @Override - public PageResult getPageResultByRead(NotifyMessageMyPageReqVO pageVO) { - Boolean readStatus = pageVO.getReadStatus(); - Long userId = getLoginUserId(); - List notifyMessageDOS = notifyMessageMapper.selectList(new LambdaQueryWrapperX() - .eq(NotifyMessageDO::getUserType, 2) - .in(NotifyMessageDO::getUserId, Arrays.asList(userId, 0L)) - .betweenIfPresent(NotifyMessageDO::getCreateTime, pageVO.getCreateTime()) - ); - Set messageIds = notifyMessageDOS.stream().map(e -> e.getId().toString()).collect(Collectors.toSet()); - Set set = bitGet(messageIds, userId); - List respVOS = notifyMessageDOS.stream() - .filter(f -> readStatus ? set.contains(f.getId().toString()): !set.contains(f.getId().toString())) - .skip((long) (pageVO.getPageNo() - 1) * pageVO.getPageSize()) - .limit(pageVO.getPageSize()) - .map(e -> { - NotifyMessageRespVO respVO = BeanUtils.toBean(e, NotifyMessageRespVO.class); - respVO.setReadStatus(readStatus); - return respVO; - }) - .toList(); - - return new PageResult<>(respVOS, (long)respVOS.size()); - } - - private Boolean bitset(Collection keys, Long userId) { - return stringRedisTemplate.execute((RedisCallback) connection -> { - AtomicReference flag = new AtomicReference<>(false); - keys.forEach(key -> flag.set(connection.setBit(joinKey(key).getBytes(StandardCharsets.UTF_8), userId, true))); - return flag.get(); - } - ); - } - - private Set bitGet(Collection keys, Long userId) { - return stringRedisTemplate.execute((RedisCallback>)connection -> { - Set set = new HashSet<>(); - keys.forEach(key->{ - Boolean bit = connection.getBit(joinKey(key).getBytes(StandardCharsets.UTF_8), userId); - if(bit) { - set.add(key); - } - }); - return set; - }); - } - - private String joinKey(String key) { - return NOTIFY_MESSAGE_KEY + key; - } - - } diff --git a/cf-module-system/cf-module-system-biz/src/main/resources/mapper/message/NotifyMessageMapper.xml b/cf-module-system/cf-module-system-biz/src/main/resources/mapper/message/NotifyMessageMapper.xml new file mode 100644 index 000000000..4cd8097cc --- /dev/null +++ b/cf-module-system/cf-module-system-biz/src/main/resources/mapper/message/NotifyMessageMapper.xml @@ -0,0 +1,55 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/cf-module-system/cf-module-system-biz/src/test/java/com/cf/imes/module/system/service/notify/NotifyMessageServiceImplTest.java b/cf-module-system/cf-module-system-biz/src/test/java/com/cf/imes/module/system/service/notify/NotifyMessageServiceImplTest.java index 54d76143c..e1bbab365 100644 --- a/cf-module-system/cf-module-system-biz/src/test/java/com/cf/imes/module/system/service/notify/NotifyMessageServiceImplTest.java +++ b/cf-module-system/cf-module-system-biz/src/test/java/com/cf/imes/module/system/service/notify/NotifyMessageServiceImplTest.java @@ -5,7 +5,6 @@ import com.cf.imes.framework.common.enums.UserTypeEnum; import com.cf.imes.framework.common.pojo.PageResult; import com.cf.imes.framework.mybatis.core.enums.SqlConstants; import com.cf.imes.framework.test.core.ut.BaseDbUnitTest; -import com.cf.imes.module.system.controller.admin.notify.vo.message.NotifyMessageMyPageReqVO; import com.cf.imes.module.system.controller.admin.notify.vo.message.NotifyMessagePageReqVO; import com.cf.imes.module.system.dal.dataobject.notify.NotifyMessageDO; import com.cf.imes.module.system.dal.dataobject.notify.NotifyTemplateDO; @@ -17,7 +16,6 @@ import org.springframework.context.annotation.Import; import javax.annotation.Resource; import java.util.Arrays; import java.util.Collection; -import java.util.List; import java.util.Map; import static cn.hutool.core.util.RandomUtil.randomEle; @@ -101,7 +99,7 @@ public class NotifyMessageServiceImplTest extends BaseDbUnitTest { reqVO.setCreateTime(buildBetweenTime(2022, 1, 1, 2022, 1, 10)); // 调用 - PageResult pageResult = notifyMessageService.getNotifyMessagePage(reqVO); + PageResult pageResult = notifyMessageService.getMyMyNotifyMessagePage(reqVO); // 断言 assertEquals(1, pageResult.getTotal()); assertEquals(1, pageResult.getList().size()); @@ -144,12 +142,14 @@ public class NotifyMessageServiceImplTest extends BaseDbUnitTest { // 准备参数 Long userId = 1L; Integer userType = UserTypeEnum.ADMIN.getValue(); - NotifyMessageMyPageReqVO reqVO = new NotifyMessageMyPageReqVO(); + NotifyMessagePageReqVO reqVO = new NotifyMessagePageReqVO(); reqVO.setReadStatus(true); reqVO.setCreateTime(buildBetweenTime(2022, 1, 1, 2022, 1, 10)); + reqVO.setUserId(userId); + reqVO.setUserType(userType); // 调用 - PageResult pageResult = notifyMessageService.getMyMyNotifyMessagePage(reqVO, userId, userType); + PageResult pageResult = notifyMessageService.getMyMyNotifyMessagePage(reqVO); // 断言 assertEquals(1, pageResult.getTotal()); assertEquals(1, pageResult.getList().size()); @@ -163,7 +163,6 @@ public class NotifyMessageServiceImplTest extends BaseDbUnitTest { NotifyMessageDO dbNotifyMessage = randomPojo(NotifyMessageDO.class, o -> { // 等会查询到 o.setUserId(1L); o.setUserType(UserTypeEnum.ADMIN.getValue()); - //o.setReadStatus(false); o.setTemplateParams(randomTemplateParams()); }); notifyMessageMapper.insert(dbNotifyMessage); @@ -176,13 +175,17 @@ public class NotifyMessageServiceImplTest extends BaseDbUnitTest { // 准备参数 Long userId = 1L; Integer userType = UserTypeEnum.ADMIN.getValue(); - Integer size = 10; - + NotifyMessagePageReqVO reqVO = new NotifyMessagePageReqVO(); + reqVO.setReadStatus(false); + reqVO.setPageSize(10); + reqVO.setPageNo(1); + reqVO.setUserId(userId); + reqVO.setUserType(userType); // 调用 - List list = notifyMessageService.getUnreadNotifyMessageList(userId, userType, size); + PageResult pageResult = notifyMessageService.getMyMyNotifyMessagePage(reqVO); // 断言 - assertEquals(1, list.size()); - assertPojoEquals(dbNotifyMessage, list.get(0)); + assertEquals(1, pageResult.getList().size()); + assertPojoEquals(dbNotifyMessage, pageResult.getList().get(0)); } @Test