mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-11-30 21:10:28 +08:00
[区域]可以设置区域-价格项目之间的价格
This commit is contained in:
130
internal/rpc/services/service_node_price_item.go
Normal file
130
internal/rpc/services/service_node_price_item.go
Normal file
@@ -0,0 +1,130 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
)
|
||||
|
||||
// 节点区域价格相关服务
|
||||
type NodePriceItemService struct {
|
||||
BaseService
|
||||
}
|
||||
|
||||
// 创建区域价格
|
||||
func (this *NodePriceItemService) CreateNodePriceItem(ctx context.Context, req *pb.CreateNodePriceItemRequest) (*pb.CreateNodePriceItemResponse, error) {
|
||||
_, err := this.ValidateAdmin(ctx, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
itemId, err := models.SharedNodePriceItemDAO.CreateItem(req.Name, req.Type, req.BitsFrom, req.BitsTo)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &pb.CreateNodePriceItemResponse{NodePriceItemId: itemId}, nil
|
||||
}
|
||||
|
||||
// 修改区域价格
|
||||
func (this *NodePriceItemService) UpdateNodePriceItem(ctx context.Context, req *pb.UpdateNodePriceItemRequest) (*pb.RPCSuccess, error) {
|
||||
_, err := this.ValidateAdmin(ctx, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = models.SharedNodePriceItemDAO.UpdateItem(req.NodePriceItemId, req.Name, req.BitsFrom, req.BitsTo)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return this.Success()
|
||||
}
|
||||
|
||||
// 删除区域价格
|
||||
func (this *NodePriceItemService) DeleteNodePriceItem(ctx context.Context, req *pb.DeleteNodePriceItemRequest) (*pb.RPCSuccess, error) {
|
||||
_, err := this.ValidateAdmin(ctx, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = models.SharedNodePriceItemDAO.DisableNodePriceItem(req.NodePriceItemId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return this.Success()
|
||||
}
|
||||
|
||||
// 查找所有区域价格
|
||||
func (this *NodePriceItemService) FindAllEnabledNodePriceItems(ctx context.Context, req *pb.FindAllEnabledNodePriceItemsRequest) (*pb.FindAllEnabledNodePriceItemsResponse, error) {
|
||||
_, err := this.ValidateAdmin(ctx, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
prices, err := models.SharedNodePriceItemDAO.FindAllEnabledRegionPrices(req.Type)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result := []*pb.NodePriceItem{}
|
||||
for _, price := range prices {
|
||||
result = append(result, &pb.NodePriceItem{
|
||||
Id: int64(price.Id),
|
||||
IsOn: price.IsOn == 1,
|
||||
Name: price.Name,
|
||||
Type: price.Type,
|
||||
BitsFrom: int64(price.BitsFrom),
|
||||
BitsTo: int64(price.BitsTo),
|
||||
})
|
||||
}
|
||||
|
||||
return &pb.FindAllEnabledNodePriceItemsResponse{NodePriceItems: result}, nil
|
||||
}
|
||||
|
||||
// 查找所有启用的区域价格
|
||||
func (this *NodePriceItemService) FindAllEnabledAndOnNodePriceItems(ctx context.Context, req *pb.FindAllEnabledAndOnNodePriceItemsRequest) (*pb.FindAllEnabledAndOnNodePriceItemsResponse, error) {
|
||||
_, err := this.ValidateAdmin(ctx, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
prices, err := models.SharedNodePriceItemDAO.FindAllEnabledAndOnRegionPrices(req.Type)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result := []*pb.NodePriceItem{}
|
||||
for _, price := range prices {
|
||||
result = append(result, &pb.NodePriceItem{
|
||||
Id: int64(price.Id),
|
||||
IsOn: price.IsOn == 1,
|
||||
Name: price.Name,
|
||||
Type: price.Type,
|
||||
BitsFrom: int64(price.BitsFrom),
|
||||
BitsTo: int64(price.BitsTo),
|
||||
})
|
||||
}
|
||||
|
||||
return &pb.FindAllEnabledAndOnNodePriceItemsResponse{NodePriceItems: result}, nil
|
||||
}
|
||||
|
||||
// 查找单个区域信息
|
||||
func (this *NodePriceItemService) FindEnabledNodePriceItem(ctx context.Context, req *pb.FindEnabledNodePriceItemRequest) (*pb.FindEnabledNodePriceItemResponse, error) {
|
||||
_, err := this.ValidateAdmin(ctx, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
price, err := models.SharedNodePriceItemDAO.FindEnabledNodePriceItem(req.NodePriceItemId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if price == nil {
|
||||
return &pb.FindEnabledNodePriceItemResponse{NodePriceItem: nil}, nil
|
||||
}
|
||||
return &pb.FindEnabledNodePriceItemResponse{NodePriceItem: &pb.NodePriceItem{
|
||||
Id: int64(price.Id),
|
||||
IsOn: price.IsOn == 1,
|
||||
Name: price.Name,
|
||||
Type: price.Type,
|
||||
BitsFrom: int64(price.BitsFrom),
|
||||
BitsTo: int64(price.BitsTo),
|
||||
}}, nil
|
||||
}
|
||||
@@ -63,9 +63,10 @@ func (this *NodeRegionService) FindAllEnabledNodeRegions(ctx context.Context, re
|
||||
result := []*pb.NodeRegion{}
|
||||
for _, region := range regions {
|
||||
result = append(result, &pb.NodeRegion{
|
||||
Id: int64(region.Id),
|
||||
IsOn: region.IsOn == 1,
|
||||
Name: region.Name,
|
||||
Id: int64(region.Id),
|
||||
IsOn: region.IsOn == 1,
|
||||
Name: region.Name,
|
||||
PricesJSON: []byte(region.Prices),
|
||||
})
|
||||
}
|
||||
return &pb.FindAllEnabledNodeRegionsResponse{NodeRegions: result}, nil
|
||||
@@ -84,9 +85,10 @@ func (this *NodeRegionService) FindAllEnabledAndOnNodeRegions(ctx context.Contex
|
||||
result := []*pb.NodeRegion{}
|
||||
for _, region := range regions {
|
||||
result = append(result, &pb.NodeRegion{
|
||||
Id: int64(region.Id),
|
||||
IsOn: region.IsOn == 1,
|
||||
Name: region.Name,
|
||||
Id: int64(region.Id),
|
||||
IsOn: region.IsOn == 1,
|
||||
Name: region.Name,
|
||||
PricesJSON: []byte(region.Prices),
|
||||
})
|
||||
}
|
||||
return &pb.FindAllEnabledAndOnNodeRegionsResponse{NodeRegions: result}, nil
|
||||
@@ -119,8 +121,22 @@ func (this *NodeRegionService) FindEnabledNodeRegion(ctx context.Context, req *p
|
||||
return &pb.FindEnabledNodeRegionResponse{NodeRegion: nil}, nil
|
||||
}
|
||||
return &pb.FindEnabledNodeRegionResponse{NodeRegion: &pb.NodeRegion{
|
||||
Id: int64(region.Id),
|
||||
IsOn: region.IsOn == 1,
|
||||
Name: region.Name,
|
||||
Id: int64(region.Id),
|
||||
IsOn: region.IsOn == 1,
|
||||
Name: region.Name,
|
||||
PricesJSON: []byte(region.Prices),
|
||||
}}, nil
|
||||
}
|
||||
|
||||
// 修改价格项价格
|
||||
func (this *NodeRegionService) UpdateNodeRegionPrice(ctx context.Context, req *pb.UpdateNodeRegionPriceRequest) (*pb.RPCSuccess, error) {
|
||||
_, err := this.ValidateAdmin(ctx, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
err = models.SharedNodeRegionDAO.UpdateRegionItemPrice(req.NodeRegionId, req.NodeItemId, req.Price)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return this.Success()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user