mirror of
https://gitee.com/qqtt-ah/ai-crm.git
synced 2026-11-25 02:29:20 +08:00
feat: 落地邮件方案A,支持模板手发与发送记录
This commit is contained in:
@@ -129,6 +129,11 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-validation</artifactId>
|
||||
</dependency>
|
||||
<!-- CRM 动态 SMTP 发信(按公司账户建 Session,不用全局 MailSender) -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-mail</artifactId>
|
||||
</dependency>
|
||||
<!-- Spring AOP -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.crm.mail.account.controller;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import top.xyquan.ecomdmbbackend.common.annotation.RequirePermission;
|
||||
import top.xyquan.ecomdmbbackend.common.response.ApiResponse;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.account.model.dto.CreateCrmMailAccountDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.account.model.dto.UpdateCrmMailAccountDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.account.model.vo.CrmMailAccountVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.account.service.CrmMailAccountService;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/** 发件账户管理。 */
|
||||
@RestController
|
||||
@RequestMapping("/api/crm/mail-account")
|
||||
public class CrmMailAccountController {
|
||||
|
||||
private final CrmMailAccountService mailAccountService;
|
||||
|
||||
public CrmMailAccountController(CrmMailAccountService mailAccountService) {
|
||||
this.mailAccountService = mailAccountService;
|
||||
}
|
||||
|
||||
@PostMapping("/create")
|
||||
@RequirePermission(value = "crm:mail-account:create", description = "创建发件账户")
|
||||
public ApiResponse<CrmMailAccountVO> create(@Valid @RequestBody CreateCrmMailAccountDTO dto) {
|
||||
return ApiResponse.success("创建成功", mailAccountService.create(dto));
|
||||
}
|
||||
|
||||
@PutMapping("/update/{id}")
|
||||
@RequirePermission(value = "crm:mail-account:update", description = "更新发件账户")
|
||||
public ApiResponse<CrmMailAccountVO> update(
|
||||
@PathVariable Long id, @Valid @RequestBody UpdateCrmMailAccountDTO dto) {
|
||||
return ApiResponse.success("更新成功", mailAccountService.update(id, dto));
|
||||
}
|
||||
|
||||
@DeleteMapping("/delete/{id}")
|
||||
@RequirePermission(value = "crm:mail-account:delete", description = "删除发件账户")
|
||||
public ApiResponse<Void> delete(@PathVariable Long id) {
|
||||
mailAccountService.delete(id);
|
||||
return ApiResponse.success("删除成功");
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
@RequirePermission(value = "crm:mail-account:read", description = "发件账户详情")
|
||||
public ApiResponse<CrmMailAccountVO> get(@PathVariable Long id) {
|
||||
return ApiResponse.success("获取成功", mailAccountService.get(id));
|
||||
}
|
||||
|
||||
@GetMapping("/list")
|
||||
@RequirePermission(value = "crm:mail-account:read", description = "发件账户列表")
|
||||
public ApiResponse<List<CrmMailAccountVO>> list() {
|
||||
return ApiResponse.success("获取成功", mailAccountService.listAll());
|
||||
}
|
||||
|
||||
@GetMapping("/options")
|
||||
@RequirePermission(value = "crm:mail:send", description = "启用发件账户选项")
|
||||
public ApiResponse<List<CrmMailAccountVO>> options() {
|
||||
return ApiResponse.success("获取成功", mailAccountService.listEnabled());
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.crm.mail.account.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.account.model.po.CrmMailAccountPO;
|
||||
|
||||
/** 发件账户 Mapper。 */
|
||||
@Mapper
|
||||
public interface CrmMailAccountMapper extends BaseMapper<CrmMailAccountPO> {
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.crm.mail.account.model.dto;
|
||||
|
||||
import jakarta.validation.constraints.Email;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import jakarta.validation.constraints.Size;
|
||||
import lombok.Data;
|
||||
|
||||
/** 创建发件账户。 */
|
||||
@Data
|
||||
public class CreateCrmMailAccountDTO {
|
||||
|
||||
@NotBlank
|
||||
@Size(max = 100)
|
||||
private String name;
|
||||
|
||||
@NotBlank
|
||||
@Email
|
||||
@Size(max = 100)
|
||||
private String fromEmail;
|
||||
|
||||
@NotBlank
|
||||
@Size(max = 200)
|
||||
private String smtpHost;
|
||||
|
||||
@NotNull
|
||||
private Integer smtpPort;
|
||||
|
||||
/** 1=SSL/TLS,0=STARTTLS */
|
||||
private Integer smtpSsl;
|
||||
|
||||
@NotBlank
|
||||
@Size(max = 200)
|
||||
private String username;
|
||||
|
||||
@NotBlank
|
||||
@Size(max = 200)
|
||||
private String password;
|
||||
|
||||
private Integer enabled;
|
||||
|
||||
private Integer isDefault;
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.crm.mail.account.model.dto;
|
||||
|
||||
import jakarta.validation.constraints.Email;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import jakarta.validation.constraints.Size;
|
||||
import lombok.Data;
|
||||
|
||||
/** 更新发件账户;password 空则保留原密文。 */
|
||||
@Data
|
||||
public class UpdateCrmMailAccountDTO {
|
||||
|
||||
@NotBlank
|
||||
@Size(max = 100)
|
||||
private String name;
|
||||
|
||||
@NotBlank
|
||||
@Email
|
||||
@Size(max = 100)
|
||||
private String fromEmail;
|
||||
|
||||
@NotBlank
|
||||
@Size(max = 200)
|
||||
private String smtpHost;
|
||||
|
||||
@NotNull
|
||||
private Integer smtpPort;
|
||||
|
||||
private Integer smtpSsl;
|
||||
|
||||
@NotBlank
|
||||
@Size(max = 200)
|
||||
private String username;
|
||||
|
||||
/** 留空表示不改密码 */
|
||||
@Size(max = 200)
|
||||
private String password;
|
||||
|
||||
private Integer enabled;
|
||||
|
||||
private Integer isDefault;
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.crm.mail.account.model.po;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.*;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/** 公司 SMTP 发件账户。 */
|
||||
@Data
|
||||
@TableName("crm_mail_account")
|
||||
public class CrmMailAccountPO {
|
||||
|
||||
@TableId(value = "id", type = IdType.AUTO)
|
||||
private Long id;
|
||||
@TableField("company_id")
|
||||
private Long companyId;
|
||||
@TableField("name")
|
||||
private String name;
|
||||
@TableField("from_email")
|
||||
private String fromEmail;
|
||||
@TableField("smtp_host")
|
||||
private String smtpHost;
|
||||
@TableField("smtp_port")
|
||||
private Integer smtpPort;
|
||||
@TableField("smtp_ssl")
|
||||
private Integer smtpSsl;
|
||||
@TableField("username")
|
||||
private String username;
|
||||
@TableField("password_cipher")
|
||||
private String passwordCipher;
|
||||
@TableField("enabled")
|
||||
private Integer enabled;
|
||||
@TableField("is_default")
|
||||
private Integer isDefault;
|
||||
@TableField(value = "create_time", fill = FieldFill.INSERT)
|
||||
private LocalDateTime createTime;
|
||||
@TableField(value = "update_time", fill = FieldFill.INSERT_UPDATE)
|
||||
private LocalDateTime updateTime;
|
||||
@TableField("create_by")
|
||||
private Long createBy;
|
||||
@TableField("update_by")
|
||||
private Long updateBy;
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.crm.mail.account.model.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/** 发件账户视图(不含密码)。 */
|
||||
@Data
|
||||
public class CrmMailAccountVO {
|
||||
private Long id;
|
||||
private Long companyId;
|
||||
private String name;
|
||||
private String fromEmail;
|
||||
private String smtpHost;
|
||||
private Integer smtpPort;
|
||||
private Integer smtpSsl;
|
||||
private String username;
|
||||
private Integer enabled;
|
||||
private Integer isDefault;
|
||||
private Boolean passwordSet;
|
||||
private LocalDateTime createTime;
|
||||
private LocalDateTime updateTime;
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.crm.mail.account.service;
|
||||
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.account.model.dto.CreateCrmMailAccountDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.account.model.dto.UpdateCrmMailAccountDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.account.model.po.CrmMailAccountPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.account.model.vo.CrmMailAccountVO;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/** 发件账户用例。 */
|
||||
public interface CrmMailAccountService {
|
||||
|
||||
CrmMailAccountVO create(CreateCrmMailAccountDTO dto);
|
||||
|
||||
CrmMailAccountVO update(Long id, UpdateCrmMailAccountDTO dto);
|
||||
|
||||
void delete(Long id);
|
||||
|
||||
CrmMailAccountVO get(Long id);
|
||||
|
||||
List<CrmMailAccountVO> listAll();
|
||||
|
||||
/** 启用账户下拉(发信用)。 */
|
||||
List<CrmMailAccountVO> listEnabled();
|
||||
|
||||
/** 解析发件账户:指定 id,否则取默认启用账户。 */
|
||||
CrmMailAccountPO requireForSend(Long companyId, Long accountId);
|
||||
|
||||
String decryptPassword(CrmMailAccountPO account);
|
||||
}
|
||||
+196
@@ -0,0 +1,196 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.crm.mail.account.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StringUtils;
|
||||
import top.xyquan.ecomdmbbackend.common.exception.BusinessException;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.account.mapper.CrmMailAccountMapper;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.account.model.dto.CreateCrmMailAccountDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.account.model.dto.UpdateCrmMailAccountDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.account.model.po.CrmMailAccountPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.account.model.vo.CrmMailAccountVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.account.service.CrmMailAccountService;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.support.CrmSecretCrypto;
|
||||
import top.xyquan.ecomdmbbackend.utils.CurrentUserContext;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 发件账户:密码入库加密;VO 不回传明文。
|
||||
*/
|
||||
@Service
|
||||
public class CrmMailAccountServiceImpl extends ServiceImpl<CrmMailAccountMapper, CrmMailAccountPO>
|
||||
implements CrmMailAccountService {
|
||||
|
||||
private final CrmSecretCrypto secretCrypto;
|
||||
private final CurrentUserContext currentUserContext;
|
||||
|
||||
public CrmMailAccountServiceImpl(CrmSecretCrypto secretCrypto,
|
||||
CurrentUserContext currentUserContext) {
|
||||
this.secretCrypto = secretCrypto;
|
||||
this.currentUserContext = currentUserContext;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public CrmMailAccountVO create(CreateCrmMailAccountDTO dto) {
|
||||
Long companyId = requireCompanyId();
|
||||
Long userId = currentUserContext.getCurrentUserId();
|
||||
CrmMailAccountPO po = new CrmMailAccountPO();
|
||||
po.setCompanyId(companyId);
|
||||
applyFields(po, dto.getName(), dto.getFromEmail(), dto.getSmtpHost(), dto.getSmtpPort(),
|
||||
dto.getSmtpSsl(), dto.getUsername(), dto.getEnabled(), dto.getIsDefault());
|
||||
po.setPasswordCipher(secretCrypto.encrypt(dto.getPassword()));
|
||||
po.setCreateBy(userId);
|
||||
po.setUpdateBy(userId);
|
||||
this.save(po);
|
||||
if (Integer.valueOf(1).equals(po.getIsDefault())) {
|
||||
clearOtherDefaults(companyId, po.getId());
|
||||
}
|
||||
return toVo(po);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public CrmMailAccountVO update(Long id, UpdateCrmMailAccountDTO dto) {
|
||||
CrmMailAccountPO po = requireOwned(id);
|
||||
applyFields(po, dto.getName(), dto.getFromEmail(), dto.getSmtpHost(), dto.getSmtpPort(),
|
||||
dto.getSmtpSsl(), dto.getUsername(), dto.getEnabled(), dto.getIsDefault());
|
||||
if (StringUtils.hasText(dto.getPassword())) {
|
||||
po.setPasswordCipher(secretCrypto.encrypt(dto.getPassword().trim()));
|
||||
}
|
||||
po.setUpdateBy(currentUserContext.getCurrentUserId());
|
||||
this.updateById(po);
|
||||
if (Integer.valueOf(1).equals(po.getIsDefault())) {
|
||||
clearOtherDefaults(po.getCompanyId(), po.getId());
|
||||
}
|
||||
return toVo(po);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void delete(Long id) {
|
||||
requireOwned(id);
|
||||
this.removeById(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CrmMailAccountVO get(Long id) {
|
||||
return toVo(requireOwned(id));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<CrmMailAccountVO> listAll() {
|
||||
Long companyId = requireCompanyId();
|
||||
return this.list(new LambdaQueryWrapper<CrmMailAccountPO>()
|
||||
.eq(CrmMailAccountPO::getCompanyId, companyId)
|
||||
.orderByDesc(CrmMailAccountPO::getIsDefault)
|
||||
.orderByDesc(CrmMailAccountPO::getId))
|
||||
.stream().map(this::toVo).toList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<CrmMailAccountVO> listEnabled() {
|
||||
Long companyId = requireCompanyId();
|
||||
return this.list(new LambdaQueryWrapper<CrmMailAccountPO>()
|
||||
.eq(CrmMailAccountPO::getCompanyId, companyId)
|
||||
.eq(CrmMailAccountPO::getEnabled, 1)
|
||||
.orderByDesc(CrmMailAccountPO::getIsDefault)
|
||||
.orderByDesc(CrmMailAccountPO::getId))
|
||||
.stream().map(this::toVo).toList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public CrmMailAccountPO requireForSend(Long companyId, Long accountId) {
|
||||
CrmMailAccountPO account;
|
||||
if (accountId != null) {
|
||||
account = this.getById(accountId);
|
||||
if (account == null || !companyId.equals(account.getCompanyId())) {
|
||||
throw new BusinessException("发件账户不存在");
|
||||
}
|
||||
} else {
|
||||
account = this.getOne(new LambdaQueryWrapper<CrmMailAccountPO>()
|
||||
.eq(CrmMailAccountPO::getCompanyId, companyId)
|
||||
.eq(CrmMailAccountPO::getEnabled, 1)
|
||||
.eq(CrmMailAccountPO::getIsDefault, 1)
|
||||
.last("LIMIT 1"));
|
||||
if (account == null) {
|
||||
account = this.getOne(new LambdaQueryWrapper<CrmMailAccountPO>()
|
||||
.eq(CrmMailAccountPO::getCompanyId, companyId)
|
||||
.eq(CrmMailAccountPO::getEnabled, 1)
|
||||
.orderByDesc(CrmMailAccountPO::getId)
|
||||
.last("LIMIT 1"));
|
||||
}
|
||||
}
|
||||
if (account == null) {
|
||||
throw new BusinessException("请先配置启用的发件账户");
|
||||
}
|
||||
if (!Integer.valueOf(1).equals(account.getEnabled())) {
|
||||
throw new BusinessException("发件账户未启用");
|
||||
}
|
||||
return account;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String decryptPassword(CrmMailAccountPO account) {
|
||||
return secretCrypto.decrypt(account.getPasswordCipher());
|
||||
}
|
||||
|
||||
private void clearOtherDefaults(Long companyId, Long keepId) {
|
||||
this.update(null, new LambdaUpdateWrapper<CrmMailAccountPO>()
|
||||
.eq(CrmMailAccountPO::getCompanyId, companyId)
|
||||
.ne(CrmMailAccountPO::getId, keepId)
|
||||
.eq(CrmMailAccountPO::getIsDefault, 1)
|
||||
.set(CrmMailAccountPO::getIsDefault, 0));
|
||||
}
|
||||
|
||||
private void applyFields(CrmMailAccountPO po, String name, String fromEmail, String host,
|
||||
Integer port, Integer ssl, String username, Integer enabled, Integer isDefault) {
|
||||
po.setName(name.trim());
|
||||
po.setFromEmail(fromEmail.trim());
|
||||
po.setSmtpHost(host.trim());
|
||||
po.setSmtpPort(port != null && port > 0 ? port : 465);
|
||||
po.setSmtpSsl(ssl == null || ssl == 1 ? 1 : 0);
|
||||
po.setUsername(username.trim());
|
||||
po.setEnabled(enabled == null || enabled == 1 ? 1 : 0);
|
||||
po.setIsDefault(isDefault != null && isDefault == 1 ? 1 : 0);
|
||||
}
|
||||
|
||||
private CrmMailAccountVO toVo(CrmMailAccountPO po) {
|
||||
CrmMailAccountVO vo = new CrmMailAccountVO();
|
||||
vo.setId(po.getId());
|
||||
vo.setCompanyId(po.getCompanyId());
|
||||
vo.setName(po.getName());
|
||||
vo.setFromEmail(po.getFromEmail());
|
||||
vo.setSmtpHost(po.getSmtpHost());
|
||||
vo.setSmtpPort(po.getSmtpPort());
|
||||
vo.setSmtpSsl(po.getSmtpSsl());
|
||||
vo.setUsername(po.getUsername());
|
||||
vo.setEnabled(po.getEnabled());
|
||||
vo.setIsDefault(po.getIsDefault());
|
||||
vo.setPasswordSet(StringUtils.hasText(po.getPasswordCipher()));
|
||||
vo.setCreateTime(po.getCreateTime());
|
||||
vo.setUpdateTime(po.getUpdateTime());
|
||||
return vo;
|
||||
}
|
||||
|
||||
private CrmMailAccountPO requireOwned(Long id) {
|
||||
Long companyId = requireCompanyId();
|
||||
CrmMailAccountPO po = this.getById(id);
|
||||
if (po == null || !companyId.equals(po.getCompanyId())) {
|
||||
throw new BusinessException("发件账户不存在");
|
||||
}
|
||||
return po;
|
||||
}
|
||||
|
||||
private Long requireCompanyId() {
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
if (companyId == null) {
|
||||
throw new BusinessException("请先选择公司");
|
||||
}
|
||||
return companyId;
|
||||
}
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.crm.mail.log.controller;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import top.xyquan.ecomdmbbackend.common.annotation.RequirePermission;
|
||||
import top.xyquan.ecomdmbbackend.common.model.dto.PageQueryDTO;
|
||||
import top.xyquan.ecomdmbbackend.common.model.vo.PageResultVO;
|
||||
import top.xyquan.ecomdmbbackend.common.response.ApiResponse;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.log.model.dto.CrmMailLogFilterDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.log.model.vo.CrmMailLogVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.log.service.CrmMailLogService;
|
||||
|
||||
/** 邮件发送记录。 */
|
||||
@RestController
|
||||
@RequestMapping("/api/crm/mail-log")
|
||||
public class CrmMailLogController {
|
||||
|
||||
private final CrmMailLogService mailLogService;
|
||||
|
||||
public CrmMailLogController(CrmMailLogService mailLogService) {
|
||||
this.mailLogService = mailLogService;
|
||||
}
|
||||
|
||||
@PostMapping("/list")
|
||||
@RequirePermission(value = "crm:mail-log:read", description = "发送记录列表")
|
||||
public ApiResponse<PageResultVO<CrmMailLogVO>> list(
|
||||
@Valid @RequestBody PageQueryDTO<CrmMailLogFilterDTO> query) {
|
||||
return ApiResponse.success("获取成功", mailLogService.page(query));
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
@RequirePermission(value = "crm:mail-log:read", description = "发送记录详情")
|
||||
public ApiResponse<CrmMailLogVO> get(@PathVariable Long id) {
|
||||
return ApiResponse.success("获取成功", mailLogService.get(id));
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.crm.mail.log.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.log.model.po.CrmMailLogPO;
|
||||
|
||||
/** 发送记录 Mapper。 */
|
||||
@Mapper
|
||||
public interface CrmMailLogMapper extends BaseMapper<CrmMailLogPO> {
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.crm.mail.log.model.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/** 发送记录筛选。 */
|
||||
@Data
|
||||
public class CrmMailLogFilterDTO {
|
||||
private String keyword;
|
||||
private String status;
|
||||
private Long customerId;
|
||||
private String refType;
|
||||
private Long refId;
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.crm.mail.log.model.po;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.*;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/** 邮件发送记录。 */
|
||||
@Data
|
||||
@TableName("crm_mail_log")
|
||||
public class CrmMailLogPO {
|
||||
|
||||
@TableId(value = "id", type = IdType.AUTO)
|
||||
private Long id;
|
||||
@TableField("company_id")
|
||||
private Long companyId;
|
||||
@TableField("account_id")
|
||||
private Long accountId;
|
||||
@TableField("template_id")
|
||||
private Long templateId;
|
||||
@TableField("customer_id")
|
||||
private Long customerId;
|
||||
@TableField("ref_type")
|
||||
private String refType;
|
||||
@TableField("ref_id")
|
||||
private Long refId;
|
||||
@TableField("to_email")
|
||||
private String toEmail;
|
||||
@TableField("cc_email")
|
||||
private String ccEmail;
|
||||
@TableField("subject")
|
||||
private String subject;
|
||||
@TableField("body_html")
|
||||
private String bodyHtml;
|
||||
@TableField("status")
|
||||
private String status;
|
||||
@TableField("error_message")
|
||||
private String errorMessage;
|
||||
@TableField("sent_at")
|
||||
private LocalDateTime sentAt;
|
||||
@TableField(value = "create_time", fill = FieldFill.INSERT)
|
||||
private LocalDateTime createTime;
|
||||
@TableField("create_by")
|
||||
private Long createBy;
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.crm.mail.log.model.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/** 发送记录视图。 */
|
||||
@Data
|
||||
public class CrmMailLogVO {
|
||||
private Long id;
|
||||
private Long companyId;
|
||||
private Long accountId;
|
||||
private String accountName;
|
||||
private Long templateId;
|
||||
private String templateName;
|
||||
private Long customerId;
|
||||
private String customerName;
|
||||
private String refType;
|
||||
private Long refId;
|
||||
private String toEmail;
|
||||
private String ccEmail;
|
||||
private String subject;
|
||||
private String bodyHtml;
|
||||
private String status;
|
||||
private String errorMessage;
|
||||
private LocalDateTime sentAt;
|
||||
private LocalDateTime createTime;
|
||||
private Long createBy;
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.crm.mail.log.service;
|
||||
|
||||
import top.xyquan.ecomdmbbackend.common.model.dto.PageQueryDTO;
|
||||
import top.xyquan.ecomdmbbackend.common.model.vo.PageResultVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.log.model.dto.CrmMailLogFilterDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.log.model.po.CrmMailLogPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.log.model.vo.CrmMailLogVO;
|
||||
|
||||
/** 发送记录查询与落库。 */
|
||||
public interface CrmMailLogService {
|
||||
|
||||
PageResultVO<CrmMailLogVO> page(PageQueryDTO<CrmMailLogFilterDTO> query);
|
||||
|
||||
CrmMailLogVO get(Long id);
|
||||
|
||||
void insertLog(CrmMailLogPO log);
|
||||
}
|
||||
+183
@@ -0,0 +1,183 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.crm.mail.log.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.StringUtils;
|
||||
import top.xyquan.ecomdmbbackend.common.exception.BusinessException;
|
||||
import top.xyquan.ecomdmbbackend.common.model.dto.PageQueryDTO;
|
||||
import top.xyquan.ecomdmbbackend.common.model.vo.PageResultVO;
|
||||
import top.xyquan.ecomdmbbackend.common.utils.BeanCopyUtils;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.customer.mapper.CrmCustomerMapper;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.customer.model.po.CrmCustomerPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.account.mapper.CrmMailAccountMapper;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.account.model.po.CrmMailAccountPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.log.mapper.CrmMailLogMapper;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.log.model.dto.CrmMailLogFilterDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.log.model.po.CrmMailLogPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.log.model.vo.CrmMailLogVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.log.service.CrmMailLogService;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.template.mapper.CrmMailTemplateMapper;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.template.model.po.CrmMailTemplatePO;
|
||||
import top.xyquan.ecomdmbbackend.utils.CurrentUserContext;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/** 发送记录:只读列表 + 写入。 */
|
||||
@Service
|
||||
public class CrmMailLogServiceImpl extends ServiceImpl<CrmMailLogMapper, CrmMailLogPO>
|
||||
implements CrmMailLogService {
|
||||
|
||||
private final CrmCustomerMapper customerMapper;
|
||||
private final CrmMailAccountMapper accountMapper;
|
||||
private final CrmMailTemplateMapper templateMapper;
|
||||
private final CurrentUserContext currentUserContext;
|
||||
|
||||
public CrmMailLogServiceImpl(CrmCustomerMapper customerMapper,
|
||||
CrmMailAccountMapper accountMapper,
|
||||
CrmMailTemplateMapper templateMapper,
|
||||
CurrentUserContext currentUserContext) {
|
||||
this.customerMapper = customerMapper;
|
||||
this.accountMapper = accountMapper;
|
||||
this.templateMapper = templateMapper;
|
||||
this.currentUserContext = currentUserContext;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageResultVO<CrmMailLogVO> page(PageQueryDTO<CrmMailLogFilterDTO> query) {
|
||||
Long companyId = requireCompanyId();
|
||||
CrmMailLogFilterDTO filters =
|
||||
query.getFilters() != null ? query.getFilters() : new CrmMailLogFilterDTO();
|
||||
LambdaQueryWrapper<CrmMailLogPO> w = new LambdaQueryWrapper<>();
|
||||
w.eq(CrmMailLogPO::getCompanyId, companyId);
|
||||
if (StringUtils.hasText(filters.getStatus())) {
|
||||
w.eq(CrmMailLogPO::getStatus, filters.getStatus().trim());
|
||||
}
|
||||
if (filters.getCustomerId() != null) {
|
||||
w.eq(CrmMailLogPO::getCustomerId, filters.getCustomerId());
|
||||
}
|
||||
if (StringUtils.hasText(filters.getRefType())) {
|
||||
w.eq(CrmMailLogPO::getRefType, filters.getRefType().trim());
|
||||
}
|
||||
if (filters.getRefId() != null) {
|
||||
w.eq(CrmMailLogPO::getRefId, filters.getRefId());
|
||||
}
|
||||
if (StringUtils.hasText(filters.getKeyword())) {
|
||||
String kw = filters.getKeyword().trim();
|
||||
w.and(x -> x.like(CrmMailLogPO::getToEmail, kw)
|
||||
.or().like(CrmMailLogPO::getSubject, kw));
|
||||
}
|
||||
w.orderByDesc(CrmMailLogPO::getId);
|
||||
IPage<CrmMailLogPO> page = this.page(new Page<>(query.getPage(), query.getPageSize()), w);
|
||||
|
||||
Set<Long> customerIds = page.getRecords().stream()
|
||||
.map(CrmMailLogPO::getCustomerId).filter(Objects::nonNull).collect(Collectors.toSet());
|
||||
Set<Long> accountIds = page.getRecords().stream()
|
||||
.map(CrmMailLogPO::getAccountId).filter(Objects::nonNull).collect(Collectors.toSet());
|
||||
Set<Long> templateIds = page.getRecords().stream()
|
||||
.map(CrmMailLogPO::getTemplateId).filter(Objects::nonNull).collect(Collectors.toSet());
|
||||
|
||||
Map<Long, String> customers = loadCustomerNames(companyId, customerIds);
|
||||
Map<Long, String> accounts = loadAccountNames(companyId, accountIds);
|
||||
Map<Long, String> templates = loadTemplateNames(companyId, templateIds);
|
||||
|
||||
List<CrmMailLogVO> records = page.getRecords().stream().map(log -> {
|
||||
CrmMailLogVO vo = toVo(log);
|
||||
if (log.getCustomerId() != null) {
|
||||
vo.setCustomerName(customers.get(log.getCustomerId()));
|
||||
}
|
||||
if (log.getAccountId() != null) {
|
||||
vo.setAccountName(accounts.get(log.getAccountId()));
|
||||
}
|
||||
if (log.getTemplateId() != null) {
|
||||
vo.setTemplateName(templates.get(log.getTemplateId()));
|
||||
}
|
||||
return vo;
|
||||
}).toList();
|
||||
return PageResultVO.create(records, page.getTotal(), query.getPage(), query.getPageSize());
|
||||
}
|
||||
|
||||
@Override
|
||||
public CrmMailLogVO get(Long id) {
|
||||
Long companyId = requireCompanyId();
|
||||
CrmMailLogPO log = this.getById(id);
|
||||
if (log == null || !companyId.equals(log.getCompanyId())) {
|
||||
throw new BusinessException("发送记录不存在");
|
||||
}
|
||||
CrmMailLogVO vo = toVo(log);
|
||||
if (log.getCustomerId() != null) {
|
||||
CrmCustomerPO c = customerMapper.selectById(log.getCustomerId());
|
||||
if (c != null) {
|
||||
vo.setCustomerName(c.getName());
|
||||
}
|
||||
}
|
||||
if (log.getAccountId() != null) {
|
||||
CrmMailAccountPO a = accountMapper.selectById(log.getAccountId());
|
||||
if (a != null) {
|
||||
vo.setAccountName(a.getName());
|
||||
}
|
||||
}
|
||||
if (log.getTemplateId() != null) {
|
||||
CrmMailTemplatePO t = templateMapper.selectById(log.getTemplateId());
|
||||
if (t != null) {
|
||||
vo.setTemplateName(t.getName());
|
||||
}
|
||||
}
|
||||
return vo;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void insertLog(CrmMailLogPO log) {
|
||||
this.baseMapper.insert(log);
|
||||
}
|
||||
|
||||
private CrmMailLogVO toVo(CrmMailLogPO po) {
|
||||
CrmMailLogVO vo = new CrmMailLogVO();
|
||||
BeanCopyUtils.copyProperties(po, vo);
|
||||
return vo;
|
||||
}
|
||||
|
||||
private Map<Long, String> loadCustomerNames(Long companyId, Set<Long> ids) {
|
||||
if (ids.isEmpty()) {
|
||||
return Map.of();
|
||||
}
|
||||
return customerMapper.selectList(new LambdaQueryWrapper<CrmCustomerPO>()
|
||||
.eq(CrmCustomerPO::getCompanyId, companyId)
|
||||
.in(CrmCustomerPO::getId, ids))
|
||||
.stream()
|
||||
.collect(Collectors.toMap(CrmCustomerPO::getId, CrmCustomerPO::getName, (a, b) -> a));
|
||||
}
|
||||
|
||||
private Map<Long, String> loadAccountNames(Long companyId, Set<Long> ids) {
|
||||
if (ids.isEmpty()) {
|
||||
return Map.of();
|
||||
}
|
||||
return accountMapper.selectList(new LambdaQueryWrapper<CrmMailAccountPO>()
|
||||
.eq(CrmMailAccountPO::getCompanyId, companyId)
|
||||
.in(CrmMailAccountPO::getId, ids))
|
||||
.stream()
|
||||
.collect(Collectors.toMap(CrmMailAccountPO::getId, CrmMailAccountPO::getName, (a, b) -> a));
|
||||
}
|
||||
|
||||
private Map<Long, String> loadTemplateNames(Long companyId, Set<Long> ids) {
|
||||
if (ids.isEmpty()) {
|
||||
return Map.of();
|
||||
}
|
||||
return templateMapper.selectList(new LambdaQueryWrapper<CrmMailTemplatePO>()
|
||||
.eq(CrmMailTemplatePO::getCompanyId, companyId)
|
||||
.in(CrmMailTemplatePO::getId, ids))
|
||||
.stream()
|
||||
.collect(Collectors.toMap(CrmMailTemplatePO::getId, CrmMailTemplatePO::getName, (a, b) -> a));
|
||||
}
|
||||
|
||||
private Long requireCompanyId() {
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
if (companyId == null) {
|
||||
throw new BusinessException("请先选择公司");
|
||||
}
|
||||
return companyId;
|
||||
}
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.crm.mail.send.controller;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import top.xyquan.ecomdmbbackend.common.annotation.RequirePermission;
|
||||
import top.xyquan.ecomdmbbackend.common.response.ApiResponse;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.log.model.vo.CrmMailLogVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.send.model.dto.CrmMailPreviewDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.send.model.dto.CrmMailSendDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.send.model.vo.CrmMailPreviewVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.send.service.CrmMailSendService;
|
||||
|
||||
/** 手发邮件:预览与确认发送。 */
|
||||
@RestController
|
||||
@RequestMapping("/api/crm/mail")
|
||||
public class CrmMailSendController {
|
||||
|
||||
private final CrmMailSendService mailSendService;
|
||||
|
||||
public CrmMailSendController(CrmMailSendService mailSendService) {
|
||||
this.mailSendService = mailSendService;
|
||||
}
|
||||
|
||||
@PostMapping("/preview")
|
||||
@RequirePermission(value = "crm:mail:send", description = "预览邮件")
|
||||
public ApiResponse<CrmMailPreviewVO> preview(@Valid @RequestBody CrmMailPreviewDTO dto) {
|
||||
return ApiResponse.success("预览成功", mailSendService.preview(dto));
|
||||
}
|
||||
|
||||
@PostMapping("/send")
|
||||
@RequirePermission(value = "crm:mail:send", description = "发送邮件")
|
||||
public ApiResponse<CrmMailLogVO> send(@Valid @RequestBody CrmMailSendDTO dto) {
|
||||
return ApiResponse.success("发送成功", mailSendService.send(dto));
|
||||
}
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.crm.mail.send.model.dto;
|
||||
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.Data;
|
||||
|
||||
/** 预览:模板 + 客户/业务单上下文。 */
|
||||
@Data
|
||||
public class CrmMailPreviewDTO {
|
||||
|
||||
@NotNull(message = "模板不能为空")
|
||||
private Long templateId;
|
||||
|
||||
@NotNull(message = "客户不能为空")
|
||||
private Long customerId;
|
||||
|
||||
/** quote / shipment,可空 */
|
||||
private String refType;
|
||||
|
||||
private Long refId;
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.crm.mail.send.model.dto;
|
||||
|
||||
import jakarta.validation.constraints.Email;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import jakarta.validation.constraints.Size;
|
||||
import lombok.Data;
|
||||
|
||||
/** 确认发送:用户可改主题/正文后再发。 */
|
||||
@Data
|
||||
public class CrmMailSendDTO {
|
||||
|
||||
/** 空则用默认发件账户 */
|
||||
private Long accountId;
|
||||
|
||||
private Long templateId;
|
||||
|
||||
@NotNull(message = "客户不能为空")
|
||||
private Long customerId;
|
||||
|
||||
private String refType;
|
||||
|
||||
private Long refId;
|
||||
|
||||
@NotBlank
|
||||
@Email
|
||||
@Size(max = 200)
|
||||
private String toEmail;
|
||||
|
||||
@Size(max = 500)
|
||||
private String ccEmail;
|
||||
|
||||
@NotBlank
|
||||
@Size(max = 500)
|
||||
private String subject;
|
||||
|
||||
@NotBlank
|
||||
private String bodyHtml;
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.crm.mail.send.model.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/** 预览结果(已替换变量)。 */
|
||||
@Data
|
||||
public class CrmMailPreviewVO {
|
||||
private Long templateId;
|
||||
private String templateName;
|
||||
private String scene;
|
||||
private Long customerId;
|
||||
private String toEmail;
|
||||
private String subject;
|
||||
private String bodyHtml;
|
||||
private Map<String, String> variables;
|
||||
}
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.crm.mail.send.service;
|
||||
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.log.model.vo.CrmMailLogVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.send.model.dto.CrmMailPreviewDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.send.model.dto.CrmMailSendDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.send.model.vo.CrmMailPreviewVO;
|
||||
|
||||
/** 手发邮件:预览变量 → 确认发送 → 写日志。 */
|
||||
public interface CrmMailSendService {
|
||||
|
||||
CrmMailPreviewVO preview(CrmMailPreviewDTO dto);
|
||||
|
||||
CrmMailLogVO send(CrmMailSendDTO dto);
|
||||
}
|
||||
+276
@@ -0,0 +1,276 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.crm.mail.send.service.impl;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StringUtils;
|
||||
import top.xyquan.ecomdmbbackend.common.exception.BusinessException;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.customer.mapper.CrmCustomerMapper;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.customer.model.po.CrmCustomerPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.account.model.po.CrmMailAccountPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.account.service.CrmMailAccountService;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.log.model.po.CrmMailLogPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.log.model.vo.CrmMailLogVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.log.service.CrmMailLogService;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.send.model.dto.CrmMailPreviewDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.send.model.dto.CrmMailSendDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.send.model.vo.CrmMailPreviewVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.send.service.CrmMailSendService;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.template.model.po.CrmMailTemplatePO;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.template.service.CrmMailTemplateService;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.profile.model.vo.CrmCompanyProfileVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.profile.service.CrmCompanyProfileService;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.quote.mapper.CrmQuoteMapper;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.quote.model.po.CrmQuotePO;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.shipment.mapper.CrmShipmentMapper;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.shipment.model.po.CrmShipmentPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.support.CrmMailVariableRenderer;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.support.CrmSmtpClient;
|
||||
import top.xyquan.ecomdmbbackend.utils.CurrentUserContext;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 邮件发送编排:拼变量 → SMTP → 落 crm_mail_log(成功/失败均记)。
|
||||
*/
|
||||
@Service
|
||||
public class CrmMailSendServiceImpl implements CrmMailSendService {
|
||||
|
||||
private final CrmMailTemplateService templateService;
|
||||
private final CrmMailAccountService accountService;
|
||||
private final CrmMailLogService mailLogService;
|
||||
private final CrmCustomerMapper customerMapper;
|
||||
private final CrmQuoteMapper quoteMapper;
|
||||
private final CrmShipmentMapper shipmentMapper;
|
||||
private final CrmCompanyProfileService profileService;
|
||||
private final CrmMailVariableRenderer variableRenderer;
|
||||
private final CrmSmtpClient smtpClient;
|
||||
private final CurrentUserContext currentUserContext;
|
||||
|
||||
public CrmMailSendServiceImpl(CrmMailTemplateService templateService,
|
||||
CrmMailAccountService accountService,
|
||||
CrmMailLogService mailLogService,
|
||||
CrmCustomerMapper customerMapper,
|
||||
CrmQuoteMapper quoteMapper,
|
||||
CrmShipmentMapper shipmentMapper,
|
||||
CrmCompanyProfileService profileService,
|
||||
CrmMailVariableRenderer variableRenderer,
|
||||
CrmSmtpClient smtpClient,
|
||||
CurrentUserContext currentUserContext) {
|
||||
this.templateService = templateService;
|
||||
this.accountService = accountService;
|
||||
this.mailLogService = mailLogService;
|
||||
this.customerMapper = customerMapper;
|
||||
this.quoteMapper = quoteMapper;
|
||||
this.shipmentMapper = shipmentMapper;
|
||||
this.profileService = profileService;
|
||||
this.variableRenderer = variableRenderer;
|
||||
this.smtpClient = smtpClient;
|
||||
this.currentUserContext = currentUserContext;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CrmMailPreviewVO preview(CrmMailPreviewDTO dto) {
|
||||
Long companyId = requireCompanyId();
|
||||
CrmMailTemplatePO template = templateService.requireOwned(companyId, dto.getTemplateId());
|
||||
CrmCustomerPO customer = requireCustomer(companyId, dto.getCustomerId());
|
||||
Map<String, String> vars = buildVariables(companyId, customer, dto.getRefType(), dto.getRefId());
|
||||
|
||||
CrmMailPreviewVO vo = new CrmMailPreviewVO();
|
||||
vo.setTemplateId(template.getId());
|
||||
vo.setTemplateName(template.getName());
|
||||
vo.setScene(template.getScene());
|
||||
vo.setCustomerId(customer.getId());
|
||||
vo.setToEmail(customer.getEmail());
|
||||
vo.setSubject(variableRenderer.render(template.getSubject(), vars));
|
||||
vo.setBodyHtml(variableRenderer.render(template.getBodyHtml(), vars));
|
||||
vo.setVariables(vars);
|
||||
return vo;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public CrmMailLogVO send(CrmMailSendDTO dto) {
|
||||
Long companyId = requireCompanyId();
|
||||
Long userId = currentUserContext.getCurrentUserId();
|
||||
requireCustomer(companyId, dto.getCustomerId());
|
||||
if (dto.getTemplateId() != null) {
|
||||
templateService.requireOwned(companyId, dto.getTemplateId());
|
||||
}
|
||||
validateRef(companyId, dto.getCustomerId(), dto.getRefType(), dto.getRefId());
|
||||
|
||||
CrmMailAccountPO account = accountService.requireForSend(companyId, dto.getAccountId());
|
||||
String password = accountService.decryptPassword(account);
|
||||
|
||||
CrmMailLogPO log = new CrmMailLogPO();
|
||||
log.setCompanyId(companyId);
|
||||
log.setAccountId(account.getId());
|
||||
log.setTemplateId(dto.getTemplateId());
|
||||
log.setCustomerId(dto.getCustomerId());
|
||||
log.setRefType(trimToNull(dto.getRefType()));
|
||||
log.setRefId(dto.getRefId());
|
||||
log.setToEmail(dto.getToEmail().trim());
|
||||
log.setCcEmail(trimToNull(dto.getCcEmail()));
|
||||
log.setSubject(dto.getSubject().trim());
|
||||
log.setBodyHtml(dto.getBodyHtml());
|
||||
log.setCreateBy(userId);
|
||||
|
||||
try {
|
||||
smtpClient.send(
|
||||
new CrmSmtpClient.SmtpAccount(
|
||||
account.getName(),
|
||||
account.getFromEmail(),
|
||||
account.getSmtpHost(),
|
||||
account.getSmtpPort(),
|
||||
Integer.valueOf(1).equals(account.getSmtpSsl()),
|
||||
account.getUsername(),
|
||||
password),
|
||||
log.getToEmail(),
|
||||
log.getCcEmail(),
|
||||
log.getSubject(),
|
||||
log.getBodyHtml());
|
||||
log.setStatus("success");
|
||||
log.setSentAt(LocalDateTime.now());
|
||||
} catch (Exception e) {
|
||||
log.setStatus("failed");
|
||||
String msg = e.getMessage();
|
||||
if (msg != null && msg.length() > 1000) {
|
||||
msg = msg.substring(0, 1000);
|
||||
}
|
||||
log.setErrorMessage(msg);
|
||||
mailLogService.insertLog(log);
|
||||
throw new BusinessException("发送失败: " + e.getMessage());
|
||||
}
|
||||
mailLogService.insertLog(log);
|
||||
return mailLogService.get(log.getId());
|
||||
}
|
||||
|
||||
private Map<String, String> buildVariables(Long companyId, CrmCustomerPO customer,
|
||||
String refType, Long refId) {
|
||||
Map<String, String> vars = new LinkedHashMap<>();
|
||||
vars.put("customerName", nz(firstNonBlank(customer.getNameEn(), customer.getName())));
|
||||
vars.put("contactName", nz(customer.getContactName()));
|
||||
vars.put("customerEmail", nz(customer.getEmail()));
|
||||
vars.put("customerCountry", nz(customer.getCountry()));
|
||||
vars.put("customerPhone", nz(customer.getPhone()));
|
||||
|
||||
CrmCompanyProfileVO profile = profileService.getCurrent();
|
||||
vars.put("companyName", nz(firstNonBlank(profile.getLegalNameEn(), profile.getLegalNameZh())));
|
||||
vars.put("companyEmail", nz(profile.getEmail()));
|
||||
vars.put("companyPhone", nz(profile.getPhone()));
|
||||
|
||||
vars.put("quoteNo", "");
|
||||
vars.put("quoteDate", "");
|
||||
vars.put("totalAmount", "");
|
||||
vars.put("currency", nz(customer.getCurrency()));
|
||||
vars.put("incoterm", "");
|
||||
vars.put("shipmentNo", "");
|
||||
vars.put("destinationPort", "");
|
||||
vars.put("planShipDate", "");
|
||||
|
||||
if (!StringUtils.hasText(refType) || refId == null) {
|
||||
return vars;
|
||||
}
|
||||
String type = refType.trim().toLowerCase(Locale.ROOT);
|
||||
if ("quote".equals(type)) {
|
||||
CrmQuotePO quote = quoteMapper.selectById(refId);
|
||||
if (quote == null || !companyId.equals(quote.getCompanyId())) {
|
||||
throw new BusinessException("报价不存在");
|
||||
}
|
||||
if (!customer.getId().equals(quote.getCustomerId())) {
|
||||
throw new BusinessException("报价与客户不匹配");
|
||||
}
|
||||
vars.put("quoteNo", nz(quote.getQuoteNo()));
|
||||
vars.put("quoteDate", quote.getQuoteDate() != null ? quote.getQuoteDate().toString() : "");
|
||||
vars.put("totalAmount", quote.getTotalAmount() != null ? quote.getTotalAmount().toPlainString() : "");
|
||||
vars.put("currency", nz(quote.getCurrency()));
|
||||
vars.put("incoterm", nz(quote.getIncoterm()));
|
||||
} else if ("shipment".equals(type)) {
|
||||
CrmShipmentPO ship = shipmentMapper.selectById(refId);
|
||||
if (ship == null || !companyId.equals(ship.getCompanyId())) {
|
||||
throw new BusinessException("出货单不存在");
|
||||
}
|
||||
if (!customer.getId().equals(ship.getCustomerId())) {
|
||||
throw new BusinessException("出货与客户不匹配");
|
||||
}
|
||||
vars.put("shipmentNo", nz(ship.getShipmentNo()));
|
||||
vars.put("destinationPort", nz(ship.getDestinationPort()));
|
||||
vars.put("planShipDate",
|
||||
ship.getPlanShipDate() != null ? ship.getPlanShipDate().toString() : "");
|
||||
vars.put("totalAmount", ship.getTotalAmount() != null ? ship.getTotalAmount().toPlainString() : "");
|
||||
vars.put("currency", nz(ship.getCurrency()));
|
||||
vars.put("incoterm", nz(ship.getIncoterm()));
|
||||
if (ship.getQuoteId() != null) {
|
||||
CrmQuotePO quote = quoteMapper.selectById(ship.getQuoteId());
|
||||
if (quote != null) {
|
||||
vars.put("quoteNo", nz(quote.getQuoteNo()));
|
||||
}
|
||||
}
|
||||
}
|
||||
return vars;
|
||||
}
|
||||
|
||||
private void validateRef(Long companyId, Long customerId, String refType, Long refId) {
|
||||
if (!StringUtils.hasText(refType) && refId == null) {
|
||||
return;
|
||||
}
|
||||
if (!StringUtils.hasText(refType) || refId == null) {
|
||||
throw new BusinessException("关联业务单类型与 ID 需同时提供");
|
||||
}
|
||||
String type = refType.trim().toLowerCase(Locale.ROOT);
|
||||
if ("quote".equals(type)) {
|
||||
CrmQuotePO quote = quoteMapper.selectById(refId);
|
||||
if (quote == null || !companyId.equals(quote.getCompanyId())
|
||||
|| !customerId.equals(quote.getCustomerId())) {
|
||||
throw new BusinessException("关联报价无效");
|
||||
}
|
||||
} else if ("shipment".equals(type)) {
|
||||
CrmShipmentPO ship = shipmentMapper.selectById(refId);
|
||||
if (ship == null || !companyId.equals(ship.getCompanyId())
|
||||
|| !customerId.equals(ship.getCustomerId())) {
|
||||
throw new BusinessException("关联出货无效");
|
||||
}
|
||||
} else {
|
||||
throw new BusinessException("不支持的关联类型: " + refType);
|
||||
}
|
||||
}
|
||||
|
||||
private CrmCustomerPO requireCustomer(Long companyId, Long customerId) {
|
||||
CrmCustomerPO customer = customerMapper.selectById(customerId);
|
||||
if (customer == null || !companyId.equals(customer.getCompanyId())) {
|
||||
throw new BusinessException("客户不存在");
|
||||
}
|
||||
return customer;
|
||||
}
|
||||
|
||||
private Long requireCompanyId() {
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
if (companyId == null) {
|
||||
throw new BusinessException("请先选择公司");
|
||||
}
|
||||
return companyId;
|
||||
}
|
||||
|
||||
private static String nz(String s) {
|
||||
return s == null ? "" : s;
|
||||
}
|
||||
|
||||
private static String trimToNull(String s) {
|
||||
if (!StringUtils.hasText(s)) {
|
||||
return null;
|
||||
}
|
||||
return s.trim();
|
||||
}
|
||||
|
||||
private static String firstNonBlank(String a, String b) {
|
||||
if (StringUtils.hasText(a)) {
|
||||
return a.trim();
|
||||
}
|
||||
if (StringUtils.hasText(b)) {
|
||||
return b.trim();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
}
|
||||
+67
@@ -0,0 +1,67 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.crm.mail.template.controller;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import top.xyquan.ecomdmbbackend.common.annotation.RequirePermission;
|
||||
import top.xyquan.ecomdmbbackend.common.model.dto.PageQueryDTO;
|
||||
import top.xyquan.ecomdmbbackend.common.model.vo.PageResultVO;
|
||||
import top.xyquan.ecomdmbbackend.common.response.ApiResponse;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.template.model.dto.CreateCrmMailTemplateDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.template.model.dto.CrmMailTemplateFilterDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.template.model.dto.UpdateCrmMailTemplateDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.template.model.vo.CrmMailTemplateVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.template.service.CrmMailTemplateService;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/** 邮件模板管理。 */
|
||||
@RestController
|
||||
@RequestMapping("/api/crm/mail-template")
|
||||
public class CrmMailTemplateController {
|
||||
|
||||
private final CrmMailTemplateService mailTemplateService;
|
||||
|
||||
public CrmMailTemplateController(CrmMailTemplateService mailTemplateService) {
|
||||
this.mailTemplateService = mailTemplateService;
|
||||
}
|
||||
|
||||
@PostMapping("/create")
|
||||
@RequirePermission(value = "crm:mail-template:create", description = "创建邮件模板")
|
||||
public ApiResponse<CrmMailTemplateVO> create(@Valid @RequestBody CreateCrmMailTemplateDTO dto) {
|
||||
return ApiResponse.success("创建成功", mailTemplateService.create(dto));
|
||||
}
|
||||
|
||||
@PutMapping("/update/{id}")
|
||||
@RequirePermission(value = "crm:mail-template:update", description = "更新邮件模板")
|
||||
public ApiResponse<CrmMailTemplateVO> update(
|
||||
@PathVariable Long id, @Valid @RequestBody UpdateCrmMailTemplateDTO dto) {
|
||||
return ApiResponse.success("更新成功", mailTemplateService.update(id, dto));
|
||||
}
|
||||
|
||||
@DeleteMapping("/delete/{id}")
|
||||
@RequirePermission(value = "crm:mail-template:delete", description = "删除邮件模板")
|
||||
public ApiResponse<Void> delete(@PathVariable Long id) {
|
||||
mailTemplateService.delete(id);
|
||||
return ApiResponse.success("删除成功");
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
@RequirePermission(value = "crm:mail-template:read", description = "邮件模板详情")
|
||||
public ApiResponse<CrmMailTemplateVO> get(@PathVariable Long id) {
|
||||
return ApiResponse.success("获取成功", mailTemplateService.get(id));
|
||||
}
|
||||
|
||||
@PostMapping("/list")
|
||||
@RequirePermission(value = "crm:mail-template:read", description = "邮件模板列表")
|
||||
public ApiResponse<PageResultVO<CrmMailTemplateVO>> list(
|
||||
@Valid @RequestBody PageQueryDTO<CrmMailTemplateFilterDTO> query) {
|
||||
return ApiResponse.success("获取成功", mailTemplateService.page(query));
|
||||
}
|
||||
|
||||
@GetMapping("/options")
|
||||
@RequirePermission(value = "crm:mail:send", description = "启用模板选项")
|
||||
public ApiResponse<List<CrmMailTemplateVO>> options(
|
||||
@RequestParam(required = false) String scene) {
|
||||
return ApiResponse.success("获取成功", mailTemplateService.listEnabled(scene));
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.crm.mail.template.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.template.model.po.CrmMailTemplatePO;
|
||||
|
||||
/** 邮件模板 Mapper。 */
|
||||
@Mapper
|
||||
public interface CrmMailTemplateMapper extends BaseMapper<CrmMailTemplatePO> {
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.crm.mail.template.model.dto;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.Size;
|
||||
import lombok.Data;
|
||||
|
||||
/** 创建邮件模板。 */
|
||||
@Data
|
||||
public class CreateCrmMailTemplateDTO {
|
||||
|
||||
@NotBlank
|
||||
@Size(max = 100)
|
||||
private String name;
|
||||
|
||||
@NotBlank
|
||||
@Size(max = 32)
|
||||
private String scene;
|
||||
|
||||
@NotBlank
|
||||
@Size(max = 500)
|
||||
private String subject;
|
||||
|
||||
@NotBlank
|
||||
private String bodyHtml;
|
||||
|
||||
private Integer enabled;
|
||||
|
||||
@Size(max = 500)
|
||||
private String remark;
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.crm.mail.template.model.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/** 模板列表筛选。 */
|
||||
@Data
|
||||
public class CrmMailTemplateFilterDTO {
|
||||
private String keyword;
|
||||
private String scene;
|
||||
private Integer enabled;
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.crm.mail.template.model.dto;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.Size;
|
||||
import lombok.Data;
|
||||
|
||||
/** 更新邮件模板。 */
|
||||
@Data
|
||||
public class UpdateCrmMailTemplateDTO {
|
||||
|
||||
@NotBlank
|
||||
@Size(max = 100)
|
||||
private String name;
|
||||
|
||||
@NotBlank
|
||||
@Size(max = 32)
|
||||
private String scene;
|
||||
|
||||
@NotBlank
|
||||
@Size(max = 500)
|
||||
private String subject;
|
||||
|
||||
@NotBlank
|
||||
private String bodyHtml;
|
||||
|
||||
private Integer enabled;
|
||||
|
||||
@Size(max = 500)
|
||||
private String remark;
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.crm.mail.template.model.po;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.*;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/** 邮件模板。 */
|
||||
@Data
|
||||
@TableName("crm_mail_template")
|
||||
public class CrmMailTemplatePO {
|
||||
|
||||
@TableId(value = "id", type = IdType.AUTO)
|
||||
private Long id;
|
||||
@TableField("company_id")
|
||||
private Long companyId;
|
||||
@TableField("name")
|
||||
private String name;
|
||||
@TableField("scene")
|
||||
private String scene;
|
||||
@TableField("subject")
|
||||
private String subject;
|
||||
@TableField("body_html")
|
||||
private String bodyHtml;
|
||||
@TableField("enabled")
|
||||
private Integer enabled;
|
||||
@TableField("remark")
|
||||
private String remark;
|
||||
@TableField(value = "create_time", fill = FieldFill.INSERT)
|
||||
private LocalDateTime createTime;
|
||||
@TableField(value = "update_time", fill = FieldFill.INSERT_UPDATE)
|
||||
private LocalDateTime updateTime;
|
||||
@TableField("create_by")
|
||||
private Long createBy;
|
||||
@TableField("update_by")
|
||||
private Long updateBy;
|
||||
@TableLogic
|
||||
@TableField("delete_time")
|
||||
private LocalDateTime deleteTime;
|
||||
@TableField("delete_by")
|
||||
private Long deleteBy;
|
||||
}
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.crm.mail.template.model.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/** 邮件模板视图。 */
|
||||
@Data
|
||||
public class CrmMailTemplateVO {
|
||||
private Long id;
|
||||
private Long companyId;
|
||||
private String name;
|
||||
private String scene;
|
||||
private String subject;
|
||||
private String bodyHtml;
|
||||
private Integer enabled;
|
||||
private String remark;
|
||||
private LocalDateTime createTime;
|
||||
private LocalDateTime updateTime;
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.crm.mail.template.service;
|
||||
|
||||
import top.xyquan.ecomdmbbackend.common.model.dto.PageQueryDTO;
|
||||
import top.xyquan.ecomdmbbackend.common.model.vo.PageResultVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.template.model.dto.CreateCrmMailTemplateDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.template.model.dto.CrmMailTemplateFilterDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.template.model.dto.UpdateCrmMailTemplateDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.template.model.po.CrmMailTemplatePO;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.template.model.vo.CrmMailTemplateVO;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/** 邮件模板用例。 */
|
||||
public interface CrmMailTemplateService {
|
||||
|
||||
CrmMailTemplateVO create(CreateCrmMailTemplateDTO dto);
|
||||
|
||||
CrmMailTemplateVO update(Long id, UpdateCrmMailTemplateDTO dto);
|
||||
|
||||
void delete(Long id);
|
||||
|
||||
CrmMailTemplateVO get(Long id);
|
||||
|
||||
PageResultVO<CrmMailTemplateVO> page(PageQueryDTO<CrmMailTemplateFilterDTO> query);
|
||||
|
||||
/** 启用模板下拉(发信用)。 */
|
||||
List<CrmMailTemplateVO> listEnabled(String scene);
|
||||
|
||||
CrmMailTemplatePO requireOwned(Long companyId, Long id);
|
||||
}
|
||||
+157
@@ -0,0 +1,157 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.crm.mail.template.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StringUtils;
|
||||
import top.xyquan.ecomdmbbackend.common.exception.BusinessException;
|
||||
import top.xyquan.ecomdmbbackend.common.model.dto.PageQueryDTO;
|
||||
import top.xyquan.ecomdmbbackend.common.model.vo.PageResultVO;
|
||||
import top.xyquan.ecomdmbbackend.common.utils.BeanCopyUtils;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.template.mapper.CrmMailTemplateMapper;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.template.model.dto.CreateCrmMailTemplateDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.template.model.dto.CrmMailTemplateFilterDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.template.model.dto.UpdateCrmMailTemplateDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.template.model.po.CrmMailTemplatePO;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.template.model.vo.CrmMailTemplateVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.crm.mail.template.service.CrmMailTemplateService;
|
||||
import top.xyquan.ecomdmbbackend.utils.CurrentUserContext;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Set;
|
||||
|
||||
/** 邮件模板 CRUD。 */
|
||||
@Service
|
||||
public class CrmMailTemplateServiceImpl extends ServiceImpl<CrmMailTemplateMapper, CrmMailTemplatePO>
|
||||
implements CrmMailTemplateService {
|
||||
|
||||
private static final Set<String> SCENES = Set.of("outreach", "quote_followup", "shipment_notice");
|
||||
|
||||
private final CurrentUserContext currentUserContext;
|
||||
|
||||
public CrmMailTemplateServiceImpl(CurrentUserContext currentUserContext) {
|
||||
this.currentUserContext = currentUserContext;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public CrmMailTemplateVO create(CreateCrmMailTemplateDTO dto) {
|
||||
Long companyId = requireCompanyId();
|
||||
Long userId = currentUserContext.getCurrentUserId();
|
||||
CrmMailTemplatePO po = new CrmMailTemplatePO();
|
||||
po.setCompanyId(companyId);
|
||||
apply(po, dto.getName(), dto.getScene(), dto.getSubject(), dto.getBodyHtml(),
|
||||
dto.getEnabled(), dto.getRemark());
|
||||
po.setCreateBy(userId);
|
||||
po.setUpdateBy(userId);
|
||||
this.save(po);
|
||||
return toVo(po);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public CrmMailTemplateVO update(Long id, UpdateCrmMailTemplateDTO dto) {
|
||||
CrmMailTemplatePO po = requireOwnedLocal(id);
|
||||
apply(po, dto.getName(), dto.getScene(), dto.getSubject(), dto.getBodyHtml(),
|
||||
dto.getEnabled(), dto.getRemark());
|
||||
po.setUpdateBy(currentUserContext.getCurrentUserId());
|
||||
this.updateById(po);
|
||||
return toVo(po);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
public void delete(Long id) {
|
||||
CrmMailTemplatePO po = requireOwnedLocal(id);
|
||||
po.setDeleteBy(currentUserContext.getCurrentUserId());
|
||||
this.updateById(po);
|
||||
this.removeById(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CrmMailTemplateVO get(Long id) {
|
||||
return toVo(requireOwnedLocal(id));
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageResultVO<CrmMailTemplateVO> page(PageQueryDTO<CrmMailTemplateFilterDTO> query) {
|
||||
Long companyId = requireCompanyId();
|
||||
CrmMailTemplateFilterDTO filters =
|
||||
query.getFilters() != null ? query.getFilters() : new CrmMailTemplateFilterDTO();
|
||||
LambdaQueryWrapper<CrmMailTemplatePO> w = new LambdaQueryWrapper<>();
|
||||
w.eq(CrmMailTemplatePO::getCompanyId, companyId);
|
||||
if (StringUtils.hasText(filters.getScene())) {
|
||||
w.eq(CrmMailTemplatePO::getScene, filters.getScene().trim());
|
||||
}
|
||||
if (filters.getEnabled() != null) {
|
||||
w.eq(CrmMailTemplatePO::getEnabled, filters.getEnabled());
|
||||
}
|
||||
if (StringUtils.hasText(filters.getKeyword())) {
|
||||
String kw = filters.getKeyword().trim();
|
||||
w.and(x -> x.like(CrmMailTemplatePO::getName, kw)
|
||||
.or().like(CrmMailTemplatePO::getSubject, kw));
|
||||
}
|
||||
w.orderByDesc(CrmMailTemplatePO::getId);
|
||||
IPage<CrmMailTemplatePO> page = this.page(new Page<>(query.getPage(), query.getPageSize()), w);
|
||||
List<CrmMailTemplateVO> records = page.getRecords().stream().map(this::toVo).toList();
|
||||
return PageResultVO.create(records, page.getTotal(), query.getPage(), query.getPageSize());
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<CrmMailTemplateVO> listEnabled(String scene) {
|
||||
Long companyId = requireCompanyId();
|
||||
LambdaQueryWrapper<CrmMailTemplatePO> w = new LambdaQueryWrapper<>();
|
||||
w.eq(CrmMailTemplatePO::getCompanyId, companyId)
|
||||
.eq(CrmMailTemplatePO::getEnabled, 1);
|
||||
if (StringUtils.hasText(scene)) {
|
||||
w.eq(CrmMailTemplatePO::getScene, scene.trim());
|
||||
}
|
||||
w.orderByDesc(CrmMailTemplatePO::getId);
|
||||
return this.list(w).stream().map(this::toVo).toList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public CrmMailTemplatePO requireOwned(Long companyId, Long id) {
|
||||
CrmMailTemplatePO po = this.getById(id);
|
||||
if (po == null || !companyId.equals(po.getCompanyId())) {
|
||||
throw new BusinessException("邮件模板不存在");
|
||||
}
|
||||
return po;
|
||||
}
|
||||
|
||||
private void apply(CrmMailTemplatePO po, String name, String scene, String subject,
|
||||
String bodyHtml, Integer enabled, String remark) {
|
||||
String sceneNorm = scene.trim().toLowerCase(Locale.ROOT);
|
||||
if (!SCENES.contains(sceneNorm)) {
|
||||
throw new BusinessException("场景不合法,可用: outreach / quote_followup / shipment_notice");
|
||||
}
|
||||
po.setName(name.trim());
|
||||
po.setScene(sceneNorm);
|
||||
po.setSubject(subject.trim());
|
||||
po.setBodyHtml(bodyHtml);
|
||||
po.setEnabled(enabled == null || enabled == 1 ? 1 : 0);
|
||||
po.setRemark(StringUtils.hasText(remark) ? remark.trim() : null);
|
||||
}
|
||||
|
||||
private CrmMailTemplateVO toVo(CrmMailTemplatePO po) {
|
||||
CrmMailTemplateVO vo = new CrmMailTemplateVO();
|
||||
BeanCopyUtils.copyProperties(po, vo);
|
||||
return vo;
|
||||
}
|
||||
|
||||
private CrmMailTemplatePO requireOwnedLocal(Long id) {
|
||||
return requireOwned(requireCompanyId(), id);
|
||||
}
|
||||
|
||||
private Long requireCompanyId() {
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
if (companyId == null) {
|
||||
throw new BusinessException("请先选择公司");
|
||||
}
|
||||
return companyId;
|
||||
}
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.crm.support;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* 邮件模板变量替换:{{key}} → 上下文值。
|
||||
*/
|
||||
@Component
|
||||
public class CrmMailVariableRenderer {
|
||||
|
||||
private static final Pattern VAR = Pattern.compile("\\{\\{\\s*([a-zA-Z0-9_]+)\\s*}}");
|
||||
|
||||
public String render(String template, Map<String, String> vars) {
|
||||
if (!StringUtils.hasText(template)) {
|
||||
return "";
|
||||
}
|
||||
Matcher m = VAR.matcher(template);
|
||||
StringBuilder sb = new StringBuilder();
|
||||
while (m.find()) {
|
||||
String key = m.group(1);
|
||||
String value = vars.getOrDefault(key, "");
|
||||
m.appendReplacement(sb, Matcher.quoteReplacement(value == null ? "" : value));
|
||||
}
|
||||
m.appendTail(sb);
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.crm.support;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
import top.xyquan.ecomdmbbackend.common.exception.BusinessException;
|
||||
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.SecretKey;
|
||||
import javax.crypto.spec.GCMParameterSpec;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.Base64;
|
||||
|
||||
/**
|
||||
* CRM 敏感字段 AES-GCM(SMTP 密码等);密钥由 jwt.secret 派生。
|
||||
*/
|
||||
@Component
|
||||
public class CrmSecretCrypto {
|
||||
|
||||
private static final String TRANSFORMATION = "AES/GCM/NoPadding";
|
||||
private static final int GCM_IV_LENGTH = 12;
|
||||
private static final int GCM_TAG_LENGTH = 128;
|
||||
|
||||
private final String jwtSecret;
|
||||
|
||||
public CrmSecretCrypto(@Value("${jwt.secret}") String jwtSecret) {
|
||||
this.jwtSecret = jwtSecret;
|
||||
}
|
||||
|
||||
public String encrypt(String plain) {
|
||||
try {
|
||||
byte[] iv = new byte[GCM_IV_LENGTH];
|
||||
new SecureRandom().nextBytes(iv);
|
||||
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
|
||||
cipher.init(Cipher.ENCRYPT_MODE, secretKey(), new GCMParameterSpec(GCM_TAG_LENGTH, iv));
|
||||
byte[] cipherBytes = cipher.doFinal(plain.getBytes(StandardCharsets.UTF_8));
|
||||
ByteBuffer buffer = ByteBuffer.allocate(iv.length + cipherBytes.length);
|
||||
buffer.put(iv);
|
||||
buffer.put(cipherBytes);
|
||||
return Base64.getEncoder().encodeToString(buffer.array());
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException("加密失败");
|
||||
}
|
||||
}
|
||||
|
||||
public String decrypt(String token) {
|
||||
try {
|
||||
byte[] all = Base64.getDecoder().decode(token);
|
||||
ByteBuffer buffer = ByteBuffer.wrap(all);
|
||||
byte[] iv = new byte[GCM_IV_LENGTH];
|
||||
buffer.get(iv);
|
||||
byte[] cipherBytes = new byte[buffer.remaining()];
|
||||
buffer.get(cipherBytes);
|
||||
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
|
||||
cipher.init(Cipher.DECRYPT_MODE, secretKey(), new GCMParameterSpec(GCM_TAG_LENGTH, iv));
|
||||
return new String(cipher.doFinal(cipherBytes), StandardCharsets.UTF_8);
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException("解密失败,请检查 jwt.secret 是否变更");
|
||||
}
|
||||
}
|
||||
|
||||
private SecretKey secretKey() throws Exception {
|
||||
byte[] digest = MessageDigest.getInstance("SHA-256")
|
||||
.digest(jwtSecret.getBytes(StandardCharsets.UTF_8));
|
||||
return new SecretKeySpec(digest, "AES");
|
||||
}
|
||||
}
|
||||
+72
@@ -0,0 +1,72 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.crm.support;
|
||||
|
||||
import jakarta.mail.Authenticator;
|
||||
import jakarta.mail.Message;
|
||||
import jakarta.mail.PasswordAuthentication;
|
||||
import jakarta.mail.Session;
|
||||
import jakarta.mail.Transport;
|
||||
import jakarta.mail.internet.InternetAddress;
|
||||
import jakarta.mail.internet.MimeMessage;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.StringUtils;
|
||||
import top.xyquan.ecomdmbbackend.common.exception.BusinessException;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* 按账户动态建 SMTP Session 发送;不持有全局 MailSender。
|
||||
*/
|
||||
@Component
|
||||
public class CrmSmtpClient {
|
||||
|
||||
public void send(SmtpAccount account, String to, String cc, String subject, String bodyHtml) {
|
||||
try {
|
||||
Properties props = new Properties();
|
||||
props.put("mail.smtp.host", account.host());
|
||||
props.put("mail.smtp.port", String.valueOf(account.port()));
|
||||
props.put("mail.smtp.auth", "true");
|
||||
if (account.ssl()) {
|
||||
props.put("mail.smtp.ssl.enable", "true");
|
||||
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
|
||||
props.put("mail.smtp.socketFactory.port", String.valueOf(account.port()));
|
||||
} else {
|
||||
props.put("mail.smtp.starttls.enable", "true");
|
||||
}
|
||||
props.put("mail.smtp.connectiontimeout", "15000");
|
||||
props.put("mail.smtp.timeout", "30000");
|
||||
|
||||
Session session = Session.getInstance(props, new Authenticator() {
|
||||
@Override
|
||||
protected PasswordAuthentication getPasswordAuthentication() {
|
||||
return new PasswordAuthentication(account.username(), account.password());
|
||||
}
|
||||
});
|
||||
|
||||
MimeMessage message = new MimeMessage(session);
|
||||
message.setFrom(new InternetAddress(account.fromEmail(), account.fromName()));
|
||||
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
|
||||
if (StringUtils.hasText(cc)) {
|
||||
message.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc, false));
|
||||
}
|
||||
message.setSubject(subject, "UTF-8");
|
||||
message.setContent(bodyHtml, "text/html; charset=UTF-8");
|
||||
Transport.send(message);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new BusinessException("邮件发送失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/** SMTP 发件参数(明文密码仅在内存短暂使用)。 */
|
||||
public record SmtpAccount(
|
||||
String fromName,
|
||||
String fromEmail,
|
||||
String host,
|
||||
int port,
|
||||
boolean ssl,
|
||||
String username,
|
||||
String password
|
||||
) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
-- CRM Phase3 邮件方案 A 菜单权限(幂等)
|
||||
|
||||
-- 发件账户
|
||||
INSERT INTO `sys_permission` (`id`, `parent_id`, `code`, `name`, `type`, `status`, `remark`, `sort_order`)
|
||||
SELECT 6600, (SELECT id FROM `sys_permission` WHERE `code` = 'crm' LIMIT 1), 'crm:mail-account', '发件账户', 'module', 1, '', 50
|
||||
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM `sys_permission` WHERE `id` = 6600 OR `code` = 'crm:mail-account');
|
||||
|
||||
INSERT INTO `sys_permission` (`id`, `parent_id`, `code`, `name`, `type`, `status`, `remark`, `sort_order`)
|
||||
SELECT 6601, (SELECT id FROM `sys_permission` WHERE `code` = 'crm:mail-account' LIMIT 1), 'crm:mail-account:read', '查看', 'api', 1, '', 1
|
||||
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM `sys_permission` WHERE `id` = 6601 OR `code` = 'crm:mail-account:read');
|
||||
|
||||
INSERT INTO `sys_permission` (`id`, `parent_id`, `code`, `name`, `type`, `status`, `remark`, `sort_order`)
|
||||
SELECT 6602, (SELECT id FROM `sys_permission` WHERE `code` = 'crm:mail-account' LIMIT 1), 'crm:mail-account:create', '创建', 'api', 1, '', 2
|
||||
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM `sys_permission` WHERE `id` = 6602 OR `code` = 'crm:mail-account:create');
|
||||
|
||||
INSERT INTO `sys_permission` (`id`, `parent_id`, `code`, `name`, `type`, `status`, `remark`, `sort_order`)
|
||||
SELECT 6603, (SELECT id FROM `sys_permission` WHERE `code` = 'crm:mail-account' LIMIT 1), 'crm:mail-account:update', '更新', 'api', 1, '', 3
|
||||
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM `sys_permission` WHERE `id` = 6603 OR `code` = 'crm:mail-account:update');
|
||||
|
||||
INSERT INTO `sys_permission` (`id`, `parent_id`, `code`, `name`, `type`, `status`, `remark`, `sort_order`)
|
||||
SELECT 6604, (SELECT id FROM `sys_permission` WHERE `code` = 'crm:mail-account' LIMIT 1), 'crm:mail-account:delete', '删除', 'api', 1, '', 4
|
||||
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM `sys_permission` WHERE `id` = 6604 OR `code` = 'crm:mail-account:delete');
|
||||
|
||||
-- 邮件模板
|
||||
INSERT INTO `sys_permission` (`id`, `parent_id`, `code`, `name`, `type`, `status`, `remark`, `sort_order`)
|
||||
SELECT 6700, (SELECT id FROM `sys_permission` WHERE `code` = 'crm' LIMIT 1), 'crm:mail-template', '邮件模板', 'module', 1, '', 60
|
||||
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM `sys_permission` WHERE `id` = 6700 OR `code` = 'crm:mail-template');
|
||||
|
||||
INSERT INTO `sys_permission` (`id`, `parent_id`, `code`, `name`, `type`, `status`, `remark`, `sort_order`)
|
||||
SELECT 6701, (SELECT id FROM `sys_permission` WHERE `code` = 'crm:mail-template' LIMIT 1), 'crm:mail-template:read', '查看', 'api', 1, '', 1
|
||||
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM `sys_permission` WHERE `id` = 6701 OR `code` = 'crm:mail-template:read');
|
||||
|
||||
INSERT INTO `sys_permission` (`id`, `parent_id`, `code`, `name`, `type`, `status`, `remark`, `sort_order`)
|
||||
SELECT 6702, (SELECT id FROM `sys_permission` WHERE `code` = 'crm:mail-template' LIMIT 1), 'crm:mail-template:create', '创建', 'api', 1, '', 2
|
||||
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM `sys_permission` WHERE `id` = 6702 OR `code` = 'crm:mail-template:create');
|
||||
|
||||
INSERT INTO `sys_permission` (`id`, `parent_id`, `code`, `name`, `type`, `status`, `remark`, `sort_order`)
|
||||
SELECT 6703, (SELECT id FROM `sys_permission` WHERE `code` = 'crm:mail-template' LIMIT 1), 'crm:mail-template:update', '更新', 'api', 1, '', 3
|
||||
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM `sys_permission` WHERE `id` = 6703 OR `code` = 'crm:mail-template:update');
|
||||
|
||||
INSERT INTO `sys_permission` (`id`, `parent_id`, `code`, `name`, `type`, `status`, `remark`, `sort_order`)
|
||||
SELECT 6704, (SELECT id FROM `sys_permission` WHERE `code` = 'crm:mail-template' LIMIT 1), 'crm:mail-template:delete', '删除', 'api', 1, '', 4
|
||||
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM `sys_permission` WHERE `id` = 6704 OR `code` = 'crm:mail-template:delete');
|
||||
|
||||
-- 手发
|
||||
INSERT INTO `sys_permission` (`id`, `parent_id`, `code`, `name`, `type`, `status`, `remark`, `sort_order`)
|
||||
SELECT 6750, (SELECT id FROM `sys_permission` WHERE `code` = 'crm' LIMIT 1), 'crm:mail:send', '发送邮件', 'api', 1, '', 65
|
||||
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM `sys_permission` WHERE `id` = 6750 OR `code` = 'crm:mail:send');
|
||||
|
||||
-- 发送记录
|
||||
INSERT INTO `sys_permission` (`id`, `parent_id`, `code`, `name`, `type`, `status`, `remark`, `sort_order`)
|
||||
SELECT 6800, (SELECT id FROM `sys_permission` WHERE `code` = 'crm' LIMIT 1), 'crm:mail-log', '发送记录', 'module', 1, '', 70
|
||||
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM `sys_permission` WHERE `id` = 6800 OR `code` = 'crm:mail-log');
|
||||
|
||||
INSERT INTO `sys_permission` (`id`, `parent_id`, `code`, `name`, `type`, `status`, `remark`, `sort_order`)
|
||||
SELECT 6801, (SELECT id FROM `sys_permission` WHERE `code` = 'crm:mail-log' LIMIT 1), 'crm:mail-log:read', '查看', 'api', 1, '', 1
|
||||
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM `sys_permission` WHERE `id` = 6801 OR `code` = 'crm:mail-log:read');
|
||||
|
||||
INSERT INTO `sys_menu` (`id`, `parent_id`, `super`, `route_name`, `title`, `path`, `component`, `redirect`, `icon`, `type`, `visible`, `status`, `keep_alive`, `order`)
|
||||
SELECT 14106, (SELECT id FROM `sys_menu` WHERE `id` = 14100 OR `route_name` = 'CrmCatalog' LIMIT 1), 0, 'CrmMailAccount', '发件账户', '/crm/mail-account', '/crm/mail-account/index', '', 'lucide:mailbox', 'menu', 1, 1, 0, 5
|
||||
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM `sys_menu` WHERE `id` = 14106 OR `route_name` = 'CrmMailAccount');
|
||||
|
||||
INSERT INTO `sys_menu` (`id`, `parent_id`, `super`, `route_name`, `title`, `path`, `component`, `redirect`, `icon`, `type`, `visible`, `status`, `keep_alive`, `order`)
|
||||
SELECT 14107, (SELECT id FROM `sys_menu` WHERE `id` = 14100 OR `route_name` = 'CrmCatalog' LIMIT 1), 0, 'CrmMailTemplate', '邮件模板', '/crm/mail-template', '/crm/mail-template/index', '', 'lucide:mail', 'menu', 1, 1, 1, 6
|
||||
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM `sys_menu` WHERE `id` = 14107 OR `route_name` = 'CrmMailTemplate');
|
||||
|
||||
INSERT INTO `sys_menu` (`id`, `parent_id`, `super`, `route_name`, `title`, `path`, `component`, `redirect`, `icon`, `type`, `visible`, `status`, `keep_alive`, `order`)
|
||||
SELECT 14108, (SELECT id FROM `sys_menu` WHERE `id` = 14100 OR `route_name` = 'CrmCatalog' LIMIT 1), 0, 'CrmMailLog', '发送记录', '/crm/mail-log', '/crm/mail-log/index', '', 'lucide:scroll-text', 'menu', 1, 1, 0, 7
|
||||
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM `sys_menu` WHERE `id` = 14108 OR `route_name` = 'CrmMailLog');
|
||||
|
||||
INSERT IGNORE INTO `sys_menu_permission` (`menu_id`, `permission_id`)
|
||||
SELECT m.id, p.id FROM `sys_menu` m JOIN `sys_permission` p ON p.code LIKE 'crm:mail-account:%' WHERE m.route_name = 'CrmMailAccount';
|
||||
|
||||
INSERT IGNORE INTO `sys_menu_permission` (`menu_id`, `permission_id`)
|
||||
SELECT m.id, p.id FROM `sys_menu` m JOIN `sys_permission` p ON p.code LIKE 'crm:mail-template:%' WHERE m.route_name = 'CrmMailTemplate';
|
||||
|
||||
INSERT IGNORE INTO `sys_menu_permission` (`menu_id`, `permission_id`)
|
||||
SELECT m.id, p.id FROM `sys_menu` m JOIN `sys_permission` p ON p.code = 'crm:mail:send' WHERE m.route_name = 'CrmMailTemplate';
|
||||
|
||||
INSERT IGNORE INTO `sys_menu_permission` (`menu_id`, `permission_id`)
|
||||
SELECT m.id, p.id FROM `sys_menu` m JOIN `sys_permission` p ON p.code = 'crm:mail-log:read' WHERE m.route_name = 'CrmMailLog';
|
||||
|
||||
INSERT IGNORE INTO `sys_menu_permission` (`menu_id`, `permission_id`)
|
||||
SELECT m.id, p.id FROM `sys_menu` m JOIN `sys_permission` p ON p.code = 'crm:mail:send' WHERE m.route_name IN ('CrmCustomer', 'CrmQuote');
|
||||
@@ -660,3 +660,86 @@ FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM `sys_menu` WHERE `id` = 14105 OR `rout
|
||||
|
||||
INSERT IGNORE INTO `sys_menu_permission` (`menu_id`, `permission_id`)
|
||||
SELECT m.id, p.id FROM `sys_menu` m JOIN `sys_permission` p ON p.code LIKE 'crm:shipment:%' WHERE m.route_name = 'CrmShipment';
|
||||
|
||||
-- ------------------------------------------------
|
||||
-- CRM Phase3:邮件方案 A
|
||||
-- ------------------------------------------------
|
||||
|
||||
INSERT INTO `sys_permission` (`id`, `parent_id`, `code`, `name`, `type`, `status`, `remark`, `sort_order`)
|
||||
SELECT 6600, (SELECT id FROM `sys_permission` WHERE `code` = 'crm' LIMIT 1), 'crm:mail-account', '发件账户', 'module', 1, '', 50
|
||||
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM `sys_permission` WHERE `id` = 6600 OR `code` = 'crm:mail-account');
|
||||
|
||||
INSERT INTO `sys_permission` (`id`, `parent_id`, `code`, `name`, `type`, `status`, `remark`, `sort_order`)
|
||||
SELECT 6601, (SELECT id FROM `sys_permission` WHERE `code` = 'crm:mail-account' LIMIT 1), 'crm:mail-account:read', '查看', 'api', 1, '', 1
|
||||
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM `sys_permission` WHERE `id` = 6601 OR `code` = 'crm:mail-account:read');
|
||||
|
||||
INSERT INTO `sys_permission` (`id`, `parent_id`, `code`, `name`, `type`, `status`, `remark`, `sort_order`)
|
||||
SELECT 6602, (SELECT id FROM `sys_permission` WHERE `code` = 'crm:mail-account' LIMIT 1), 'crm:mail-account:create', '创建', 'api', 1, '', 2
|
||||
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM `sys_permission` WHERE `id` = 6602 OR `code` = 'crm:mail-account:create');
|
||||
|
||||
INSERT INTO `sys_permission` (`id`, `parent_id`, `code`, `name`, `type`, `status`, `remark`, `sort_order`)
|
||||
SELECT 6603, (SELECT id FROM `sys_permission` WHERE `code` = 'crm:mail-account' LIMIT 1), 'crm:mail-account:update', '更新', 'api', 1, '', 3
|
||||
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM `sys_permission` WHERE `id` = 6603 OR `code` = 'crm:mail-account:update');
|
||||
|
||||
INSERT INTO `sys_permission` (`id`, `parent_id`, `code`, `name`, `type`, `status`, `remark`, `sort_order`)
|
||||
SELECT 6604, (SELECT id FROM `sys_permission` WHERE `code` = 'crm:mail-account' LIMIT 1), 'crm:mail-account:delete', '删除', 'api', 1, '', 4
|
||||
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM `sys_permission` WHERE `id` = 6604 OR `code` = 'crm:mail-account:delete');
|
||||
|
||||
INSERT INTO `sys_permission` (`id`, `parent_id`, `code`, `name`, `type`, `status`, `remark`, `sort_order`)
|
||||
SELECT 6700, (SELECT id FROM `sys_permission` WHERE `code` = 'crm' LIMIT 1), 'crm:mail-template', '邮件模板', 'module', 1, '', 60
|
||||
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM `sys_permission` WHERE `id` = 6700 OR `code` = 'crm:mail-template');
|
||||
|
||||
INSERT INTO `sys_permission` (`id`, `parent_id`, `code`, `name`, `type`, `status`, `remark`, `sort_order`)
|
||||
SELECT 6701, (SELECT id FROM `sys_permission` WHERE `code` = 'crm:mail-template' LIMIT 1), 'crm:mail-template:read', '查看', 'api', 1, '', 1
|
||||
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM `sys_permission` WHERE `id` = 6701 OR `code` = 'crm:mail-template:read');
|
||||
|
||||
INSERT INTO `sys_permission` (`id`, `parent_id`, `code`, `name`, `type`, `status`, `remark`, `sort_order`)
|
||||
SELECT 6702, (SELECT id FROM `sys_permission` WHERE `code` = 'crm:mail-template' LIMIT 1), 'crm:mail-template:create', '创建', 'api', 1, '', 2
|
||||
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM `sys_permission` WHERE `id` = 6702 OR `code` = 'crm:mail-template:create');
|
||||
|
||||
INSERT INTO `sys_permission` (`id`, `parent_id`, `code`, `name`, `type`, `status`, `remark`, `sort_order`)
|
||||
SELECT 6703, (SELECT id FROM `sys_permission` WHERE `code` = 'crm:mail-template' LIMIT 1), 'crm:mail-template:update', '更新', 'api', 1, '', 3
|
||||
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM `sys_permission` WHERE `id` = 6703 OR `code` = 'crm:mail-template:update');
|
||||
|
||||
INSERT INTO `sys_permission` (`id`, `parent_id`, `code`, `name`, `type`, `status`, `remark`, `sort_order`)
|
||||
SELECT 6704, (SELECT id FROM `sys_permission` WHERE `code` = 'crm:mail-template' LIMIT 1), 'crm:mail-template:delete', '删除', 'api', 1, '', 4
|
||||
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM `sys_permission` WHERE `id` = 6704 OR `code` = 'crm:mail-template:delete');
|
||||
|
||||
INSERT INTO `sys_permission` (`id`, `parent_id`, `code`, `name`, `type`, `status`, `remark`, `sort_order`)
|
||||
SELECT 6750, (SELECT id FROM `sys_permission` WHERE `code` = 'crm' LIMIT 1), 'crm:mail:send', '发送邮件', 'api', 1, '', 65
|
||||
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM `sys_permission` WHERE `id` = 6750 OR `code` = 'crm:mail:send');
|
||||
|
||||
INSERT INTO `sys_permission` (`id`, `parent_id`, `code`, `name`, `type`, `status`, `remark`, `sort_order`)
|
||||
SELECT 6800, (SELECT id FROM `sys_permission` WHERE `code` = 'crm' LIMIT 1), 'crm:mail-log', '发送记录', 'module', 1, '', 70
|
||||
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM `sys_permission` WHERE `id` = 6800 OR `code` = 'crm:mail-log');
|
||||
|
||||
INSERT INTO `sys_permission` (`id`, `parent_id`, `code`, `name`, `type`, `status`, `remark`, `sort_order`)
|
||||
SELECT 6801, (SELECT id FROM `sys_permission` WHERE `code` = 'crm:mail-log' LIMIT 1), 'crm:mail-log:read', '查看', 'api', 1, '', 1
|
||||
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM `sys_permission` WHERE `id` = 6801 OR `code` = 'crm:mail-log:read');
|
||||
|
||||
INSERT INTO `sys_menu` (`id`, `parent_id`, `super`, `route_name`, `title`, `path`, `component`, `redirect`, `icon`, `type`, `visible`, `status`, `keep_alive`, `order`)
|
||||
SELECT 14106, (SELECT id FROM `sys_menu` WHERE `id` = 14100 OR `route_name` = 'CrmCatalog' LIMIT 1), 0, 'CrmMailAccount', '发件账户', '/crm/mail-account', '/crm/mail-account/index', '', 'lucide:mailbox', 'menu', 1, 1, 0, 5
|
||||
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM `sys_menu` WHERE `id` = 14106 OR `route_name` = 'CrmMailAccount');
|
||||
|
||||
INSERT INTO `sys_menu` (`id`, `parent_id`, `super`, `route_name`, `title`, `path`, `component`, `redirect`, `icon`, `type`, `visible`, `status`, `keep_alive`, `order`)
|
||||
SELECT 14107, (SELECT id FROM `sys_menu` WHERE `id` = 14100 OR `route_name` = 'CrmCatalog' LIMIT 1), 0, 'CrmMailTemplate', '邮件模板', '/crm/mail-template', '/crm/mail-template/index', '', 'lucide:mail', 'menu', 1, 1, 1, 6
|
||||
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM `sys_menu` WHERE `id` = 14107 OR `route_name` = 'CrmMailTemplate');
|
||||
|
||||
INSERT INTO `sys_menu` (`id`, `parent_id`, `super`, `route_name`, `title`, `path`, `component`, `redirect`, `icon`, `type`, `visible`, `status`, `keep_alive`, `order`)
|
||||
SELECT 14108, (SELECT id FROM `sys_menu` WHERE `id` = 14100 OR `route_name` = 'CrmCatalog' LIMIT 1), 0, 'CrmMailLog', '发送记录', '/crm/mail-log', '/crm/mail-log/index', '', 'lucide:scroll-text', 'menu', 1, 1, 0, 7
|
||||
FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM `sys_menu` WHERE `id` = 14108 OR `route_name` = 'CrmMailLog');
|
||||
|
||||
INSERT IGNORE INTO `sys_menu_permission` (`menu_id`, `permission_id`)
|
||||
SELECT m.id, p.id FROM `sys_menu` m JOIN `sys_permission` p ON p.code LIKE 'crm:mail-account:%' WHERE m.route_name = 'CrmMailAccount';
|
||||
|
||||
INSERT IGNORE INTO `sys_menu_permission` (`menu_id`, `permission_id`)
|
||||
SELECT m.id, p.id FROM `sys_menu` m JOIN `sys_permission` p ON p.code LIKE 'crm:mail-template:%' WHERE m.route_name = 'CrmMailTemplate';
|
||||
|
||||
INSERT IGNORE INTO `sys_menu_permission` (`menu_id`, `permission_id`)
|
||||
SELECT m.id, p.id FROM `sys_menu` m JOIN `sys_permission` p ON p.code = 'crm:mail:send' WHERE m.route_name = 'CrmMailTemplate';
|
||||
|
||||
INSERT IGNORE INTO `sys_menu_permission` (`menu_id`, `permission_id`)
|
||||
SELECT m.id, p.id FROM `sys_menu` m JOIN `sys_permission` p ON p.code = 'crm:mail-log:read' WHERE m.route_name = 'CrmMailLog';
|
||||
|
||||
INSERT IGNORE INTO `sys_menu_permission` (`menu_id`, `permission_id`)
|
||||
SELECT m.id, p.id FROM `sys_menu` m JOIN `sys_permission` p ON p.code = 'crm:mail:send' WHERE m.route_name IN ('CrmCustomer', 'CrmQuote');
|
||||
|
||||
Reference in New Issue
Block a user