This commit is contained in:
Owen 2025-01-09 16:22:55 +08:00
parent b7070ea28c
commit 89f599899b
7 changed files with 104 additions and 13 deletions

View File

@ -113,4 +113,6 @@ public class SmsLogRespVO {
@ExcelProperty("创建时间")
private LocalDateTime createTime;
private String taskName;
private Long orderId;
}

View File

@ -158,4 +158,6 @@ public class SmsLogDO extends BaseDO {
*/
private String apiReceiveMsg;
private String taskName;
private Long orderId;
}

View File

@ -31,6 +31,10 @@ public interface SmsLogService {
Long createSmsLog(String mobile, Long userId, Integer userType, Boolean isSend,
SmsTemplateDO template, String templateContent, Map<String, Object> templateParams);
public Long createSmsLog(String mobile, Long userId, Integer userType, Boolean isSend,
SmsTemplateDO template, String templateContent, Map<String, Object> templateParams,String taskName,
Long orderId);
/**
* 更新日志的发送结果
*

View File

@ -51,6 +51,32 @@ public class SmsLogServiceImpl implements SmsLogService {
return logDO.getId();
}
public Long createSmsLog(String mobile, Long userId, Integer userType, Boolean isSend,
SmsTemplateDO template, String templateContent, Map<String, Object> templateParams,String taskName,
Long orderId) {
SmsLogDO.SmsLogDOBuilder logBuilder = SmsLogDO.builder();
// 根据是否要发送设置状态
logBuilder.sendStatus(Objects.equals(isSend, true) ? SmsSendStatusEnum.INIT.getStatus()
: SmsSendStatusEnum.IGNORE.getStatus());
// 设置手机相关字段
logBuilder.mobile(mobile).userId(userId).userType(userType);
// 设置模板相关字段
logBuilder.templateId(template.getId()).templateCode(template.getCode()).templateType(template.getType());
logBuilder.templateContent(templateContent).templateParams(templateParams)
.apiTemplateId(template.getApiTemplateId());
// 设置渠道相关字段
logBuilder.channelId(template.getChannelId()).channelCode(template.getChannelCode());
// 设置接收相关字段
logBuilder.receiveStatus(SmsReceiveStatusEnum.INIT.getStatus());
// 插入数据库
SmsLogDO logDO = logBuilder.build();
logDO.setTaskName(taskName);
logDO.setOrderId(orderId);
smsLogMapper.insert(logDO);
return logDO.getId();
}
@Override
public void updateSmsSendResult(Long id, Boolean success,
String apiSendCode, String apiSendMsg,

View File

@ -14,12 +14,12 @@ public interface SmsSendService {
/**
* 发送单条短信给管理后台的用户
*
* <p>
* mobile 为空时使用 userId 加载对应管理员的手机号
*
* @param mobile 手机号
* @param userId 用户编号
* @param templateCode 短信模板编号
* @param mobile 手机号
* @param userId 用户编号
* @param templateCode 短信模板编号
* @param templateParams 短信模板参数
* @return 发送日志编号
*/
@ -28,25 +28,28 @@ public interface SmsSendService {
/**
* 发送单条短信给用户 APP 的用户
*
* <p>
* mobile 为空时使用 userId 加载对应会员的手机号
*
* @param mobile 手机号
* @param userId 用户编号
* @param templateCode 短信模板编号
* @param mobile 手机号
* @param userId 用户编号
* @param templateCode 短信模板编号
* @param templateParams 短信模板参数
* @return 发送日志编号
*/
Long sendSingleSmsToMember(String mobile, Long userId,
String templateCode, Map<String, Object> templateParams);
Long sendSingleSmsWithChannel(String mobile, Long userId, Integer userType, String taskName, Long orderId,
String templateCode, Map<String, Object> templateParams, Long channelId);
/**
* 发送单条短信给用户
*
* @param mobile 手机号
* @param userId 用户编号
* @param userType 用户类型
* @param templateCode 短信模板编号
* @param mobile 手机号
* @param userId 用户编号
* @param userType 用户类型
* @param templateCode 短信模板编号
* @param templateParams 短信模板参数
* @return 发送日志编号
*/
@ -70,7 +73,7 @@ public interface SmsSendService {
* 接收短信的接收结果
*
* @param channelCode 渠道编码
* @param text 结果内容
* @param text 结果内容
* @throws Throwable 处理失败时抛出异常
*/
void receiveSmsStatus(String channelCode, String text) throws Throwable;

View File

@ -77,6 +77,34 @@ public class SmsSendServiceImpl implements SmsSendService {
return sendSingleSms(mobile, userId, UserTypeEnum.MEMBER.getValue(), templateCode, templateParams);
}
@Override
public Long sendSingleSmsWithChannel(String mobile, Long userId, Integer userType,String taskName,Long orderId,
String templateCode, Map<String, Object> templateParams, Long channelId) {
// 校验短信模板是否合法
SmsTemplateDO template = validateSmsTemplate(templateCode);
// 校验短信渠道是否合法
SmsChannelDO smsChannel = validateSmsChannel(channelId);
// 校验手机号码是否存在
mobile = validateMobile(mobile);
// 构建有序的模板参数为什么放在这个位置是提前保证模板参数的正确性而不是到了插入发送日志
List<KeyValue<String, Object>> newTemplateParams = buildTemplateParams(template, templateParams);
// 创建发送日志如果模板被禁用则不发送短信只记录日志
Boolean isSend = CommonStatusEnum.ENABLE.getStatus().equals(template.getStatus())
&& CommonStatusEnum.ENABLE.getStatus().equals(smsChannel.getStatus());
String content = smsTemplateService.formatSmsTemplateContent(template.getContent(), templateParams);
Long sendLogId = smsLogService.createSmsLog(mobile, userId, userType, isSend, template, content, templateParams,taskName,orderId);
// 发送 MQ 消息异步执行发送短信
if (isSend) {
smsProducer.sendSmsSendMessage(sendLogId, mobile, channelId,
template.getApiTemplateId(), newTemplateParams);
}
return sendLogId;
}
@Override
public Long sendSingleSms(String mobile, Long userId, Integer userType,
String templateCode, Map<String, Object> templateParams) {

View File

@ -0,0 +1,26 @@
DROP TABLE IF EXISTS `haoka_sms_task`;
CREATE TABLE `haoka_sms_task`
(
`id` bigint(20) NOT NULL PRIMARY KEY COMMENT 'ID',
`name` varchar(512) NOT NULL COMMENT '任务名称',
`order_status` bigint(20) COMMENT '订单状态',
`order_source_list` longtext COMMENT '订单来源',
`order_on_sale_product_list` longtext COMMENT '商品名称',
`call_status` int(11) COMMENT '是否外呼',
`send_time` int(11) NOT NULL COMMENT '发送时间段',
`sms_channel_id` bigint(20) NOT NULL COMMENT '短信渠道',
`sms_type` int(11) NOT NULL COMMENT '短信类型',
`sms_template_id` int(20) NOT NULL COMMENT '短信内容',
`test_phone` varchar(2048) COMMENT '测试手机',
`dept_id` bigint(20) COMMENT '部门ID',
`creator` varchar(64) NOT NULL DEFAULT '' COMMENT '创建者',
`create_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`updater` varchar(64) NOT NULL DEFAULT '' COMMENT '更新者',
`update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
`deleted` bit(1) NOT NULL DEFAULT b'0' COMMENT '是否删除',
`tenant_id` bigint(20) NOT NULL DEFAULT 0 COMMENT '租户编号'
) COMMENT = '短信任务';
ALTER TABLE `system_sms_log`
ADD COLUMN `task_name` varchar(1024) NULL COMMENT '任务名称' AFTER `deleted`,
ADD COLUMN `order_id` bigint(20) NULL COMMENT '订单ID' AFTER `task_name`;