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
| @Autowired private SysNoticeUserService sysNoticeUserService;
@Autowired private SysUserService sysUserService;
@Override public ResponseResult<PageVo> getNoticeList(Integer pageNum, Integer pageSize, NoticeListDto noticeListDto) { Long userId = SecurityUtils.getUserId(); boolean isAdmin = SecurityUtils.isAdmin();
LambdaQueryWrapper<SysNotice> queryWrapper = new LambdaQueryWrapper<>();
if (noticeListDto != null && StringUtils.hasText(noticeListDto.getTitle())) { queryWrapper.like(SysNotice::getTitle, noticeListDto.getTitle()); }
if (isAdmin) { if (noticeListDto != null && StringUtils.hasText(noticeListDto.getStatus())) { queryWrapper.eq(SysNotice::getStatus, noticeListDto.getStatus()); } } else { queryWrapper.eq(SysNotice::getStatus, SystemConstants.NOTICE_STATUS_PUBLISHED); }
if (!isAdmin) { queryWrapper.and(wrapper -> wrapper.eq(SysNotice::getTargetType, SystemConstants.TARGET_TYPE_IS_ALL) .or() .apply("concat(',', target_user_ids, ',') like {0}", "%," + userId + ",%")); }
queryWrapper.orderByAsc(SysNotice::getId);
Page<SysNotice> page = new Page<>(pageNum, pageSize); this.page(page, queryWrapper);
List<SysNotice> noticeList = page.getRecords();
List<NoticeListVo> noticeListVos = BeanCopyUtils.copyBeanList(noticeList, NoticeListVo.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, SysUser::getNickname, (oldVal, newVal) -> oldVal)); }
for (int i = 0; i < noticeList.size(); i++) { SysNotice notice = noticeList.get(i); NoticeListVo noticeListVo = noticeListVos.get(i); noticeListVo.setPublisherName(publisherNameMap.get(notice.getPublisherId())); }
for (NoticeListVo 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 (NoticeListVo noticeListVo : noticeListVos) { noticeListVo.setIsRead(noticeReadMap.getOrDefault(noticeListVo.getId(), SystemConstants.NOTICE_NOT_READ)); } }
PageVo pageVo = new PageVo(noticeListVos, page.getTotal());
return ResponseResult.okResult(pageVo); }
|