MessageService.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package com.bosshand.virgo.message.service;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.bosshand.virgo.core.dao.MgrUserDao;
  4. import com.bosshand.virgo.exception.ServiceException;
  5. import com.bosshand.virgo.message.beetl.FlowMessageGenerator;
  6. import com.bosshand.virgo.message.beetl.MessageGenerator;
  7. import com.bosshand.virgo.message.dao.NotificationMessageDao;
  8. import com.bosshand.virgo.message.model.NotificationMessage;
  9. import org.apache.commons.logging.Log;
  10. import org.apache.commons.logging.LogFactory;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.stereotype.Service;
  13. import java.io.IOException;
  14. import java.util.*;
  15. @Service
  16. public class MessageService {
  17. final static String EVENT_ACCEPTED = "FLOW_ACCEPTED";
  18. final static String EVENT_SUBMITTED = "FLOW_SUBMITTED";
  19. final static String EVENT_CANCELLED = "FLOW_CANCELLED";
  20. final static String EVENT_COMPELETED = "FLOW_COMPELETED";
  21. final static String EVENT_REJECTED = "FLOW_REJECTED";
  22. final static String EVENT_RETURNED = "FLOW_RETURNED";
  23. private static final Log log = LogFactory.getLog(MessageService.class);
  24. @Autowired
  25. private NotificationMessageDao notificationMessageDao;
  26. @Autowired
  27. private MessagePushService messagePushService;
  28. @Autowired
  29. private CompleteDataService completeDataService;
  30. @Autowired
  31. private ApiClient apiClient;
  32. @Autowired
  33. private MgrUserDao mgrUserDao;
  34. public int insert(NotificationMessage notificationMessage) {
  35. return notificationMessageDao.insert(notificationMessage);
  36. }
  37. public int updateViewed(long id) {
  38. return notificationMessageDao.updateViewed(id);
  39. }
  40. public NotificationMessage get(long id) {
  41. return notificationMessageDao.get(id);
  42. }
  43. public List<NotificationMessage> getMessageByUserId(long userId) {
  44. return notificationMessageDao.getMessageByUserId(userId);
  45. }
  46. public List<NotificationMessage> getMessageByUserId(int messageType, long userId) {
  47. return notificationMessageDao.getMessageByMessageType(messageType, userId);
  48. }
  49. public List<HashMap<Integer,Long>> countUnread(long userId) {
  50. return notificationMessageDao.countUnread(userId);
  51. }
  52. public List<NotificationMessage> getMessageByViewed(long userId, boolean viewed) {
  53. return notificationMessageDao.getMessageByViewed(userId, viewed);
  54. }
  55. public void pushMessage(List<Long> receiptList, NotificationMessage message) throws ServiceException {
  56. pushMessage(receiptList, message, false);
  57. }
  58. public void pushMessage(List<Long> receiptList, MessageGenerator messageGenerator) throws ServiceException {
  59. try {
  60. pushMessage(receiptList, messageGenerator.generateMessage());
  61. } catch (IOException e) {
  62. log.error("Fail to push message", e);
  63. throw new ServiceException("Fail to push message", e);
  64. }
  65. }
  66. private void pushMessage(List<Long> receiptList, NotificationMessage message, boolean sendPush) {
  67. if (receiptList != null && receiptList.size() > 0) {
  68. receiptList.forEach(receipt -> {
  69. message.setUserId(receipt);
  70. insert(message);
  71. });
  72. }
  73. /*List<NotificationMessage> list = notificationMessageDao.getList();
  74. for (NotificationMessage s : list) {
  75. if (s.isPushed() == false) {
  76. messagePushService.sendMessage(s);
  77. notificationMessageDao.updatePushed(s.getId());
  78. }
  79. }*/
  80. }
  81. public void pushMessage(JSONObject json) {
  82. FlowMessageGenerator messageGenerator = new FlowMessageGenerator();
  83. messageGenerator.setTemplateName(getTemplateName(json.getString("templateName")));
  84. messageGenerator.setSender(json.getString("sender"));
  85. messageGenerator.setSystemMessage(json.getBoolean("systemMessage"));
  86. messageGenerator.setContext((Map) json.get("context"));
  87. List<Long> receiptList = new ArrayList<Long>();
  88. json.getJSONArray("receiptList").forEach(id -> receiptList.add(Long.parseLong(id.toString())));
  89. pushMessage(receiptList, messageGenerator);
  90. if (FlowMessageGenerator.MESSAGE_FLOW_COMPELETED.equals(messageGenerator.getTemplateName())) {
  91. completeDataService.saveCompleteData(messageGenerator.getContext());
  92. }
  93. }
  94. private String getTemplateName(String notifyType) {
  95. switch (notifyType) {
  96. case EVENT_SUBMITTED:
  97. return FlowMessageGenerator.MESSAGE_FLOW_SUBMITTED;
  98. case EVENT_ACCEPTED:
  99. return FlowMessageGenerator.MESSAGE_FLOW_ACCEPTED;
  100. case EVENT_CANCELLED:
  101. return FlowMessageGenerator.MESSAGE_FLOW_CANCELLED;
  102. case EVENT_COMPELETED:
  103. return FlowMessageGenerator.MESSAGE_FLOW_COMPELETED;
  104. case EVENT_REJECTED:
  105. return FlowMessageGenerator.MESSAGE_FLOW_REJECTED;
  106. case EVENT_RETURNED:
  107. return FlowMessageGenerator.MESSAGE_FLOW_RETURNED;
  108. }
  109. return null;
  110. }
  111. public List<NotificationMessage> getMessage(long userId, boolean b) {
  112. return getMessageByViewed(userId, b);
  113. }
  114. public void pushOtherMesssage(String userIds, NotificationMessage message) {
  115. String[] split = userIds.split(",");
  116. for (int i = 0; i < split.length; i++) {
  117. message.setSentTime(new Date());
  118. message.setUserId(Long.parseLong(split[i]));
  119. insert(message);
  120. }
  121. }
  122. }