1
zj
5 days ago effab9619e17b97ac98b035eaa64ba99b27f5dc6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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;
    }
}