123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- package com.bosshand.virgo.message.service;
- import com.alibaba.fastjson.JSONObject;
- import com.bosshand.virgo.core.dao.MgrUserDao;
- import com.bosshand.virgo.exception.ServiceException;
- import com.bosshand.virgo.message.beetl.FlowMessageGenerator;
- import com.bosshand.virgo.message.beetl.MessageGenerator;
- import com.bosshand.virgo.message.dao.NotificationMessageDao;
- import com.bosshand.virgo.message.model.NotificationMessage;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import java.io.IOException;
- import java.util.*;
- @Service
- public class MessageService {
-
- final static String EVENT_ACCEPTED = "FLOW_ACCEPTED";
- final static String EVENT_SUBMITTED = "FLOW_SUBMITTED";
- final static String EVENT_CANCELLED = "FLOW_CANCELLED";
- final static String EVENT_COMPELETED = "FLOW_COMPELETED";
- final static String EVENT_REJECTED = "FLOW_REJECTED";
- final static String EVENT_RETURNED = "FLOW_RETURNED";
- private static final Log log = LogFactory.getLog(MessageService.class);
- @Autowired
- private NotificationMessageDao notificationMessageDao;
-
- @Autowired
- private MessagePushService messagePushService;
-
- @Autowired
- private CompleteDataService completeDataService;
-
- @Autowired
- private ApiClient apiClient;
- @Autowired
- private MgrUserDao mgrUserDao;
- public int insert(NotificationMessage notificationMessage) {
- return notificationMessageDao.insert(notificationMessage);
- }
- public int updateViewed(long id) {
- return notificationMessageDao.updateViewed(id);
- }
- public NotificationMessage get(long id) {
- return notificationMessageDao.get(id);
- }
- public List<NotificationMessage> getMessageByUserId(long userId) {
- return notificationMessageDao.getMessageByUserId(userId);
- }
- public List<NotificationMessage> getMessageByUserId(int messageType, long userId) {
- return notificationMessageDao.getMessageByMessageType(messageType, userId);
- }
- public List<HashMap<Integer,Long>> countUnread(long userId) {
- return notificationMessageDao.countUnread(userId);
- }
- public List<NotificationMessage> getMessageByViewed(long userId, boolean viewed) {
- return notificationMessageDao.getMessageByViewed(userId, viewed);
- }
- public void pushMessage(List<Long> receiptList, NotificationMessage message) throws ServiceException {
- pushMessage(receiptList, message, false);
- }
- public void pushMessage(List<Long> receiptList, MessageGenerator messageGenerator) throws ServiceException {
- try {
- pushMessage(receiptList, messageGenerator.generateMessage());
- } catch (IOException e) {
- log.error("Fail to push message", e);
- throw new ServiceException("Fail to push message", e);
- }
- }
- private void pushMessage(List<Long> receiptList, NotificationMessage message, boolean sendPush) {
- if (receiptList != null && receiptList.size() > 0) {
- receiptList.forEach(receipt -> {
- message.setUserId(receipt);
- insert(message);
- });
- }
- /*List<NotificationMessage> list = notificationMessageDao.getList();
- for (NotificationMessage s : list) {
- if (s.isPushed() == false) {
- messagePushService.sendMessage(s);
- notificationMessageDao.updatePushed(s.getId());
- }
- }*/
-
- }
-
- public void pushMessage(JSONObject json) {
- FlowMessageGenerator messageGenerator = new FlowMessageGenerator();
- messageGenerator.setTemplateName(getTemplateName(json.getString("templateName")));
- messageGenerator.setSender(json.getString("sender"));
- messageGenerator.setSystemMessage(json.getBoolean("systemMessage"));
- messageGenerator.setContext((Map) json.get("context"));
- List<Long> receiptList = new ArrayList<Long>();
- json.getJSONArray("receiptList").forEach(id -> receiptList.add(Long.parseLong(id.toString())));
- pushMessage(receiptList, messageGenerator);
- if (FlowMessageGenerator.MESSAGE_FLOW_COMPELETED.equals(messageGenerator.getTemplateName())) {
- completeDataService.saveCompleteData(messageGenerator.getContext());
- }
- }
- private String getTemplateName(String notifyType) {
- switch (notifyType) {
- case EVENT_SUBMITTED:
- return FlowMessageGenerator.MESSAGE_FLOW_SUBMITTED;
- case EVENT_ACCEPTED:
- return FlowMessageGenerator.MESSAGE_FLOW_ACCEPTED;
- case EVENT_CANCELLED:
- return FlowMessageGenerator.MESSAGE_FLOW_CANCELLED;
- case EVENT_COMPELETED:
- return FlowMessageGenerator.MESSAGE_FLOW_COMPELETED;
- case EVENT_REJECTED:
- return FlowMessageGenerator.MESSAGE_FLOW_REJECTED;
- case EVENT_RETURNED:
- return FlowMessageGenerator.MESSAGE_FLOW_RETURNED;
- }
- return null;
- }
- public List<NotificationMessage> getMessage(long userId, boolean b) {
- return getMessageByViewed(userId, b);
- }
- public void pushOtherMesssage(String userIds, NotificationMessage message) {
- String[] split = userIds.split(",");
- for (int i = 0; i < split.length; i++) {
- message.setSentTime(new Date());
- message.setUserId(Long.parseLong(split[i]));
- insert(message);
- }
- }
- }
|