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;
|
}
|
}
|