支持默认价格设置

This commit is contained in:
刘祥超
2022-01-23 20:16:06 +08:00
parent 24c21c5513
commit fc28798c9f
6 changed files with 134 additions and 44 deletions

View File

@@ -31,13 +31,24 @@ func init() {
} }
// CreateOrUpdateServerBill 创建账单 // CreateOrUpdateServerBill 创建账单
func (this *ServerBillDAO) CreateOrUpdateServerBill(tx *dbs.Tx, userId int64, serverId int64, month string, userPlanId int64, planId int64, totalTrafficBytes int64, bandwidthPercentileBytes int64, bandwidthPercentile int, fee float64) error { func (this *ServerBillDAO) CreateOrUpdateServerBill(tx *dbs.Tx,
userId int64,
serverId int64,
month string,
userPlanId int64,
planId int64,
totalTrafficBytes int64,
bandwidthPercentileBytes int64,
bandwidthPercentile int,
priceType string,
fee float64) error {
fee = math.Floor(fee*100) / 100 fee = math.Floor(fee*100) / 100
return this.Query(tx). return this.Query(tx).
InsertOrUpdateQuickly(maps.Map{ InsertOrUpdateQuickly(maps.Map{
"userId": userId, "userId": userId,
"serverId": serverId, "serverId": serverId,
"month": month, "month": month,
"priceType": priceType,
"amount": fee, "amount": fee,
"userPlanId": userPlanId, "userPlanId": userPlanId,
"planId": planId, "planId": planId,
@@ -47,6 +58,7 @@ func (this *ServerBillDAO) CreateOrUpdateServerBill(tx *dbs.Tx, userId int64, se
"createdAt": time.Now().Unix(), "createdAt": time.Now().Unix(),
}, maps.Map{ }, maps.Map{
"userId": userId, "userId": userId,
"priceType": priceType,
"amount": fee, "amount": fee,
"userPlanId": userPlanId, "userPlanId": userPlanId,
"planId": planId, "planId": planId,

View File

@@ -12,7 +12,7 @@ func TestServerBillDAO_CreateOrUpdateServerBill(t *testing.T) {
var dao = NewServerBillDAO() var dao = NewServerBillDAO()
var tx *dbs.Tx var tx *dbs.Tx
var month = timeutil.Format("Y02") var month = timeutil.Format("Y02")
err := dao.CreateOrUpdateServerBill(tx, 1, 2, month, 4, 5, 6, 7, 95, 100) err := dao.CreateOrUpdateServerBill(tx, 1, 2, month, 4, 5, 6, 7, 95, "", 100)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }

View File

@@ -13,6 +13,7 @@ type ServerBill struct {
TotalTrafficBytes uint64 `field:"totalTrafficBytes"` // 总流量 TotalTrafficBytes uint64 `field:"totalTrafficBytes"` // 总流量
BandwidthPercentileBytes uint64 `field:"bandwidthPercentileBytes"` // 带宽百分位字节 BandwidthPercentileBytes uint64 `field:"bandwidthPercentileBytes"` // 带宽百分位字节
BandwidthPercentile uint8 `field:"bandwidthPercentile"` // 带宽百分位 BandwidthPercentile uint8 `field:"bandwidthPercentile"` // 带宽百分位
PriceType string `field:"priceType"` // 计费类型
} }
type ServerBillOperator struct { type ServerBillOperator struct {
@@ -27,6 +28,7 @@ type ServerBillOperator struct {
TotalTrafficBytes interface{} // 总流量 TotalTrafficBytes interface{} // 总流量
BandwidthPercentileBytes interface{} // 带宽百分位字节 BandwidthPercentileBytes interface{} // 带宽百分位字节
BandwidthPercentile interface{} // 带宽百分位 BandwidthPercentile interface{} // 带宽百分位
PriceType interface{} // 计费类型
} }
func NewServerBillOperator() *ServerBillOperator { func NewServerBillOperator() *ServerBillOperator {

View File

@@ -158,6 +158,24 @@ func (this *SysSettingDAO) ReadUserServerConfig(tx *dbs.Tx) (*userconfigs.UserSe
return config, nil return config, nil
} }
// ReadUserFinanceConfig 读取用户服务配置
func (this *SysSettingDAO) ReadUserFinanceConfig(tx *dbs.Tx) (*userconfigs.UserFinanceConfig, error) {
valueJSON, err := this.ReadSetting(tx, systemconfigs.SettingCodeUserFinanceConfig)
if err != nil {
return nil, err
}
if len(valueJSON) == 0 {
return userconfigs.DefaultUserFinanceConfig(), nil
}
var config = userconfigs.DefaultUserFinanceConfig()
err = json.Unmarshal(valueJSON, config)
if err != nil {
return nil, err
}
return config, nil
}
// ReadAdminUIConfig 读取管理员界面配置 // ReadAdminUIConfig 读取管理员界面配置
func (this *SysSettingDAO) ReadAdminUIConfig(tx *dbs.Tx, cacheMap *utils.CacheMap) (*systemconfigs.AdminUIConfig, error) { func (this *SysSettingDAO) ReadAdminUIConfig(tx *dbs.Tx, cacheMap *utils.CacheMap) (*systemconfigs.AdminUIConfig, error) {
var cacheKey = this.Table + ":ReadAdminUIConfig" var cacheKey = this.Table + ":ReadAdminUIConfig"

View File

@@ -182,6 +182,12 @@ func (this *UserBillDAO) GenerateBills(tx *dbs.Tx, month string) error {
} }
} }
// 默认计费方式
userFinanceConfig, err := SharedSysSettingDAO.ReadUserFinanceConfig(tx)
if err != nil {
return err
}
// 计算服务套餐费用 // 计算服务套餐费用
plans, err := SharedPlanDAO.FindAllEnabledPlans(tx) plans, err := SharedPlanDAO.FindAllEnabledPlans(tx)
if err != nil { if err != nil {
@@ -212,7 +218,63 @@ func (this *UserBillDAO) GenerateBills(tx *dbs.Tx, month string) error {
userIds = append(userIds, userId) userIds = append(userIds, userId)
if userPlanId == 0 { if userPlanId == 0 {
// 总流量
totalTrafficBytes, err := SharedServerDailyStatDAO.SumMonthlyBytes(tx, serverId, month)
if err != nil {
return err
}
// 默认计费方式
if userFinanceConfig != nil && userFinanceConfig.IsOn { // 默认计费方式
switch userFinanceConfig.PriceType {
case serverconfigs.PlanPriceTypeTraffic:
var config = userFinanceConfig.TrafficPriceConfig
var fee float64 = 0
if config != nil && config.Base > 0 {
fee = float64(totalTrafficBytes) / 1024 / 1024 / 1024 * float64(config.Base)
}
// 百分位
var percentile = 95
percentileBytes, err := SharedServerDailyStatDAO.FindMonthlyPercentile(tx, serverId, month, percentile)
if err != nil {
return err
}
err = SharedServerBillDAO.CreateOrUpdateServerBill(tx, userId, serverId, month, userPlanId, 0, totalTrafficBytes, percentileBytes, percentile, userFinanceConfig.PriceType, fee)
if err != nil {
return err
}
case serverconfigs.PlanPriceTypeBandwidth:
// 百分位
var percentile = 95
var config = userFinanceConfig.BandwidthPriceConfig
if config != nil {
percentile = config.Percentile
if percentile <= 0 {
percentile = 95
} else if percentile > 100 {
percentile = 100
}
}
percentileBytes, err := SharedServerDailyStatDAO.FindMonthlyPercentile(tx, serverId, month, percentile)
if err != nil {
return err
}
var mb = float32(percentileBytes) / 1024 / 1024
var price float32
if config != nil {
price = config.LookupPrice(mb)
}
var fee = float64(price)
err = SharedServerBillDAO.CreateOrUpdateServerBill(tx, userId, serverId, month, userPlanId, 0, totalTrafficBytes, percentileBytes, percentile, userFinanceConfig.PriceType, fee)
if err != nil {
return err
}
}
} else { // 区域流量计费
var fee float64 var fee float64
for _, region := range regions { for _, region := range regions {
var regionId = int64(region.Id) var regionId = int64(region.Id)
var pricesMap = region.DecodePriceMap() var pricesMap = region.DecodePriceMap()
@@ -242,12 +304,6 @@ func (this *UserBillDAO) GenerateBills(tx *dbs.Tx, month string) error {
fee += regionFee fee += regionFee
} }
// 总流量
totalTrafficBytes, err := SharedServerDailyStatDAO.SumMonthlyBytes(tx, serverId, month)
if err != nil {
return err
}
// 百分位 // 百分位
var percentile = 95 var percentile = 95
percentileBytes, err := SharedServerDailyStatDAO.FindMonthlyPercentile(tx, serverId, month, percentile) percentileBytes, err := SharedServerDailyStatDAO.FindMonthlyPercentile(tx, serverId, month, percentile)
@@ -255,10 +311,11 @@ func (this *UserBillDAO) GenerateBills(tx *dbs.Tx, month string) error {
return err return err
} }
err = SharedServerBillDAO.CreateOrUpdateServerBill(tx, userId, serverId, month, userPlanId, 0, totalTrafficBytes, percentileBytes, percentile, fee) err = SharedServerBillDAO.CreateOrUpdateServerBill(tx, userId, serverId, month, userPlanId, 0, totalTrafficBytes, percentileBytes, percentile, "", fee)
if err != nil { if err != nil {
return err return err
} }
}
} else { } else {
userPlan, err := SharedUserPlanDAO.FindUserPlanWithoutState(tx, userPlanId, cacheMap) userPlan, err := SharedUserPlanDAO.FindUserPlanWithoutState(tx, userPlanId, cacheMap)
if err != nil { if err != nil {
@@ -291,7 +348,7 @@ func (this *UserBillDAO) GenerateBills(tx *dbs.Tx, month string) error {
return err return err
} }
err = SharedServerBillDAO.CreateOrUpdateServerBill(tx, int64(userPlan.UserId), serverId, month, userPlanId, int64(userPlan.PlanId), totalTrafficBytes, percentileBytes, percentile, fee) err = SharedServerBillDAO.CreateOrUpdateServerBill(tx, int64(userPlan.UserId), serverId, month, userPlanId, int64(userPlan.PlanId), totalTrafficBytes, percentileBytes, percentile, plan.PriceType, fee)
if err != nil { if err != nil {
return err return err
} }
@@ -309,7 +366,7 @@ func (this *UserBillDAO) GenerateBills(tx *dbs.Tx, month string) error {
return err return err
} }
err = SharedServerBillDAO.CreateOrUpdateServerBill(tx, int64(userPlan.UserId), serverId, month, userPlanId, int64(userPlan.PlanId), totalTrafficBytes, percentileBytes, percentile, fee) err = SharedServerBillDAO.CreateOrUpdateServerBill(tx, int64(userPlan.UserId), serverId, month, userPlanId, int64(userPlan.PlanId), totalTrafficBytes, percentileBytes, percentile, plan.PriceType, fee)
if err != nil { if err != nil {
return err return err
} }
@@ -335,7 +392,7 @@ func (this *UserBillDAO) GenerateBills(tx *dbs.Tx, month string) error {
price = config.LookupPrice(mb) price = config.LookupPrice(mb)
} }
var fee = float64(price) var fee = float64(price)
err = SharedServerBillDAO.CreateOrUpdateServerBill(tx, int64(userPlan.UserId), serverId, month, userPlanId, int64(userPlan.PlanId), totalTrafficBytes, percentileBytes, percentile, fee) err = SharedServerBillDAO.CreateOrUpdateServerBill(tx, int64(userPlan.UserId), serverId, month, userPlanId, int64(userPlan.PlanId), totalTrafficBytes, percentileBytes, percentile, plan.PriceType, fee)
if err != nil { if err != nil {
return err return err
} }

View File

@@ -114,6 +114,7 @@ func (this *ServerBillService) ListServerBills(ctx context.Context, req *pb.List
UserId: int64(bill.UserId), UserId: int64(bill.UserId),
ServerId: int64(bill.ServerId), ServerId: int64(bill.ServerId),
Amount: float32(bill.Amount), Amount: float32(bill.Amount),
PriceType: bill.PriceType,
CreatedAt: int64(bill.CreatedAt), CreatedAt: int64(bill.CreatedAt),
UserPlanId: int64(bill.UserPlanId), UserPlanId: int64(bill.UserPlanId),
PlanId: int64(bill.PlanId), PlanId: int64(bill.PlanId),