MessageExtendService.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. package com.qxgmat.service.extend;
  2. import com.nuliji.tools.exception.ParameterException;
  3. import com.qxgmat.data.constants.enums.MessageCategory;
  4. import com.qxgmat.data.constants.enums.MessageMethod;
  5. import com.qxgmat.data.constants.enums.MessageType;
  6. import com.qxgmat.data.constants.enums.status.AnswerStatus;
  7. import com.qxgmat.data.dao.entity.*;
  8. import com.qxgmat.help.MailHelp;
  9. import com.qxgmat.help.SmsHelp;
  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 SmsHelp smsHelp;
  28. @Resource
  29. private UserMessageService userMessageService;
  30. @Resource
  31. private MessageTemplateService messageTemplateService;
  32. @Resource
  33. private ToolsService toolsService;
  34. @Value("${url.pc}")
  35. private String pcUrl;
  36. @Value("${url.h5}")
  37. private String h5Url;
  38. private void send(User user, MessageCategory category, Map<String, String>params){
  39. List<MessageTemplate> templateList = messageTemplateService.listByCategory(category);
  40. for(MessageTemplate template : templateList){
  41. MessageMethod messageMethod = MessageMethod.ValueOf(template.getMessageMethod());
  42. String title = replaceBody(template.getTitle(), params);
  43. String content = replaceBody(template.getContent(), params);
  44. switch(messageMethod){
  45. case EMAIL:
  46. sendEmail(params.getOrDefault("emails", user.getEmail()), title, content);
  47. break;
  48. case INSIDE:
  49. sendInside(user.getId(), title, content, template.getLink(), category);
  50. break;
  51. case WECHAT:
  52. sendWechat(user.getWechatOpenidWechat(), category, params);
  53. break;
  54. case SMS:
  55. sendSms(user.getArea(), user.getMobile(), category, params);
  56. break;
  57. default:
  58. throw new ParameterException("消息发送方式错误");
  59. }
  60. messageTemplateService.accumulation(template.getId(), 1);
  61. }
  62. }
  63. private UserMessage sendInside(Integer userId, String title, String content, String link, MessageCategory category){
  64. // 根据模版创建
  65. return userMessageService.add(UserMessage.builder()
  66. .userId(userId)
  67. .title(title)
  68. .content(content)
  69. .link(link)
  70. .type(MessageType.FromCategory(category).key)
  71. .isRead(0)
  72. .build());
  73. }
  74. private void sendEmail(String emails, String title, String body){
  75. mailHelp.sendBaseMail(emails, title, body);
  76. }
  77. private void sendWechat(String openId, MessageCategory category, Map<String, String> params){
  78. wechatHelp.sendMessage(openId, category, params);
  79. }
  80. private void sendSms(String area, String mobile, MessageCategory category, Map<String, String> params){
  81. String[] template;
  82. Map<String, String> smsParams = new HashMap<>();
  83. SimpleDateFormat sdf;
  84. String time;
  85. switch(category){
  86. case LOGIN_ABNORMAL:
  87. template = smsHelp.ABNORMAL_TEMPLATE;
  88. // time: yyyy年MM月dd日hh:mm:ss
  89. sdf = new SimpleDateFormat("yyyy年MM月dd hh:mm:ss");
  90. smsParams.put("time", sdf.format(params.get("time")));
  91. break;
  92. case TEXTBOOK_LIBRARY:
  93. template = smsHelp.LIBRARY_TEMPLATE;
  94. // 最新换库时间是%zx_date%日
  95. // 上次换库时间是%sc_date%日
  96. // 间隔%jgts%天,库长%kcts%天
  97. // 获取机经: %jjdz%
  98. // 也可搜索微信订阅号“%dyh%”查阅机经。
  99. smsParams.put("dyh", "");
  100. smsParams.put("jjdz", pcUrl+"/textbook");
  101. break;
  102. case COURSE_USE_EXPIRE:
  103. template = smsHelp.COURSE_USE_EXPIRE_TEMPLATE;
  104. // yhnc: 用户昵称
  105. // %kcmc%: 课程名称
  106. // %date%: 格式为:yyyy年MM月dd日
  107. sdf = new SimpleDateFormat("yyyy年MM月dd");
  108. smsParams.put("date", sdf.format(params.get("time")));
  109. smsParams.put("yhnc", params.get("nickname"));
  110. smsParams.put("kcmc", params.get("title"));
  111. break;
  112. case COURSE_EXPIRE:
  113. case TEXTBOOK_EXPIRE:
  114. case QX_CAT_EXPIRE:
  115. template = smsHelp.EXPIRE_TEMPLATE;
  116. // yhnc: 用户昵称
  117. // %date%: 格式为:yyyy年MM月dd日
  118. // wktfw: 未开通的服务名称
  119. sdf = new SimpleDateFormat("yyyy年MM月dd");
  120. smsParams.put("date", sdf.format(params.get("time")));
  121. smsParams.put("yhnc", params.get("nickname"));
  122. smsParams.put("wktfw", params.get("title"));
  123. break;
  124. default:
  125. // 其余没有模版id,不发送
  126. return;
  127. }
  128. smsHelp.send(area, mobile, template, smsParams);
  129. }
  130. public void refreshMessage(List<UserMessage> messageList){
  131. }
  132. public void sendCustom(User user, MessageTemplate template, Map<String, String>params){
  133. params.put("nickname", user.getNickname());
  134. MessageMethod messageMethod = MessageMethod.ValueOf(template.getMessageMethod());
  135. String title = replaceBody(template.getTitle(), params);
  136. String content = replaceBody(template.getContent(), params);
  137. switch(messageMethod){
  138. case EMAIL:
  139. sendEmail(user.getEmail(), title, content);
  140. break;
  141. case INSIDE:
  142. sendInside(user.getId(), title, content, template.getLink(), MessageCategory.CUSTOM);
  143. break;
  144. default:
  145. throw new ParameterException("消息发送方式错误");
  146. }
  147. }
  148. /**
  149. * 发送邀请邮件
  150. * @param emails
  151. */
  152. public void sendInviteEmail(User user, String[] emails, String code){
  153. Map<String, String> map = new HashMap<>();
  154. map.put("emails", Strings.join(Arrays.stream(emails).collect(Collectors.toList()), ';'));
  155. map.put("code", code);
  156. map.put("nickname", user.getNickname());
  157. map.put("url", pcUrl+"/id/"+code);
  158. send(user, MessageCategory.INVITED, map);
  159. }
  160. /**
  161. * 发送机经更新
  162. */
  163. public void sendTextbookUpdate(User user, TextbookLibrary textbookLibrary){
  164. Map<String, String> map = new HashMap<>();
  165. send(user, MessageCategory.TEXTBOOK_LIBRARY, map);
  166. }
  167. /**
  168. * 发送登录异常
  169. */
  170. public void sendLoginAbnormal(User user, UserAbnormal userAbnormal){
  171. Map<String, String> map = new HashMap<>();
  172. map.put("nickname", user.getNickname());
  173. map.put("ip", userAbnormal.getLoginIp());
  174. map.put("city", userAbnormal.getLoginCity());
  175. send(user, MessageCategory.LOGIN_ABNORMAL, map);
  176. }
  177. /**
  178. * 发送预习作业到期通知:6小时,去除夜间12-7点
  179. * 课程名称:{courseTitle}
  180. * 作业名称:{assignTitle}
  181. */
  182. public void sendPreviewNotice(User user, Course course, PreviewAssign previewAssign){
  183. Map<String, String> map = new HashMap<>();
  184. map.put("courseTitle", course.getTitle());
  185. map.put("title", previewAssign.getTitle());
  186. send(user, MessageCategory.PREVIEW_NOTICE, map);
  187. }
  188. /**
  189. * 购买成功提醒
  190. * 购买服务,课程名:{title}
  191. * 支付金额:{money}
  192. * 开通有效期:{expireTime}
  193. */
  194. public void sendPayed(User user, UserOrder userOrder){
  195. Map<String, String> map = new HashMap<>();
  196. map.put("money", userOrder.getMoney().toString());
  197. send(user, MessageCategory.PAYED, map);
  198. }
  199. /**
  200. * 发送资料更新
  201. * 资料名称:{title}
  202. * 更新时间:{updateTime}
  203. */
  204. public void sendDataUpdate(User user, CourseData data, CourseDataHistory history){
  205. Map<String, String> map = new HashMap<>();
  206. map.put("title", data.getTitle());
  207. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  208. String time = sdf.format(history.getTime());
  209. map.put("time", time);
  210. send(user, MessageCategory.DATA_UPDATE, map);
  211. }
  212. /**
  213. * 提问回复
  214. * 提问简介:{content}
  215. * 提问时间:{askTime}
  216. * 提问状态:{status}, 精选、隐藏
  217. */
  218. public void sendAskQuestion(User user,UserAskQuestion userAskQuestion){
  219. Map<String, String> map = new HashMap<>();
  220. map.put("content", userAskQuestion.getContent());
  221. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  222. String time = sdf.format(new Date());
  223. map.put("time", time);
  224. AnswerStatus status = AnswerStatus.ValueOf(userAskQuestion.getAnswerStatus());
  225. if (status == AnswerStatus.ANSWER){
  226. map.put("status", "已回答");
  227. }else if (status == AnswerStatus.IGNORE){
  228. map.put("status", "已隐藏");
  229. }
  230. if(userAskQuestion.getShowStatus() > 0){
  231. map.put("status", "精选");
  232. }
  233. send(user, MessageCategory.ASK_QUESTION, map);
  234. }
  235. /**
  236. * 提问回复
  237. * 提问简介:{content}
  238. * 提问时间:{askTime}
  239. * 提问状态:{status}, 精选、隐藏
  240. */
  241. public void sendAskCourse(User user, UserAskCourse userAskCourse){
  242. Map<String, String> map = new HashMap<>();
  243. map.put("content", userAskCourse.getContent());
  244. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  245. String time = sdf.format(new Date());
  246. map.put("time", time);
  247. AnswerStatus status = AnswerStatus.ValueOf(userAskCourse.getAnswerStatus());
  248. if (status == AnswerStatus.ANSWER){
  249. map.put("status", "已回答");
  250. }else if (status == AnswerStatus.IGNORE){
  251. map.put("status", "已隐藏");
  252. }
  253. if(userAskCourse.getShowStatus() > 0){
  254. map.put("status", "精选");
  255. }
  256. send(user, MessageCategory.ASK_COURSE, map);
  257. }
  258. /**
  259. * 咨询回复:{answer}
  260. * 咨询板块:{channel}
  261. * 咨询简介:{content}
  262. */
  263. public void sendFaqCallback(User user, Faq faq){
  264. Map<String, String> map = new HashMap<>();
  265. map.put("answer", faq.getAnswer());
  266. map.put("content", faq.getContent());
  267. String channel = "";
  268. switch(faq.getChannel()){
  269. case "course-video":
  270. channel="在线课程";
  271. break;
  272. case "course-vs":
  273. channel = "1v1课程";
  274. break;
  275. case "course-package":
  276. channel = "课程套餐";
  277. break;
  278. case "course_data":
  279. channel = "资料";
  280. break;
  281. case "getready":
  282. channel = "GetReady";
  283. break;
  284. case "examination-cat":
  285. channel = "模考Cat";
  286. break;
  287. case "examination-base":
  288. channel = "模考";
  289. break;
  290. case "textbook":
  291. channel = "数学机经";
  292. break;
  293. case "library":
  294. channel = "换库表";
  295. break;
  296. case "course-index":
  297. channel = "课堂";
  298. break;
  299. case "course-video_index":
  300. channel= "在线课程";
  301. break;
  302. case "course-vs_index":
  303. channel = "1v1课程";
  304. break;
  305. case "course-package_index":
  306. channel="课程套餐";
  307. break;
  308. }
  309. map.put("channel", channel);
  310. send(user, MessageCategory.FAQ_CALLBACK, map);
  311. }
  312. /**
  313. * 纠错对象:{object}
  314. * 错误内容:{content}
  315. * 应更正:{correct}
  316. * 处理结果:{status}
  317. */
  318. public void sendFeedbackAnswer(User user, UserFeedbackError feedbackError){
  319. Map<String, String> map = new HashMap<>();
  320. map.put("content", feedbackError.getOriginContent());
  321. AnswerStatus status = AnswerStatus.ValueOf(feedbackError.getStatus());
  322. if (status == AnswerStatus.ANSWER){
  323. map.put("status", "已采纳");
  324. }else if (status == AnswerStatus.IGNORE){
  325. map.put("status", "已忽略");
  326. }else if (status == AnswerStatus.NOHANDLE){
  327. map.put("status", "无需处理");
  328. }
  329. map.put("correct", feedbackError.getContent());
  330. map.put("object", feedbackError.getTitle());
  331. send(user, MessageCategory.FEEDBACK_CALLBACK, map);
  332. }
  333. /**
  334. * 注册成功通知
  335. * @param user
  336. */
  337. public void sendRegister(User user){
  338. Map<String, String> map = new HashMap<>();
  339. map.put("mobile", user.getMobile());
  340. send(user, MessageCategory.REGISTER, map);
  341. }
  342. /**
  343. * 邮箱绑定
  344. * @param user
  345. */
  346. public void sendEmailBind(User user){
  347. Map<String, String> map = new HashMap<>();
  348. map.put("nickname", user.getNickname());
  349. map.put("email", user.getEmail());
  350. send(user, MessageCategory.EMAIL_CHANGE, map);
  351. }
  352. /**
  353. * 邮箱变更
  354. * @param user
  355. */
  356. public void sendEmailChange(User user, String email){
  357. if(email != null && !email.isEmpty()){
  358. Map<String, String> unbindMap = new HashMap<>();
  359. unbindMap.put("nickname", user.getNickname());
  360. unbindMap.put("emails", email);
  361. send(user, MessageCategory.EMAIL_UNNBIND, unbindMap);
  362. }
  363. Map<String, String> changeMap = new HashMap<>();
  364. changeMap.put("nickname", user.getNickname());
  365. send(user, MessageCategory.EMAIL_CHANGE, changeMap);
  366. }
  367. private String replaceBody(String body, Map<String, String> params){
  368. if (body == null) return body;
  369. for(String key :params.keySet()){
  370. body = body.replaceAll("\\{"+key+"}", params.getOrDefault(key, ""));
  371. }
  372. return body;
  373. }
  374. }