193 lines
5.5 KiB
Go
193 lines
5.5 KiB
Go
// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
|
//go:build plus
|
|
|
|
package plans
|
|
|
|
import (
|
|
"encoding/json"
|
|
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/langs/codes"
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
|
"github.com/iwind/TeaGo/actions"
|
|
"github.com/iwind/TeaGo/maps"
|
|
)
|
|
|
|
type CreateAction struct {
|
|
actionutils.ParentAction
|
|
}
|
|
|
|
func (this *CreateAction) Init() {
|
|
this.Nav("", "", "create")
|
|
}
|
|
|
|
func (this *CreateAction) RunGet(params struct{}) {
|
|
// features
|
|
featuresResp, err := this.RPC().UserRPC().FindAllUserFeatureDefinitions(this.AdminContext(), &pb.FindAllUserFeatureDefinitionsRequest{})
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
var allFeatures = featuresResp.Features
|
|
|
|
var featureMaps = []maps.Map{}
|
|
for _, feature := range allFeatures {
|
|
if !feature.SupportPlan {
|
|
continue
|
|
}
|
|
featureMaps = append(featureMaps, maps.Map{
|
|
"name": feature.Name,
|
|
"code": feature.Code,
|
|
"description": feature.Description,
|
|
"isChecked": true,
|
|
})
|
|
}
|
|
this.Data["features"] = featureMaps
|
|
|
|
this.Show()
|
|
}
|
|
|
|
func (this *CreateAction) RunPost(params struct {
|
|
Name string
|
|
Description string
|
|
ClusterId int64
|
|
TrafficLimitJSON []byte
|
|
BandwidthLimitPerNodeJSON []byte
|
|
PriceType string
|
|
MonthlyPrice float32
|
|
SeasonallyPrice float32
|
|
YearlyPrice float32
|
|
TrafficPriceJSON []byte
|
|
BandwidthPriceJSON []byte
|
|
TotalServers int32
|
|
TotalServerNames int32
|
|
TotalServerNamesPerServer int32
|
|
DailyRequests int64
|
|
MonthlyRequests int64
|
|
DailyWebsocketConnections int64
|
|
MonthlyWebsocketConnections int64
|
|
MaxUploadSizeJSON []byte
|
|
|
|
HasFullFeatures bool
|
|
FeatureCodes []string
|
|
|
|
Must *actions.Must
|
|
CSRF *actionutils.CSRF
|
|
}) {
|
|
var planId = int64(0)
|
|
defer func() {
|
|
this.CreateLogInfo(codes.Plan_LogCreatePlan, planId)
|
|
}()
|
|
|
|
params.Must.
|
|
Field("name", params.Name).
|
|
Require("请输入套餐名称").
|
|
Field("clusterId", params.ClusterId).
|
|
Gt(0, "请选择关联集群")
|
|
|
|
if params.MonthlyPrice <= 0 {
|
|
params.MonthlyPrice = 0
|
|
}
|
|
if params.SeasonallyPrice <= 0 {
|
|
params.SeasonallyPrice = 0
|
|
}
|
|
if params.YearlyPrice <= 0 {
|
|
params.YearlyPrice = 0
|
|
}
|
|
|
|
switch params.PriceType {
|
|
case serverconfigs.PlanPriceTypePeriod:
|
|
if params.MonthlyPrice == 0 && params.SeasonallyPrice == 0 && params.YearlyPrice == 0 {
|
|
this.Fail("月度、季度、年度至少需要设置一个价格。")
|
|
return
|
|
}
|
|
|
|
if params.MonthlyPrice > 0 {
|
|
if params.SeasonallyPrice == 0 {
|
|
this.Fail("由于你设置了月度价格,所以必须设置季度价格")
|
|
return
|
|
}
|
|
if params.YearlyPrice == 0 {
|
|
this.Fail("由于你设置了月度价格,所以必须设置年度价格")
|
|
return
|
|
}
|
|
}
|
|
if params.SeasonallyPrice > 0 {
|
|
if params.YearlyPrice == 0 {
|
|
this.Fail("由于你设置了季度价格,所以必须设置年度价格")
|
|
return
|
|
}
|
|
}
|
|
case serverconfigs.PlanPriceTypeTraffic:
|
|
if len(params.TrafficPriceJSON) == 0 {
|
|
this.Fail("请设置流量价格")
|
|
}
|
|
var config = &serverconfigs.PlanTrafficPriceConfig{}
|
|
err := json.Unmarshal(params.TrafficPriceJSON, config)
|
|
if err != nil {
|
|
this.Fail("流量价格设置错误:" + err.Error())
|
|
}
|
|
if config.Base <= 0 {
|
|
this.Fail("基础价格必须大于0")
|
|
}
|
|
case serverconfigs.PlanPriceTypeBandwidth:
|
|
if len(params.BandwidthPriceJSON) == 0 {
|
|
this.Fail("请设置带宽价格")
|
|
}
|
|
var config = &serverconfigs.PlanBandwidthPriceConfig{}
|
|
err := json.Unmarshal(params.BandwidthPriceJSON, config)
|
|
if err != nil {
|
|
this.Fail("带宽价格设置错误:" + err.Error())
|
|
}
|
|
if config.Percentile <= 0 {
|
|
this.Fail("百分位必须大于0")
|
|
}
|
|
if len(config.Ranges) == 0 {
|
|
this.Fail("请添加带宽价格")
|
|
}
|
|
}
|
|
|
|
// features
|
|
var featureCodes = []string{}
|
|
if !params.HasFullFeatures && params.FeatureCodes != nil {
|
|
featureCodes = params.FeatureCodes
|
|
}
|
|
|
|
featureCodesJSON, err := json.Marshal(featureCodes)
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
|
|
createResp, err := this.RPC().PlanRPC().CreatePlan(this.AdminContext(), &pb.CreatePlanRequest{
|
|
Name: params.Name,
|
|
Description: params.Description,
|
|
ClusterId: params.ClusterId,
|
|
TrafficLimitJSON: params.TrafficLimitJSON,
|
|
BandwidthLimitPerNodeJSON: params.BandwidthLimitPerNodeJSON,
|
|
HasFullFeatures: params.HasFullFeatures,
|
|
FeaturesJSON: featureCodesJSON,
|
|
PriceType: params.PriceType,
|
|
TrafficPriceJSON: params.TrafficPriceJSON,
|
|
BandwidthPriceJSON: params.BandwidthPriceJSON,
|
|
MonthlyPrice: params.MonthlyPrice,
|
|
SeasonallyPrice: params.SeasonallyPrice,
|
|
YearlyPrice: params.YearlyPrice,
|
|
TotalServers: params.TotalServers,
|
|
TotalServerNames: params.TotalServerNames,
|
|
TotalServerNamesPerServer: params.TotalServerNamesPerServer,
|
|
DailyRequests: params.DailyRequests,
|
|
MonthlyRequests: params.MonthlyRequests,
|
|
DailyWebsocketConnections: params.DailyWebsocketConnections,
|
|
MonthlyWebsocketConnections: params.MonthlyWebsocketConnections,
|
|
MaxUploadSizeJSON: params.MaxUploadSizeJSON,
|
|
})
|
|
if err != nil {
|
|
this.ErrorPage(err)
|
|
return
|
|
}
|
|
planId = createResp.PlanId
|
|
|
|
this.Success()
|
|
}
|