liujie
2026-07-29 c560a88ab429cd164d8eb6fcec42a273b898e230
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
package com.tourguide.modules.system.service;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tourguide.common.exception.BusinessException;
import com.tourguide.modules.system.dto.GuideNotificationResponse;
import com.tourguide.modules.system.entity.GuideNoticeRead;
import com.tourguide.modules.system.entity.SystemNotice;
import com.tourguide.modules.system.mapper.GuideNoticeReadMapper;
import com.tourguide.modules.system.mapper.SystemNoticeMapper;
import org.springframework.stereotype.Service;
 
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
 
@Service
public class GuideNotificationService {
    private final SystemNoticeMapper noticeMapper;
    private final GuideNoticeReadMapper readMapper;
 
    public GuideNotificationService(SystemNoticeMapper noticeMapper, GuideNoticeReadMapper readMapper) {
        this.noticeMapper = noticeMapper;
        this.readMapper = readMapper;
    }
 
    public long unreadCount(Long guideId) {
        List<SystemNotice> notices = publishedNotices();
        if (notices.isEmpty()) return 0L;
        Set<Long> readIds = readNoticeIds(guideId, notices);
        return notices.stream().filter(notice -> !readIds.contains(notice.getId())).count();
    }
 
    public List<GuideNotificationResponse> list(Long guideId) {
        List<SystemNotice> notices = publishedNotices();
        Set<Long> readIds = readNoticeIds(guideId, notices);
        return notices.stream()
                .map(notice -> new GuideNotificationResponse(notice, readIds.contains(notice.getId())))
                .collect(Collectors.toList());
    }
 
    public void markRead(Long guideId, Long noticeId) {
        SystemNotice notice = noticeMapper.selectOne(new LambdaQueryWrapper<SystemNotice>()
                .eq(SystemNotice::getId, noticeId)
                .eq(SystemNotice::getStatus, 1)
                .last("LIMIT 1"));
        if (notice == null) throw new BusinessException(404, "Notification not found");
        Long count = readMapper.selectCount(new LambdaQueryWrapper<GuideNoticeRead>()
                .eq(GuideNoticeRead::getGuideId, guideId)
                .eq(GuideNoticeRead::getNoticeId, noticeId));
        if (count > 0) return;
        GuideNoticeRead read = new GuideNoticeRead();
        read.setGuideId(guideId);
        read.setNoticeId(noticeId);
        read.setReadAt(LocalDateTime.now());
        readMapper.insert(read);
    }
 
    private List<SystemNotice> publishedNotices() {
        return noticeMapper.selectList(new LambdaQueryWrapper<SystemNotice>()
                .eq(SystemNotice::getStatus, 1)
                .isNotNull(SystemNotice::getPublishTime)
                .le(SystemNotice::getPublishTime, LocalDateTime.now())
                .orderByDesc(SystemNotice::getPublishTime));
    }
 
    private Set<Long> readNoticeIds(Long guideId, List<SystemNotice> notices) {
        if (notices.isEmpty()) return Collections.emptySet();
        List<Long> noticeIds = notices.stream().map(SystemNotice::getId).collect(Collectors.toList());
        return readMapper.selectList(new LambdaQueryWrapper<GuideNoticeRead>()
                        .eq(GuideNoticeRead::getGuideId, guideId)
                        .in(GuideNoticeRead::getNoticeId, noticeIds))
                .stream().map(GuideNoticeRead::getNoticeId).collect(Collectors.toSet());
    }
}