feat():learning后台管理项目初始化
This commit is contained in:
128
internal/services/quota_service.go
Normal file
128
internal/services/quota_service.go
Normal file
@@ -0,0 +1,128 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"goalfymax-admin/internal/models"
|
||||
"goalfymax-admin/pkg/utils"
|
||||
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// QuotaService 配额服务接口
|
||||
type QuotaService interface {
|
||||
GetQuotaHistory(req *models.QuotaHistoryRequest) (*models.QuotaHistoryResponse, error)
|
||||
HealthCheck() error
|
||||
GetQuotaRules() (*models.QuotaRulesResponse, error)
|
||||
CreateQuotaRule(body any) (*models.QuotaRulesResponse, error)
|
||||
UpdateQuotaRule(id string, body any) (*models.QuotaRulesResponse, error)
|
||||
DeleteQuotaRule(id string) (*models.QuotaRulesResponse, error)
|
||||
}
|
||||
|
||||
type quotaService struct {
|
||||
gatewayClient *GatewayClient
|
||||
logger *utils.Logger
|
||||
}
|
||||
|
||||
// NewQuotaService 创建配额服务实例
|
||||
func NewQuotaService(gatewayClient *GatewayClient, logger *utils.Logger) QuotaService {
|
||||
return "aService{
|
||||
gatewayClient: gatewayClient,
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
// GetQuotaHistory 获取配额历史数据
|
||||
func (s *quotaService) GetQuotaHistory(req *models.QuotaHistoryRequest) (*models.QuotaHistoryResponse, error) {
|
||||
// 验证请求参数
|
||||
if err := s.validateQuotaHistoryRequest(req); err != nil {
|
||||
s.logger.Error("配额历史请求参数验证失败", zap.Error(err))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 调用网关客户端
|
||||
response, err := s.gatewayClient.GetQuotaHistory(req)
|
||||
if err != nil {
|
||||
s.logger.Error("获取配额历史数据失败", zap.Error(err))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
s.logger.Info("配额历史数据获取成功",
|
||||
zap.String("start_date", req.StartDate),
|
||||
zap.String("end_date", req.EndDate),
|
||||
zap.Int("data_count", len(response.Data)),
|
||||
)
|
||||
|
||||
return response, nil
|
||||
}
|
||||
|
||||
// HealthCheck 健康检查
|
||||
func (s *quotaService) HealthCheck() error {
|
||||
err := s.gatewayClient.HealthCheck()
|
||||
if err != nil {
|
||||
s.logger.Error("配额服务健康检查失败", zap.Error(err))
|
||||
return err
|
||||
}
|
||||
|
||||
s.logger.Info("配额服务健康检查成功")
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetQuotaRules 获取配额规则列表
|
||||
func (s *quotaService) GetQuotaRules() (*models.QuotaRulesResponse, error) {
|
||||
resp, err := s.gatewayClient.GetQuotaRules("")
|
||||
if err != nil {
|
||||
s.logger.Error("获取配额规则失败", zap.Error(err))
|
||||
return nil, err
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (s *quotaService) CreateQuotaRule(body any) (*models.QuotaRulesResponse, error) {
|
||||
return s.gatewayClient.CreateQuotaRule("", body)
|
||||
}
|
||||
|
||||
func (s *quotaService) UpdateQuotaRule(id string, body any) (*models.QuotaRulesResponse, error) {
|
||||
if id == "" {
|
||||
return nil, errors.New("缺少规则ID")
|
||||
}
|
||||
return s.gatewayClient.UpdateQuotaRule("", id, body)
|
||||
}
|
||||
|
||||
func (s *quotaService) DeleteQuotaRule(id string) (*models.QuotaRulesResponse, error) {
|
||||
if id == "" {
|
||||
return nil, errors.New("缺少规则ID")
|
||||
}
|
||||
return s.gatewayClient.DeleteQuotaRule("", id)
|
||||
}
|
||||
|
||||
// validateQuotaHistoryRequest 验证配额历史请求参数
|
||||
func (s *quotaService) validateQuotaHistoryRequest(req *models.QuotaHistoryRequest) error {
|
||||
if req.StartDate == "" {
|
||||
return errors.New("开始日期不能为空")
|
||||
}
|
||||
|
||||
if req.EndDate == "" {
|
||||
return errors.New("结束日期不能为空")
|
||||
}
|
||||
|
||||
// 验证日期格式 (简单验证,实际项目中可以使用更严格的验证)
|
||||
if len(req.StartDate) != 10 || len(req.EndDate) != 10 {
|
||||
return errors.New("日期格式不正确,应为 YYYY-MM-DD")
|
||||
}
|
||||
|
||||
// 验证周期参数
|
||||
if req.Period != "" && req.Period != "daily" && req.Period != "monthly" {
|
||||
return errors.New("周期参数只能是 daily 或 monthly")
|
||||
}
|
||||
|
||||
// 设置默认值
|
||||
if req.Period == "" {
|
||||
req.Period = "daily"
|
||||
}
|
||||
|
||||
if len(req.GroupBy) == 0 {
|
||||
req.GroupBy = []string{"user_id"}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user