“mitao”
2026-04-30 aeb9818e02480af6055479aa798457b8ddfec7a4
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
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.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 MANUAL_NOTICE_TEST_SCENE = "MANUAL_NOTICE_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) {
        return pushNoticeType(request,
                AppSystemNoticeType.MEMBER_EXPIRE_REMIND,
                "会员到期提醒",
                "您的会员有效期将于测试日期到期",
                false);
    }
 
    /**
     * 生成直播提醒测试通知并尝试极光推送。
     */
    public JpushManualPushVO pushLiveRemind(JpushManualPushRequest request) {
        return pushNoticeType(request,
                AppSystemNoticeType.LIVE_REMIND,
                "直播提醒",
                "测试直播课程 即将开播",
                true);
    }
 
    /**
     * 生成活动通知测试通知并尝试极光推送。
     */
    public JpushManualPushVO pushActivityNotice(JpushManualPushRequest request) {
        return pushNoticeType(request,
                AppSystemNoticeType.ACTIVITY_NOTICE,
                "活动通知",
                "这是一条活动通知联调测试消息",
                true);
    }
 
    /**
     * 生成订单状态更新测试通知并尝试极光推送。
     */
    public JpushManualPushVO pushOrderStatusUpdate(JpushManualPushRequest request) {
        return pushNoticeType(request,
                AppSystemNoticeType.ORDER_STATUS_UPDATE,
                "订单状态更新",
                "您的礼品兑换订单状态已更新",
                true);
    }
 
    /**
     * 生成课程更新测试通知并尝试极光推送。
     */
    public JpushManualPushVO pushCourseUpdateNotice(JpushManualPushRequest request) {
        return pushNoticeType(request,
                AppSystemNoticeType.COURSE_UPDATE_NOTICE,
                "课程更新通知",
                "测试课程 已更新新章节",
                true);
    }
 
    /**
     * 按指定通知类型生成系统通知,并复用现有通知推送开关和极光推送链路。
     */
    private JpushManualPushVO pushNoticeType(JpushManualPushRequest request,
                                             String noticeTypeCode,
                                             String title,
                                             String content,
                                             boolean requireUserSetting) {
        Long referenceId = System.currentTimeMillis();
        AppSystemNoticeDispatchCommand command = new AppSystemNoticeDispatchCommand();
        command.setAppUserId(request.getAppUserId());
        command.setNoticeTypeCode(noticeTypeCode);
        command.setTitle(title);
        command.setContent(content);
        command.setReferenceType("MANUAL_TEST_" + noticeTypeCode);
        command.setReferenceId(referenceId);
        command.setRequireUserSetting(requireUserSetting);
        command.setPayload(buildManualNoticePayload(request.getAppUserId(), noticeTypeCode, referenceId));
        AppSystemNotice notice = appSystemNoticeDispatchService.createAndPush(command);
        return toNoticePushVO(notice);
    }
 
    /**
     * 构造手动通知测试透传参数,便于APP端区分联调消息。
     */
    private Map<String, String> buildManualNoticePayload(Long appUserId, String noticeTypeCode, Long referenceId) {
        Map<String, String> payload = new LinkedHashMap<String, String>();
        payload.put("scene", MANUAL_NOTICE_TEST_SCENE);
        payload.put("noticeTypeCode", noticeTypeCode);
        payload.put("appUserId", String.valueOf(appUserId));
        payload.put("referenceId", String.valueOf(referenceId));
        return payload;
    }
 
    /**
     * 转换系统通知推送结果为联调接口响应。
     */
    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;
    }
}