Commit 7be8dd0d authored by qinxunjia's avatar qinxunjia

优化

parent b80c7b07
......@@ -70,6 +70,11 @@
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
</dependencies>
<build>
<finalName>dmhub-plugin</finalName>
......
package com.jz.sms.api;
import com.alibaba.fastjson.JSONObject;
import com.jz.sms.chuanlan.service.ChuanLanSmsService;
import com.jz.sms.dto.XfaceDmHubTemplateRequestDTO;
import com.jz.sms.dto.XfaceDmHubTemplateResponseDTO;
import com.jz.sms.service.MessageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.Map;
@RestController
......@@ -18,7 +23,7 @@ public class BizController {
@Autowired
private ChuanLanSmsService service;
private MessageService messageService;
/**
......@@ -27,8 +32,10 @@ public class BizController {
* @return
*/
@PostMapping("/sms/template")
public String template(@RequestParam Map<String, Object> params) {
log.info("**********创建模板入参*******:{}", params);
public String template(@RequestBody XfaceDmHubTemplateRequestDTO params) {
log.info("**********创建模板入参*******:{}", JSONObject.toJSONString(params));
XfaceDmHubTemplateResponseDTO template = messageService.createTemplate(params);
return "ok";
}
......
package com.jz.sms.dto;
package com.jz.sms.chuanlan.dto;
public class XfaceClSendFiexdRequestDTO {
......@@ -58,14 +58,14 @@ public class XfaceClSendFiexdRequestDTO {
this.report = report;
}
public XfaceClSendFiexdRequestDTO(String account, String password, String msg, String phone, String report, String sendtime) {
public XfaceClSendFiexdRequestDTO(String account, String password, String msg, String phone, String report, String uid) {
super();
this.account = account;
this.password = password;
this.msg = msg;
this.phone = phone;
this.sendtime = sendtime;
this.report = report;
this.uid = uid;
}
public XfaceClSendFiexdRequestDTO(String account, String password, String msg, String phone, String sendtime, String report, String uid) {
......
package com.jz.sms.dto;
package com.jz.sms.chuanlan.dto;
import com.jz.sms.util.DateUtil;
import java.text.DateFormat;
public class XfaceClSendFiexdResponseDTO {
/**
......@@ -18,6 +22,14 @@ public class XfaceClSendFiexdResponseDTO {
*/
private String code;
public XfaceClSendFiexdResponseDTO(){}
public XfaceClSendFiexdResponseDTO(String code,String errorMsg){
this.code = code;
this.errorMsg = errorMsg;
}
public String getTime() {
return time;
}
......
package com.jz.sms.dto;
package com.jz.sms.chuanlan.dto;
public class XfaceClSendVariableRequestDTO {
......
package com.jz.sms.dto;
package com.jz.sms.chuanlan.dto;
public class XfaceClSendVariableResponseDTO {
/**
......
package com.jz.sms.chuanlan.service;
import com.jz.sms.chuanlan.dto.XfaceClSendFiexdResponseDTO;
import com.jz.sms.dto.*;
import java.util.List;
public interface ChuanLanSmsService {
XfaceDmHubTemplateResponseDTO createTemplate(XfaceDmHubTemplateRequestDTO requestDTO);
XfaceDmHubSendResponseDTO sendByFixed(XfaceDmHubSendRequestDTO requestDTO);
XfaceClSendFiexdResponseDTO sendByFixed(String batchId, String content, List<String> phoneList, String type) throws Exception;
XfaceDmHubSendResponseDTO sendByVariable(XfaceDmHubSendRequestDTO requestDTO);
XfaceClSendFiexdResponseDTO sendByVariable(String batchId, String content, List<String> phoneList, String type) throws Exception;
XfaceDmHubSendResponseDTO sendBatchByFixed(XfaceDmHubSendRequestDTO requestDTO);
......
package com.jz.sms.chuanlan.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jz.sms.api.BizController;
import com.jz.sms.chuanlan.dto.XfaceClSendFiexdRequestDTO;
import com.jz.sms.chuanlan.dto.XfaceClSendFiexdResponseDTO;
import com.jz.sms.chuanlan.dto.XfaceClTemplateRequestDTO;
import com.jz.sms.chuanlan.dto.XfaceClTemplateResponseDTO;
import com.jz.sms.chuanlan.service.ChuanLanSmsService;
import com.jz.sms.chuanlan.utils.ChuangLanSmsUtil;
import com.jz.sms.config.SMSConfig;
import com.jz.sms.dto.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import sun.swing.StringUIClientPropertyKey;
import java.util.ArrayList;
import java.util.List;
@SuppressWarnings("DuplicatedCode")
@Service
public class ChuanLanSmsServiceImpl implements ChuanLanSmsService {
private static final Logger log = LoggerFactory.getLogger(BizController.class);
@Override
public XfaceDmHubTemplateResponseDTO createTemplate(XfaceDmHubTemplateRequestDTO requestDTO) {
......@@ -18,7 +36,6 @@ public class ChuanLanSmsServiceImpl implements ChuanLanSmsService {
*/
XfaceClTemplateRequestDTO clRequestDto = new XfaceClTemplateRequestDTO();
XfaceClTemplateResponseDTO clResponseDTO = new XfaceClTemplateResponseDTO();
/**
* 调用结果包装到DM Hub Response中
*/
......@@ -28,16 +45,76 @@ public class ChuanLanSmsServiceImpl implements ChuanLanSmsService {
}
@Override
public XfaceDmHubSendResponseDTO sendByFixed(XfaceDmHubSendRequestDTO requestDTO) {
public XfaceClSendFiexdResponseDTO sendByFixed(String batchId, String content, List<String> phoneList, String type) throws Exception {
String account;
String password;
if ("notification".equals(type)) {
account = SMSConfig.notificationAccount;
password = SMSConfig.notificationPassword;
log.info("发送短信的模式为:{通知类}");
} else if ("marketing".equals(type)) {
account = SMSConfig.marketingAccount;
password = SMSConfig.marketingPassword;
log.info("发送短信的模式为:{营销类}");
} else {
log.info("发送短信的模式为:{未知类型}");
throw new Exception("123");
}
XfaceClSendFiexdRequestDTO request = new XfaceClSendFiexdRequestDTO(account, password, content, listToString(phoneList), "true", batchId);
return null;
String requestJson = JSONObject.toJSONString(request);
log.info("【{}】发送固定内容短信,请求上游数据为:{}", batchId, requestJson);
String responseStr = ChuangLanSmsUtil.sendSmsByPost(SMSConfig.sendFixed, requestJson);
log.info("【{}】发送固定内容短信,上游返回数据为:{}", batchId, requestJson);
if (StringUtils.isBlank(responseStr)) {
return new XfaceClSendFiexdResponseDTO("500", "渠道同步响应为空");
}
XfaceClSendFiexdResponseDTO response = JSON.parseObject(responseStr, XfaceClSendFiexdResponseDTO.class);
return response;
}
private String listToString(List<String> list) {
String s = list.toString();
return s;
}
public static void main(String[] args) {
List<String> arr = new ArrayList<>();
arr.add("aa,444");
System.out.printf(arr.toString());
}
@Override
public XfaceDmHubSendResponseDTO sendByVariable(XfaceDmHubSendRequestDTO requestDTO) {
public XfaceClSendFiexdResponseDTO sendByVariable(String batchId, String content, List<String> params, String type) throws Exception {
String account;
String password;
if ("notification".equals(type)) {
account = SMSConfig.notificationAccount;
password = SMSConfig.notificationPassword;
log.info("发送短信的模式为:{通知类}");
} else if ("marketing".equals(type)) {
account = SMSConfig.marketingAccount;
password = SMSConfig.marketingPassword;
log.info("发送短信的模式为:{营销类}");
} else {
log.info("发送短信的模式为:{未知类型}");
throw new Exception("123");
}
return null;
XfaceClSendFiexdRequestDTO request = new XfaceClSendFiexdRequestDTO(account, password, content, listToString(params), "true", batchId);
String requestJson = JSONObject.toJSONString(request);
log.info("【{}】发送固定内容短信,请求上游数据为:{}", batchId, requestJson);
String responseStr = ChuangLanSmsUtil.sendSmsByPost(SMSConfig.sendFixed, requestJson);
log.info("【{}】发送固定内容短信,上游返回数据为:{}", batchId, requestJson);
if (StringUtils.isBlank(responseStr)) {
return new XfaceClSendFiexdResponseDTO("500", "渠道同步响应为空");
}
XfaceClSendFiexdResponseDTO response = JSON.parseObject(responseStr, XfaceClSendFiexdResponseDTO.class);
return response;
}
@Override
......
package com.jz.sms.chuanlan.utils;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
//import java.io.PrintWriter;
/**
*
* @author tianyh
* @Description:HTTP 请求
*/
public class ChuangLanSmsUtil {
/**
*
* @author tianyh
* @Description
* @param path
* @param postContent
* @return String
* @throws
*/
public static String sendSmsByPost(String path, String postContent) {
URL url = null;
try {
url = new URL(path);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");// 提交模式
httpURLConnection.setConnectTimeout(10000);//连接超时 单位毫秒
httpURLConnection.setReadTimeout(10000);//读取超时 单位毫秒
// 发送POST请求必须设置如下两行
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
httpURLConnection.setRequestProperty("Charset", "UTF-8");
httpURLConnection.setRequestProperty("Content-Type", "application/json");
// PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
// printWriter.write(postContent);
// printWriter.flush();
httpURLConnection.connect();
OutputStream os=httpURLConnection.getOutputStream();
os.write(postContent.getBytes("UTF-8"));
os.flush();
StringBuilder sb = new StringBuilder();
int httpRspCode = httpURLConnection.getResponseCode();
if (httpRspCode == HttpURLConnection.HTTP_OK) {
// 开始获取数据
BufferedReader br = new BufferedReader(
new InputStreamReader(httpURLConnection.getInputStream(), "utf-8"));
String line = null;
while ((line = br.readLine()) != null) {
sb.append(line);
}
br.close();
return sb.toString();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
package com.jz.sms.dto;
public class XfaceDmHubTemplateRequestDTO {
private String tenantId;
private String templateId;
private String templateContent;
private String smsType;
private String signature;
private String templateName;
public String getTenantId() {
return tenantId;
}
public void setTenantId(String tenantId) {
this.tenantId = tenantId;
}
public String getTemplateId() {
return templateId;
}
public void setTemplateId(String templateId) {
this.templateId = templateId;
}
public String getTemplateContent() {
return templateContent;
}
public void setTemplateContent(String templateContent) {
this.templateContent = templateContent;
}
public String getSmsType() {
return smsType;
}
public void setSmsType(String smsType) {
this.smsType = smsType;
}
public String getSignature() {
return signature;
}
public void setSignature(String signature) {
this.signature = signature;
}
public String getTemplateName() {
return templateName;
}
public void setTemplateName(String templateName) {
this.templateName = templateName;
}
}
package com.jz.sms.repository.domain;
import com.baomidou.mybatisplus.annotations.TableName;
import java.util.Date;
@TableName("dm_batch")
public class DmBatchInfo {
......
package com.jz.sms.repository.domain;
import com.baomidou.mybatisplus.annotations.TableName;
import java.util.Date;
@TableName("sms_template")
public class SmsTemplateInfo {
......@@ -19,6 +23,10 @@ public class SmsTemplateInfo {
private Date lastUpdated;
private String signature;
private String tenantId;
public SmsTemplateInfo() {
}
......@@ -27,6 +35,23 @@ public class SmsTemplateInfo {
this.dmTemplateId = dmTemplateId;
}
public String getSignature() {
return signature;
}
public void setSignature(String signature) {
this.signature = signature;
}
public String getTenantId() {
return tenantId;
}
public void setTenantId(String tenantId) {
this.tenantId = tenantId;
}
public Long getId() {
return id;
}
......
package com.jz.sms.repository.domain;
import com.baomidou.mybatisplus.annotations.TableName;
import java.util.Date;
@TableName("sys_batch")
public class SysBatchInfo {
private Long id;
......@@ -14,6 +17,8 @@ public class SysBatchInfo {
private String report;
private Integer smsNum;
private String extend;
private String msgId;
......@@ -113,4 +118,12 @@ public class SysBatchInfo {
public void setLastUpdated(Date lastUpdated) {
this.lastUpdated = lastUpdated;
}
public Integer getSmsNum() {
return smsNum;
}
public void setSmsNum(Integer smsNum) {
this.smsNum = smsNum;
}
}
package com.jz.sms.service;
import com.baomidou.mybatisplus.service.IService;
import com.jz.sms.repository.domain.SmsTemplateInfo;
public interface SmsTemplateService extends IService<SmsTemplateInfo> {
}
package com.jz.sms.service;
public interface SysBatchService {
}
package com.jz.sms.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.jz.sms.api.BizController;
import com.jz.sms.chuanlan.dto.XfaceClSendFiexdResponseDTO;
import com.jz.sms.chuanlan.service.ChuanLanSmsService;
import com.jz.sms.dto.*;
import com.jz.sms.repository.domain.SmsTemplateInfo;
import com.jz.sms.repository.domain.SysBatchInfo;
import com.jz.sms.repository.mapper.SysBatchMapper;
import com.jz.sms.repository.mapper.TemplateMapper;
import com.jz.sms.service.MessageService;
import com.jz.sms.service.SmsTemplateService;
import com.jz.sms.util.id.IdHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Service
public class MessageServiceImpl implements MessageService {
private static final Logger log = LoggerFactory.getLogger(MessageServiceImpl.class);
@Autowired
private SysBatchMapper sysBatchMapper;
//
private SysBatchServiceImpl sysBatchService;
@Autowired
private TemplateMapper templateMapper;
private SmsTemplateService smsTemplateService;
@Autowired
private ChuanLanSmsService chuanLanSmsService;
......@@ -30,13 +46,32 @@ public class MessageServiceImpl implements MessageService {
*/
@Override
public XfaceDmHubTemplateResponseDTO createTemplate(XfaceDmHubTemplateRequestDTO requestDTO) {
log.info("进入创建短信模板业务逻辑,入参:{}", requestDTO);
XfaceDmHubTemplateResponseDTO responseDTO = new XfaceDmHubTemplateResponseDTO();
try {
// XfaceClTemplateRequestDTO clTemplateRequestDTO = new XfaceClTemplateRequestDTO();
// XfaceClTemplateResponseDTO clTemplateResponseDTO = new XfaceClTemplateResponseDTO();
// logger.info("短信通道请求URL:"+url);
// logger.info("短信通道返回内容:"+responseStr);
String templateContent = requestDTO.getTemplateContent();
String signature = requestDTO.getSignature();
String smsType = requestDTO.getSmsType();
String tenantId = requestDTO.getTenantId();
String templateId = requestDTO.getTemplateId();
String templateName = requestDTO.getTemplateName();
SmsTemplateInfo info = new SmsTemplateInfo();
info.setContent(templateContent);
info.setType(smsType);
info.setDmTemplateId(templateId);
info.setTenantId(tenantId);
info.setId(IdHandler.nextId());
boolean insert = smsTemplateService.insert(info);
if (!insert) {
//TODO 发送告警
}
} catch (Exception exception) {
// 记录异常日志
throw exception;
......@@ -46,27 +81,50 @@ public class MessageServiceImpl implements MessageService {
}
@Override
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
public XfaceDmHubSendResponseDTO send(XfaceDmHubSendRequestDTO requestDTO) {
try {
String dmHubBatchId = requestDTO.getBatchId();
String templateId = requestDTO.getTemplateId();
String audienceIdType = requestDTO.getAudienceIdType();
JSONObject data = requestDTO.getData();
// 获取模板信息
// SmsTemplateInfo templateInfo = templateMapper.selectOne(new SmsTemplateInfo(templateId));
// if (null == templateInfo) {
// return null;
// }
//
// String content = templateInfo.getContent();
// String smsType = templateInfo.getType();
//
// // 系统记录批次,不管是单发还是批量发送都当是批量发送处理
// SysBatchInfo info = new SysBatchInfo();
// info.setBatchId(IdHandler.nextId());
// Integer insert = sysBatchMapper.insert(info);
// chuanLanSmsService.sendByFixed(content);
// 1、获取模板信息
SmsTemplateInfo templateInfo = smsTemplateService.selectOne(new EntityWrapper<SmsTemplateInfo>().eq("dm_template_id", dmHubBatchId));
if (null == templateInfo) {
return null;
}
// 2、根据请求信息获取用户的手机号码(此处不考虑配置DMHUB系统不是SMS的情况),直接拿请求体中的_audienceId字段值,该值为手机号码
String phone = data.getString("_audienceId");
List<String> phoneList = new ArrayList<>();
phoneList.add(phone);
String type = templateInfo.getType();
// 3、记录批次信息
SysBatchInfo sysBatchInfo = new SysBatchInfo();
sysBatchInfo.setBatchId(IdHandler.nextId());
sysBatchInfo.setDmBatchId(dmHubBatchId);
sysBatchInfo.setId(IdHandler.nextId());
sysBatchInfo.setDmTemplateId(templateId);
sysBatchInfo.setReport("1");
sysBatchInfo.setSmsNum(1);
sysBatchInfo.setDateCreated(new Date());
sysBatchInfo.setLastUpdated(new Date());
boolean insert = sysBatchService.insert(sysBatchInfo);
// 判断模板是固定短信还是变量短信
String content = templateInfo.getContent();
if (content.contains("${")) {// TODO 判断方式待定
// 变量模板
// 3、模板参数文本替换。获取模板中的动态参内容,替换成创蓝需要的格式${var}.将请求中的参数信息和模板中的变量名对应起来,封装创蓝请求参数
XfaceClSendFiexdResponseDTO chuanglanResponse = chuanLanSmsService.sendByFixed(dmHubBatchId, content, phoneList, type);
} else {
// 固定模板
// 4、封裝數據
chuanLanSmsService.sendByVariable(dmHubBatchId, content, phoneList, type);
}
} catch (Exception e) {
......
package com.jz.sms.service.impl;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jz.sms.repository.domain.SmsTemplateInfo;
import com.jz.sms.repository.mapper.TemplateMapper;
import com.jz.sms.service.SmsTemplateService;
import org.springframework.stereotype.Service;
@Service
public class SmsTemplateServiceImpl extends ServiceImpl<TemplateMapper, SmsTemplateInfo> implements SmsTemplateService {
}
package com.jz.sms.service.impl;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jz.sms.repository.domain.SysBatchInfo;
import com.jz.sms.repository.mapper.SysBatchMapper;
import com.jz.sms.service.SysBatchService;
import org.springframework.stereotype.Service;
@Service
public class SysBatchServiceImpl extends ServiceImpl<SysBatchMapper, SysBatchInfo> implements SysBatchService {
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment