package com.yami.trading.service.notify;
|
|
import com.yami.trading.bean.model.User;
|
import com.yami.trading.service.EmailSendService;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
|
import java.math.BigDecimal;
|
|
/**
|
* Sends English notification emails for successful deposits and withdrawals.
|
* Recipient: {@link User#getUserMail()} if set, otherwise {@link User#getUserName()} when it looks like an email.
|
*/
|
@Service
|
@Slf4j
|
public class WalletTransactionMailNotificationService {
|
|
@Autowired
|
private EmailSendService emailSendService;
|
|
public void sendRechargeSuccess(User user, String orderNo, BigDecimal amount, String assetDescription) {
|
String to = resolveRecipientEmail(user);
|
if (to == null) {
|
log.warn("Skip deposit success email: no email for userId={}", user != null ? user.getUserId() : null);
|
return;
|
}
|
String subject = "Deposit successful";
|
String amt = amount == null ? "-" : amount.stripTrailingZeros().toPlainString();
|
String asset = assetDescription == null ? "" : assetDescription;
|
String body = "Hello,\n\n"
|
+ "Your deposit has been credited successfully.\n\n"
|
+ "Order number: " + orderNo + "\n"
|
+ "Amount: " + amt + " " + asset + "\n\n"
|
+ "If you did not make this transaction, please contact support immediately.\n\n"
|
+ "Best regards";
|
try {
|
emailSendService.sendEmail(to, subject, body);
|
} catch (Exception e) {
|
log.error("Failed to send deposit success email, orderNo={}", orderNo, e);
|
}
|
}
|
|
public void sendWithdrawSuccess(User user, String orderNo, BigDecimal amount, BigDecimal fee, String assetDescription) {
|
String to = resolveRecipientEmail(user);
|
if (to == null) {
|
log.warn("Skip withdrawal success email: no email for userId={}", user != null ? user.getUserId() : null);
|
return;
|
}
|
String subject = "Withdrawal successful";
|
String amt = amount == null ? "-" : amount.stripTrailingZeros().toPlainString();
|
String feeStr = (fee == null || fee.compareTo(BigDecimal.ZERO) == 0)
|
? "none"
|
: fee.stripTrailingZeros().toPlainString();
|
String asset = assetDescription == null ? "" : assetDescription;
|
String body = "Hello,\n\n"
|
+ "Your withdrawal has been completed successfully.\n\n"
|
+ "Order number: " + orderNo + "\n"
|
+ "Amount: " + amt + " " + asset + "\n"
|
+ "Fee: " + feeStr + "\n\n"
|
+ "If you did not request this withdrawal, please contact support immediately.\n\n"
|
+ "Best regards";
|
try {
|
emailSendService.sendEmail(to, subject, body);
|
} catch (Exception e) {
|
log.error("Failed to send withdrawal success email, orderNo={}", orderNo, e);
|
}
|
}
|
|
private static String resolveRecipientEmail(User user) {
|
if (user == null) {
|
return null;
|
}
|
if (user.getUserMail() != null && !user.getUserMail().trim().isEmpty()) {
|
return user.getUserMail().trim();
|
}
|
if (user.getUserName() != null && user.getUserName().contains("@")) {
|
return user.getUserName().trim();
|
}
|
return null;
|
}
|
}
|