optimize codes
This commit is contained in:
@@ -1,8 +1,14 @@
|
||||
package com.mgm.mcrm.notification.services;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.mgm.mcrm.authorization.models.SysRouterPermission;
|
||||
import com.mgm.mcrm.authorization.services.SysGroupPermissionService;
|
||||
import com.mgm.mcrm.basesetting.daylight.dtos.response.ProfileDetailResponse;
|
||||
import com.mgm.mcrm.basesetting.property.models.CfgProperties;
|
||||
import com.mgm.mcrm.common.dtos.BaseApiResponse;
|
||||
import com.mgm.mcrm.common.dtos.HttpResult;
|
||||
import com.mgm.mcrm.common.httpclient.HttpHeaderBuilder;
|
||||
import com.mgm.mcrm.common.services.HttpService;
|
||||
import com.mgm.mcrm.common.utils.LogUtil;
|
||||
import com.mgm.mcrm.logging.models.SysLogDeviceToken;
|
||||
import com.mgm.mcrm.logging.repositories.SysLogDeviceTokenDao;
|
||||
@@ -13,28 +19,24 @@ import com.mgm.mcrm.notification.dtos.response.GetNotificationResponse;
|
||||
import com.mgm.mcrm.notification.models.SysLogNotification;
|
||||
import com.mgm.mcrm.notification.repositories.SysLogNotificationRepository;
|
||||
import jakarta.annotation.Resource;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.hc.core5.http.Header;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
import java.util.*;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
public class NotificationService {
|
||||
@Resource
|
||||
private SysLogNotificationRepository sysLogNotificationRepository;
|
||||
@Resource
|
||||
private ApnNotificationService apnNotificationService;
|
||||
@Resource
|
||||
private SysLogDeviceTokenDao sysLogDeviceTokenDao;
|
||||
// @Resource
|
||||
// private ApprovalRoutingDao approvalRoutingDao;
|
||||
@Resource
|
||||
private SysGroupPermissionService sysGroupPermissionService;
|
||||
|
||||
@Resource
|
||||
private HttpService httpService;
|
||||
|
||||
public GetNotificationResponse getNotificationByUserId(String userId){
|
||||
GetNotificationResponse response = new GetNotificationResponse();
|
||||
@@ -58,10 +60,6 @@ public class NotificationService {
|
||||
}
|
||||
}
|
||||
|
||||
// if (viewApprovalRouting) {
|
||||
// response.setApprovalRoutingCount( approvalRoutingDao.countApprovalList(userId));
|
||||
// count+=approvalRoutingDao.countApprovalList(userId);
|
||||
// }
|
||||
if (viewNotification) {
|
||||
response.setNotificationCount(sysLogNotificationRepository.countByUserIdAndStatus(userId, 0));
|
||||
count+=sysLogNotificationRepository.countByUserIdAndStatus(userId, 0);
|
||||
@@ -98,104 +96,23 @@ public class NotificationService {
|
||||
}
|
||||
|
||||
|
||||
public BaseApiResponse<String> sendNotificationByRequest(SendNotificationRequest requestData, String senderId) {
|
||||
BaseApiResponse<String> result = new BaseApiResponse<>();
|
||||
String[] userIds = requestData.getEmployeeId().split("[;,]");
|
||||
String transactionId = requestData.getTransactionId();
|
||||
if(StringUtils.isBlank(transactionId)){
|
||||
transactionId = UUID.randomUUID().toString();
|
||||
}
|
||||
result.setData(transactionId);
|
||||
if(userIds.length>0){
|
||||
int successCount =0;
|
||||
for(String userId:userIds){
|
||||
if(StringUtils.isNotBlank(userId)){
|
||||
userId = userId.trim();
|
||||
SysLogDeviceToken deviceToken = sysLogDeviceTokenDao.findByUserId(userId);
|
||||
String deviceTokenValue =null;
|
||||
if(deviceToken!=null){
|
||||
deviceTokenValue = deviceToken.getDeviceToken();
|
||||
}
|
||||
public BaseApiResponse<String> sendNotificationByRequest(HttpServletRequest request, SendNotificationRequest requestData) {
|
||||
BaseApiResponse<String> response = new BaseApiResponse<>();
|
||||
|
||||
Integer useLanguage = deviceToken == null ? null : deviceToken.getUseLanguageType();
|
||||
String notificationTitle = resolveNotificationText(requestData.getTitle(), useLanguage);
|
||||
String notificationContent = resolveNotificationText(requestData.getContent(), useLanguage);
|
||||
|
||||
//这里需要加上判断,判断deviceToken中的UseLanguage是什么语言 0:英文 1:中文 根据不同的Language来推送不同语言的通知
|
||||
|
||||
SysLogNotification notificationLog = createNotificationLogByRequest(requestData,transactionId,senderId,userId,deviceTokenValue,notificationTitle,notificationContent);
|
||||
if(StringUtils.isBlank(deviceTokenValue)){
|
||||
notificationLog.setStatus(-1);
|
||||
notificationLog.setResultMessage("No APN token is setup for user:"+userId);
|
||||
notificationLog.setUpdatedAt(LocalDateTime.now());
|
||||
}
|
||||
sysLogNotificationRepository.save(notificationLog);
|
||||
//-- Process APN sending
|
||||
BaseApiResponse<String> sendResult = apnNotificationService.pushApnMessage(
|
||||
transactionId,
|
||||
deviceTokenValue,
|
||||
notificationTitle,
|
||||
notificationContent,
|
||||
true,
|
||||
requestData.getConfirmationNumber());
|
||||
if(sendResult.isSuccess()){
|
||||
successCount++;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(successCount>0){
|
||||
result.setSuccess(true);
|
||||
}else{
|
||||
result.setSuccess(false);
|
||||
result.setMessage("No message was success processed");
|
||||
}
|
||||
}else{
|
||||
result.setSuccess(false);
|
||||
result.setMessage("employeeId list is empty");
|
||||
}
|
||||
return result;
|
||||
try {
|
||||
String url = "/send_notification";
|
||||
// 获取对应的值
|
||||
Header[] headers = HttpHeaderBuilder.custom()
|
||||
.authorization(request.getHeader("Authorization"))
|
||||
.contentType("application/json")
|
||||
.build();
|
||||
HttpResult responseData = httpService.sslSendNotification(url, headers, requestData);
|
||||
response.setSuccess(responseData.getStatusCode()==200);
|
||||
response.setCode(String.valueOf(responseData.getStatusCode()));
|
||||
} catch (Exception e) {
|
||||
log.info("invoke api gateway send notification request failed, ERROR MSG:{}",e.getMessage());
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
private SysLogNotification createNotificationLogByRequest(SendNotificationRequest requestData,String transactionId, String senderId, String userId, String deviceToken, String title, String content){
|
||||
SysLogNotification notificationLog = new SysLogNotification();
|
||||
notificationLog.setUserId(userId);
|
||||
notificationLog.setDeviceToken(deviceToken);
|
||||
notificationLog.setSenderId(senderId);
|
||||
notificationLog.setStatus(0);
|
||||
notificationLog.setConfirmationNo(requestData.getConfirmationNumber());
|
||||
notificationLog.setBookingType(requestData.getType());
|
||||
notificationLog.setTitle(title);
|
||||
notificationLog.setContent(content);
|
||||
notificationLog.setTransactionId(transactionId);
|
||||
notificationLog.setCreatedAt(LocalDateTime.now());
|
||||
return notificationLog;
|
||||
}
|
||||
|
||||
private static String resolveNotificationText(String text, Integer useLanguage) {
|
||||
if (StringUtils.isBlank(text)) {
|
||||
return text;
|
||||
}
|
||||
String preferredTag = Integer.valueOf(1).equals(useLanguage) ? "CN" : "EN";
|
||||
String fallbackTag = Integer.valueOf(1).equals(useLanguage) ? "EN" : "CN";
|
||||
String preferredText = extractNotificationText(text, preferredTag);
|
||||
if (StringUtils.isNotBlank(preferredText)) {
|
||||
return preferredText;
|
||||
}
|
||||
String fallbackText = extractNotificationText(text, fallbackTag);
|
||||
return StringUtils.isNotBlank(fallbackText) ? fallbackText : text;
|
||||
}
|
||||
|
||||
private static String extractNotificationText(String text, String tag) {
|
||||
String marker = "#" + tag + "#";
|
||||
int start = text.indexOf(marker);
|
||||
if (start < 0) {
|
||||
return null;
|
||||
}
|
||||
start += marker.length();
|
||||
int end = text.indexOf(marker, start);
|
||||
if (end < 0) {
|
||||
return null;
|
||||
}
|
||||
return text.substring(start, end).trim();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user