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