Search in sources :

Example 1 with MessageTemplate

use of com.diboot.message.entity.MessageTemplate in project diboot by dibo-software.

the class MessageServiceImpl method send.

@Override
public boolean send(Message message, BaseVariableData variableData) {
    // 获取发送通道
    ChannelStrategy channelStrategy = channelStrategyMap.get(message.getChannel());
    if (V.isEmpty(channelStrategy)) {
        log.error("[获取发送通道失败],当前发送通道为:{}", message.getChannel());
        throw new InvalidUsageException("获取发送通道失败! " + message.getChannel());
    }
    // 是否根据模板构建邮件内容
    LambdaQueryWrapper<MessageTemplate> queryWrapper = Wrappers.<MessageTemplate>lambdaQuery().eq(V.notEmpty(message.getTemplateId()), MessageTemplate::getId, message.getTemplateId()).eq(V.notEmpty(message.getTemplateCode()), MessageTemplate::getCode, message.getTemplateCode());
    if (queryWrapper.nonEmptyOfNormal()) {
        MessageTemplate messageTemplate = messageTemplateService.getSingleEntity(queryWrapper);
        if (V.isEmpty(messageTemplate)) {
            if (V.isEmpty(message.getTemplateCode())) {
                log.error("[获取模版失败] 模版id为:{}", message.getTemplateId());
            } else if (V.isEmpty(message.getTemplateId())) {
                log.error("[获取模版失败] 模版code为:{}", message.getTemplateCode());
            } else {
                log.error("[获取模版失败] 模版id为:{} ,模版code为:{}", message.getTemplateId(), message.getTemplateCode());
            }
            throw new BusinessException(Status.FAIL_OPERATION, "获取模版失败!");
        }
        message.setTemplateId(messageTemplate.getId());
        try {
            // 设置模版内容
            String content = templateVariableService.parseTemplate2Content(messageTemplate.getContent(), variableData);
            message.setContent(content);
        } catch (Exception e) {
            log.error("[消息解析失败],消息体为:{}", message);
            throw new BusinessException(Status.FAIL_OPERATION, "消息解析失败!");
        }
    }
    if (V.isEmpty(message.getContent())) {
        throw new BusinessException("消息内容不能为 null");
    }
    // 设置定时发送,则等待定时任务发送
    if (V.notEmpty(message.getScheduleTime())) {
        message.setStatus("SCHEDULE");
    }
    // 创建Message
    boolean success = createEntity(message);
    // 停止发送
    if (V.notEmpty(message.getScheduleTime())) {
        return true;
    }
    if (!success) {
        log.error("[消息创建失败],消息体为:{}", message);
        throw new BusinessException(Status.FAIL_OPERATION, "消息发送失败!");
    }
    // 异步发送消息
    channelStrategy.send(message);
    return true;
}
Also used : MessageTemplate(com.diboot.message.entity.MessageTemplate) BusinessException(com.diboot.core.exception.BusinessException) ChannelStrategy(com.diboot.message.channel.ChannelStrategy) InvalidUsageException(com.diboot.core.exception.InvalidUsageException) BusinessException(com.diboot.core.exception.BusinessException) InvalidUsageException(com.diboot.core.exception.InvalidUsageException)

Aggregations

BusinessException (com.diboot.core.exception.BusinessException)1 InvalidUsageException (com.diboot.core.exception.InvalidUsageException)1 ChannelStrategy (com.diboot.message.channel.ChannelStrategy)1 MessageTemplate (com.diboot.message.entity.MessageTemplate)1