f
This commit is contained in:
@@ -3,6 +3,7 @@ package services
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
"go.uber.org/zap"
|
||||
@@ -11,6 +12,7 @@ import (
|
||||
"tyapi-server/internal/domains/api/entities"
|
||||
api_repositories "tyapi-server/internal/domains/api/repositories"
|
||||
user_repositories "tyapi-server/internal/domains/user/repositories"
|
||||
"tyapi-server/internal/infrastructure/external/notification"
|
||||
"tyapi-server/internal/infrastructure/external/sms"
|
||||
)
|
||||
|
||||
@@ -27,6 +29,7 @@ type BalanceAlertServiceImpl struct {
|
||||
smsService *sms.AliSMSService
|
||||
config *config.Config
|
||||
logger *zap.Logger
|
||||
wechatWorkService *notification.WeChatWorkService
|
||||
}
|
||||
|
||||
// NewBalanceAlertService 创建余额预警服务
|
||||
@@ -38,6 +41,10 @@ func NewBalanceAlertService(
|
||||
config *config.Config,
|
||||
logger *zap.Logger,
|
||||
) BalanceAlertService {
|
||||
var wechatSvc *notification.WeChatWorkService
|
||||
if config != nil && config.WechatWork.WebhookURL != "" {
|
||||
wechatSvc = notification.NewWeChatWorkService(config.WechatWork.WebhookURL, config.WechatWork.Secret, logger)
|
||||
}
|
||||
return &BalanceAlertServiceImpl{
|
||||
apiUserRepo: apiUserRepo,
|
||||
userRepo: userRepo,
|
||||
@@ -45,6 +52,7 @@ func NewBalanceAlertService(
|
||||
smsService: smsService,
|
||||
config: config,
|
||||
logger: logger,
|
||||
wechatWorkService: wechatSvc,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -154,7 +162,27 @@ func (s *BalanceAlertServiceImpl) sendArrearsAlert(ctx context.Context, apiUser
|
||||
zap.Float64("balance", balance),
|
||||
zap.String("enterprise_name", enterpriseName))
|
||||
|
||||
return s.smsService.SendBalanceAlert(ctx, apiUser.AlertPhone, balance, 0, "arrears", enterpriseName)
|
||||
if err := s.smsService.SendBalanceAlert(ctx, apiUser.AlertPhone, balance, 0, "arrears", enterpriseName); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 企业微信欠费告警通知(仅展示企业名称和联系手机)
|
||||
if s.wechatWorkService != nil {
|
||||
content := fmt.Sprintf(
|
||||
"### 【天远API】用户余额欠费告警\n"+
|
||||
"<font color=\"warning\">该企业已发生欠费,请及时联系并处理。</font>\n"+
|
||||
"> 企业名称:%s\n"+
|
||||
"> 联系手机:%s\n"+
|
||||
"> 当前余额:%.2f 元\n"+
|
||||
"> 时间:%s\n",
|
||||
enterpriseName,
|
||||
apiUser.AlertPhone,
|
||||
balance,
|
||||
time.Now().Format("2006-01-02 15:04:05"),
|
||||
)
|
||||
_ = s.wechatWorkService.SendMarkdownMessage(ctx, content)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// sendLowBalanceAlert 发送低余额预警
|
||||
@@ -182,5 +210,27 @@ func (s *BalanceAlertServiceImpl) sendLowBalanceAlert(ctx context.Context, apiUs
|
||||
zap.Float64("threshold", apiUser.BalanceAlertThreshold),
|
||||
zap.String("enterprise_name", enterpriseName))
|
||||
|
||||
return s.smsService.SendBalanceAlert(ctx, apiUser.AlertPhone, balance, apiUser.BalanceAlertThreshold, "low_balance", enterpriseName)
|
||||
if err := s.smsService.SendBalanceAlert(ctx, apiUser.AlertPhone, balance, apiUser.BalanceAlertThreshold, "low_balance", enterpriseName); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 企业微信余额预警通知(仅展示企业名称和联系手机)
|
||||
if s.wechatWorkService != nil {
|
||||
content := fmt.Sprintf(
|
||||
"### 【天远API】用户余额预警\n"+
|
||||
"<font color=\"warning\">用户余额已低于预警阈值,请及时跟进。</font>\n"+
|
||||
"> 企业名称:%s\n"+
|
||||
"> 联系手机:%s\n"+
|
||||
"> 当前余额:%.2f 元\n"+
|
||||
"> 预警阈值:%.2f 元\n"+
|
||||
"> 时间:%s\n",
|
||||
enterpriseName,
|
||||
apiUser.AlertPhone,
|
||||
balance,
|
||||
apiUser.BalanceAlertThreshold,
|
||||
time.Now().Format("2006-01-02 15:04:05"),
|
||||
)
|
||||
_ = s.wechatWorkService.SendMarkdownMessage(ctx, content)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user