提交 b8f5ace2 编写于 作者: zhouweidong's avatar zhouweidong

消息版本检查

上级 e81fd98e
...@@ -2,19 +2,11 @@ package cn.ibizlab.core.extensions.domain; ...@@ -2,19 +2,11 @@ package cn.ibizlab.core.extensions.domain;
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONArray;
import lombok.Data; import lombok.Data;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull; import javax.validation.constraints.NotNull;
@Data @Data
public class Template { public class Template {
@NotBlank(message="系统标识不能为空")
private String system;
@NotNull(message="模板不能为空") @NotNull(message="模板不能为空")
private JSONArray template; private JSONArray template;
@NotNull(message="模板类型不能为空")
private int templtypes;
} }
...@@ -9,6 +9,7 @@ import cn.ibizlab.core.notify.service.IMsgOpenAccessService; ...@@ -9,6 +9,7 @@ import cn.ibizlab.core.notify.service.IMsgOpenAccessService;
import cn.ibizlab.core.notify.service.IMsgTemplateService; import cn.ibizlab.core.notify.service.IMsgTemplateService;
import cn.ibizlab.core.notify.service.IMsgUserAccountService; import cn.ibizlab.core.notify.service.IMsgUserAccountService;
import cn.ibizlab.util.errors.BadRequestAlertException; import cn.ibizlab.util.errors.BadRequestAlertException;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest; import com.aliyuncs.CommonRequest;
...@@ -31,6 +32,7 @@ import org.springframework.context.annotation.Lazy; ...@@ -31,6 +32,7 @@ import org.springframework.context.annotation.Lazy;
import org.springframework.http.HttpEntity; import org.springframework.http.HttpEntity;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import java.sql.Timestamp; import java.sql.Timestamp;
...@@ -72,10 +74,11 @@ public class NotifyCoreService { ...@@ -72,10 +74,11 @@ public class NotifyCoreService {
aliyun, aliyun,
wechat, wechat,
} }
/** /**
* 平台消息类型代码表 * 平台消息类型代码表
*/ */
private static Map<Integer,String> msgTypes = new HashMap(){{ private static Map<Integer,String> templateType = new HashMap(){{
put(2,"aliyun-email"); put(2,"aliyun-email");
put(4,"aliyun-sms"); put(4,"aliyun-sms");
put(32,"wechat-msg"); put(32,"wechat-msg");
...@@ -314,39 +317,89 @@ public class NotifyCoreService { ...@@ -314,39 +317,89 @@ public class NotifyCoreService {
* @return * @return
*/ */
public boolean createMsgTemplate(Template template){ public boolean createMsgTemplate(Template template){
String system=template.getSystem().toLowerCase();
JSONArray templates = template.getTemplate(); JSONArray templates = template.getTemplate();
for(int a=0;a<templates.size();a++){ for(int a=0;a<templates.size();a++){
List<MsgOpenAccess> openAccesses=new ArrayList<>(); List<MsgOpenAccess> openAccesses=new ArrayList<>();
List<MsgTemplate> msgTemplates=new ArrayList<>(); List<MsgTemplate> msgTemplates=new ArrayList<>();
for(String templateType : getMsgType(template.getTempltypes())){ MsgTemplate msgTempl =JSONObject.toJavaObject(templates.getJSONObject(a),MsgTemplate.class);
//创建消息平台 boolean isExist=checkMsgTemplate(msgTempl);
MsgOpenAccess openAccess=openAccessService.getById(system+templateType); if(!isExist){
if(openAccess==null) //rt预置消息类型
{ Collection<String> templTypes = templateType.values();
openAccess=new MsgOpenAccess(); //创建开放平台
openAccess.setId(system+"-"+templateType); for(String openAccessType: getOpenAccessType(templTypes)){
openAccess.setName(system+"-"+templateType); String openAccessId=String.format("%s-%s",msgTempl.getTid(),openAccessType);
openAccess.setDisabled(0); MsgOpenAccess openAccess=openAccessService.getById(openAccessId);
openAccess.setOpenType(templateType.contains("-")?templateType.substring(0,templateType.indexOf("-")):templateType); if(openAccess==null)
openAccesses.add(openAccess); {
openAccess=new MsgOpenAccess();
openAccess.setId(openAccessId);
openAccess.setName(openAccessType);
openAccess.setDisabled(0);
openAccess.setOpenType(openAccessType);
openAccesses.add(openAccess);
}
} }
//创建消息模板 //创建消息模板
MsgTemplate msgTemplate =JSONObject.toJavaObject(templates.getJSONObject(a),MsgTemplate.class); for(String templateType : templTypes){
msgTemplate.setAccessId(openAccess.getId()); MsgTemplate msgTemplate=new MsgTemplate();
msgTemplate.setOpenType(templateType.contains("-")?templateType.substring(0,templateType.indexOf("-")):templateType); String openAccessType=templateType.contains("-")?templateType.substring(0,templateType.indexOf("-")):templateType;
msgTemplate.setTemplateType(templateType); msgTemplate.setAccessId(String.format("%s-%s",msgTempl.getTid(),openAccessType));
msgTemplate.setTid(msgTemplate.getTid()+"-"+templateType); msgTemplate.setOpenType(openAccessType);
msgTemplates.add(msgTemplate); msgTemplate.setTemplateType(templateType);
msgTemplate.setTid(String.format("%s-%s",msgTempl.getTid(),templateType));
msgTemplate.setTemplateName(msgTempl.getTemplateName());
msgTemplate.setContent(msgTempl.getContent());
msgTemplates.add(msgTemplate);
}
if(openAccesses.size()>0)
openAccessService.createBatch(openAccesses);
if(msgTemplates.size()>0)
templateService.createBatch(msgTemplates);
} }
if(openAccesses.size()>0) }
openAccessService.saveOrUpdateBatch(openAccesses);
if(msgTemplates.size()>0)
templateService.saveOrUpdateBatch(msgTemplates);
}
return true; return true;
} }
/**
* 检查模板是否存在
* @param msgTemplate
* @return
*/
private boolean checkMsgTemplate(MsgTemplate msgTemplate) {
boolean isExist=false;
if(!ObjectUtils.isEmpty(msgTemplate.getTid())){
QueryWrapper<MsgOpenAccess> qw=new QueryWrapper();
qw.likeRight("accessid",msgTemplate.getTid());
if(openAccessService.count(qw)>0){
isExist=true;
log.info(String.format("消息模板[%s]已存在,忽略发布!",msgTemplate.getTemplateName()));
}
}
return isExist;
}
/**
* 获取开放平台
* @param templTypes
* @return
*/
private List<String> getOpenAccessType(Collection<String> templTypes){
List openAccess=new ArrayList();
if(templTypes.size()==0)
return openAccess;
for(String template:templTypes){
if(template.contains("-")){
String openAccessType = template.substring(0,template.indexOf("-"));
if(!openAccess.contains(openAccessType)){
openAccess.add(openAccessType);
}
}
}
return openAccess;
}
/** /**
* 发送消息 * 发送消息
* @param msg * @param msg
...@@ -364,7 +417,7 @@ public class NotifyCoreService { ...@@ -364,7 +417,7 @@ public class NotifyCoreService {
else{ else{
switch(msgType){ switch(msgType){
case "aliyun-sms": case "aliyun-sms":
sendAliSms(msgTemplate.getTid(),userIds,templParams); sendAliSms(msgTemplate.getTid(),userIds,templParams==null?null:JSONObject.parseObject(JSON.toJSONString(templParams)));
break; break;
case "aliyun-email": case "aliyun-email":
sendAliEMail(msgTemplate.getTid(),userIds); sendAliEMail(msgTemplate.getTid(),userIds);
...@@ -373,7 +426,7 @@ public class NotifyCoreService { ...@@ -373,7 +426,7 @@ public class NotifyCoreService {
sendDingTalkMsg(msgTemplate.getTid(),userIds); sendDingTalkMsg(msgTemplate.getTid(),userIds);
break; break;
case "wechat-msg": case "wechat-msg":
sendWeChatMsg(msgTemplate.getTid(),userIds,templParams); sendWeChatMsg(msgTemplate.getTid(),userIds,getWechatMsgTemplParams(templParams));
break; break;
default: default:
throw new BadRequestAlertException(String.format("发送消息失败,尚未提供[%s]类型对应的消息服务",msgType),"",""); throw new BadRequestAlertException(String.format("发送消息失败,尚未提供[%s]类型对应的消息服务",msgType),"","");
...@@ -383,6 +436,25 @@ public class NotifyCoreService { ...@@ -383,6 +436,25 @@ public class NotifyCoreService {
return true; return true;
} }
/**
* 获取微信消息模板参数
* @param templParams
* @return
*/
private JSONObject getWechatMsgTemplParams(Map<String, Object> templParams){
if(templParams==null)
return null;
JSONObject params=new JSONObject();
for (Map.Entry<String, Object> entry : templParams.entrySet()) {
JSONObject param=new JSONObject();
param.put("value",entry.getValue());
params.put(entry.getKey(),param);
}
return params;
}
/** /**
* 解析数值代码表获取消息类型 * 解析数值代码表获取消息类型
* @param msgType * @param msgType
...@@ -390,7 +462,7 @@ public class NotifyCoreService { ...@@ -390,7 +462,7 @@ public class NotifyCoreService {
*/ */
private List<String> getMsgType(int msgType){ private List<String> getMsgType(int msgType){
List<String> useMsg=new ArrayList<>(); List<String> useMsg=new ArrayList<>();
for (Map.Entry<Integer,String> msg : msgTypes.entrySet()){ for (Map.Entry<Integer,String> msg : templateType.entrySet()){
if((msgType & msg.getKey()) == msg.getKey()){ if((msgType & msg.getKey()) == msg.getKey()){
useMsg.add(msg.getValue()); useMsg.add(msg.getValue());
} }
......
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册