123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329 |
- 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, Map<String, String>params){
- List<MessageTemplate> 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<String, String> params){
- wechatHelp.sendMessage(openId, category, params);
- }
- public void sendCustom(User user, MessageTemplate template, Map<String, String>params){
- 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<String, String> 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<String, String> map = new HashMap<>();
- send(user, MessageCategory.TEXTBOOK_UPDATE, map);
- }
- /**
- * 发送登录异常
- */
- public void sendLoginAbnormal(User user, UserAbnormal userAbnormal){
- Map<String, String> 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<String, String> 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<String, String> 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<String, String> 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<String, String> 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<String, String> 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<String, String> 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<String, String> 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<String, String> map = new HashMap<>();
- map.put("mobile", user.getMobile());
- send(user, MessageCategory.REGISTER, map);
- }
- /**
- * 邮箱变更
- * @param user
- */
- public void sendEmailChange(User user){
- Map<String, String> 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<String, String> params){
- for(String key :params.keySet()){
- body = body.replaceAll("{"+key+"}", params.get(key));
- }
- return body;
- }
- }
|