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
| @Override public ResponseResult<PageVo> getMyNoticeList(Integer pageNum, Integer pageSize, NoticeListDto noticeListDto) { Long userId = SecurityUtils.getUserId(); if (userId == null) { return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN); }
LambdaQueryWrapper<SysNotice> queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.eq(SysNotice::getStatus, SystemConstants.NOTICE_STATUS_PUBLISHED) .eq(SysNotice::getDelFlag, SystemConstants.STATUS_NORMAL) .and(wrapper -> wrapper.eq(SysNotice::getTargetType, SystemConstants.TARGET_TYPE_IS_ALL) .or() .apply("find_in_set({0}, target_user_ids)", userId));
if (noticeListDto != null && StringUtils.hasText(noticeListDto.getTitle())) { queryWrapper.like(SysNotice::getTitle, noticeListDto.getTitle()); }
queryWrapper.orderByDesc(SysNotice::getPublishTime);
Page<SysNotice> page = new Page<>(pageNum, pageSize); this.page(page, queryWrapper);
List<SysNotice> noticeList = page.getRecords(); List<MyNoticeListVo> myNoticeListVos = BeanCopyUtils.copyBeanList(noticeList, MyNoticeListVo.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)); }
for (int i = 0; i < noticeList.size(); i++) { SysNotice notice = noticeList.get(i); MyNoticeListVo myNoticeListVo = myNoticeListVos.get(i); myNoticeListVo.setPublisherName(publisherNameMap.get(notice.getPublisherId())); }
for (MyNoticeListVo myNoticeListVo : myNoticeListVos) { myNoticeListVo.setIsRead(SystemConstants.NOTICE_NOT_READ); }
if (!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 (MyNoticeListVo myNoticeListVo : myNoticeListVos) { myNoticeListVo.setIsRead(noticeReadMap.getOrDefault(myNoticeListVo.getId(), SystemConstants.NOTICE_NOT_READ)); } }
myNoticeListVos.sort(Comparator.comparing((MyNoticeListVo vo) -> SystemConstants.NOTICE_IS_READ.equals(vo.getIsRead())) .thenComparing(MyNoticeListVo::getPublishTime, Comparator.nullsLast(Comparator.reverseOrder())));
PageVo pageVo = new PageVo(myNoticeListVos, page.getTotal()); return ResponseResult.okResult(pageVo); }
|