106 lines
2.7 KiB
Go
106 lines
2.7 KiB
Go
package luckygift
|
|
|
|
import (
|
|
"chatapp3-golang/internal/config"
|
|
"chatapp3-golang/internal/model"
|
|
"context"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type luckyGiftRuntimeAccount struct {
|
|
AppID string
|
|
AppChannel string
|
|
AppKey string
|
|
}
|
|
|
|
func (s *LuckyGiftService) resolveProviderConfig(ctx context.Context, req LuckyGiftDrawRequest) (config.LuckyGiftProviderConfig, error) {
|
|
base, ok := findLuckyGiftConfig(s.cfg.LuckyGift.Providers, req.StandardID)
|
|
if !ok {
|
|
return config.LuckyGiftProviderConfig{}, NewAppError(
|
|
http.StatusServiceUnavailable,
|
|
"lucky_gift_config_missing",
|
|
"lucky gift provider config is missing",
|
|
)
|
|
}
|
|
|
|
runtimeAccount, err := s.resolveRuntimeAccount(ctx, req.SysOrigin)
|
|
if err != nil {
|
|
return config.LuckyGiftProviderConfig{}, NewAppError(
|
|
http.StatusInternalServerError,
|
|
"lucky_gift_runtime_config_failed",
|
|
err.Error(),
|
|
)
|
|
}
|
|
return mergeLuckyGiftProviderConfig(base, runtimeAccount), nil
|
|
}
|
|
|
|
func (s *LuckyGiftService) resolveRuntimeAccount(ctx context.Context, sysOrigin string) (luckyGiftRuntimeAccount, error) {
|
|
account := defaultLuckyGiftRuntimeAccount(s.cfg)
|
|
|
|
var row model.BaishunProviderConfig
|
|
err := s.repo.DB.WithContext(ctx).
|
|
Where("sys_origin = ?", normalizeLuckyGiftSysOrigin(sysOrigin)).
|
|
Limit(1).
|
|
First(&row).Error
|
|
if err == nil {
|
|
return applyLuckyGiftRuntimeAccount(account, row), nil
|
|
}
|
|
if err == gorm.ErrRecordNotFound {
|
|
return account, nil
|
|
}
|
|
return luckyGiftRuntimeAccount{}, err
|
|
}
|
|
|
|
func defaultLuckyGiftRuntimeAccount(cfg config.Config) luckyGiftRuntimeAccount {
|
|
account := luckyGiftRuntimeAccount{
|
|
AppChannel: strings.TrimSpace(cfg.Baishun.AppChannel),
|
|
AppKey: strings.TrimSpace(cfg.Baishun.AppKey),
|
|
}
|
|
if cfg.Baishun.AppID > 0 {
|
|
account.AppID = strconv.FormatInt(cfg.Baishun.AppID, 10)
|
|
}
|
|
return account
|
|
}
|
|
|
|
func applyLuckyGiftRuntimeAccount(base luckyGiftRuntimeAccount, row model.BaishunProviderConfig) luckyGiftRuntimeAccount {
|
|
if row.AppID > 0 {
|
|
base.AppID = strconv.FormatInt(row.AppID, 10)
|
|
}
|
|
if strings.TrimSpace(row.AppChannel) != "" {
|
|
base.AppChannel = strings.TrimSpace(row.AppChannel)
|
|
}
|
|
if strings.TrimSpace(row.AppKey) != "" {
|
|
base.AppKey = strings.TrimSpace(row.AppKey)
|
|
}
|
|
return base
|
|
}
|
|
|
|
func mergeLuckyGiftProviderConfig(
|
|
base config.LuckyGiftProviderConfig,
|
|
runtime luckyGiftRuntimeAccount,
|
|
) config.LuckyGiftProviderConfig {
|
|
merged := base
|
|
if runtime.AppID != "" {
|
|
merged.AppID = runtime.AppID
|
|
}
|
|
if runtime.AppChannel != "" {
|
|
merged.AppChannel = runtime.AppChannel
|
|
}
|
|
if runtime.AppKey != "" {
|
|
merged.AppKey = runtime.AppKey
|
|
}
|
|
return merged
|
|
}
|
|
|
|
func normalizeLuckyGiftSysOrigin(sysOrigin string) string {
|
|
sysOrigin = strings.ToUpper(strings.TrimSpace(sysOrigin))
|
|
if sysOrigin == "" {
|
|
return "LIKEI"
|
|
}
|
|
return sysOrigin
|
|
}
|