liujie
2026-09-11 26dae811b447b0f13500a63e3a972a377f00be3c
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package com.tourguide.modules.system.service;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tourguide.common.exception.BusinessException;
import com.tourguide.modules.order.entity.TourOrder;
import com.tourguide.modules.system.dto.GuideNotificationResponse;
import com.tourguide.modules.system.entity.GuideNoticeRead;
import com.tourguide.modules.system.entity.GuideNotification;
import com.tourguide.modules.system.entity.SystemNotice;
import com.tourguide.modules.system.mapper.GuideNotificationMapper;
import com.tourguide.modules.system.mapper.GuideNoticeReadMapper;
import com.tourguide.modules.system.mapper.SystemNoticeMapper;
import com.tourguide.modules.user.service.GuidePushDeviceService;
import org.springframework.stereotype.Service;
 
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.Comparator;
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;
    private final GuideNotificationMapper guideNotificationMapper;
    private final GuidePushDeviceService pushDeviceService;
 
    public GuideNotificationService(SystemNoticeMapper noticeMapper, GuideNoticeReadMapper readMapper,
                                    GuideNotificationMapper guideNotificationMapper,
                                    GuidePushDeviceService pushDeviceService) {
        this.noticeMapper = noticeMapper;
        this.readMapper = readMapper;
        this.guideNotificationMapper = guideNotificationMapper;
        this.pushDeviceService = pushDeviceService;
    }
 
    public long unreadCount(Long guideId) {
        List<SystemNotice> notices = publishedNotices();
        Set<Long> readIds = readNoticeIds(guideId, notices);
        long systemUnread = notices.stream().filter(notice -> !readIds.contains(notice.getId())).count();
        Long personalUnread = guideNotificationMapper.selectCount(new LambdaQueryWrapper<GuideNotification>()
                .eq(GuideNotification::getGuideId, guideId)
                .isNull(GuideNotification::getReadAt));
        return systemUnread + personalUnread;
    }
 
    public List<GuideNotificationResponse> list(Long guideId) {
        List<SystemNotice> notices = publishedNotices();
        Set<Long> readIds = readNoticeIds(guideId, notices);
        List<GuideNotificationResponse> responses = notices.stream()
                .map(notice -> new GuideNotificationResponse(notice, readIds.contains(notice.getId())))
                .collect(Collectors.toList());
        responses.addAll(guideNotificationMapper.selectList(new LambdaQueryWrapper<GuideNotification>()
                        .eq(GuideNotification::getGuideId, guideId)
                        .orderByDesc(GuideNotification::getCreatedAt))
                .stream().map(GuideNotificationResponse::new).collect(Collectors.toList()));
        responses.sort(Comparator.comparing(GuideNotificationResponse::getTime,
                Comparator.nullsLast(Comparator.reverseOrder())));
        return responses;
    }
 
    public void markRead(Long guideId, String notificationId) {
        if (notificationId != null && notificationId.startsWith("guide:")) {
            markPersonalRead(guideId, parseId(notificationId, "guide:"));
            return;
        }
        Long noticeId = parseId(notificationId, "system:");
        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);
    }
 
    public void notifyOrderAssigned(TourOrder order) {
        if (order == null || order.getGuideId() == null) return;
        String title = "接单提醒";
        String content = "您有一个新的讲解订单,请查看";
        String targetUrl = orderTargetUrl(order.getId());
        create(order.getGuideId(), "ORDER_ASSIGNED", title, content, order.getId(), targetUrl);
        pushDeviceService.push(order.getGuideId(), title, content, targetUrl);
    }
 
    public void notifyVisitorCancelledOrder(TourOrder order) {
        if (order == null || order.getGuideId() == null) return;
        String title = "取消提醒";
        String content = "您有一个讲解订单被取消,请查看";
        String targetUrl = orderTargetUrl(order.getId());
        create(order.getGuideId(), "ORDER_CANCELLED", title, content, order.getId(), targetUrl);
        pushDeviceService.push(order.getGuideId(), title, content, targetUrl);
    }
 
    public void notifyOrderCancelled(TourOrder order) {
        if (order == null || order.getGuideId() == null) return;
        String title = "取消提醒";
        String content = "您有一个讲解订单被取消,请查看";
        String targetUrl = orderTargetUrl(order.getId());
        create(order.getGuideId(), "ORDER_CANCELLED", title, content, order.getId(), targetUrl);
        pushDeviceService.push(order.getGuideId(), title, content, targetUrl);
    }
 
    public void notifyOrderReviewed(TourOrder order) {
        if (order == null || order.getGuideId() == null) return;
        String title = "评价提醒";
        String content = "您有一条新的订单评价,请查看";
        String targetUrl = orderTargetUrl(order.getId());
        create(order.getGuideId(), "ORDER_REVIEWED", title, content, order.getId(), targetUrl);
        pushDeviceService.push(order.getGuideId(), title, content, targetUrl);
    }
 
    public boolean pushServiceReminder(TourOrder order) {
        if (order == null || order.getGuideId() == null) return false;
        String title = "请准备前往汇合点";
        String content = "您的讲解服务即将开始,请提前到达 "
                + valueOrUnknown(order.getMeetingPointName()) + "。";
        return pushDeviceService.push(order.getGuideId(), title, content, orderTargetUrl(order.getId()));
    }
 
    public void notifyAuditResult(Long guideId, boolean approved) {
        create(guideId, approved ? "AUDIT_APPROVED" : "AUDIT_REJECTED", "系统提醒",
                approved ? "您的资料审核已通过" : "您的资料审核未通过",
                null, null);
    }
 
    private void create(Long guideId, String type, String title, String content, Long orderId, String targetUrl) {
        if (guideId == null) return;
        GuideNotification notification = new GuideNotification();
        notification.setGuideId(guideId);
        notification.setNotificationType(type);
        notification.setTitle(title);
        notification.setContent(content);
        notification.setOrderId(orderId);
        notification.setTargetUrl(targetUrl);
        notification.setCreatedAt(LocalDateTime.now());
        guideNotificationMapper.insert(notification);
    }
 
    private String orderTargetUrl(Long orderId) {
        return orderId == null ? null : "/pages/order-detail/index?id=" + orderId;
    }
 
    private String valueOrUnknown(String primary, String fallback) {
        if (primary != null && !primary.trim().isEmpty()) return primary;
        return valueOrUnknown(fallback);
    }
 
    private String valueOrUnknown(String value) {
        return value == null || value.trim().isEmpty() ? "未知" : value;
    }
 
    private void markPersonalRead(Long guideId, Long notificationId) {
        GuideNotification notification = guideNotificationMapper.selectOne(new LambdaQueryWrapper<GuideNotification>()
                .eq(GuideNotification::getId, notificationId)
                .eq(GuideNotification::getGuideId, guideId)
                .last("LIMIT 1"));
        if (notification == null) throw new BusinessException(404, "Notification not found");
        if (notification.getReadAt() != null) return;
        notification.setReadAt(LocalDateTime.now());
        guideNotificationMapper.updateById(notification);
    }
 
    private Long parseId(String value, String prefix) {
        String raw = value == null ? "" : value;
        if (raw.startsWith(prefix)) raw = raw.substring(prefix.length());
        try {
            return Long.valueOf(raw);
        } catch (NumberFormatException exception) {
            throw new BusinessException(400, "Notification id is invalid");
        }
    }
 
    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());
    }
}