JtMessageProducerServiceImpl.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package com.goafanti.message.service.impl;
  2. import java.util.ArrayList;
  3. import java.util.Date;
  4. import java.util.HashMap;
  5. import java.util.Iterator;
  6. import java.util.List;
  7. import java.util.Map;
  8. import java.util.UUID;
  9. import java.util.regex.Matcher;
  10. import java.util.regex.Pattern;
  11. import org.apache.commons.lang3.StringUtils;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.beans.factory.annotation.Value;
  14. import org.springframework.stereotype.Service;
  15. import com.goafanti.common.dao.AdminMapper;
  16. import com.goafanti.common.dao.JtMessageConsumerMapper;
  17. import com.goafanti.common.dao.JtMessageProducerMapper;
  18. import com.goafanti.common.dao.UserMapper;
  19. import com.goafanti.common.model.JtMessageConsumer;
  20. import com.goafanti.common.model.JtMessageProducer;
  21. import com.goafanti.common.utils.MobileMessageUtils;
  22. import com.goafanti.core.mybatis.BaseMybatisDao;
  23. import com.goafanti.core.mybatis.page.Pagination;
  24. import com.goafanti.core.shiro.token.TokenManager;
  25. import com.goafanti.message.JGMessageHelper;
  26. import com.goafanti.message.service.JtMessageProducerService;
  27. @Service
  28. public class JtMessageProducerServiceImpl extends BaseMybatisDao<JtMessageProducerMapper> implements JtMessageProducerService{
  29. @Autowired
  30. private JtMessageProducerMapper jtMessageProducerMapper;
  31. @Autowired
  32. private UserMapper userMapper;
  33. @Autowired
  34. private AdminMapper adminMapper;
  35. @Autowired
  36. private JtMessageConsumerMapper jtMessageConsumerMapper;
  37. @Autowired
  38. private JGMessageHelper jgMessageHelper;
  39. @Value(value = "${accessKey}")
  40. private String accessKey = null;
  41. @Value(value = "${accessSecret}")
  42. private String accessSecret = null;
  43. @Override
  44. public int addMessage(JtMessageProducer messageProducer) {
  45. //处理消息内容
  46. messageProducer.setId(UUID.randomUUID().toString());
  47. //获得当前登录用户名字
  48. String create = adminMapper.selectByPrimaryKey(TokenManager.getAdminId()).getName();
  49. if(StringUtils.isBlank(create)){
  50. create = userMapper.selectByPrimaryKey(TokenManager.getUserId()).getUsername();
  51. }
  52. if(StringUtils.isBlank(create)){
  53. return -1;
  54. }
  55. messageProducer.setCreater(create);
  56. if(null != messageProducer.getCreateTime()){
  57. messageProducer.setCreateTime(new Date());
  58. }
  59. if (messageProducer.getResourceType()==null) {
  60. messageProducer.setResourceType(-1);//安卓跳转处理
  61. }
  62. return jtMessageProducerMapper.insertSelective(messageProducer);
  63. }
  64. @Override
  65. public int addSendMessage(String messageId) {
  66. // 获得所有正常使用的注册用户
  67. List<JtMessageConsumer> list = userMapper.getAllUser();
  68. Iterator<JtMessageConsumer> it = list.iterator();
  69. while (it.hasNext()) {
  70. JtMessageConsumer mc = (JtMessageConsumer) it.next();
  71. mc.setId(UUID.randomUUID().toString());
  72. mc.setMessageId(messageId);
  73. }
  74. JtMessageProducer messageProducer = new JtMessageProducer();
  75. messageProducer.setId(messageId);
  76. messageProducer.setSendTime(new Date());
  77. messageProducer.setIsSend(true);
  78. messageProducer.setIsDraft(false);
  79. jtMessageProducerMapper.updateByPrimaryKeySelective(messageProducer);
  80. JtMessageProducer producer=jtMessageProducerMapper.selectByPrimaryKey(messageId);
  81. if (producer!=null) {
  82. if (producer.getSubject()==1) {
  83. //推送消息
  84. jgMessageHelper.sendPushMessage(producer);
  85. }
  86. }
  87. return jtMessageConsumerMapper.addBatch(list);
  88. }
  89. @Override
  90. public JtMessageProducer getMessageById(String messageId) {
  91. return jtMessageProducerMapper.selectByPrimaryKey(messageId);
  92. }
  93. @Override
  94. public int updateMessageById(JtMessageProducer messageProducer) {
  95. int count = 0;
  96. // 修改消息
  97. messageProducer.setCreateTime(null);//不修改创建时间
  98. count = jtMessageProducerMapper.updateByPrimaryKeySelective(messageProducer);
  99. // 推送消息
  100. if(messageProducer.getIsSend() && !messageProducer.getDeleteSign() && !messageProducer.getIsDraft()){
  101. if(null == messageProducer.getSendTime()) messageProducer.setSendTime(new Date());
  102. count += addSendMessage(messageProducer.getId());
  103. }
  104. return count;
  105. }
  106. @SuppressWarnings("unchecked")
  107. @Override
  108. public Pagination<JtMessageProducer> getMessageList(JtMessageProducer messageProducer, String sendStartTime, String sendEndTime,
  109. String messageCode,Integer subject,Integer pageNo,Integer pageSize){
  110. if(pageNo==null || pageNo<1)pageNo=1;
  111. if(pageSize==null ||pageSize<1)pageSize=10;
  112. Map<String, Object> params=new HashMap<String,Object>();
  113. //params.put("create", TokenManager.getUserId());
  114. if(StringUtils.isNotBlank(messageProducer.getTitle()))params.put("title",messageProducer.getTitle());
  115. if(null != messageProducer.getIsDraft())params.put("isDraft",messageProducer.getIsDraft());
  116. if(null != messageProducer.getIsSend())params.put("isSend",messageProducer.getIsSend());
  117. if(StringUtils.isNotBlank(sendStartTime))params.put("sendStartTime",sendStartTime);
  118. if(StringUtils.isNotBlank(sendEndTime))params.put("sendEndTime",sendEndTime);
  119. if(StringUtils.isNotBlank(messageCode))params.put("code",messageCode);
  120. if(null!=subject)params.put("subject",subject);
  121. return (Pagination<JtMessageProducer>) findPage("getMyMessageList", "getMyMessageCount", params, pageNo, pageSize);
  122. }
  123. @Override
  124. public int MobileMessage(String messageCode) {
  125. String regex = "^(1[1-9][0-9])\\d{8}$";
  126. Pattern p = Pattern.compile(regex);
  127. List<String> mobileList=userMapper.getUserListMobile();
  128. String mobile="";
  129. for (String string : mobileList) {
  130. if (null!=string) {
  131. Matcher m = p.matcher(string);
  132. if ( m.matches()) {
  133. mobile+=string+",";
  134. }
  135. }
  136. }
  137. if(mobile.length()>1)mobile=mobile.substring(0, mobile.length()-1);
  138. MobileMessageUtils.sendMessage(messageCode, null, mobile, accessKey, accessSecret);
  139. JtMessageProducer producer=new JtMessageProducer();
  140. producer.setId(UUID.randomUUID().toString());
  141. producer.setSubject(2);
  142. producer.setIsSend(true);
  143. producer.setIsDraft(false);
  144. producer.setDeleteSign(false);
  145. producer.setCode(messageCode);
  146. producer.setSendTime(new Date());
  147. jtMessageProducerMapper.insertSelective(producer);
  148. return 1;
  149. }
  150. }