MessageExtendService.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. package com.qxgmat.service.extend;
  2. import com.nuliji.tools.exception.ParameterException;
  3. import com.nuliji.tools.exception.SystemException;
  4. import com.qxgmat.data.constants.enums.MessageCategory;
  5. import com.qxgmat.data.constants.enums.MessageMethod;
  6. import com.qxgmat.data.constants.enums.MessageType;
  7. import com.qxgmat.data.constants.enums.status.AskStatus;
  8. import com.qxgmat.data.dao.entity.*;
  9. import com.qxgmat.help.MailHelp;
  10. import com.qxgmat.help.WechatHelp;
  11. import com.qxgmat.service.inline.MessageTemplateService;
  12. import com.qxgmat.service.inline.UserMessageService;
  13. import org.apache.logging.log4j.util.Strings;
  14. import org.springframework.beans.factory.annotation.Value;
  15. import org.springframework.stereotype.Service;
  16. import javax.annotation.Resource;
  17. import java.text.SimpleDateFormat;
  18. import java.util.*;
  19. import java.util.stream.Collectors;
  20. @Service
  21. public class MessageExtendService {
  22. @Resource
  23. private MailHelp mailHelp;
  24. @Resource
  25. private WechatHelp wechatHelp;
  26. @Resource
  27. private UserMessageService userMessageService;
  28. @Resource
  29. private MessageTemplateService messageTemplateService;
  30. @Resource
  31. private ToolsService toolsService;
  32. @Value("${url.pc}")
  33. private String pcUrl;
  34. @Value("${url.h5}")
  35. private String h5Url;
  36. private void send(User user, MessageCategory category, Map<String, String>params){
  37. List<MessageTemplate> templateList = messageTemplateService.listByCategory(category);
  38. for(MessageTemplate template : templateList){
  39. MessageMethod messageMethod = MessageMethod.ValueOf(template.getMessageMethod());
  40. String title = replaceBody(template.getTitle(), params);
  41. String content = replaceBody(template.getContent(), params);
  42. switch(messageMethod){
  43. case EMAIL:
  44. sendEmail(params.getOrDefault("emails", user.getEmail()), title, content);
  45. break;
  46. case INSIDE:
  47. sendInside(user.getId(), title, content, template.getLink(), category);
  48. break;
  49. case WECHAT:
  50. sendWechat(user.getWechatOpenidWechat(), category, params);
  51. break;
  52. default:
  53. throw new ParameterException("消息发送方式错误");
  54. }
  55. messageTemplateService.accumulation(template.getId(), 1);
  56. }
  57. }
  58. private UserMessage sendInside(Integer userId, String title, String content, String link, MessageCategory category){
  59. // 根据模版创建
  60. return userMessageService.add(UserMessage.builder()
  61. .userId(userId)
  62. .title(title)
  63. .content(content)
  64. .link(link)
  65. .type(MessageType.FromCategory(category).key)
  66. .isRead(0)
  67. .build());
  68. }
  69. private void sendEmail(String emails, String title, String body){
  70. mailHelp.sendBaseMail(emails, title, body);
  71. }
  72. private void sendWechat(String openId, MessageCategory category, Map<String, String> params){
  73. wechatHelp.sendMessage(openId, category, params);
  74. }
  75. public void sendCustom(User user, MessageTemplate template, Map<String, String>params){
  76. params.put("nickname", user.getNickname());
  77. MessageMethod messageMethod = MessageMethod.ValueOf(template.getMessageMethod());
  78. String title = replaceBody(template.getTitle(), params);
  79. String content = replaceBody(template.getContent(), params);
  80. switch(messageMethod){
  81. case EMAIL:
  82. sendEmail(user.getEmail(), title, content);
  83. break;
  84. case INSIDE:
  85. sendInside(user.getId(), title, content, template.getLink(), MessageCategory.CUSTOM);
  86. break;
  87. default:
  88. throw new ParameterException("消息发送方式错误");
  89. }
  90. }
  91. /**
  92. * 发送邀请邮件
  93. * @param emails
  94. */
  95. public void sendInviteEmail(User user, String[] emails, String code){
  96. Map<String, String> map = new HashMap<>();
  97. map.put("emails", Strings.join(Arrays.stream(emails).collect(Collectors.toList()), ';'));
  98. map.put("code", code);
  99. map.put("nickname", user.getNickname());
  100. map.put("url", pcUrl+"/id/"+code);
  101. send(user, MessageCategory.INVITED, map);
  102. }
  103. /**
  104. * 发送机经更新
  105. */
  106. public void sendTextbookUpdate(User user, TextbookLibrary textbookLibrary){
  107. Map<String, String> map = new HashMap<>();
  108. send(user, MessageCategory.TEXTBOOK_UPDATE, map);
  109. }
  110. /**
  111. * 发送登录异常
  112. */
  113. public void sendLoginAbnormal(User user, UserAbnormal userAbnormal){
  114. Map<String, String> map = new HashMap<>();
  115. map.put("nickname", user.getNickname());
  116. map.put("ip", userAbnormal.getLoginIp());
  117. map.put("city", userAbnormal.getLoginCity());
  118. send(user, MessageCategory.LOGIN_ABNORMAL, map);
  119. }
  120. /**
  121. * 发送预习作业到期通知:6小时,去除夜间12-7点
  122. * 课程名称:{courseTitle}
  123. * 作业名称:{assignTitle}
  124. */
  125. public void sendPreviewNotice(User user, Course course, PreviewAssign previewAssign){
  126. Map<String, String> map = new HashMap<>();
  127. map.put("courseTitle", course.getTitle());
  128. map.put("title", previewAssign.getTitle());
  129. send(user, MessageCategory.PREVIEW_NOTICE, map);
  130. }
  131. /**
  132. * 购买成功提醒
  133. * 购买服务,课程名:{title}
  134. * 支付金额:{money}
  135. * 开通有效期:{expireTime}
  136. */
  137. public void sendPayed(User user, UserOrder userOrder){
  138. Map<String, String> map = new HashMap<>();
  139. map.put("money", userOrder.getMoney().toString());
  140. send(user, MessageCategory.PAYED, map);
  141. }
  142. /**
  143. * 发送资料更新
  144. * 资料名称:{title}
  145. * 更新时间:{updateTime}
  146. */
  147. public void sendDataUpdate(User user, CourseData data, CourseDataHistory history){
  148. Map<String, String> map = new HashMap<>();
  149. map.put("title", data.getTitle());
  150. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  151. String time = sdf.format(history.getTime());
  152. map.put("time", time);
  153. send(user, MessageCategory.DATA_UPDATE, map);
  154. }
  155. /**
  156. * 提问回复
  157. * 提问简介:{content}
  158. * 提问时间:{askTime}
  159. * 提问状态:{status}, 精选、隐藏
  160. */
  161. public void sendAskQuestion(User user,UserAskQuestion userAskQuestion){
  162. Map<String, String> map = new HashMap<>();
  163. map.put("content", userAskQuestion.getContent());
  164. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  165. String time = sdf.format(new Date());
  166. map.put("time", time);
  167. AskStatus status = AskStatus.ValueOf(userAskQuestion.getAnswerStatus());
  168. if (status == AskStatus.ANSWER){
  169. map.put("status", "已回答");
  170. }else if (status == AskStatus.IGNORE){
  171. map.put("status", "已隐藏");
  172. }
  173. if(userAskQuestion.getShowStatus() > 0){
  174. map.put("status", "精选");
  175. }
  176. send(user, MessageCategory.ASK_QUESTION, map);
  177. }
  178. /**
  179. * 提问回复
  180. * 提问简介:{content}
  181. * 提问时间:{askTime}
  182. * 提问状态:{status}, 精选、隐藏
  183. */
  184. public void sendAskCourse(User user, UserAskCourse userAskCourse){
  185. Map<String, String> map = new HashMap<>();
  186. map.put("content", userAskCourse.getContent());
  187. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  188. String time = sdf.format(new Date());
  189. map.put("time", time);
  190. AskStatus status = AskStatus.ValueOf(userAskCourse.getAnswerStatus());
  191. if (status == AskStatus.ANSWER){
  192. map.put("status", "已回答");
  193. }else if (status == AskStatus.IGNORE){
  194. map.put("status", "已隐藏");
  195. }
  196. if(userAskCourse.getShowStatus() > 0){
  197. map.put("status", "精选");
  198. }
  199. send(user, MessageCategory.ASK_COURSE, map);
  200. }
  201. /**
  202. * 咨询回复:{answer}
  203. * 咨询板块:{channel}
  204. * 咨询简介:{content}
  205. */
  206. public void sendFaqCallback(User user, Faq faq){
  207. Map<String, String> map = new HashMap<>();
  208. map.put("answer", faq.getAnswer());
  209. map.put("content", faq.getContent());
  210. String channel = "";
  211. switch(faq.getChannel()){
  212. case "course-video":
  213. channel="在线课程";
  214. break;
  215. case "course-vs":
  216. channel = "1v1课程";
  217. break;
  218. case "course-package":
  219. channel = "课程套餐";
  220. break;
  221. case "course_data":
  222. channel = "资料";
  223. break;
  224. case "getready":
  225. channel = "GetReady";
  226. break;
  227. case "examination-cat":
  228. channel = "模考Cat";
  229. break;
  230. case "examination-base":
  231. channel = "模考";
  232. break;
  233. case "textbook":
  234. channel = "数学机经";
  235. break;
  236. case "library":
  237. channel = "换库表";
  238. break;
  239. case "course-index":
  240. channel = "课堂";
  241. break;
  242. case "course-video_index":
  243. channel= "在线课程";
  244. break;
  245. case "course-vs_index":
  246. channel = "1v1课程";
  247. break;
  248. case "course-package_index":
  249. channel="课程套餐";
  250. break;
  251. }
  252. map.put("channel", channel);
  253. send(user, MessageCategory.FAQ_CALLBACK, map);
  254. }
  255. /**
  256. * 纠错对象:{object}
  257. * 错误内容:{content}
  258. * 应更正:{correct}
  259. * 处理结果:{status}
  260. */
  261. public void sendFeedbackAnswer(User user, UserFeedbackError feedbackError){
  262. Map<String, String> map = new HashMap<>();
  263. map.put("content", feedbackError.getOriginContent());
  264. AskStatus status = AskStatus.ValueOf(feedbackError.getStatus());
  265. if (status == AskStatus.ANSWER){
  266. map.put("status", "已采纳");
  267. }else if (status == AskStatus.IGNORE){
  268. map.put("status", "已忽略");
  269. }else if (status == AskStatus.NOHANDLE){
  270. map.put("status", "无需处理");
  271. }
  272. map.put("correct", feedbackError.getContent());
  273. map.put("object", feedbackError.getTitle());
  274. send(user, MessageCategory.FEEDBACK_CALLBACK, map);
  275. }
  276. /**
  277. * 注册成功通知
  278. * @param user
  279. */
  280. public void sendRegister(User user){
  281. Map<String, String> map = new HashMap<>();
  282. map.put("mobile", user.getMobile());
  283. send(user, MessageCategory.REGISTER, map);
  284. }
  285. /**
  286. * 邮箱变更
  287. * @param user
  288. */
  289. public void sendEmailChange(User user){
  290. Map<String, String> map = new HashMap<>();
  291. map.put("nickname", user.getNickname());
  292. map.put("email", user.getEmail());
  293. send(user, MessageCategory.EMAIL_CHANGE, map);
  294. }
  295. private String replaceBody(String body, Map<String, String> params){
  296. for(String key :params.keySet()){
  297. body = body.replaceAll("{"+key+"}", params.get(key));
  298. }
  299. return body;
  300. }
  301. }