init
This commit is contained in:
@@ -0,0 +1,201 @@
|
||||
package com.mgm.mcrm.notification.services;
|
||||
|
||||
import com.mgm.mcrm.authorization.models.SysRouterPermission;
|
||||
import com.mgm.mcrm.authorization.services.SysGroupPermissionService;
|
||||
import com.mgm.mcrm.common.dtos.BaseApiResponse;
|
||||
import com.mgm.mcrm.common.utils.LogUtil;
|
||||
import com.mgm.mcrm.logging.models.SysLogDeviceToken;
|
||||
import com.mgm.mcrm.logging.repositories.SysLogDeviceTokenDao;
|
||||
import com.mgm.mcrm.notification.dtos.request.SendNotificationRequest;
|
||||
import com.mgm.mcrm.notification.dtos.response.ChangeNotificationStatusResponse;
|
||||
import com.mgm.mcrm.notification.dtos.response.CountNotificationByUserIdResponse;
|
||||
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 org.apache.commons.lang3.StringUtils;
|
||||
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;
|
||||
|
||||
@Service
|
||||
public class NotificationService {
|
||||
@Resource
|
||||
private SysLogNotificationRepository sysLogNotificationRepository;
|
||||
@Resource
|
||||
private ApnNotificationService apnNotificationService;
|
||||
@Resource
|
||||
private SysLogDeviceTokenDao sysLogDeviceTokenDao;
|
||||
// @Resource
|
||||
// private ApprovalRoutingDao approvalRoutingDao;
|
||||
@Resource
|
||||
private SysGroupPermissionService sysGroupPermissionService;
|
||||
|
||||
|
||||
public GetNotificationResponse getNotificationByUserId(String userId){
|
||||
GetNotificationResponse response = new GetNotificationResponse();
|
||||
response.setNotifications(sysLogNotificationRepository.findByUserIdAndStatus(userId, 0));
|
||||
response.setSuccess(!response.getNotifications().isEmpty());
|
||||
return response;
|
||||
}
|
||||
|
||||
public CountNotificationByUserIdResponse countNotificationByUserId (String userId,String groupName){
|
||||
CountNotificationByUserIdResponse response = new CountNotificationByUserIdResponse();
|
||||
List<SysRouterPermission> userPermissions = new ArrayList<SysRouterPermission>();
|
||||
boolean viewNotification = false;
|
||||
boolean viewApprovalRouting = false;
|
||||
Long count = 0L;
|
||||
|
||||
userPermissions = sysGroupPermissionService.getRouterPermissionsByGroupName(groupName);
|
||||
if(userPermissions.size()>0) {
|
||||
for(SysRouterPermission per:userPermissions) {
|
||||
if(StringUtils.equals(per.getId(), "10001_05") ) viewApprovalRouting = true;
|
||||
if(StringUtils.equals(per.getId(), "10001_06") ) viewNotification = true;
|
||||
}
|
||||
}
|
||||
|
||||
// if (viewApprovalRouting) {
|
||||
// response.setApprovalRoutingCount( approvalRoutingDao.countApprovalList(userId));
|
||||
// count+=approvalRoutingDao.countApprovalList(userId);
|
||||
// }
|
||||
if (viewNotification) {
|
||||
response.setNotificationCount(sysLogNotificationRepository.countByUserIdAndStatus(userId, 0));
|
||||
count+=sysLogNotificationRepository.countByUserIdAndStatus(userId, 0);
|
||||
}
|
||||
response.setCount(count);
|
||||
return response;
|
||||
}
|
||||
|
||||
public ChangeNotificationStatusResponse changeStatus(Long id) {
|
||||
ChangeNotificationStatusResponse response = new ChangeNotificationStatusResponse();
|
||||
if (id == null) {
|
||||
response.setSuccess(false);
|
||||
response.setCode("400");
|
||||
response.setMessage("id is required");
|
||||
return response;
|
||||
}
|
||||
Optional<SysLogNotification> optional = sysLogNotificationRepository.findById(id);
|
||||
if (optional.isEmpty()) {
|
||||
response.setSuccess(false);
|
||||
response.setCode("404");
|
||||
response.setMessage("Notification not found");
|
||||
return response;
|
||||
}
|
||||
LogUtil.info(NotificationService.class,"Notification status changed,Id = "+id);
|
||||
SysLogNotification notification = optional.get();
|
||||
notification.setStatus(1);
|
||||
notification.setUpdatedAt(LocalDateTime.now());
|
||||
SysLogNotification saved = sysLogNotificationRepository.save(notification);
|
||||
response.setSuccess(true);
|
||||
response.setCode("200");
|
||||
response.setId(saved.getId());
|
||||
response.setStatus(saved.getStatus());
|
||||
return response;
|
||||
}
|
||||
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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