new-api/common/topup-ratio.go

42 lines
932 B
Go
Raw Normal View History

2023-10-11 14:58:09 +08:00
package common
2024-06-27 19:30:17 +08:00
import (
"encoding/json"
"sync"
2024-06-27 19:30:17 +08:00
)
2023-10-11 14:58:09 +08:00
var topupGroupRatio = map[string]float64{
2023-10-11 14:58:09 +08:00
"default": 1,
"vip": 1,
"svip": 1,
}
var topupGroupRatioMutex sync.RWMutex
2023-10-11 14:58:09 +08:00
func TopupGroupRatio2JSONString() string {
topupGroupRatioMutex.RLock()
defer topupGroupRatioMutex.RUnlock()
jsonBytes, err := json.Marshal(topupGroupRatio)
2023-10-11 14:58:09 +08:00
if err != nil {
SysError("error marshalling topup group ratio: " + err.Error())
2023-10-11 14:58:09 +08:00
}
return string(jsonBytes)
}
func UpdateTopupGroupRatioByJSONString(jsonStr string) error {
topupGroupRatioMutex.Lock()
defer topupGroupRatioMutex.Unlock()
topupGroupRatio = make(map[string]float64)
return json.Unmarshal([]byte(jsonStr), &topupGroupRatio)
2023-10-11 14:58:09 +08:00
}
func GetTopupGroupRatio(name string) float64 {
topupGroupRatioMutex.RLock()
defer topupGroupRatioMutex.RUnlock()
ratio, ok := topupGroupRatio[name]
2023-10-11 14:58:09 +08:00
if !ok {
SysError("topup group ratio not found: " + name)
return 1
}
return ratio
}