支持多产品计划展示、LICOFF 离线签发、支付独立配置与调试,以及分析看板;同步更新 README 与集成文档。 Co-authored-by: Cursor <cursoragent@cursor.com>
164 lines
4.2 KiB
Go
164 lines
4.2 KiB
Go
package plan
|
|
|
|
import (
|
|
"backend/internal/dto"
|
|
"backend/internal/model"
|
|
"backend/utils"
|
|
"errors"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// PlanService 定价计划业务逻辑层 // Logan.Li
|
|
type PlanService struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewPlanService 创建 service 实例 // Logan.Li
|
|
func NewPlanService(db *gorm.DB) *PlanService {
|
|
return &PlanService{db: db}
|
|
}
|
|
|
|
// List 分页查询定价计划 // Logan.Li
|
|
func (s *PlanService) List(page, pageSize int, productID uint) ([]dto.PlanResponse, int64, error) {
|
|
var plans []model.Plan
|
|
var total int64
|
|
|
|
query := s.db.Model(&model.Plan{}).Preload("Product")
|
|
if productID > 0 {
|
|
query = query.Where("product_id = ?", productID)
|
|
}
|
|
|
|
query.Count(&total)
|
|
if err := query.Order("sort_order ASC, created_at DESC").
|
|
Offset((page - 1) * pageSize).Limit(pageSize).
|
|
Find(&plans).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
results := make([]dto.PlanResponse, 0, len(plans))
|
|
for _, p := range plans {
|
|
results = append(results, toPlanResponse(p))
|
|
}
|
|
return results, total, nil
|
|
}
|
|
|
|
// GetByProduct 查询某产品的上架计划(公开接口) // Logan.Li
|
|
func (s *PlanService) GetByProduct(productID uint) ([]dto.PlanResponse, error) {
|
|
var plans []model.Plan
|
|
if err := s.db.Preload("Product").Where("product_id = ? AND status = ?", productID, 1).
|
|
Order("sort_order ASC").Find(&plans).Error; err != nil {
|
|
return nil, errors.New("获取定价计划失败")
|
|
}
|
|
results := make([]dto.PlanResponse, 0, len(plans))
|
|
for _, p := range plans {
|
|
results = append(results, toPlanResponse(p))
|
|
}
|
|
return results, nil
|
|
}
|
|
|
|
// Create 创建定价计划 // Logan.Li
|
|
func (s *PlanService) Create(req dto.PlanCreate) (*dto.PlanResponse, error) {
|
|
var product model.Product
|
|
if err := s.db.First(&product, req.ProductID).Error; err != nil {
|
|
return nil, errors.New("产品不存在")
|
|
}
|
|
|
|
status := 1
|
|
if req.Status != nil {
|
|
status = *req.Status
|
|
}
|
|
currency := req.Currency
|
|
if currency == "" {
|
|
currency = "USD"
|
|
}
|
|
|
|
plan := model.Plan{
|
|
ProductID: req.ProductID,
|
|
Name: req.Name,
|
|
PlanType: req.PlanType,
|
|
Price: req.Price,
|
|
Currency: currency,
|
|
Duration: req.Duration,
|
|
MaxDevices: req.MaxDevices,
|
|
Features: utils.FormatFeatures(req.Features),
|
|
SortOrder: req.SortOrder,
|
|
Status: status,
|
|
}
|
|
if err := s.db.Create(&plan).Error; err != nil {
|
|
return nil, errors.New("创建定价计划失败")
|
|
}
|
|
plan.Product = product
|
|
resp := toPlanResponse(plan)
|
|
return &resp, nil
|
|
}
|
|
|
|
// Update 更新定价计划 // Logan.Li
|
|
func (s *PlanService) Update(id uint, req dto.PlanUpdate) (*dto.PlanResponse, error) {
|
|
var plan model.Plan
|
|
if err := s.db.Preload("Product").First(&plan, id).Error; err != nil {
|
|
return nil, errors.New("定价计划不存在")
|
|
}
|
|
|
|
updates := map[string]interface{}{}
|
|
if req.Name != "" {
|
|
updates["name"] = req.Name
|
|
}
|
|
if req.Price != nil {
|
|
updates["price"] = *req.Price
|
|
}
|
|
if req.Currency != "" {
|
|
updates["currency"] = req.Currency
|
|
}
|
|
if req.Duration != nil {
|
|
updates["duration"] = *req.Duration
|
|
}
|
|
if req.MaxDevices != nil {
|
|
updates["max_devices"] = *req.MaxDevices
|
|
}
|
|
if req.Features != nil {
|
|
updates["features"] = utils.FormatFeatures(req.Features)
|
|
}
|
|
if req.SortOrder != nil {
|
|
updates["sort_order"] = *req.SortOrder
|
|
}
|
|
if req.Status != nil {
|
|
updates["status"] = *req.Status
|
|
}
|
|
|
|
if err := s.db.Model(&plan).Updates(updates).Error; err != nil {
|
|
return nil, errors.New("更新定价计划失败")
|
|
}
|
|
s.db.Preload("Product").First(&plan, id)
|
|
resp := toPlanResponse(plan)
|
|
return &resp, nil
|
|
}
|
|
|
|
// Delete 删除定价计划 // Logan.Li
|
|
func (s *PlanService) Delete(id uint) error {
|
|
return s.db.Delete(&model.Plan{}, id).Error
|
|
}
|
|
|
|
// ToggleStatus 切换上下架状态 // Logan.Li
|
|
func (s *PlanService) ToggleStatus(id uint, status int) error {
|
|
return s.db.Model(&model.Plan{}).Where("id = ?", id).Update("status", status).Error
|
|
}
|
|
|
|
// toPlanResponse 模型转响应 DTO // Logan.Li
|
|
func toPlanResponse(p model.Plan) dto.PlanResponse {
|
|
return dto.PlanResponse{
|
|
ID: p.ID,
|
|
ProductID: p.ProductID,
|
|
ProductName: p.Product.Name,
|
|
Name: p.Name,
|
|
PlanType: p.PlanType,
|
|
Price: p.Price,
|
|
Currency: p.Currency,
|
|
Duration: p.Duration,
|
|
MaxDevices: p.MaxDevices,
|
|
Features: utils.ParseFeatures(p.Features),
|
|
SortOrder: p.SortOrder,
|
|
Status: p.Status,
|
|
}
|
|
}
|