mirror of
https://github.com/TeaOSLab/EdgeCommon.git
synced 2026-02-24 19:35:37 +08:00
实现带宽计费套餐
This commit is contained in:
@@ -2,23 +2,83 @@
|
||||
|
||||
package serverconfigs
|
||||
|
||||
// PlanPriceType 套餐类型
|
||||
type PlanPriceType = string
|
||||
|
||||
const (
|
||||
PlanPriceTypeTraffic PlanPriceType = "traffic"
|
||||
PlanPriceTypePeriod PlanPriceType = "period"
|
||||
PlanPriceTypeTraffic PlanPriceType = "traffic" // 流量
|
||||
PlanPriceTypePeriod PlanPriceType = "period" // 周期
|
||||
PlanPriceTypeBandwidth PlanPriceType = "bandwidth" // 百分位
|
||||
)
|
||||
|
||||
func FindPlanPriceTypeName(priceType PlanPriceType) string {
|
||||
switch priceType {
|
||||
case PlanPriceTypeTraffic:
|
||||
return "带宽用量"
|
||||
return "流量"
|
||||
case PlanPriceTypePeriod:
|
||||
return "时间周期"
|
||||
case PlanPriceTypeBandwidth:
|
||||
return "带宽"
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type PlanTrafficPrice struct {
|
||||
// PlanTrafficPriceConfig 按流量计费价格配置
|
||||
type PlanTrafficPriceConfig struct {
|
||||
Base float32 `yaml:"base" json:"base"` // 基础价格,单位是 元/GB
|
||||
}
|
||||
|
||||
// PlanBandwidthPriceConfig 按百分位带宽计费配置
|
||||
type PlanBandwidthPriceConfig struct {
|
||||
Percentile int `yaml:"percentile" json:"percentile"` // 百分位
|
||||
Ranges []*PlanBandwidthPriceRangeConfig `yaml:"ranges" json:"ranges"`
|
||||
}
|
||||
|
||||
func (this *PlanBandwidthPriceConfig) LookupRange(sizeMB float32) *PlanBandwidthPriceRangeConfig {
|
||||
if len(this.Ranges) == 0 {
|
||||
return nil
|
||||
}
|
||||
for _, r := range this.Ranges {
|
||||
if sizeMB >= r.MinMB && (r.MaxMB <= 0 || r.MaxMB >= sizeMB) {
|
||||
return r
|
||||
}
|
||||
}
|
||||
|
||||
// 寻找最接近的
|
||||
for index, r := range this.Ranges {
|
||||
if r.MinMB >= sizeMB {
|
||||
if index > 0 {
|
||||
return this.Ranges[index-1]
|
||||
}
|
||||
return r
|
||||
}
|
||||
}
|
||||
|
||||
for _, r := range this.Ranges {
|
||||
if r.MaxMB <= 0 || r.MaxMB >= sizeMB {
|
||||
return r
|
||||
}
|
||||
}
|
||||
|
||||
// 获取最大值
|
||||
return this.Ranges[len(this.Ranges)-1]
|
||||
}
|
||||
|
||||
func (this *PlanBandwidthPriceConfig) LookupPrice(sizeMB float32) float32 {
|
||||
var r = this.LookupRange(sizeMB)
|
||||
if r == nil {
|
||||
return 0
|
||||
}
|
||||
|
||||
if r.TotalPrice > 0 {
|
||||
return r.TotalPrice
|
||||
}
|
||||
return r.PricePerMB * sizeMB
|
||||
}
|
||||
|
||||
type PlanBandwidthPriceRangeConfig struct {
|
||||
MinMB float32 `yaml:"minMB" json:"minMB"`
|
||||
MaxMB float32 `yaml:"maxMB" json:"maxMB"`
|
||||
PricePerMB float32 `yaml:"pricePerMB" json:"pricePerMB"` // 单位价格,元/MB
|
||||
TotalPrice float32 `yaml:"totalPrice" json:"totalPrice"` // 总价格
|
||||
}
|
||||
|
||||
41
pkg/serverconfigs/plan_price_types_test.go
Normal file
41
pkg/serverconfigs/plan_price_types_test.go
Normal file
@@ -0,0 +1,41 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package serverconfigs_test
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPlanBandwidthPriceConfig_Lookup(t *testing.T) {
|
||||
{
|
||||
var config = &serverconfigs.PlanBandwidthPriceConfig{}
|
||||
t.Log(config.LookupRange(1))
|
||||
}
|
||||
|
||||
{
|
||||
var config = &serverconfigs.PlanBandwidthPriceConfig{
|
||||
Ranges: []*serverconfigs.PlanBandwidthPriceRangeConfig{
|
||||
{
|
||||
MinMB: 1,
|
||||
MaxMB: 1.5,
|
||||
},
|
||||
{
|
||||
MinMB: 1.5,
|
||||
MaxMB: 2,
|
||||
},
|
||||
{
|
||||
MinMB: 5,
|
||||
MaxMB: 10,
|
||||
},
|
||||
{
|
||||
MinMB: 20,
|
||||
MaxMB: 100,
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, mb := range []float32{0.5, 1, 3, 5, 7, 20, 50, 1000} {
|
||||
t.Log(mb, config.LookupRange(mb))
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user