1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
| @Autowired private SysNoticeUserService sysNoticeUserService;
@Autowired private SysUserService sysUserService;
@Override public ResponseResult<PageVo> getNoticeList(Integer pageNum, Integer pageSize) { Long userId = SecurityUtils.getUserId();
LambdaQueryWrapper<SysNotice> queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(SysNotice::getStatus, SystemConstants.NOTICE_STATUS_PUBLISHED) .eq(SysNotice::getDelFlag, SystemConstants.STATUS_NORMAL); if (userId == null) { queryWrapper.eq(SysNotice::getTargetType, SystemConstants.TARGET_TYPE_IS_ALL); } else { queryWrapper.and(wrapper -> wrapper .eq(SysNotice::getTargetType, SystemConstants.TARGET_TYPE_IS_ALL) .or(orWrapper -> orWrapper .eq(SysNotice::getTargetType, SystemConstants.TARGET_TYPE_IS_SPECIFIED) .apply("find_in_set({0}, target_user_ids)", userId))); }
queryWrapper.orderByDesc(SysNotice::getPublishTime);
Page<SysNotice> page = new Page<>(pageNum, pageSize); this.page(page, queryWrapper);
List<SysNotice> noticeList = page.getRecords();
List<BlogNoticeListVo> noticeListVos = BeanCopyUtils.copyBeanList(noticeList, BlogNoticeListVo.class);
List<Long> publisherIds = noticeList.stream() .map(SysNotice::getPublisherId) .filter(Objects::nonNull) .distinct() .collect(Collectors.toList()); Map<Long, String> publisherNameMap = new HashMap<>(); if (!publisherIds.isEmpty()) { List<SysUser> publisherList = sysUserService.listByIds(publisherIds); publisherNameMap = publisherList.stream() .collect(Collectors.toMap(SysUser::getId, user -> StringUtils.hasText(user.getNickname()) ? user.getNickname() : user.getUsername(), (oldVal, newVal) -> oldVal)); }
Map<Long, String> targetUserNameMap = new HashMap<>(); List<Long> targetUserIds = noticeList.stream() .filter(notice -> SystemConstants.TARGET_TYPE_IS_SPECIFIED.equals(notice.getTargetType())) .map(SysNotice::getTargetUserIds) .filter(StringUtils::hasText) .flatMap(targetUserIdsStr -> Arrays.stream(targetUserIdsStr.split(","))) .map(String::trim) .filter(StringUtils::hasText) .map(Long::valueOf) .distinct() .collect(Collectors.toList()); if (!targetUserIds.isEmpty()) { List<SysUser> targetUserList = sysUserService.listByIds(targetUserIds); targetUserNameMap = targetUserList.stream() .collect(Collectors.toMap(SysUser::getId, user -> StringUtils.hasText(user.getNickname()) ? user.getNickname() : user.getUsername(), (oldVal, newVal) -> oldVal)); }
for (int i = 0; i < noticeList.size(); i++) { SysNotice notice = noticeList.get(i); BlogNoticeListVo noticeListVo = noticeListVos.get(i); noticeListVo.setPublisherName(publisherNameMap.get(notice.getPublisherId())); noticeListVo.setTargetUserIds(notice.getTargetUserIds()); if (SystemConstants.TARGET_TYPE_IS_SPECIFIED.equals(notice.getTargetType()) && StringUtils.hasText(notice.getTargetUserIds())) { List<String> targetNames = Arrays.stream(notice.getTargetUserIds().split(",")) .map(String::trim) .filter(StringUtils::hasText) .map(Long::valueOf) .map(targetUserNameMap::get) .filter(StringUtils::hasText) .collect(Collectors.toList()); noticeListVo.setTargetName(targetNames); } }
for (BlogNoticeListVo noticeListVo : noticeListVos) { noticeListVo.setIsRead(SystemConstants.NOTICE_NOT_READ); }
if (userId != null && !noticeList.isEmpty()) { List<Long> noticeIds = noticeList.stream().map(SysNotice::getId).collect(Collectors.toList()); List<SysNoticeUser> noticeUserList = sysNoticeUserService.list(new LambdaQueryWrapper<SysNoticeUser>() .eq(SysNoticeUser::getUserId, userId) .in(SysNoticeUser::getNoticeId, noticeIds));
Map<Long, String> noticeReadMap = noticeUserList.stream() .collect(Collectors.toMap(SysNoticeUser::getNoticeId, SysNoticeUser::getIsRead, (oldVal, newVal) -> oldVal));
for (BlogNoticeListVo noticeListVo : noticeListVos) { noticeListVo.setIsRead(noticeReadMap.getOrDefault(noticeListVo.getId(), SystemConstants.NOTICE_NOT_READ)); } noticeListVos.sort(Comparator.comparing((BlogNoticeListVo vo) -> SystemConstants.NOTICE_IS_READ.equals(vo.getIsRead())) .thenComparing(BlogNoticeListVo::getPublishTime, Comparator.nullsLast(Comparator.reverseOrder()))); }
PageVo pageVo = new PageVo(noticeListVos, page.getTotal()); return ResponseResult.okResult(pageVo); }
|