|
|
@@ -1,15 +1,14 @@
|
|
|
package cn.reghao.tnb.message.app.service;
|
|
|
|
|
|
-import cn.reghao.jutil.jdk.db.PageList;
|
|
|
import cn.reghao.jutil.jdk.result.Result;
|
|
|
-import cn.reghao.jutil.jdk.string.StringRegexp;
|
|
|
-import cn.reghao.tnb.message.app.model.dto.EmailDto;
|
|
|
+import cn.reghao.tnb.common.util.StringRegexp;
|
|
|
import cn.reghao.tnb.message.app.db.mapper.EmailAccountMapper;
|
|
|
+import cn.reghao.tnb.message.app.db.mapper.WebhookMapper;
|
|
|
import cn.reghao.tnb.message.app.model.po.EmailAccount;
|
|
|
+import cn.reghao.tnb.message.app.model.po.Webhook;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.util.List;
|
|
|
-import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* @author reghao
|
|
|
@@ -17,106 +16,84 @@ import java.util.stream.Collectors;
|
|
|
*/
|
|
|
@Service
|
|
|
public class EmailAccountService {
|
|
|
+ private final int maxSize = 10;
|
|
|
private final EmailAccountMapper emailAccountMapper;
|
|
|
+ private final WebhookMapper webhookMapper;
|
|
|
|
|
|
- public EmailAccountService(EmailAccountMapper emailAccountMapper) {
|
|
|
+ public EmailAccountService(EmailAccountMapper emailAccountMapper, WebhookMapper webhookMapper) {
|
|
|
this.emailAccountMapper = emailAccountMapper;
|
|
|
+ this.webhookMapper = webhookMapper;
|
|
|
}
|
|
|
|
|
|
- public Result addEmailAccount(EmailDto emailDto) {
|
|
|
- String email = emailDto.getUsername();
|
|
|
+ public Result addEmailAccount(EmailAccount emailAccount) {
|
|
|
+ if (emailAccountMapper.findAll().size() > maxSize) {
|
|
|
+ String errMsg = String.format("可使用的邮箱数量不能超过 %s 个, 请删除一些邮箱后再尝试添加", maxSize);
|
|
|
+ return Result.fail(errMsg);
|
|
|
+ }
|
|
|
+
|
|
|
+ String email = emailAccount.getUsername();
|
|
|
boolean matched = StringRegexp.matchEmail(email);
|
|
|
if (!matched) {
|
|
|
- return Result.fail("邮箱地址格式不正确, 请检查");
|
|
|
+ return Result.fail("邮箱格式不正确");
|
|
|
}
|
|
|
|
|
|
- EmailAccount emailAccount = emailAccountMapper.findByUsername(email);
|
|
|
- if (emailAccount != null) {
|
|
|
+ EmailAccount emailAccount1 = emailAccountMapper.findByUsername(email);
|
|
|
+ if (emailAccount1 != null) {
|
|
|
String errMsg = String.format("邮箱 %s 已存在", email);
|
|
|
return Result.fail(errMsg);
|
|
|
}
|
|
|
-
|
|
|
- emailAccount = new EmailAccount(emailDto);
|
|
|
- int total = emailAccountMapper.count();
|
|
|
- if (total == 0) {
|
|
|
- emailAccount.setDefaultSender(true);
|
|
|
- }
|
|
|
-
|
|
|
emailAccountMapper.save(emailAccount);
|
|
|
return Result.success();
|
|
|
}
|
|
|
|
|
|
- public Result updatePassword(int emailId, String password) {
|
|
|
- EmailAccount emailAccount = emailAccountMapper.findById(emailId);
|
|
|
+ public Result deleteEmailAccount(String username) {
|
|
|
+ EmailAccount emailAccount = emailAccountMapper.findByUsername(username);
|
|
|
if (emailAccount == null) {
|
|
|
String errMsg = String.format("邮箱不存在");
|
|
|
return Result.fail(errMsg);
|
|
|
}
|
|
|
|
|
|
- emailAccountMapper.updatePassword(emailId, password);
|
|
|
+ emailAccountMapper.deleteById(emailAccount.getId());
|
|
|
return Result.success();
|
|
|
}
|
|
|
|
|
|
- public Result updateDefaultSender(int emailId) {
|
|
|
- EmailAccount emailAccount = emailAccountMapper.findById(emailId);
|
|
|
- if (emailAccount == null) {
|
|
|
- String errMsg = String.format("邮箱不存在");
|
|
|
- return Result.fail(errMsg);
|
|
|
- }
|
|
|
-
|
|
|
- if (!emailAccount.getDefaultSender()) {
|
|
|
- EmailAccount emailAccount1 = emailAccountMapper.findDefaultSender();
|
|
|
- int emailId1 = emailAccount1.getId();
|
|
|
-
|
|
|
- emailAccountMapper.updateDefaultSender(emailId, true);
|
|
|
- emailAccountMapper.updateDefaultSender(emailId1, false);
|
|
|
- }
|
|
|
+ public EmailAccount getEmailSender() {
|
|
|
+ String username = "";
|
|
|
+ EmailAccount emailAccount = emailAccountMapper.findByUsername(username);
|
|
|
+ return emailAccount;
|
|
|
+ }
|
|
|
|
|
|
- return Result.success();
|
|
|
+ public List<EmailAccount> getEmailAccountList() {
|
|
|
+ return emailAccountMapper.findAll();
|
|
|
}
|
|
|
|
|
|
- public Result deleteEmailAccount(int emailId) {
|
|
|
- EmailAccount emailAccount = emailAccountMapper.findById(emailId);
|
|
|
- if (emailAccount == null) {
|
|
|
- String errMsg = String.format("邮箱不存在");
|
|
|
+ public Result addWebhook(Webhook webhook) {
|
|
|
+ if (webhookMapper.findAll().size() > maxSize) {
|
|
|
+ String errMsg = String.format("可使用的 webhook 数量不能超过 %s 个, 请删除一些 webhook 后再尝试添加", maxSize);
|
|
|
return Result.fail(errMsg);
|
|
|
}
|
|
|
|
|
|
- if (emailAccount.getDefaultSender()) {
|
|
|
- String errMsg = String.format("不能删除默认发送邮箱");
|
|
|
+ String name = webhook.getName();
|
|
|
+ Webhook webhook1 = webhookMapper.findByName(name);
|
|
|
+ if (webhook1 != null) {
|
|
|
+ String errMsg = String.format("webhook %s 已存在", name);
|
|
|
return Result.fail(errMsg);
|
|
|
}
|
|
|
-
|
|
|
- emailAccountMapper.deleteById(emailId);
|
|
|
+ webhookMapper.save(webhook);
|
|
|
return Result.success();
|
|
|
}
|
|
|
|
|
|
- public EmailDto getEmailAccount(int emailId) {
|
|
|
- EmailAccount emailAccount = emailAccountMapper.findById(emailId);
|
|
|
- return getEmailDto(emailAccount);
|
|
|
- }
|
|
|
-
|
|
|
- public EmailDto getEmailSender() {
|
|
|
- EmailAccount emailAccount = emailAccountMapper.findDefaultSender();
|
|
|
- if (emailAccount == null) {
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- return getEmailDto(emailAccount);
|
|
|
+ public Result deleteWebhook(String name) {
|
|
|
+ webhookMapper.deleteByName(name);
|
|
|
+ return Result.success();
|
|
|
}
|
|
|
|
|
|
- private EmailDto getEmailDto(EmailAccount emailAccount) {
|
|
|
- int id = emailAccount.getId();
|
|
|
- String smtp = emailAccount.getSmtp();
|
|
|
- String username = emailAccount.getUsername();
|
|
|
- String password = emailAccount.getPassword();
|
|
|
- String personal = emailAccount.getPersonal();
|
|
|
- String defaultSender = emailAccount.getDefaultSender() ? "YES" : "NO";
|
|
|
- return new EmailDto(id, smtp, username, password, personal, defaultSender);
|
|
|
+ public Webhook getWebhookReceiver() {
|
|
|
+ String name = "";
|
|
|
+ return webhookMapper.findByName(name);
|
|
|
}
|
|
|
|
|
|
- public List<EmailDto> getEmailAccounts() {
|
|
|
- List<EmailDto> list = emailAccountMapper.findAll().stream().map(this::getEmailDto).collect(Collectors.toList());
|
|
|
- return list;
|
|
|
+ public List<Webhook> getWebhookList() {
|
|
|
+ return webhookMapper.findAll();
|
|
|
}
|
|
|
}
|