package com.mgm.mcrm.notification.services; import com.alibaba.fastjson.JSON; import com.mgm.mcrm.approval.repositories.ApprovalRoutingDao; 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; 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 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.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.time.LocalDateTime; import java.util.*; @Slf4j @Service public class NotificationService { @Resource private SysLogNotificationRepository sysLogNotificationRepository; @Resource private SysGroupPermissionService sysGroupPermissionService; @Resource private HttpService httpService; @Autowired private ApprovalRoutingDao approvalRoutingDao; 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 userPermissions = new ArrayList(); 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 (viewNotification) { response.setNotificationCount(sysLogNotificationRepository.countByUserIdAndStatus(userId, 0)); count+=sysLogNotificationRepository.countByUserIdAndStatus(userId, 0); } if (viewApprovalRouting) { response.setApprovalRoutingCount(approvalRoutingDao.countApprovalList(userId)); count+=approvalRoutingDao.countApprovalList(userId); } 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 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 sendNotificationByRequest(HttpServletRequest request, SendNotificationRequest requestData) { BaseApiResponse response = new BaseApiResponse<>(); try { String url = "/send_notification"; // 获取对应的值 Header[] headers = HttpHeaderBuilder.custom() .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; } public BaseApiResponse readAllNotifications(String userId) { BaseApiResponse response = new BaseApiResponse<>(); int updated = sysLogNotificationRepository.markAllAsReadByUserId(userId, LocalDateTime.now()); response.setSuccess(true); response.setCode("200"); response.setData(updated); return response; } }