Files
waf-platform/EdgeAdmin/internal/web/actions/default/plans/plan/update.go

308 lines
8.9 KiB
Go

// Copyright 2021 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
//go:build plus
package plan
import (
"encoding/json"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/plans/plan/planutils"
"github.com/TeaOSLab/EdgeCommon/pkg/langs/codes"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/lists"
"github.com/iwind/TeaGo/maps"
)
type UpdateAction struct {
actionutils.ParentAction
}
func (this *UpdateAction) Init() {
this.Nav("", "", "update")
}
func (this *UpdateAction) RunGet(params struct {
PlanId int64
}) {
plan, err := planutils.InitPlan(this.Parent(), params.PlanId)
if err != nil {
this.ErrorPage(err)
return
}
// 集群
clusterResp, err := this.RPC().NodeClusterRPC().FindEnabledNodeCluster(this.AdminContext(), &pb.FindEnabledNodeClusterRequest{NodeClusterId: plan.ClusterId})
if err != nil {
this.ErrorPage(err)
return
}
var clusterMap = maps.Map{
"id": 0,
"name": "",
}
if clusterResp.NodeCluster != nil {
clusterMap["id"] = clusterResp.NodeCluster.Id
clusterMap["name"] = clusterResp.NodeCluster.Name
}
// 流量价格
var trafficPrice = &serverconfigs.PlanTrafficPriceConfig{}
if len(plan.TrafficPriceJSON) > 0 {
err = json.Unmarshal(plan.TrafficPriceJSON, trafficPrice)
if err != nil {
this.ErrorPage(err)
return
}
}
// 带宽价格
var bandwidthPrice = &serverconfigs.PlanBandwidthPriceConfig{}
if len(plan.BandwidthPriceJSON) > 0 {
err = json.Unmarshal(plan.BandwidthPriceJSON, bandwidthPrice)
if err != nil {
this.ErrorPage(err)
return
}
}
// 流量限制
var trafficLimit = &serverconfigs.TrafficLimitConfig{}
if len(plan.TrafficLimitJSON) > 0 {
err = json.Unmarshal(plan.TrafficLimitJSON, trafficLimit)
if err != nil {
this.ErrorPage(err)
return
}
}
// 带宽限制
// bandwidth limit
var bandwidthLimitPerNode = &shared.BitSizeCapacity{}
if len(plan.BandwidthLimitPerNodeJSON) > 0 {
err = json.Unmarshal(plan.BandwidthLimitPerNodeJSON, bandwidthLimitPerNode)
if err != nil {
this.ErrorPage(err)
return
}
}
// features
var selectedFeatureCodes []string
if !plan.HasFullFeatures {
if len(plan.FeaturesJSON) > 0 {
err = json.Unmarshal(plan.FeaturesJSON, &selectedFeatureCodes)
if err != nil {
this.ErrorPage(err)
return
}
}
}
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
}
var isChecked = plan.HasFullFeatures || lists.ContainsString(selectedFeatureCodes, feature.Code)
featureMaps = append(featureMaps, maps.Map{
"name": feature.Name,
"code": feature.Code,
"description": feature.Description,
"isChecked": isChecked,
})
}
this.Data["features"] = featureMaps
// max upload size
var maxUploadSize = &shared.SizeCapacity{}
if len(plan.MaxUploadSizeJSON) > 0 {
err = json.Unmarshal(plan.MaxUploadSizeJSON, maxUploadSize)
if err != nil {
this.ErrorPage(err)
return
}
}
this.Data["plan"] = maps.Map{
"id": plan.Id,
"name": plan.Name,
"description": plan.Description,
"isOn": plan.IsOn,
"cluster": clusterMap,
"priceType": plan.PriceType,
"trafficPrice": trafficPrice,
"bandwidthPrice": bandwidthPrice,
"monthlyPrice": plan.MonthlyPrice,
"seasonallyPrice": plan.SeasonallyPrice,
"yearlyPrice": plan.YearlyPrice,
"trafficLimit": trafficLimit,
"bandwidthLimitPerNode": bandwidthLimitPerNode,
"totalServers": plan.TotalServers,
"totalServerNames": plan.TotalServerNames,
"totalServerNamesPerServer": plan.TotalServerNamesPerServer,
"dailyRequests": plan.DailyRequests,
"monthlyRequests": plan.MonthlyRequests,
"dailyWebsocketConnections": plan.DailyWebsocketConnections,
"monthlyWebsocketConnections": plan.MonthlyWebsocketConnections,
"maxUploadSize": maxUploadSize,
"hasFullFeatures": plan.HasFullFeatures,
}
this.Show()
}
func (this *UpdateAction) RunPost(params struct {
PlanId int64
Name string
Description string
IsOn bool
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
}) {
this.CreateLogInfo(codes.Plan_LogUpdatePlan, params.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
}
_, err = this.RPC().PlanRPC().UpdatePlan(this.AdminContext(), &pb.UpdatePlanRequest{
PlanId: params.PlanId,
Name: params.Name,
Description: params.Description,
IsOn: params.IsOn,
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
}
this.Success()
}