|
@@ -2,18 +2,32 @@ package com.txz.mall.service.impl;
|
|
|
|
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
|
import cn.hutool.core.collection.CollectionUtil;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.txz.mall.configurer.AsyncConfig;
|
|
|
import com.txz.mall.dao.NoticeMapper;
|
|
|
import com.txz.mall.enums.NoticeEnum;
|
|
|
import com.txz.mall.model.Notice;
|
|
|
+import com.txz.mall.model.UserJPush;
|
|
|
import com.txz.mall.service.NoticeService;
|
|
|
+import com.txz.mall.service.UserJPushService;
|
|
|
+import com.txz.mall.util.NoticeI18nUtil;
|
|
|
import dto.NoticeDTO;
|
|
|
+import io.github.engagelab.api.PushApi;
|
|
|
+import io.github.engagelab.bean.push.PushParam;
|
|
|
+import io.github.engagelab.bean.push.PushResult;
|
|
|
+import io.github.engagelab.bean.push.message.notification.NotificationMessage;
|
|
|
+import io.github.engagelab.bean.push.to.To;
|
|
|
+import io.github.engagelab.enums.Platform;
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* @author: MTD®️
|
|
@@ -21,8 +35,15 @@ import java.util.List;
|
|
|
*/
|
|
|
@Slf4j
|
|
|
@Service
|
|
|
+@AllArgsConstructor
|
|
|
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService {
|
|
|
|
|
|
+ private final PushApi pushApi;
|
|
|
+
|
|
|
+ private final UserJPushService userJPushService;
|
|
|
+
|
|
|
+ private final AsyncConfig asyncConfig;
|
|
|
+
|
|
|
@Override
|
|
|
public void addNotice(NoticeDTO notice, Long... uids) {
|
|
|
if (CollectionUtil.isEmpty(Arrays.asList(uids))) {
|
|
@@ -30,6 +51,7 @@ public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> impleme
|
|
|
}
|
|
|
List<Notice> saveBatch = new ArrayList<>();
|
|
|
Notice info = BeanUtil.copyProperties(notice, Notice.class);
|
|
|
+
|
|
|
Arrays.stream(uids).forEach(uid -> {
|
|
|
info.setUid(uid);
|
|
|
info.setReadFlag(Boolean.FALSE);
|
|
@@ -37,6 +59,137 @@ public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> impleme
|
|
|
saveBatch.add(info);
|
|
|
});
|
|
|
this.saveBatch(saveBatch);
|
|
|
+
|
|
|
+ asyncConfig.taskExecutor().execute(() -> {
|
|
|
+ try {
|
|
|
+ // 极光推送
|
|
|
+ List<UserJPush> pushList = userJPushService.list(Wrappers.<UserJPush>lambdaQuery()
|
|
|
+ .in(UserJPush::getUserId, Arrays.asList(uids))
|
|
|
+ );
|
|
|
+
|
|
|
+ PushParam param = new PushParam();
|
|
|
+ PushParam.Body body = new PushParam.Body();
|
|
|
+
|
|
|
+ Map<String, List<UserJPush>> languages = pushList.stream().collect(Collectors.groupingBy(UserJPush::getUserLanguage));
|
|
|
+
|
|
|
+ // 单独处理孟加拉区域
|
|
|
+ List<UserJPush> bns = languages.get("bn");
|
|
|
+ if (CollectionUtil.isNotEmpty(bns)) {
|
|
|
+ // android 通知内容
|
|
|
+ NotificationMessage.Android android = new NotificationMessage.Android();
|
|
|
+ // ios 通知内容
|
|
|
+ NotificationMessage.IOS ios = new NotificationMessage.IOS();
|
|
|
+ switch (notice.getNoticeType()) {
|
|
|
+ default:
|
|
|
+ android.setTitle(NoticeI18nUtil.get("bn", notice.getNoticeType().getTitle()));
|
|
|
+ android.setAlert(String.format(NoticeI18nUtil.get("bn", notice.getNoticeType().getContent()), notice.getNoticeMessage()));
|
|
|
+ ios.setAlert(NoticeI18nUtil.get("bn", notice.getNoticeType().getTitle()) + "," + String.format(NoticeI18nUtil.get("bn", notice.getNoticeType().getContent()), notice.getNoticeMessage()));
|
|
|
+ break;
|
|
|
+ case OTHER:
|
|
|
+ android.setTitle(notice.getNoticeTitle());
|
|
|
+ android.setAlert(notice.getNoticeMessage());
|
|
|
+ ios.setAlert(notice.getNoticeTitle() + "," + notice.getNoticeMessage());
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ NotificationMessage notificationMessage = new NotificationMessage();
|
|
|
+ notificationMessage.setAlert(NoticeI18nUtil.get("bn", notice.getNoticeType().getTitle()));
|
|
|
+ notificationMessage.setAndroid(android);
|
|
|
+ notificationMessage.setIos(ios);
|
|
|
+ body.setNotification(notificationMessage);
|
|
|
+ // 目标人群
|
|
|
+ To to = new To();
|
|
|
+ to.setRegistrationIdList(bns.stream().map(UserJPush::getJpushDeviceId).collect(Collectors.toList()));
|
|
|
+
|
|
|
+ // 指定平台
|
|
|
+ body.setPlatform(Arrays.asList(Platform.android, Platform.ios));
|
|
|
+
|
|
|
+ // 发送
|
|
|
+ param.setBody(body);
|
|
|
+ PushResult result = pushApi.push(param);
|
|
|
+ log.info("孟加拉极光推送结果:{}", result);
|
|
|
+ }
|
|
|
+
|
|
|
+ List<UserJPush> ens = languages.get("en");
|
|
|
+ if (CollectionUtil.isNotEmpty(ens)) {
|
|
|
+ // android 通知内容
|
|
|
+ NotificationMessage.Android android = new NotificationMessage.Android();
|
|
|
+ // ios 通知内容
|
|
|
+ NotificationMessage.IOS ios = new NotificationMessage.IOS();
|
|
|
+ switch (notice.getNoticeType()) {
|
|
|
+ default:
|
|
|
+ android.setTitle(NoticeI18nUtil.get("en", notice.getNoticeType().getTitle()));
|
|
|
+ android.setAlert(String.format(NoticeI18nUtil.get("en", notice.getNoticeType().getContent()), notice.getNoticeMessage()));
|
|
|
+ ios.setAlert(NoticeI18nUtil.get("en", notice.getNoticeType().getTitle()) + "," + String.format(NoticeI18nUtil.get("en", notice.getNoticeType().getContent()), notice.getNoticeMessage()));
|
|
|
+ break;
|
|
|
+ case OTHER:
|
|
|
+ android.setTitle(notice.getNoticeTitle());
|
|
|
+ android.setAlert(notice.getNoticeMessage());
|
|
|
+ ios.setAlert(notice.getNoticeTitle() + "," + notice.getNoticeMessage());
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ NotificationMessage notificationMessage = new NotificationMessage();
|
|
|
+ notificationMessage.setAlert(NoticeI18nUtil.get("en", notice.getNoticeType().getTitle()));
|
|
|
+ notificationMessage.setAndroid(android);
|
|
|
+ notificationMessage.setIos(ios);
|
|
|
+ body.setNotification(notificationMessage);
|
|
|
+ // 目标人群
|
|
|
+ To to = new To();
|
|
|
+ to.setRegistrationIdList(ens.stream().map(UserJPush::getJpushDeviceId).collect(Collectors.toList()));
|
|
|
+ // 指定目标
|
|
|
+ param.setTo(to);
|
|
|
+
|
|
|
+ // 指定平台
|
|
|
+ body.setPlatform(Arrays.asList(Platform.android, Platform.ios));
|
|
|
+
|
|
|
+ // 发送
|
|
|
+ param.setBody(body);
|
|
|
+ PushResult result = pushApi.push(param);
|
|
|
+ log.info("英文极光推送结果:{}", result);
|
|
|
+ }
|
|
|
+
|
|
|
+ List<UserJPush> zhs = languages.get("zh");
|
|
|
+ if (CollectionUtil.isNotEmpty(zhs)) {
|
|
|
+ // android 通知内容
|
|
|
+ NotificationMessage.Android android = new NotificationMessage.Android();
|
|
|
+ // ios 通知内容
|
|
|
+ NotificationMessage.IOS ios = new NotificationMessage.IOS();
|
|
|
+ switch (notice.getNoticeType()) {
|
|
|
+ default:
|
|
|
+ android.setTitle(NoticeI18nUtil.get("zh", notice.getNoticeType().getTitle()));
|
|
|
+ android.setAlert(String.format(NoticeI18nUtil.get("zh", notice.getNoticeType().getContent()), notice.getNoticeMessage()));
|
|
|
+ ios.setAlert(NoticeI18nUtil.get("zh", notice.getNoticeType().getTitle()) + "," + String.format(NoticeI18nUtil.get("zh", notice.getNoticeType().getContent()), notice.getNoticeMessage()));
|
|
|
+ break;
|
|
|
+ case OTHER:
|
|
|
+ android.setTitle(notice.getNoticeTitle());
|
|
|
+ android.setAlert(notice.getNoticeMessage());
|
|
|
+ ios.setAlert(notice.getNoticeTitle() + "," + notice.getNoticeMessage());
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ NotificationMessage notificationMessage = new NotificationMessage();
|
|
|
+ notificationMessage.setAlert(NoticeI18nUtil.get("zh", notice.getNoticeType().getTitle()));
|
|
|
+ notificationMessage.setAndroid(android);
|
|
|
+ notificationMessage.setIos(ios);
|
|
|
+ body.setNotification(notificationMessage);
|
|
|
+ // 目标人群
|
|
|
+ To to = new To();
|
|
|
+ to.setRegistrationIdList(zhs.stream().map(UserJPush::getJpushDeviceId).collect(Collectors.toList()));
|
|
|
+ // 指定目标
|
|
|
+ param.setTo(to);
|
|
|
+
|
|
|
+ // 指定平台
|
|
|
+ body.setPlatform(Arrays.asList(Platform.android, Platform.ios));
|
|
|
+
|
|
|
+ // 发送
|
|
|
+ param.setBody(body);
|
|
|
+ PushResult result = pushApi.push(param);
|
|
|
+ log.info("中文极光推送结果:{}", result);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ log.error("极光推送失败:{}", e.getMessage());
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
}
|
|
|
|
|
|
@Override
|