增加套餐相关代码

This commit is contained in:
刘祥超
2021-10-29 14:02:40 +08:00
parent a0ff24adb6
commit d36f7d01df
14 changed files with 504 additions and 2 deletions

View File

@@ -0,0 +1,179 @@
package models
import (
"github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
)
const (
PlanStateEnabled = 1 // 已启用
PlanStateDisabled = 0 // 已禁用
)
type PlanDAO dbs.DAO
func NewPlanDAO() *PlanDAO {
return dbs.NewDAO(&PlanDAO{
DAOObject: dbs.DAOObject{
DB: Tea.Env,
Table: "edgePlans",
Model: new(Plan),
PkName: "id",
},
}).(*PlanDAO)
}
var SharedPlanDAO *PlanDAO
func init() {
dbs.OnReady(func() {
SharedPlanDAO = NewPlanDAO()
})
}
// EnablePlan 启用条目
func (this *PlanDAO) EnablePlan(tx *dbs.Tx, id uint32) error {
_, err := this.Query(tx).
Pk(id).
Set("state", PlanStateEnabled).
Update()
return err
}
// DisablePlan 禁用条目
func (this *PlanDAO) DisablePlan(tx *dbs.Tx, id int64) error {
_, err := this.Query(tx).
Pk(id).
Set("state", PlanStateDisabled).
Update()
return err
}
// FindEnabledPlan 查找启用中的条目
func (this *PlanDAO) FindEnabledPlan(tx *dbs.Tx, id int64) (*Plan, error) {
result, err := this.Query(tx).
Pk(id).
Attr("state", PlanStateEnabled).
Find()
if result == nil {
return nil, err
}
return result.(*Plan), err
}
// FindPlanName 根据主键查找名称
func (this *PlanDAO) FindPlanName(tx *dbs.Tx, id int64) (string, error) {
return this.Query(tx).
Pk(id).
Result("name").
FindStringCol("")
}
// CreatePlan 创建套餐
func (this *PlanDAO) CreatePlan(tx *dbs.Tx, name string, clusterId int64, bandwidthLimitJSON []byte, featuresJSON []byte, priceType serverconfigs.PlanPriceType, bandwidthPriceJSON []byte, monthlyPrice float32, seasonallyPrice float32, yearlyPrice float32) (int64, error) {
var op = NewPlanOperator()
op.Name = name
op.ClusterId = clusterId
if len(bandwidthLimitJSON) > 0 {
op.BandwidthLimit = bandwidthLimitJSON
}
if len(featuresJSON) > 0 {
op.Features = featuresJSON
}
op.PriceType = priceType
if len(bandwidthPriceJSON) > 0 {
op.BandwidthPrice = bandwidthPriceJSON
}
if monthlyPrice >= 0 {
op.MonthlyPrice = monthlyPrice
}
if seasonallyPrice >= 0 {
op.SeasonallyPrice = seasonallyPrice
}
if yearlyPrice >= 0 {
op.YearlyPrice = yearlyPrice
}
op.IsOn = true
op.State = PlanStateEnabled
return this.SaveInt64(tx, op)
}
// UpdatePlan 修改套餐
func (this *PlanDAO) UpdatePlan(tx *dbs.Tx, planId int64, name string, isOn bool, clusterId int64, bandwidthLimitJSON []byte, featuresJSON []byte, priceType serverconfigs.PlanPriceType, bandwidthPriceJSON []byte, monthlyPrice float32, seasonallyPrice float32, yearlyPrice float32) error {
if planId <= 0 {
return errors.New("invalid planId")
}
var op = NewPlanOperator()
op.Id = planId
op.Name = name
op.IsOn = isOn
op.ClusterId = clusterId
if len(bandwidthLimitJSON) > 0 {
op.BandwidthLimit = bandwidthLimitJSON
}
if len(featuresJSON) > 0 {
op.Features = featuresJSON
}
op.PriceType = priceType
if len(bandwidthPriceJSON) > 0 {
op.BandwidthPrice = bandwidthPriceJSON
}
if monthlyPrice >= 0 {
op.MonthlyPrice = monthlyPrice
} else {
op.MonthlyPrice = 0
}
if seasonallyPrice >= 0 {
op.SeasonallyPrice = seasonallyPrice
} else {
op.SeasonallyPrice = 0
}
if yearlyPrice >= 0 {
op.YearlyPrice = yearlyPrice
} else {
op.YearlyPrice = 0
}
return this.Save(tx, op)
}
// CountAllEnabledPlans 计算套餐的数量
func (this *PlanDAO) CountAllEnabledPlans(tx *dbs.Tx) (int64, error) {
return this.Query(tx).
State(PlanStateEnabled).
Count()
}
// ListEnabledPlans 列出单页套餐
func (this *PlanDAO) ListEnabledPlans(tx *dbs.Tx, offset int64, size int64) (result []*Plan, err error) {
_, err = this.Query(tx).
State(PlanStateEnabled).
Offset(offset).
Limit(size).
Slice(&result).
Desc("order").
DescPk().
FindAll()
return
}
// SortPlans 增加排序
func (this *PlanDAO) SortPlans(tx *dbs.Tx, planIds []int64) error {
if len(planIds) == 0 {
return nil
}
var order = len(planIds)
for _, planId := range planIds {
err := this.Query(tx).
Pk(planId).
Set("order", order).
UpdateQuickly()
if err != nil {
return err
}
order--
}
return nil
}

