邮件模板由后端保存,基础增删改查
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
package com.ruoyi.oa.service;
|
||||
|
||||
import com.ruoyi.oa.domain.OaEmailTemplate;
|
||||
import com.ruoyi.oa.domain.vo.OaEmailTemplateVo;
|
||||
import com.ruoyi.oa.domain.bo.OaEmailTemplateBo;
|
||||
import com.ruoyi.common.core.page.TableDataInfo;
|
||||
import com.ruoyi.common.core.domain.PageQuery;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 邮件模板Service接口
|
||||
*
|
||||
* @author ruoyi
|
||||
* @date 2025-07-16
|
||||
*/
|
||||
public interface IOaEmailTemplateService {
|
||||
|
||||
/**
|
||||
* 查询邮件模板
|
||||
*/
|
||||
OaEmailTemplateVo queryById(Long id);
|
||||
|
||||
/**
|
||||
* 查询邮件模板列表
|
||||
*/
|
||||
TableDataInfo<OaEmailTemplateVo> queryPageList(OaEmailTemplateBo bo, PageQuery pageQuery);
|
||||
|
||||
/**
|
||||
* 查询邮件模板列表
|
||||
*/
|
||||
List<OaEmailTemplateVo> queryList(OaEmailTemplateBo bo);
|
||||
|
||||
/**
|
||||
* 新增邮件模板
|
||||
*/
|
||||
Boolean insertByBo(OaEmailTemplateBo bo);
|
||||
|
||||
/**
|
||||
* 修改邮件模板
|
||||
*/
|
||||
Boolean updateByBo(OaEmailTemplateBo bo);
|
||||
|
||||
/**
|
||||
* 校验并批量删除邮件模板信息
|
||||
*/
|
||||
Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid);
|
||||
}
|
||||
@@ -29,6 +29,7 @@ import com.ruoyi.oa.domain.request.EmailSendRequest;
|
||||
import com.ruoyi.oa.utils.EmailUtil;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
import com.ruoyi.oa.mapper.OaFurnitureTableMapper;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -49,6 +50,9 @@ public class OaEmailAccountServiceImpl implements IOaEmailAccountService {
|
||||
@Autowired
|
||||
private EmailUtil emailUtil;
|
||||
|
||||
@Autowired
|
||||
private OaFurnitureTableMapper oaFurnitureTableMapper;
|
||||
|
||||
/**
|
||||
* 查询发件人邮箱账号管理
|
||||
*/
|
||||
@@ -137,51 +141,41 @@ public class OaEmailAccountServiceImpl implements IOaEmailAccountService {
|
||||
if (account == null) {
|
||||
return "发件人邮箱账号不存在";
|
||||
}
|
||||
// 通过furnitureIds查找所有邮箱
|
||||
List<String> emailList = oaFurnitureTableMapper.selectEmailsByFurnitureIds(request.getFurnitureIds());
|
||||
int type = account.getType() == null ? -1 : account.getType().intValue();
|
||||
int success = 0, fail = 0;
|
||||
StringBuilder failList = new StringBuilder();
|
||||
|
||||
if (type == 0 || type == 1) {
|
||||
if (type == 0 || type == 1) {
|
||||
// 网易/QQ邮箱 SMTP方式
|
||||
for (String to : request.getToList()) {
|
||||
for (String to : emailList) {
|
||||
try {
|
||||
// 判断是否为HTML内容
|
||||
boolean isHtml = request.getContent() != null &&
|
||||
(request.getContent().contains("<html") ||
|
||||
request.getContent().contains("<div") ||
|
||||
request.getContent().contains("<p>"));
|
||||
|
||||
// 检查是否有附件
|
||||
boolean hasAttachments = request.getAttachmentPaths() != null && !request.getAttachmentPaths().isEmpty();
|
||||
// 检查是否有内嵌图片
|
||||
boolean hasInlineImages = request.getInlineImagePaths() != null && !request.getInlineImagePaths().isEmpty();
|
||||
|
||||
if (hasAttachments && hasInlineImages) {
|
||||
// 既有附件又有内嵌图片
|
||||
if (isHtml) {
|
||||
String processedContent = emailUtil.processHtmlWithInlineImages(request.getContent(), request.getInlineImagePaths());
|
||||
emailUtil.sendHtmlMailWithAttachmentsAndDynamicConfig(account, to, request.getSubject(), processedContent, request.getAttachmentPaths());
|
||||
} else {
|
||||
// 纯文本不支持内嵌图片,只发送附件
|
||||
emailUtil.sendMailWithAttachmentAndDynamicConfig(account, to, request.getSubject(), request.getContent(), request.getAttachmentPaths().get(0));
|
||||
}
|
||||
} else if (hasAttachments) {
|
||||
// 只有附件
|
||||
if (isHtml) {
|
||||
emailUtil.sendHtmlMailWithAttachmentsAndDynamicConfig(account, to, request.getSubject(), request.getContent(), request.getAttachmentPaths());
|
||||
} else {
|
||||
emailUtil.sendMailWithAttachmentAndDynamicConfig(account, to, request.getSubject(), request.getContent(), request.getAttachmentPaths().get(0));
|
||||
}
|
||||
} else if (hasInlineImages) {
|
||||
// 只有内嵌图片
|
||||
if (isHtml) {
|
||||
emailUtil.sendHtmlMailWithInlineImagesAndDynamicConfig(account, to, request.getSubject(), request.getContent(), request.getInlineImagePaths());
|
||||
} else {
|
||||
// 纯文本不支持内嵌图片
|
||||
emailUtil.sendMailWithDynamicConfig(account, to, request.getSubject(), request.getContent());
|
||||
}
|
||||
} else {
|
||||
// 无附件无内嵌图片
|
||||
if (isHtml) {
|
||||
emailUtil.sendHtmlMailWithDynamicConfig(account, to, request.getSubject(), request.getContent());
|
||||
} else {
|
||||
@@ -194,10 +188,8 @@ public class OaEmailAccountServiceImpl implements IOaEmailAccountService {
|
||||
failList.append(to).append(", ");
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (type == 2) {
|
||||
// 阿里云邮件推送API
|
||||
for (String to : request.getToList()) {
|
||||
} else if (type == 2) {
|
||||
for (String to : emailList) {
|
||||
try {
|
||||
boolean result = sendAliyunMail(account, to, request.getSubject(), request.getContent());
|
||||
if (result) {
|
||||
@@ -212,8 +204,7 @@ public class OaEmailAccountServiceImpl implements IOaEmailAccountService {
|
||||
}
|
||||
}
|
||||
} else if (type == 3) {
|
||||
// 飞书邮件API
|
||||
for (String to : request.getToList()) {
|
||||
for (String to : emailList) {
|
||||
try {
|
||||
boolean result = sendFeishuMail(account, to, request.getSubject(), request.getContent());
|
||||
if (result) {
|
||||
@@ -227,10 +218,15 @@ public class OaEmailAccountServiceImpl implements IOaEmailAccountService {
|
||||
failList.append(to).append(", ");
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
return "不支持的邮箱类型";
|
||||
}
|
||||
// 邮件发送逻辑结束后,统计success>0时批量更新所有furnitureId
|
||||
if (success > 0 && request.getFurnitureIds() != null) {
|
||||
for (Long furnitureId : request.getFurnitureIds()) {
|
||||
oaFurnitureTableMapper.updateEmailSendInfo(furnitureId);
|
||||
}
|
||||
}
|
||||
return "发送成功" + success + "封,失败" + fail + "封" + (fail > 0 ? (",失败邮箱:" + failList) : "");
|
||||
}
|
||||
private boolean sendAliyunMail(OaEmailAccount account, String to, String subject, String content) {
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
package com.ruoyi.oa.service.impl;
|
||||
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import com.ruoyi.common.utils.StringUtils;
|
||||
import com.ruoyi.common.core.page.TableDataInfo;
|
||||
import com.ruoyi.common.core.domain.PageQuery;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
import com.ruoyi.oa.domain.bo.OaEmailTemplateBo;
|
||||
import com.ruoyi.oa.domain.vo.OaEmailTemplateVo;
|
||||
import com.ruoyi.oa.domain.OaEmailTemplate;
|
||||
import com.ruoyi.oa.mapper.OaEmailTemplateMapper;
|
||||
import com.ruoyi.oa.service.IOaEmailTemplateService;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Collection;
|
||||
|
||||
/**
|
||||
* 邮件模板Service业务层处理
|
||||
*
|
||||
* @author ruoyi
|
||||
* @date 2025-07-16
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
@Service
|
||||
public class OaEmailTemplateServiceImpl implements IOaEmailTemplateService {
|
||||
|
||||
private final OaEmailTemplateMapper baseMapper;
|
||||
|
||||
/**
|
||||
* 查询邮件模板
|
||||
*/
|
||||
@Override
|
||||
public OaEmailTemplateVo queryById(Long id){
|
||||
return baseMapper.selectVoById(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询邮件模板列表
|
||||
*/
|
||||
@Override
|
||||
public TableDataInfo<OaEmailTemplateVo> queryPageList(OaEmailTemplateBo bo, PageQuery pageQuery) {
|
||||
LambdaQueryWrapper<OaEmailTemplate> lqw = buildQueryWrapper(bo);
|
||||
Page<OaEmailTemplateVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
|
||||
return TableDataInfo.build(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询邮件模板列表
|
||||
*/
|
||||
@Override
|
||||
public List<OaEmailTemplateVo> queryList(OaEmailTemplateBo bo) {
|
||||
LambdaQueryWrapper<OaEmailTemplate> lqw = buildQueryWrapper(bo);
|
||||
return baseMapper.selectVoList(lqw);
|
||||
}
|
||||
|
||||
private LambdaQueryWrapper<OaEmailTemplate> buildQueryWrapper(OaEmailTemplateBo bo) {
|
||||
Map<String, Object> params = bo.getParams();
|
||||
LambdaQueryWrapper<OaEmailTemplate> lqw = Wrappers.lambdaQuery();
|
||||
lqw.like(StringUtils.isNotBlank(bo.getTemplateName()), OaEmailTemplate::getTemplateName, bo.getTemplateName());
|
||||
lqw.eq(StringUtils.isNotBlank(bo.getCategory()), OaEmailTemplate::getCategory, bo.getCategory());
|
||||
lqw.eq(StringUtils.isNotBlank(bo.getContent()), OaEmailTemplate::getContent, bo.getContent());
|
||||
lqw.eq(StringUtils.isNotBlank(bo.getAttachments()), OaEmailTemplate::getAttachments, bo.getAttachments());
|
||||
return lqw;
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增邮件模板
|
||||
*/
|
||||
@Override
|
||||
public Boolean insertByBo(OaEmailTemplateBo bo) {
|
||||
OaEmailTemplate add = BeanUtil.toBean(bo, OaEmailTemplate.class);
|
||||
validEntityBeforeSave(add);
|
||||
boolean flag = baseMapper.insert(add) > 0;
|
||||
if (flag) {
|
||||
bo.setId(add.getId());
|
||||
}
|
||||
return flag;
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改邮件模板
|
||||
*/
|
||||
@Override
|
||||
public Boolean updateByBo(OaEmailTemplateBo bo) {
|
||||
OaEmailTemplate update = BeanUtil.toBean(bo, OaEmailTemplate.class);
|
||||
validEntityBeforeSave(update);
|
||||
return baseMapper.updateById(update) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存前的数据校验
|
||||
*/
|
||||
private void validEntityBeforeSave(OaEmailTemplate entity){
|
||||
//TODO 做一些数据校验,如唯一约束
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量删除邮件模板
|
||||
*/
|
||||
@Override
|
||||
public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
|
||||
if(isValid){
|
||||
//TODO 做一些业务上的校验,判断是否需要校验
|
||||
}
|
||||
return baseMapper.deleteBatchIds(ids) > 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user