This commit is contained in:
2026-02-25 19:33:56 +08:00
parent 8c0c16006e
commit f17e22f4c8
8 changed files with 383 additions and 20 deletions

View File

@@ -14,6 +14,7 @@ import (
finance_services "tyapi-server/internal/domains/finance/services"
product_repositories "tyapi-server/internal/domains/product/repositories"
user_repositories "tyapi-server/internal/domains/user/repositories"
"tyapi-server/internal/infrastructure/external/notification"
"tyapi-server/internal/shared/component_report"
"tyapi-server/internal/shared/database"
"tyapi-server/internal/shared/export"
@@ -43,6 +44,7 @@ type FinanceApplicationServiceImpl struct {
exportManager *export.ExportManager
logger *zap.Logger
config *config.Config
wechatWorkService *notification.WeChatWorkService
}
// NewFinanceApplicationService 创建财务应用服务
@@ -63,6 +65,11 @@ func NewFinanceApplicationService(
config *config.Config,
exportManager *export.ExportManager,
) FinanceApplicationService {
var wechatSvc *notification.WeChatWorkService
if config != nil && config.WechatWork.WebhookURL != "" {
wechatSvc = notification.NewWeChatWorkService(config.WechatWork.WebhookURL, config.WechatWork.Secret, logger)
}
return &FinanceApplicationServiceImpl{
aliPayClient: aliPayClient,
wechatPayService: wechatPayService,
@@ -79,9 +86,46 @@ func NewFinanceApplicationService(
exportManager: exportManager,
logger: logger,
config: config,
wechatWorkService: wechatSvc,
}
}
// getUserContactInfo 获取企业名称和联系手机号(尽量用企业信息里的手机号,退化到用户登录手机号)
func (s *FinanceApplicationServiceImpl) getUserContactInfo(ctx context.Context, userID string) (companyName, phone string) {
companyName = "未知企业"
phone = ""
if userID == "" {
return
}
user, err := s.userRepo.GetByIDWithEnterpriseInfo(ctx, userID)
if err != nil {
s.logger.Warn("获取用户企业信息失败,使用默认企业名称",
zap.String("user_id", userID),
zap.Error(err),
)
return
}
// 登录手机号
if user.Phone != "" {
phone = user.Phone
}
// 企业名称和企业手机号
if user.EnterpriseInfo != nil {
if user.EnterpriseInfo.CompanyName != "" {
companyName = user.EnterpriseInfo.CompanyName
}
if user.EnterpriseInfo.LegalPersonPhone != "" {
phone = user.EnterpriseInfo.LegalPersonPhone
}
}
return
}
func (s *FinanceApplicationServiceImpl) CreateWallet(ctx context.Context, cmd *commands.CreateWalletCommand) (*responses.WalletResponse, error) {
// 调用钱包聚合服务创建钱包
wallet, err := s.walletService.CreateWallet(ctx, cmd.UserID)
@@ -936,6 +980,33 @@ func (s *FinanceApplicationServiceImpl) processAlipayPaymentSuccess(ctx context.
zap.String("amount", amount.String()),
)
// 充值成功企业微信通知(仅充值订单,且忽略发送错误)
if s.wechatWorkService != nil {
// 再次获取充值记录拿到用户ID
rechargeRecord, err := s.rechargeRecordRepo.GetByID(ctx, alipayOrder.RechargeID)
if err == nil {
companyName, phone := s.getUserContactInfo(ctx, rechargeRecord.UserID)
content := fmt.Sprintf(
"### 【天远API】用户充值成功通知\n"+
"> 企业名称:%s\n"+
"> 联系手机:%s\n"+
"> 充值渠道:支付宝\n"+
"> 充值金额:%s 元\n"+
"> 时间:%s\n",
companyName,
phone,
amount.String(),
time.Now().Format("2006-01-02 15:04:05"),
)
_ = s.wechatWorkService.SendMarkdownMessage(ctx, content)
} else {
s.logger.Warn("获取充值记录失败,跳过企业微信充值通知",
zap.String("out_trade_no", outTradeNo),
zap.Error(err),
)
}
}
return nil
}
@@ -1681,6 +1752,24 @@ func (s *FinanceApplicationServiceImpl) processWechatPaymentSuccess(ctx context.
zap.String("user_id", rechargeRecord.UserID),
)
// 微信充值成功企业微信通知(忽略发送错误)
if s.wechatWorkService != nil {
companyName, phone := s.getUserContactInfo(ctx, rechargeRecord.UserID)
content := fmt.Sprintf(
"### 【天远API】用户充值成功通知\n"+
"> 企业名称:%s\n"+
"> 联系手机:%s\n"+
"> 充值渠道:微信\n"+
"> 充值金额:%s 元\n"+
"> 时间:%s\n",
companyName,
phone,
amount.String(),
time.Now().Format("2006-01-02 15:04:05"),
)
_ = s.wechatWorkService.SendMarkdownMessage(ctx, content)
}
return nil
}