View File

@@ -0,0 +1,6 @@
package models
import (
_ "github.com/go-sql-driver/mysql"
_ "github.com/iwind/TeaGo/bootstrap"
)

View File

@@ -0,0 +1,38 @@
package models
// Plan 用户套餐
type Plan struct {
Id uint32 `field:"id"` // ID
IsOn uint8 `field:"isOn"` // 是否启用
Name string `field:"name"` // 套餐名
ClusterId uint32 `field:"clusterId"` // 集群ID
BandwidthLimit string `field:"bandwidthLimit"` // 带宽限制
Features string `field:"features"` // 允许的功能
BandwidthPrice string `field:"bandwidthPrice"` // 带宽价格设定
MonthlyPrice float64 `field:"monthlyPrice"` // 月付
SeasonallyPrice float64 `field:"seasonallyPrice"` // 季付
YearlyPrice float64 `field:"yearlyPrice"` // 年付
PriceType string `field:"priceType"` // 价格类型
Order uint32 `field:"order"` // 排序
State uint8 `field:"state"` // 状态
}
type PlanOperator struct {
Id interface{} // ID
IsOn interface{} // 是否启用
Name interface{} // 套餐名
ClusterId interface{} // 集群ID
BandwidthLimit interface{} // 带宽限制
Features interface{} // 允许的功能
BandwidthPrice interface{} // 带宽价格设定
MonthlyPrice interface{} // 月付
SeasonallyPrice interface{} // 季付
YearlyPrice interface{} // 年付
PriceType interface{} // 价格类型
Order interface{} // 排序
State interface{} // 状态
}
func NewPlanOperator() *PlanOperator {
return &PlanOperator{}
}

View File

@@ -0,0 +1 @@
package models

View File

