MessageServiceImpl.java 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. package com.uas.ps.message.service.impl;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.uas.ps.core.page.PageInfo;
  4. import com.uas.ps.core.page.PageParams;
  5. import com.uas.ps.message.dao.AppDao;
  6. import com.uas.ps.message.dao.MessageDao;
  7. import com.uas.ps.message.domain.App;
  8. import com.uas.ps.message.domain.Message;
  9. import com.uas.ps.message.domain.SmsMessage;
  10. import com.uas.ps.message.domain.User;
  11. import com.uas.ps.message.exception.IllegalOperatorException;
  12. import com.uas.ps.message.exception.ParameterMissingException;
  13. import com.uas.ps.message.service.MessageService;
  14. import com.uas.ps.message.util.Constant;
  15. import com.uas.ps.message.util.ConsumeType;
  16. import com.uas.ps.message.util.FastjsonUtils;
  17. import com.uas.ps.message.util.SMSType;
  18. import com.uas.ps.message.util.SplitChar;
  19. import com.uas.ps.message.util.account.HttpUtil;
  20. import com.uas.ps.message.util.account.HttpUtil.ResponseWrap;
  21. import com.uas.sso.util.AccountUtils;
  22. import com.uas.sso.util.FlexJsonUtil;
  23. import java.sql.ResultSet;
  24. import java.sql.SQLException;
  25. import java.util.ArrayList;
  26. import java.util.Date;
  27. import java.util.HashMap;
  28. import java.util.HashSet;
  29. import java.util.List;
  30. import java.util.Map;
  31. import java.util.Set;
  32. import org.apache.log4j.Logger;
  33. import org.springframework.beans.factory.annotation.Autowired;
  34. import org.springframework.data.domain.Page;
  35. import org.springframework.data.domain.PageImpl;
  36. import org.springframework.http.HttpHeaders;
  37. import org.springframework.http.MediaType;
  38. import org.springframework.jdbc.core.JdbcTemplate;
  39. import org.springframework.jdbc.core.RowMapper;
  40. import org.springframework.stereotype.Service;
  41. import org.springframework.ui.ModelMap;
  42. import org.springframework.util.CollectionUtils;
  43. import org.springframework.util.StringUtils;
  44. /**
  45. * Created by wangyc on 2018/1/13.
  46. *
  47. * @version 2018/1/13 15:44 wangyc
  48. */
  49. @Service
  50. public class MessageServiceImpl implements MessageService {
  51. private final MessageDao messageDao;
  52. private final AppDao appDao;
  53. private static final Logger LOGGER = Logger.getLogger(Logger.class);
  54. private final JdbcTemplate jdbcTemplate;
  55. // private RestTemplate restTemplate;
  56. // private static final String EMAIL_REGEX = "\\w[-\\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\\.)+[A-Za-z]{2,14}";
  57. // private static final String TEL_REGEXP = "^((\\(\\d{3}\\))|(\\d{3}\\-))?(13|15|18|17)\\d{9}$";
  58. private static final String PROD_URL = "https://mobile.ubtob.com/tigase/baiduPush";
  59. private static final String PUBLIC_INQUIRY_IM_URL = "https://mobile.ubtob.com:8443/openapp/?pagekind"
  60. + "=B&id=%s&uu=%s&telephone=%s";
  61. /**
  62. * 邮件服务主机地址
  63. */
  64. private static final String MAIL_CONSOLE_HOST = "http://10.10.100.136:8080";
  65. /**
  66. * 发送邮件给单个人url
  67. */
  68. private static final String MAIL_SEND_URL = "mail/send";
  69. /**
  70. * 发送邮件给多个人 url
  71. */
  72. private static final String MAIL_SEND_MANY_URL = "mail/send/o2m";
  73. /**
  74. * 短信接口
  75. */
  76. private final String messageUrl = "http://10.10.100.136:8080/sms/send";
  77. @Autowired
  78. public MessageServiceImpl(MessageDao messageDao, AppDao appDao, JdbcTemplate jdbcTemplate) {
  79. this.messageDao = messageDao;
  80. this.appDao = appDao;
  81. this.jdbcTemplate = jdbcTemplate;
  82. }
  83. @Override
  84. public Page<Message> getMessages(String receiverUu, String receiverEnuu, String consumerApp, String isRead,
  85. String keyword, PageParams pageParams) {
  86. if (StringUtils.isEmpty(receiverUu) || StringUtils.isEmpty(receiverEnuu)) {
  87. throw new ParameterMissingException("接收人信息为空");
  88. }
  89. if (StringUtils.isEmpty(consumerApp)) {
  90. throw new ParameterMissingException("接收应用信息为空");
  91. }
  92. App consumerAppExists = appDao.findByName(consumerApp);
  93. if (consumerApp == null) {
  94. throw new IllegalOperatorException("接收应用不存在");
  95. }
  96. final Long consumerAppId = consumerAppExists.getId();
  97. // 消息接收人过滤
  98. // SimpleExpression receiverUuExp = PredicateUtils.eq("receiverUu", Long.valueOf(receiverUu), true);
  99. // 消息接收企业过滤
  100. // SimpleExpression receiverEnuuExp = PredicateUtils.eq("receiverEnuu", Long.valueOf(receiverEnuu), true);
  101. // SimpleExpression[] simpleExpressions = new SimpleExpression[] {receiverUuExp, receiverEnuuExp};
  102. String sql = String.format("select count(1) from messages m where ms_receiveruu = %s and ms_receiverenuu = %s ",
  103. Long.valueOf(receiverUu), Long.valueOf(receiverEnuu));
  104. // 读取状态
  105. if (!StringUtils.isEmpty(isRead)) {
  106. // SimpleExpression isReadExp = PredicateUtils.eq("isRead", Short.valueOf(isRead), true);
  107. // simpleExpressions = ArrayUtils.concat(simpleExpressions, new SimpleExpression[] {isReadExp});
  108. sql += String.format(" and ms_isread = %s", Short.valueOf(isRead));
  109. }
  110. // 关键词搜索
  111. if (!StringUtils.isEmpty(keyword)) {
  112. // SimpleExpression contentExp = PredicateUtils.like("content", keyword, true);
  113. // simpleExpressions = ArrayUtils.concat(simpleExpressions, new SimpleExpression[] {contentExp});
  114. sql += String.format(" and ms_content like '%%%s%%'", keyword);
  115. }
  116. sql += String.format(" and exists (select 1 from message$consumeapp where app_id = %s and m.ms_id = ms_id)",
  117. consumerAppId);
  118. // final LogicalExpression logicalExpression = PredicateUtils.and(simpleExpressions);
  119. final PageInfo pageInfo = new PageInfo(pageParams);
  120. Long count = jdbcTemplate.queryForObject(sql, Long.class);
  121. List<Message> messageList = new ArrayList<>();
  122. if (count > 0) {
  123. sql += String.format("order by ms_createtime %s limit %s,%s", pageInfo.getSort().getOrderFor("createTime").getDirection(), (pageParams.getPage() - 1) * pageParams.getCount(), pageParams.getCount());
  124. messageList = jdbcTemplate.query(sql.replace("count(1)", "*"), new RowMapper<Message>() {
  125. @Override
  126. public Message mapRow(ResultSet resultSet, int i) throws SQLException {
  127. Message message = new Message();
  128. if (resultSet != null) {
  129. message.setId(resultSet.getLong("ms_id"));
  130. message.setConsumeType(resultSet.getString("ms_consumeType"));
  131. message.setContent(resultSet.getString("ms_content"));
  132. message.setCreateTime(resultSet.getTimestamp("ms_createTime"));
  133. message.setIsRead(resultSet.getShort("ms_isread"));
  134. message.setIsSent(resultSet.getShort("ms_issent"));
  135. message.setReceiverEnuu(resultSet.getLong("ms_receiverenuu"));
  136. message.setReceiverUu(resultSet.getLong("ms_receiveruu"));
  137. message.setSenderEnuu(resultSet.getLong("ms_senderenuu"));
  138. message.setSenderUu(resultSet.getLong("ms_senderuu"));
  139. message.setType(resultSet.getString("ms_type"));
  140. message.setRemark(resultSet.getString("ms_remark"));
  141. message.setSourceId(resultSet.getLong("ms_sourceid"));
  142. }
  143. return message;
  144. }
  145. });
  146. }
  147. return new PageImpl<Message>(messageList, pageInfo, count);
  148. // return messageDao.findAll(new Specification<Message>() {
  149. // @Override
  150. // public Predicate toPredicate(Root<Message> root, CriteriaQuery<?> criteriaQuery,
  151. // CriteriaBuilder criteriaBuilder) {
  152. // Predicate messagePredicate = criteriaBuilder.and(logicalExpression.toPredicate(root, criteriaQuery,
  153. // criteriaBuilder));
  154. // // 消费应用过滤
  155. // Predicate consumerAppPredicate = criteriaBuilder.equal(root.join("consumerApp").get("id"),
  156. // consumerAppId);
  157. // Predicate all = criteriaBuilder.and(messagePredicate, consumerAppPredicate);
  158. // criteriaQuery.where(all);
  159. // return null;
  160. // }
  161. // }, pageInfo);
  162. }
  163. @Override
  164. public Map<String, Object> getUnReadMessageCount(String receiverUu, String receiverEnuu, String consumerApp) {
  165. if (StringUtils.isEmpty(receiverUu) || StringUtils.isEmpty(receiverEnuu)) {
  166. throw new ParameterMissingException("接收人信息为空");
  167. }
  168. if (StringUtils.isEmpty(consumerApp)) {
  169. throw new ParameterMissingException("接收应用信息为空");
  170. }
  171. App consumerAppExists = appDao.findByName(consumerApp);
  172. if (consumerApp == null) {
  173. throw new IllegalOperatorException("接收应用不存在");
  174. }
  175. Integer messagesCount = messageDao.findCountByReceiverUuAndReceiverEnuuAndIsReadAndConsumerAppId(Long.valueOf(receiverUu),
  176. Long.valueOf(receiverEnuu), Constant.NO, consumerAppExists.getId());
  177. Map<String, Object> resultMap = new HashMap<>();
  178. resultMap.put("success", "success");
  179. resultMap.put("count", messagesCount);
  180. return resultMap;
  181. }
  182. @Override
  183. public Map<String, Object> getMessageCount(String receiverUu, String receiverEnuu, String consumerApp,
  184. String isRead) {
  185. if (StringUtils.isEmpty(receiverUu) || StringUtils.isEmpty(receiverEnuu)) {
  186. throw new ParameterMissingException("接收人信息为空");
  187. }
  188. if (StringUtils.isEmpty(consumerApp)) {
  189. throw new ParameterMissingException("接收应用信息为空");
  190. }
  191. App consumerAppExists = appDao.findByName(consumerApp);
  192. if (consumerAppExists == null) {
  193. throw new IllegalOperatorException("接收应用不存在");
  194. }
  195. Integer messagesCount = 0;
  196. if (StringUtils.isEmpty(isRead) || (!isRead.equals(Constant.NO.toString()) && !isRead.equals(Constant.YES.toString()))) {
  197. messagesCount = messageDao.findCountByReceiverUuAndReceiverEnuuAndConsumerAppId(Long.valueOf(receiverUu),
  198. Long.valueOf(receiverEnuu), consumerAppExists.getId());
  199. } else {
  200. messagesCount = messageDao.findCountByReceiverUuAndReceiverEnuuAndIsReadAndConsumerAppId(Long.valueOf(receiverUu),
  201. Long.valueOf(receiverEnuu), Short.valueOf(isRead), consumerAppExists.getId());
  202. }
  203. Map<String, Object> resultMap = new HashMap<>();
  204. resultMap.put("success", "success");
  205. resultMap.put("count", messagesCount);
  206. return resultMap;
  207. }
  208. @Override
  209. public List<Message> saveMessages(String messages) {
  210. List<JSONObject> jsonObjects = FastjsonUtils.fromJsonArray(messages, JSONObject.class);
  211. List<Message> messageList = convertFromJsonObj(jsonObjects);
  212. return messageDao.save(messageList);
  213. }
  214. @Override
  215. public Map<String, Object> sendMessage(String consumerApp) {
  216. JSONObject consumer = FastjsonUtils.parseObject(consumerApp);
  217. Object consumerAppObj = consumer.get("consumerApp");
  218. if (consumerAppObj != null && !StringUtils.isEmpty(String.valueOf(consumerAppObj))) {
  219. App consumerAppExists = appDao.findByName(String.valueOf(consumerAppObj));
  220. if (consumerAppExists == null) {
  221. throw new IllegalOperatorException("接收应用不存在");
  222. }
  223. // 获取改应用应接收的未读、未发送的需要推送的消息(取前500条)
  224. List<Message> messages = messageDao.findByConsumerAppIdAndIsReadAndIsSentNeedToSend(
  225. consumerAppExists.getId(), Constant.NO, Constant.NO);
  226. LOGGER.info("信息数量:" + messages.size());
  227. Map<String, Object> resultMap = sendMessageByAPI(messages, consumerApp);
  228. resultMap.put("success", "success");
  229. return resultMap;
  230. } else {
  231. throw new ParameterMissingException("接收应用信息为空");
  232. }
  233. }
  234. @Override
  235. public ModelMap readMessage(String consumer) {
  236. ModelMap map = new ModelMap();
  237. map.put("success", "false");
  238. if (StringUtils.isEmpty(consumer)) {
  239. throw new ParameterMissingException("接收人信息为空,无法阅读消息");
  240. }
  241. JSONObject jsonObject = FastjsonUtils.parseObject(consumer);
  242. // 消息id
  243. Object messageId = jsonObject.get("messageId");
  244. Object receiverUu = jsonObject.get("receiverUu");
  245. Object receiverEnuu = jsonObject.get("receiverEnuu");
  246. Object consumerApp = jsonObject.get("consumerApp");
  247. if (StringUtils.isEmpty(messageId)) {
  248. map.put("data", "消息id为空,请重新确认消息信息");
  249. return map;
  250. } else {
  251. String[] messageIds = String.valueOf(messageId).split(SplitChar.COMMA);
  252. List<Message> messages = new ArrayList<>();
  253. for (String id : messageIds) {
  254. Message message = messageDao.findOne(Long.valueOf(String.valueOf(id)));
  255. if (message == null) {
  256. map.put("data", "消息不存在,请重新确认消息信息");
  257. return map;
  258. }
  259. if (receiverUu == null || StringUtils.isEmpty(String.valueOf(receiverEnuu)) || receiverEnuu == null
  260. || StringUtils.isEmpty(String.valueOf(receiverUu))
  261. || StringUtils.isEmpty(String.valueOf(consumerApp))) {
  262. map.put("data", "用户信息为空,无法读取消息");
  263. return map;
  264. }
  265. if (!Long.valueOf(String.valueOf(receiverEnuu)).equals(message.getReceiverEnuu())
  266. || !Long.valueOf(String.valueOf(receiverUu)).equals(message.getReceiverUu())) {
  267. map.put("data", "此消息不属于当前用户,请重新确认后读取");
  268. return map;
  269. }
  270. if (Constant.YES.equals(message.getIsRead())) {
  271. map.put("data", "消息已阅读");
  272. return map;
  273. }
  274. App app = appDao.findByName(String.valueOf(consumerApp));
  275. if (app == null) {
  276. map.put("data", "消费应用不存在,请重新确认用户信息");
  277. return map;
  278. }
  279. if (!message.getConsumerApp().contains(app)) {
  280. map.put("data", "消息不属于当前应用");
  281. return map;
  282. }
  283. message.setIsRead(Constant.YES);
  284. messages.add(message);
  285. }
  286. messages = messageDao.save(messages);
  287. map.put("success", "success");
  288. map.put("data", FastjsonUtils.toJson(messages));
  289. return map;
  290. }
  291. }
  292. /**
  293. * jsonObject转换为Message
  294. * @param jsonObjects jsonObjects
  295. * @return messages
  296. */
  297. private List<Message> convertFromJsonObj(List<JSONObject> jsonObjects) {
  298. List<Message> messages = new ArrayList<>();
  299. for (JSONObject jsonObject : jsonObjects) {
  300. Message message = new Message();
  301. // 消息内容
  302. Object content = jsonObject.get("content");
  303. if (StringUtils.isEmpty(content)) {
  304. throw new ParameterMissingException("消息内容为空");
  305. } else {
  306. message.setContent(String.valueOf(content));
  307. }
  308. // 发送人
  309. Object senderUu = jsonObject.get("senderUu");
  310. Object senderEnuu = jsonObject.get("senderEnuu");
  311. if (StringUtils.isEmpty(senderUu) || StringUtils.isEmpty(senderEnuu)) {
  312. throw new ParameterMissingException("发送人信息为空");
  313. } else {
  314. message.setSenderUu(Long.valueOf(String.valueOf(senderUu)));
  315. message.setSenderEnuu(Long.valueOf(String.valueOf(senderEnuu)));
  316. }
  317. // 接收人
  318. Object receiverUu = jsonObject.get("receiverUu");
  319. Object receiverEnuu = jsonObject.get("receiverEnuu");
  320. if (StringUtils.isEmpty(receiverUu) || StringUtils.isEmpty(receiverEnuu)) {
  321. throw new ParameterMissingException("接收人信息为空");
  322. } else {
  323. message.setReceiverUu(Long.valueOf(String.valueOf(receiverUu)));
  324. message.setReceiverEnuu(Long.valueOf(String.valueOf(receiverEnuu)));
  325. }
  326. // 发送应用
  327. Object producerApp = jsonObject.get("producerApp");
  328. if (StringUtils.isEmpty(producerApp)) {
  329. throw new ParameterMissingException("发送应用为空");
  330. } else {
  331. App app = appDao.findByName(String.valueOf(producerApp));
  332. if (app == null) {
  333. throw new IllegalOperatorException("发送应用不存在");
  334. }
  335. message.setProducerApp(app.getId());
  336. }
  337. // 接收应用
  338. Object consumerType = jsonObject.get("consumerType");
  339. if (StringUtils.isEmpty(consumerType)) {
  340. throw new ParameterMissingException("接收应用类型为空");
  341. } else {
  342. String type = String.valueOf(consumerType);
  343. // 消息接收类型为公共,即所有子应用共享
  344. if (ConsumeType.PUBLIC.equals(type)) {
  345. List<App> apps = appDao.findAll();
  346. message.setConsumerApp(new HashSet<>(apps));
  347. message.setConsumeType(type);
  348. } else {
  349. Object consumerApp = jsonObject.get("consumerApp");
  350. if (consumerApp == null) {
  351. throw new ParameterMissingException("接收应用为空");
  352. } else {
  353. // 取出通过逗号拼接的接收应用
  354. String[] consumers = String.valueOf(consumerApp).split(SplitChar.COMMA);
  355. if (consumers.length < 1) {
  356. throw new ParameterMissingException("接收应用为空");
  357. }
  358. Set<App> consumerApps = new HashSet<>();
  359. for (String consumer : consumers) {
  360. App app = appDao.findByName(consumer);
  361. if (app == null) {
  362. throw new IllegalOperatorException("接收应用不存在");
  363. }
  364. consumerApps.add(app);
  365. }
  366. message.setConsumerApp(consumerApps);
  367. message.setConsumeType(consumers.length > 1 ? ConsumeType.MULTI : ConsumeType.SINGLE);
  368. }
  369. }
  370. }
  371. // 消息类型(暂时未定)
  372. Object type = jsonObject.get("type");
  373. if (!StringUtils.isEmpty(type)) {
  374. message.setType(String.valueOf(type));
  375. }
  376. // 推送类型
  377. Object smsType = jsonObject.get("smsType");
  378. if (StringUtils.isEmpty(smsType)) {
  379. message.setSmsType(SMSType.DONT_SEND);
  380. } else {
  381. message.setSmsType(String.valueOf(smsType));
  382. }
  383. // 备注
  384. Object remark = jsonObject.get("remark");
  385. if (!StringUtils.isEmpty(remark)) {
  386. message.setRemark(String.valueOf(remark));
  387. }
  388. // 来源id
  389. Object sourceId = jsonObject.get("sourceId");
  390. if (!StringUtils.isEmpty(sourceId)) {
  391. message.setSourceId(Long.valueOf(String.valueOf(sourceId)));
  392. }
  393. // 邮件模板
  394. Object mailTemplate = jsonObject.get("mailTemplate");
  395. if (!StringUtils.isEmpty(mailTemplate)) {
  396. message.setMailTemplate(String.valueOf(mailTemplate));
  397. }
  398. // 短信模板
  399. Object smTemplate = jsonObject.get("smTemplate");
  400. if (!StringUtils.isEmpty(smTemplate)) {
  401. message.setSmTemplate(String.valueOf(smTemplate));
  402. }
  403. message.setIsRead(Constant.NO);
  404. message.setCreateTime(new Date());
  405. message.setIsSent(Constant.NO);
  406. messages.add(message);
  407. }
  408. return messages;
  409. }
  410. /**
  411. * 推送消息(调用邮件、短信、IM接口)
  412. * @param messages 消息
  413. * @param consumerApp 消费app
  414. * @return resultMap
  415. */
  416. private Map<String, Object> sendMessageByAPI(List<Message> messages, String consumerApp) {
  417. Map<String, Object> resultMap = new HashMap<>();
  418. if (!CollectionUtils.isEmpty(messages)) {
  419. for (Message message : messages) {
  420. try {
  421. User receiver = (User) AccountUtils.getUserInfo(message.getReceiverUu(),
  422. message.getReceiverEnuu());
  423. if (receiver.getVipName() != null) {
  424. LOGGER.info("接收人:" + receiver.getVipName());
  425. User sender = (User) AccountUtils.getUserInfo(message.getSenderUu(),
  426. message.getSenderEnuu());
  427. if (sender != null && sender.getVipName() != null && sender.getSpaceName() != null) {
  428. LOGGER.info("发送人:" + sender.getVipName());
  429. boolean sendSuccess = false;
  430. // 发送邮件
  431. // if (message.getSmsType().contains(SMSType.MAIL)) {
  432. // sendMail(message, receiver);
  433. // }
  434. // 发送短息
  435. if (message.getSmsType().contains(SMSType.SM)) {
  436. LOGGER.info("开始推送短信");
  437. sendSuccess = sendSM(message, receiver, sender);
  438. }
  439. // 发送IM
  440. if (message.getSmsType().contains(SMSType.IM)) {
  441. LOGGER.info("开始推送IM");
  442. sendSuccess = sendIM(message, receiver, consumerApp);
  443. }
  444. // 推送成功
  445. if (sendSuccess) {
  446. pushSuccess(message, receiver, sender);
  447. }
  448. } else {
  449. LOGGER.info("发送人信息为空,uu:" + message.getSenderUu() + ",enuu:" + message.getSenderEnuu());
  450. }
  451. } else {
  452. LOGGER.info("接收人姓名为空,uu:" + message.getReceiverUu() + ",enuu:" + message.getReceiverEnuu());
  453. }
  454. } catch (Exception e) {
  455. message.setIsSent(Constant.ERROR);
  456. LOGGER.info(e.getMessage());
  457. }
  458. }
  459. messageDao.save(messages);
  460. }
  461. return resultMap;
  462. }
  463. /**
  464. * 发送邮件
  465. * @param message 消息
  466. * @param receiver 接收人
  467. */
  468. private void sendMail(Message message, User receiver) {
  469. if (message.getMailTemplate() != null && receiver.getEmail() != null) {
  470. HttpHeaders headers = new HttpHeaders();
  471. MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
  472. headers.setContentType(type);
  473. headers.add("Accept", MediaType.APPLICATION_JSON.toString());
  474. JSONObject object = new JSONObject();
  475. object.put("templateId", message.getMailTemplate());
  476. object.put("receiver", receiver.getEmail());
  477. object.put("params", message.getContent());
  478. // HttpEntity<String> formEntity = new HttpEntity<String>(, headers);
  479. try {
  480. ResponseWrap responseWrap = HttpUtil.doPost(MAIL_CONSOLE_HOST + MAIL_SEND_URL,
  481. FlexJsonUtil.toJson(object));
  482. } catch (Exception e) {
  483. e.printStackTrace();
  484. }
  485. }
  486. }
  487. /**
  488. * 发送短信
  489. * @param message 消息
  490. * @param receiver 接收人
  491. * @param sender 发送人
  492. * @return 推送成功状态
  493. */
  494. private boolean sendSM(Message message, User receiver, User sender) {
  495. if (!StringUtils.isEmpty(message.getSmTemplate()) && !StringUtils.isEmpty(receiver.getMobile())) {
  496. try {
  497. SmsMessage sms = new SmsMessage();
  498. List<Object> obj = new ArrayList<Object>();
  499. switch (message.getSmTemplate()) {
  500. // 公共询价单,普通短信
  501. case "e14d502a-25b8-40a0-8acf-5c61ae38d763" :
  502. obj.add(receiver.getVipName());
  503. obj.add(sender.getSpaceName());
  504. obj.add(message.getRemark());
  505. pushSm(sms, message, obj, receiver, sender);
  506. break;
  507. // 公共询价单,UU互联
  508. case "6e094dcf-f2c7-462a-9686-b2d32d684a78" :
  509. obj.add(message.getSourceId());
  510. obj.add(message.getReceiverEnuu());
  511. obj.add(receiver.getMobile());
  512. pushSm(sms, message, obj, receiver, sender);
  513. break;
  514. // 公共询价统计
  515. case "e6320a3c-89ac-4c77-a75f-62a727bce654" :
  516. obj.add(receiver.getSpaceName());
  517. obj.add(message.getRemark());
  518. pushSm(sms, message, obj, receiver, sender);
  519. default:
  520. break;
  521. }
  522. return true;
  523. } catch (Exception e) {
  524. LOGGER.info("短信推送异常:" + e.getMessage());
  525. e.printStackTrace();
  526. return false;
  527. }
  528. }
  529. return false;
  530. }
  531. /**
  532. * 调用短信接口
  533. * @param sms 短信model
  534. * @param message 消息
  535. * @param param 短信参数
  536. * @param receiver 接收人
  537. * @param sender 发送人
  538. */
  539. private void pushSm(SmsMessage sms, Message message, List<Object> param, User receiver, User sender) {
  540. sms.setParams(param);
  541. sms.setReceiver(receiver.getMobile());
  542. sms.setTemplateId(message.getSmTemplate());
  543. String response = com.uas.ps.message.util.HttpUtil
  544. .sendPost(messageUrl, FastjsonUtils.toJson(sms));
  545. LOGGER.info("短信推送成功:" + response);
  546. }
  547. /**
  548. * 设置推送送成功
  549. * @param message 消息
  550. * @param receiver 接收人
  551. * @param sender 发送人
  552. */
  553. private void pushSuccess(Message message, User receiver, User sender) {
  554. message.setIsSent(Constant.YES);
  555. message.setReceiver(FastjsonUtils.toJson(receiver));
  556. message.setSender(FastjsonUtils.toJson(sender));
  557. }
  558. /**
  559. * 发送IM
  560. * @param message 消息
  561. * @param receiver 接收人
  562. * @param consumerApp 消费app
  563. * @return 推送成功状态
  564. */
  565. private boolean sendIM(Message message, User receiver, String consumerApp) {
  566. Map<String, Object> params = new HashMap<>();
  567. if (!StringUtils.isEmpty(receiver.getImId())) {
  568. params.put("master", receiver.getSpaceName()); // 账套 公司名称
  569. params.put("userid", receiver.getImId()); // 推送目标用户
  570. params.put("title", message.getType() == null ? "" : message.getType()); // 推送标题
  571. params.put("content", message.getContent()); // 正文
  572. params.put("enUU", message.getReceiverEnuu()); // UU号
  573. params.put("masterId", null);
  574. params.put("url", String.format(PUBLIC_INQUIRY_IM_URL, message.getSourceId(),
  575. message.getReceiverEnuu(), receiver.getMobile())); // 跳转链接地址
  576. params.put("pageTitle", message.getType() == null ? "" : message.getType());
  577. params.put("platform", "B2B"); // 系统名称,ERP或B2B
  578. try {
  579. ResponseWrap res = HttpUtil.doPost(PROD_URL, params);
  580. if (res.isSuccess()) {
  581. LOGGER.info("IM推送成功");
  582. return true;
  583. } else {
  584. LOGGER.info("IM推送失败:" + res.getContent());
  585. }
  586. } catch (Exception e) {
  587. e.printStackTrace();
  588. return false;
  589. }
  590. }
  591. return false;
  592. }
  593. }