Jelajahi Sumber

feat(qhse-common): 添加短信发送记录功能

- 新增 SendRecordService 和 SendRecordDao 接口
- 实现 SendRecordServiceImpl 和 AliyunSmsServiceImpl 中的发送记录保存逻辑
- 创建 SendRecordDo 实体类用于存储发送记录
- 在 AliyunSmsServiceImpl 中添加 saveSendRecord 方法保存发送记录
lg 3 minggu lalu
induk
melakukan
6592af34fd

+ 7 - 0
qhse-common/src/main/java/com/rongwei/bscommon/sys/dao/SendRecordDao.java

@@ -0,0 +1,7 @@
+package com.rongwei.bscommon.sys.dao;
+
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.rongwei.bsentity.domain.SendRecordDo;
+
+public interface SendRecordDao extends BaseMapper<SendRecordDo> {
+}

+ 7 - 0
qhse-common/src/main/java/com/rongwei/bscommon/sys/service/SendRecordService.java

@@ -0,0 +1,7 @@
+package com.rongwei.bscommon.sys.service;
+
+import com.baomidou.mybatisplus.extension.service.IService;
+import com.rongwei.bsentity.domain.SendRecordDo;
+
+public interface SendRecordService extends IService<SendRecordDo> {
+}

+ 30 - 0
qhse-common/src/main/java/com/rongwei/bscommon/sys/service/impl/AliyunSmsServiceImpl.java

@@ -5,7 +5,12 @@ import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
 import com.aliyun.tea.TeaException;
 import com.aliyun.teautil.models.RuntimeOptions;
 import com.rongwei.bscommon.sys.service.AliyunSmsService;
+import com.rongwei.bscommon.sys.service.SendRecordService;
+import com.rongwei.bscommon.sys.utils.QHSEUtils;
+import com.rongwei.bsentity.domain.SendRecordDo;
+import com.rongwei.rwadmincommon.system.vo.SysUserVo;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 
@@ -24,6 +29,9 @@ public class AliyunSmsServiceImpl implements AliyunSmsService {
     @Value("${aliyun-sms.sign-name:#{null}}")
     private String signName;
 
+    @Autowired
+    private SendRecordService sendRecordService;
+
     private com.aliyun.dysmsapi20170525.Client createClient() throws Exception {
         // 工程代码建议使用更安全的无AK方式,凭据配置方式请参见:https://help.aliyun.com/document_detail/378657.html。
         com.aliyun.credentials.Client credential = new com.aliyun.credentials.Client();
@@ -49,6 +57,7 @@ public class AliyunSmsServiceImpl implements AliyunSmsService {
                                    Map<String, String> templateParamMap, String phone) throws Exception {
         String templateParam = com.aliyun.teautil.Common.toJSONString(templateParamMap);
         com.aliyun.dysmsapi20170525.Client client = createClient();
+        // 创建请求
         SendSmsRequest sendSmsRequest = new SendSmsRequest();
         sendSmsRequest.setSignName(signName);
         sendSmsRequest.setTemplateCode(templateId);
@@ -59,6 +68,8 @@ public class AliyunSmsServiceImpl implements AliyunSmsService {
         try {
             resp = client.sendSmsWithOptions(sendSmsRequest, runtime);
             log.info("发送短信结果:{}", com.aliyun.teautil.Common.toJSONString(resp));
+            // 保存发送记录
+            saveSendRecord(resp, sendSmsRequest);
         } catch (TeaException error) {
             // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
             // 错误 message
@@ -77,4 +88,23 @@ public class AliyunSmsServiceImpl implements AliyunSmsService {
         }
         return resp;
     }
+
+    /**
+     * 保存发送记录
+     *
+     * @param resp           发送结果
+     * @param sendSmsRequest 发送请求
+     */
+    private void saveSendRecord(SendSmsResponse resp, SendSmsRequest sendSmsRequest) {
+        SendRecordDo sendRecordDo = new SendRecordDo();
+        sendRecordDo.setResult(com.aliyun.teautil.Common.toJSONString(resp));
+        sendRecordDo.setTemplateCode(sendSmsRequest.getTemplateCode());
+        sendRecordDo.setTemplateParam(sendSmsRequest.getTemplateParam());
+        sendRecordDo.setPhoneNumber(sendSmsRequest.getPhoneNumbers());
+        sendRecordDo.setSignName(sendSmsRequest.getSignName());
+        SysUserVo currentUser = QHSEUtils.getCurrentUser();
+        QHSEUtils.initModelGeneralParameters(sendRecordDo, currentUser);
+        sendRecordService.save(sendRecordDo);
+    }
+
 }

+ 11 - 0
qhse-common/src/main/java/com/rongwei/bscommon/sys/service/impl/SendRecordServiceImpl.java

@@ -0,0 +1,11 @@
+package com.rongwei.bscommon.sys.service.impl;
+
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.rongwei.bscommon.sys.dao.SendRecordDao;
+import com.rongwei.bscommon.sys.service.SendRecordService;
+import com.rongwei.bsentity.domain.SendRecordDo;
+import org.springframework.stereotype.Service;
+
+@Service
+public class SendRecordServiceImpl extends ServiceImpl<SendRecordDao, SendRecordDo> implements SendRecordService {
+}

+ 66 - 0
qhse-entity/src/main/java/com/rongwei/bsentity/domain/SendRecordDo.java

@@ -0,0 +1,66 @@
+package com.rongwei.bsentity.domain;
+
+import com.baomidou.mybatisplus.annotation.IdType;
+import com.baomidou.mybatisplus.annotation.TableField;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableName;
+import com.rongwei.rwcommon.base.BaseDo;
+import lombok.Data;
+import lombok.EqualsAndHashCode;
+
+/**
+ * 发送短信记录表
+ */
+@EqualsAndHashCode(callSuper = true)
+@Data
+@TableName(value = "qhse_send_record")
+public class SendRecordDo extends BaseDo {
+
+    /**
+     * 主键
+     */
+    @TableId(value = "ID", type = IdType.AUTO)
+    private Long id;
+
+    /**
+     * 租户ID
+     */
+    @TableField(value = "TENANTID")
+    private String tenantId;
+
+    /**
+     * 扩展json格式配置
+     */
+    @TableField(value = "ROPTION")
+    private String roption;
+
+    /**
+     * 接收手机号
+     */
+    @TableField(value = "PHONENUMBER")
+    private String phoneNumber;
+
+    /**
+     * 短信模板CODE
+     */
+    @TableField(value = "TEMPLATECODE")
+    private String templateCode;
+
+    /**
+     * 短信签名
+     */
+    @TableField(value = "SIGNNAME")
+    private String signName;
+
+    /**
+     * 模板参数(JSON格式)
+     */
+    @TableField(value = "TEMPLATEPARAM")
+    private String templateParam;
+
+    /**
+     * 短信发送结果(JSON格式)
+     */
+    @TableField(value = "RESULT")
+    private String result;
+}