@@ -37,6 +37,7 @@ type Server struct {
BandwidthLimit string `field:"bandwidthLimit"` // 带宽限制 BandwidthLimit string `field:"bandwidthLimit"` // 带宽限制
TotalBandwidth float64 `field:"totalBandwidth"` // 总带宽用量单位GB TotalBandwidth float64 `field:"totalBandwidth"` // 总带宽用量单位GB
BandwidthLimitStatus string `field:"bandwidthLimitStatus"` // 带宽限制状态 BandwidthLimitStatus string `field:"bandwidthLimitStatus"` // 带宽限制状态
UserPlanId uint32 `field:"userPlanId"` // 所属套餐ID
} }
type ServerOperator struct { type ServerOperator struct {
@@ -75,6 +76,7 @@ type ServerOperator struct {
BandwidthLimit interface{} // 带宽限制 BandwidthLimit interface{} // 带宽限制
TotalBandwidth interface{} // 总带宽用量单位GB TotalBandwidth interface{} // 总带宽用量单位GB
BandwidthLimitStatus interface{} // 带宽限制状态 BandwidthLimitStatus interface{} // 带宽限制状态
UserPlanId interface{} // 所属套餐ID
} }
func NewServerOperator() *ServerOperator { func NewServerOperator() *ServerOperator {

View File

@@ -0,0 +1,138 @@
package models
import (
"github.com/TeaOSLab/EdgeAPI/internal/errors"
_ "github.com/go-sql-driver/mysql"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
timeutil "github.com/iwind/TeaGo/utils/time"
"time"
)
const (
UserPlanStateEnabled = 1 // 已启用
UserPlanStateDisabled = 0 // 已禁用
)
type UserPlanDAO dbs.DAO
func NewUserPlanDAO() *UserPlanDAO {
return dbs.NewDAO(&UserPlanDAO{
DAOObject: dbs.DAOObject{
DB: Tea.Env,
Table: "edgeUserPlans",
Model: new(UserPlan),
PkName: "id",
},
}).(*UserPlanDAO)
}
var SharedUserPlanDAO *UserPlanDAO
func init() {
dbs.OnReady(func() {
SharedUserPlanDAO = NewUserPlanDAO()
})
}
// EnableUserPlan 启用条目
func (this *UserPlanDAO) EnableUserPlan(tx *dbs.Tx, id uint64) error {
_, err := this.Query(tx).
Pk(id).
Set("state", UserPlanStateEnabled).
Update()
return err
}
// DisableUserPlan 禁用条目
func (this *UserPlanDAO) DisableUserPlan(tx *dbs.Tx, id int64) error {
_, err := this.Query(tx).
Pk(id).
Set("state", UserPlanStateDisabled).
Update()
return err
}
// FindEnabledUserPlan 查找启用中的条目
func (this *UserPlanDAO) FindEnabledUserPlan(tx *dbs.Tx, id int64) (*UserPlan, error) {
result, err := this.Query(tx).
Pk(id).
Attr("state", UserPlanStateEnabled).
Find()
if result == nil {
return nil, err
}
return result.(*UserPlan), err
}
// CountAllEnabledUserPlans 计算套餐数量
func (this *UserPlanDAO) CountAllEnabledUserPlans(tx *dbs.Tx, isAvailable bool, isExpired bool, expiringDays int32) (int64, error) {
var query = this.Query(tx).
State(UserPlanStateEnabled).
Where("userId IN (SELECT id FROM " + SharedUserDAO.Table + " WHERE state=1)").
Where("planId IN (SELECT id FROM " + SharedPlanDAO.Table + " WHERE state=1)")
var today = timeutil.Format("Y-m-d")
if isAvailable {
query.Gte("dayTo", today)
}
if isExpired {
query.Lt("dayTo", today)
}
if expiringDays > 0 {
var expiringDay = timeutil.Format("Y-m-d", time.Now().AddDate(0, 0, int(expiringDays)))
query.Gte("dayTo", today)
query.Lte("dayTo", expiringDay)
}
return query.Count()
}
// ListEnabledUserPlans 列出单页套餐
func (this *UserPlanDAO) ListEnabledUserPlans(tx *dbs.Tx, isAvailable bool, isExpired bool, expiringDays int32, offset int64, size int64) (result []*UserPlan, err error) {
var query = this.Query(tx).
State(UserPlanStateEnabled).
Where("userId IN (SELECT id FROM " + SharedUserDAO.Table + " WHERE state=1)").
Where("planId IN (SELECT id FROM " + SharedPlanDAO.Table + " WHERE state=1)")
var today = timeutil.Format("Y-m-d")
if isAvailable {
query.Gte("dayTo", today)
}
if isExpired {
query.Lt("dayTo", today)
}
if expiringDays > 0 {
var expiringDay = timeutil.Format("Y-m-d", time.Now().AddDate(0, 0, int(expiringDays)))
query.Gte("dayTo", today)
query.Lte("dayTo", expiringDay)
}
_, err = query.
DescPk().
Offset(offset).
Limit(size).
Slice(&result).
FindAll()
return
}
// CreateUserPlan 创建套餐
func (this *UserPlanDAO) CreateUserPlan(tx *dbs.Tx, userId int64, planId int64, dayTo string) (int64, error) {
var op = NewUserPlanOperator()
op.UserId = userId
op.PlanId = planId
op.DayTo = dayTo
op.IsOn = true
op.State = UserStateEnabled
return this.SaveInt64(tx, op)
}
// UpdateUserPlan 修改套餐
func (this *UserPlanDAO) UpdateUserPlan(tx *dbs.Tx, userPlanId int64, planId int64, dayTo string, isOn bool) error {
if userPlanId <= 0 {
return errors.New("invalid userPlanId")
}
var op = NewUserPlanOperator()
op.Id = userPlanId
op.PlanId = planId
op.DayTo = dayTo
op.IsOn = isOn
return this.Save(tx, op)
}

View File

@@ -0,0 +1,6 @@
package models
import (
_ "github.com/go-sql-driver/mysql"
_ "github.com/iwind/TeaGo/bootstrap"
)

View File

@@ -0,0 +1,24 @@
package models
// UserPlan 用户的套餐
type UserPlan struct {
Id uint64 `field:"id"` // ID
UserId uint32 `field:"userId"` // 用户ID
PlanId uint32 `field:"planId"` // 套餐ID
IsOn uint8 `field:"isOn"` // 是否启用
DayTo string `field:"dayTo"` // 结束日期
State uint8 `field:"state"` // 状态
}
type UserPlanOperator struct {
Id interface{} // ID
UserId interface{} // 用户ID
PlanId interface{} // 套餐ID
IsOn interface{} // 是否启用
DayTo interface{} // 结束日期
State interface{} // 状态
}
func NewUserPlanOperator() *UserPlanOperator {
return &UserPlanOperator{}
}

View File

@@ -0,0 +1 @@
package models

View File

@@ -526,6 +526,18 @@ func (this *APINode) registerServices(server *grpc.Server) {
this.rest(instance) this.rest(instance)
} }
{
instance := this.serviceInstance(&services.PlanService{}).(*services.PlanService)
pb.RegisterPlanServiceServer(server, instance)
this.rest(instance)
}
{
instance := this.serviceInstance(&services.UserPlanService{}).(*services.UserPlanService)
pb.RegisterUserPlanServiceServer(server, instance)
this.rest(instance)
}
APINodeServicesRegister(this, server) APINodeServicesRegister(this, server)
// TODO check service names // TODO check service names

View File

@@ -0,0 +1,50 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
//go:build community
// +build community
package services
import (
"context"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
)
// PlanService 套餐相关服务
type PlanService struct {
BaseService
}
// CreatePlan 创建套餐
func (this *PlanService) CreatePlan(ctx context.Context, req *pb.CreatePlanRequest) (*pb.CreatePlanResponse, error) {
return &pb.CreatePlanResponse{}, nil
}
// UpdatePlan 修改套餐
func (this *PlanService) UpdatePlan(ctx context.Context, req *pb.UpdatePlanRequest) (*pb.RPCSuccess, error) {
return this.Success()
}
// DeletePlan 删除套餐
func (this *PlanService) DeletePlan(ctx context.Context, req *pb.DeletePlanRequest) (*pb.RPCSuccess, error) {
return this.Success()
}
// FindEnabledPlan 查找单个套餐
func (this *PlanService) FindEnabledPlan(ctx context.Context, req *pb.FindEnabledPlanRequest) (*pb.FindEnabledPlanResponse, error) {
return &pb.FindEnabledPlanResponse{Plan: nil}, nil
}
// CountAllEnabledPlans 计算套餐数量
func (this *PlanService) CountAllEnabledPlans(ctx context.Context, req *pb.CountAllEnabledPlansRequest) (*pb.RPCCountResponse, error) {
return this.SuccessCount(0)
}
// ListEnabledPlans 列出单页套餐
func (this *PlanService) ListEnabledPlans(ctx context.Context, req *pb.ListEnabledPlansRequest) (*pb.ListEnabledPlansResponse, error) {
return &pb.ListEnabledPlansResponse{Plans: nil}, nil
}
// SortPlans 对套餐进行排序
func (this *PlanService) SortPlans(ctx context.Context, req *pb.SortPlansRequest) (*pb.RPCSuccess, error) {
return this.Success()
}

View File

@@ -0,0 +1,45 @@
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
//go:build community
// +build community
package services
import (
"context"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
)
// UserPlanService 用户购买的套餐
type UserPlanService struct {
BaseService
}
// CreateUserPlan 添加已购套餐
func (this *UserPlanService) CreateUserPlan(ctx context.Context, req *pb.CreateUserPlanRequest) (*pb.CreateUserPlanResponse, error) {
return &pb.CreateUserPlanResponse{UserPlanId: 0}, nil
}
// FindEnabledUserPlan 查找单个已购套餐信息
func (this *UserPlanService) FindEnabledUserPlan(ctx context.Context, req *pb.FindEnabledUserPlanRequest) (*pb.FindEnabledUserPlanResponse, error) {
return &pb.FindEnabledUserPlanResponse{UserPlan: nil}, nil
}
// UpdateUserPlan 修改已购套餐
func (this *UserPlanService) UpdateUserPlan(ctx context.Context, req *pb.UpdateUserPlanRequest) (*pb.RPCSuccess, error) {
return this.Success()
}
// DeleteUserPlan 删除已购套餐
func (this *UserPlanService) DeleteUserPlan(ctx context.Context, req *pb.DeleteUserPlanRequest) (*pb.RPCSuccess, error) {
return this.Success()
}
// CountAllEnabledUserPlans 计算已购套餐数
func (this *UserPlanService) CountAllEnabledUserPlans(ctx context.Context, req *pb.CountAllEnabledUserPlansRequest) (*pb.RPCCountResponse, error) {
return this.SuccessCount(0)
}
// ListEnabledUserPlans 列出单页已购套餐
func (this *UserPlanService) ListEnabledUserPlans(ctx context.Context, req *pb.ListEnabledUserPlansRequest) (*pb.ListEnabledUserPlansResponse, error) {
return &pb.ListEnabledUserPlansResponse{UserPlans: nil}, nil
}

File diff suppressed because one or more lines are too long

View File

@@ -411,7 +411,7 @@ func upgradeV0_3_2(db *dbs.DB) error {
GzipId int64 `yaml:"gzipId" json:"gzipId"` // 使用的配置ID GzipId int64 `yaml:"gzipId" json:"gzipId"` // 使用的配置ID
} }
webOnes, _, err := db.FindOnes("SELECT id, gzip FROM edgeHTTPWebs WHERE gzip IS NOT NULL") webOnes, _, err := db.FindOnes("SELECT id, gzip FROM edgeHTTPWebs WHERE gzip IS NOT NULL AND compression IS NULL")
if err != nil { if err != nil {
return err return err
} }