“mitao”
2026-04-30 0c98579fff979ae80fc30224b87c9c6eb626f49e
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package com.tcm.training.admin.system.service;
 
import com.tcm.training.admin.app.notice.entity.AppSystemNotice;
import com.tcm.training.admin.app.notice.service.AppSystemNoticeDispatchCommand;
import com.tcm.training.admin.app.notice.service.AppSystemNoticeDispatchService;
import com.tcm.training.admin.app.notice.service.AppSystemNoticeType;
import com.tcm.training.admin.system.dto.JpushManualPushRequest;
import com.tcm.training.admin.system.vo.JpushManualPushVO;
import com.tcm.training.admin.utils.JGPushUtil;
import org.springframework.stereotype.Service;
 
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.LinkedHashMap;
import java.util.Map;
 
/**
 * 极光手动推送联调应用服务。
 */
@Service
public class JpushManualPushAppService {
 
    private static final String TEST_TITLE = "极光推送联调";
    private static final String TEST_CONTENT = "这是一条极光推送联调测试消息";
    private static final String TEST_SCENE = "JPUSH_MANUAL_TEST";
    private static final String PUSH_STATUS_SUCCESS = "SUCCESS";
 
    private final JGPushUtil jgPushUtil;
    private final AppSystemNoticeDispatchService appSystemNoticeDispatchService;
 
    public JpushManualPushAppService(JGPushUtil jgPushUtil,
                                     AppSystemNoticeDispatchService appSystemNoticeDispatchService) {
        this.jgPushUtil = jgPushUtil;
        this.appSystemNoticeDispatchService = appSystemNoticeDispatchService;
    }
 
    /**
     * 按APP用户ID发起固定内容的极光推送联调。
     */
    public JpushManualPushVO push(JpushManualPushRequest request) {
        String alias = String.valueOf(request.getAppUserId());
        Map<String, String> payload = new LinkedHashMap<String, String>();
        payload.put("scene", TEST_SCENE);
        payload.put("appUserId", alias);
 
        // 极光APP端alias约定为APP用户ID字符串,用于验证设备绑定与推送链路。
        jgPushUtil.push(TEST_CONTENT, TEST_TITLE, payload, alias);
 
        JpushManualPushVO result = new JpushManualPushVO();
        result.setAppUserId(request.getAppUserId());
        result.setAlias(alias);
        result.setTitle(TEST_TITLE);
        result.setContent(TEST_CONTENT);
        result.setPushed(true);
        return result;
    }
 
    /**
     * 生成会员到期提醒测试通知并尝试极光推送。
     */
    public JpushManualPushVO pushMemberExpireRemind(JpushManualPushRequest request) {
        LocalDate expireDate = LocalDate.now().plusDays(3L);
        Map<String, String> payload = new LinkedHashMap<String, String>();
        payload.put("expireAt", expireDate.atTime(23, 59, 59).toString());
        payload.put("remainingDays", "3");
        return pushNoticeType(request,
                AppSystemNoticeType.MEMBER_EXPIRE_REMIND,
                "会员到期提醒",
                "您的会员有效期将于" + expireDate + "到期",
                "MEMBER_EXPIRE_" + expireDate,
                request.getAppUserId(),
                false,
                payload);
    }
 
    /**
     * 生成直播提醒测试通知并尝试极光推送。
     */
    public JpushManualPushVO pushLiveRemind(JpushManualPushRequest request) {
        Long sessionId = System.currentTimeMillis();
        LocalDateTime startTime = LocalDateTime.now().plusHours(1L);
        Map<String, String> payload = new LinkedHashMap<String, String>();
        payload.put("courseId", "100001");
        payload.put("sessionId", String.valueOf(sessionId));
        payload.put("startTime", String.valueOf(startTime));
        payload.put("courseTitle", "测试直播课程");
        return pushNoticeType(request,
                AppSystemNoticeType.LIVE_REMIND,
                "直播提醒",
                "测试直播课程 即将开播",
                "LIVE_SESSION",
                sessionId,
                true,
                payload);
    }
 
    /**
     * 生成活动通知测试通知并尝试极光推送。
     */
    public JpushManualPushVO pushActivityNotice(JpushManualPushRequest request) {
        Long activityNoticeId = System.currentTimeMillis();
        Map<String, String> payload = new LinkedHashMap<String, String>();
        payload.put("activityNoticeId", String.valueOf(activityNoticeId));
        payload.put("title", "活动通知");
        return pushNoticeType(request,
                AppSystemNoticeType.ACTIVITY_NOTICE,
                "活动通知",
                "活动通知",
                "ACTIVITY_NOTICE",
                activityNoticeId,
                true,
                payload);
    }
 
    /**
     * 生成礼品订单创建测试通知并尝试极光推送。
     */
    public JpushManualPushVO pushOrderStatusUpdate(JpushManualPushRequest request) {
        Long orderId = System.currentTimeMillis();
        Map<String, String> payload = new LinkedHashMap<String, String>();
        payload.put("orderId", String.valueOf(orderId));
        payload.put("orderNo", "TEST" + orderId);
        payload.put("giftName", "测试礼品");
        payload.put("itemCount", "1");
        return pushNoticeType(request,
                AppSystemNoticeType.ORDER_STATUS_UPDATE,
                "订单状态更新",
                "您有新的礼品兑换订单待处理",
                "POINTS_EXCHANGE_ORDER",
                orderId,
                true,
                payload);
    }
 
    /**
     * 生成礼品发货测试通知并尝试极光推送。
     */
    public JpushManualPushVO pushOrderShipped(JpushManualPushRequest request) {
        Long orderId = System.currentTimeMillis();
        Map<String, String> payload = new LinkedHashMap<String, String>();
        payload.put("orderId", String.valueOf(orderId));
        payload.put("orderNo", "TEST" + orderId);
        payload.put("trackingNo", "SF" + orderId);
        return pushNoticeType(request,
                AppSystemNoticeType.ORDER_STATUS_UPDATE,
                "订单状态更新",
                "您的礼品兑换订单已发货",
                "POINTS_EXCHANGE_ORDER_SHIPPED",
                orderId,
                true,
                payload);
    }
 
    /**
     * 生成课程更新测试通知并尝试极光推送。
     */
    public JpushManualPushVO pushCourseUpdateNotice(JpushManualPushRequest request) {
        Long videoId = System.currentTimeMillis();
        Map<String, String> payload = new LinkedHashMap<String, String>();
        payload.put("courseId", "100001");
        payload.put("courseTitle", "测试课程");
        payload.put("videoId", String.valueOf(videoId));
        payload.put("chapterName", "测试章节");
        return pushNoticeType(request,
                AppSystemNoticeType.COURSE_UPDATE_NOTICE,
                "课程更新通知",
                "测试课程 已更新新章节",
                "HEALTH_COURSE_VIDEO",
                videoId,
                true,
                payload);
    }
 
    /**
     * 按指定通知类型生成系统通知,并复用现有通知推送开关和极光推送链路。
     */
    private JpushManualPushVO pushNoticeType(JpushManualPushRequest request,
                                             String noticeTypeCode,
                                             String title,
                                             String content,
                                             String referenceType,
                                             Long referenceId,
                                             boolean requireUserSetting,
                                             Map<String, String> payload) {
        AppSystemNoticeDispatchCommand command = new AppSystemNoticeDispatchCommand();
        command.setAppUserId(request.getAppUserId());
        command.setNoticeTypeCode(noticeTypeCode);
        command.setTitle(title);
        command.setContent(content);
        command.setReferenceType(referenceType);
        command.setReferenceId(referenceId);
        command.setRequireUserSetting(requireUserSetting);
        command.setPayload(payload);
        AppSystemNotice notice = appSystemNoticeDispatchService.createAndPush(command);
        return toNoticePushVO(notice);
    }
 
    /**
     * 转换系统通知推送结果为联调接口响应。
     */
    private JpushManualPushVO toNoticePushVO(AppSystemNotice notice) {
        JpushManualPushVO result = new JpushManualPushVO();
        result.setNoticeId(notice.getId());
        result.setAppUserId(notice.getAppUserId());
        result.setNoticeTypeCode(notice.getNoticeTypeCode());
        result.setAlias(String.valueOf(notice.getAppUserId()));
        result.setTitle(notice.getTitle());
        result.setContent(notice.getContent());
        result.setPushStatus(notice.getPushStatus());
        result.setPushErrorMessage(notice.getPushErrorMessage());
        result.setPushedAt(notice.getPushedAt());
        result.setPushed(PUSH_STATUS_SUCCESS.equals(notice.getPushStatus()));
        return result;
    }
}