package com.qxgmat.service.extend; import com.nuliji.tools.exception.ParameterException; import com.nuliji.tools.exception.SystemException; import com.qxgmat.data.constants.enums.MessageCategory; import com.qxgmat.data.constants.enums.MessageMethod; import com.qxgmat.data.constants.enums.MessageType; import com.qxgmat.data.constants.enums.status.AskStatus; import com.qxgmat.data.dao.entity.*; import com.qxgmat.help.MailHelp; import com.qxgmat.help.WechatHelp; import com.qxgmat.service.inline.MessageTemplateService; import com.qxgmat.service.inline.UserMessageService; import org.apache.logging.log4j.util.Strings; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.text.SimpleDateFormat; import java.util.*; import java.util.stream.Collectors; @Service public class MessageExtendService { @Resource private MailHelp mailHelp; @Resource private WechatHelp wechatHelp; @Resource private UserMessageService userMessageService; @Resource private MessageTemplateService messageTemplateService; @Resource private ToolsService toolsService; @Value("${url.pc}") private String pcUrl; @Value("${url.h5}") private String h5Url; private void send(User user, MessageCategory category, Mapparams){ List templateList = messageTemplateService.listByCategory(category); for(MessageTemplate template : templateList){ MessageMethod messageMethod = MessageMethod.ValueOf(template.getMessageMethod()); String title = replaceBody(template.getTitle(), params); String content = replaceBody(template.getContent(), params); switch(messageMethod){ case EMAIL: sendEmail(params.getOrDefault("emails", user.getEmail()), title, content); break; case INSIDE: sendInside(user.getId(), title, content, template.getLink(), category); break; case WECHAT: sendWechat(user.getWechatOpenidWechat(), category, params); break; default: throw new ParameterException("消息发送方式错误"); } messageTemplateService.accumulation(template.getId(), 1); } } private UserMessage sendInside(Integer userId, String title, String content, String link, MessageCategory category){ // 根据模版创建 return userMessageService.add(UserMessage.builder() .userId(userId) .title(title) .content(content) .link(link) .type(MessageType.FromCategory(category).key) .isRead(0) .build()); } private void sendEmail(String emails, String title, String body){ mailHelp.sendBaseMail(emails, title, body); } private void sendWechat(String openId, MessageCategory category, Map params){ wechatHelp.sendMessage(openId, category, params); } public void sendCustom(User user, MessageTemplate template, Mapparams){ params.put("nickname", user.getNickname()); MessageMethod messageMethod = MessageMethod.ValueOf(template.getMessageMethod()); String title = replaceBody(template.getTitle(), params); String content = replaceBody(template.getContent(), params); switch(messageMethod){ case EMAIL: sendEmail(user.getEmail(), title, content); break; case INSIDE: sendInside(user.getId(), title, content, template.getLink(), MessageCategory.CUSTOM); break; default: throw new ParameterException("消息发送方式错误"); } } /** * 发送邀请邮件 * @param emails */ public void sendInviteEmail(User user, String[] emails, String code){ Map map = new HashMap<>(); map.put("emails", Strings.join(Arrays.stream(emails).collect(Collectors.toList()), ';')); map.put("code", code); map.put("nickname", user.getNickname()); map.put("url", pcUrl+"/id/"+code); send(user, MessageCategory.INVITED, map); } /** * 发送机经更新 */ public void sendTextbookUpdate(User user, TextbookLibrary textbookLibrary){ Map map = new HashMap<>(); send(user, MessageCategory.TEXTBOOK_UPDATE, map); } /** * 发送登录异常 */ public void sendLoginAbnormal(User user, UserAbnormal userAbnormal){ Map map = new HashMap<>(); map.put("nickname", user.getNickname()); map.put("ip", userAbnormal.getLoginIp()); map.put("city", userAbnormal.getLoginCity()); send(user, MessageCategory.LOGIN_ABNORMAL, map); } /** * 发送预习作业到期通知:6小时,去除夜间12-7点 * 课程名称:{courseTitle} * 作业名称:{assignTitle} */ public void sendPreviewNotice(User user, Course course, PreviewAssign previewAssign){ Map map = new HashMap<>(); map.put("courseTitle", course.getTitle()); map.put("title", previewAssign.getTitle()); send(user, MessageCategory.PREVIEW_NOTICE, map); } /** * 购买成功提醒 * 购买服务,课程名:{title} * 支付金额:{money} * 开通有效期:{expireTime} */ public void sendPayed(User user, UserOrder userOrder){ Map map = new HashMap<>(); map.put("money", userOrder.getMoney().toString()); send(user, MessageCategory.PAYED, map); } /** * 发送资料更新 * 资料名称:{title} * 更新时间:{updateTime} */ public void sendDataUpdate(User user, CourseData data, CourseDataHistory history){ Map map = new HashMap<>(); map.put("title", data.getTitle()); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String time = sdf.format(history.getTime()); map.put("time", time); send(user, MessageCategory.DATA_UPDATE, map); } /** * 提问回复 * 提问简介:{content} * 提问时间:{askTime} * 提问状态:{status}, 精选、隐藏 */ public void sendAskQuestion(User user,UserAskQuestion userAskQuestion){ Map map = new HashMap<>(); map.put("content", userAskQuestion.getContent()); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String time = sdf.format(new Date()); map.put("time", time); AskStatus status = AskStatus.ValueOf(userAskQuestion.getAnswerStatus()); if (status == AskStatus.ANSWER){ map.put("status", "已回答"); }else if (status == AskStatus.IGNORE){ map.put("status", "已隐藏"); } if(userAskQuestion.getShowStatus() > 0){ map.put("status", "精选"); } send(user, MessageCategory.ASK_QUESTION, map); } /** * 提问回复 * 提问简介:{content} * 提问时间:{askTime} * 提问状态:{status}, 精选、隐藏 */ public void sendAskCourse(User user, UserAskCourse userAskCourse){ Map map = new HashMap<>(); map.put("content", userAskCourse.getContent()); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String time = sdf.format(new Date()); map.put("time", time); AskStatus status = AskStatus.ValueOf(userAskCourse.getAnswerStatus()); if (status == AskStatus.ANSWER){ map.put("status", "已回答"); }else if (status == AskStatus.IGNORE){ map.put("status", "已隐藏"); } if(userAskCourse.getShowStatus() > 0){ map.put("status", "精选"); } send(user, MessageCategory.ASK_COURSE, map); } /** * 咨询回复:{answer} * 咨询板块:{channel} * 咨询简介:{content} */ public void sendFaqCallback(User user, Faq faq){ Map map = new HashMap<>(); map.put("answer", faq.getAnswer()); map.put("content", faq.getContent()); String channel = ""; switch(faq.getChannel()){ case "course-video": channel="在线课程"; break; case "course-vs": channel = "1v1课程"; break; case "course-package": channel = "课程套餐"; break; case "course_data": channel = "资料"; break; case "getready": channel = "GetReady"; break; case "examination-cat": channel = "模考Cat"; break; case "examination-base": channel = "模考"; break; case "textbook": channel = "数学机经"; break; case "library": channel = "换库表"; break; case "course-index": channel = "课堂"; break; case "course-video_index": channel= "在线课程"; break; case "course-vs_index": channel = "1v1课程"; break; case "course-package_index": channel="课程套餐"; break; } map.put("channel", channel); send(user, MessageCategory.FAQ_CALLBACK, map); } /** * 纠错对象:{object} * 错误内容:{content} * 应更正:{correct} * 处理结果:{status} */ public void sendFeedbackAnswer(User user, UserFeedbackError feedbackError){ Map map = new HashMap<>(); map.put("content", feedbackError.getOriginContent()); AskStatus status = AskStatus.ValueOf(feedbackError.getStatus()); if (status == AskStatus.ANSWER){ map.put("status", "已采纳"); }else if (status == AskStatus.IGNORE){ map.put("status", "已忽略"); }else if (status == AskStatus.NOHANDLE){ map.put("status", "无需处理"); } map.put("correct", feedbackError.getContent()); map.put("object", feedbackError.getTitle()); send(user, MessageCategory.FEEDBACK_CALLBACK, map); } /** * 注册成功通知 * @param user */ public void sendRegister(User user){ Map map = new HashMap<>(); map.put("mobile", user.getMobile()); send(user, MessageCategory.REGISTER, map); } /** * 邮箱变更 * @param user */ public void sendEmailChange(User user){ Map map = new HashMap<>(); map.put("nickname", user.getNickname()); map.put("email", user.getEmail()); send(user, MessageCategory.EMAIL_CHANGE, map); } private String replaceBody(String body, Map params){ for(String key :params.keySet()){ body = body.replaceAll("{"+key+"}", params.get(key)); } return body; } }