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 getMessageByUserId(long userId) { return notificationMessageDao.getMessageByUserId(userId); } public List getMessageByUserId(int messageType, long userId) { return notificationMessageDao.getMessageByMessageType(messageType, userId); } public List> countUnread(long userId) { return notificationMessageDao.countUnread(userId); } public List getMessageByViewed(long userId, boolean viewed) { return notificationMessageDao.getMessageByViewed(userId, viewed); } public void pushMessage(List receiptList, NotificationMessage message) throws ServiceException { pushMessage(receiptList, message, false); } public void pushMessage(List 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 receiptList, NotificationMessage message, boolean sendPush) { if (receiptList != null && receiptList.size() > 0) { receiptList.forEach(receipt -> { message.setUserId(receipt); insert(message); }); } /*List 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 receiptList = new ArrayList(); 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 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); } } }