- 新增 utils.InternalMsg:内部错误详情只进服务端日志,对外统一脱敏 消息,不再向客户端泄露 GORM/SQL 实现细节;九个 controller 的 err.Error() 出口全部切换 - 新增 .github/workflows/ci.yml:go build/vet/test(backend,go 1.25)
130 lines
4.4 KiB
Go
130 lines
4.4 KiB
Go
package plan
|
|
|
|
import (
|
|
planservice "backend/internal/service/plan"
|
|
"backend/utils"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// PlanController 定价计划控制器 // Logan.Li
|
|
type PlanController struct {
|
|
svc *planservice.PlanService
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewPlanController 创建控制器实例 // Logan.Li
|
|
func NewPlanController(db *gorm.DB) *PlanController {
|
|
return &PlanController{svc: planservice.NewPlanService(db), db: db}
|
|
}
|
|
|
|
// List 计划列表 // Logan.Li
|
|
func (ctrl *PlanController) List(c *gin.Context) {
|
|
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
|
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "10"))
|
|
productID, _ := strconv.ParseUint(c.Query("product_id"), 10, 32)
|
|
|
|
plans, total, err := ctrl.svc.List(page, pageSize, uint(productID))
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"code": 500, "message": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"code": 0, "data": gin.H{
|
|
"list": plans, "total": total, "page": page, "page_size": pageSize,
|
|
"total_pages": (total + int64(pageSize) - 1) / int64(pageSize),
|
|
}})
|
|
}
|
|
|
|
// GetByProduct 查询某产品的上架计划(公开接口) // Logan.Li
|
|
func (ctrl *PlanController) GetByProduct(c *gin.Context) {
|
|
productID, _ := strconv.ParseUint(c.Param("product_id"), 10, 32)
|
|
plans, err := ctrl.svc.GetByProduct(uint(productID))
|
|
if err != nil {
|
|
c.JSON(http.StatusOK, gin.H{"code": 1, "message": utils.InternalMsg(err)})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"code": 0, "data": plans})
|
|
}
|
|
|
|
// Create 创建计划 // Logan.Li
|
|
func (ctrl *PlanController) Create(c *gin.Context) {
|
|
var req struct {
|
|
ProductID uint `json:"product_id" binding:"required"`
|
|
Name string `json:"name" binding:"required"`
|
|
PlanType string `json:"plan_type" binding:"required"`
|
|
Price float64 `json:"price" binding:"required"`
|
|
Currency string `json:"currency"`
|
|
Duration int `json:"duration"`
|
|
MaxDevices int `json:"max_devices"`
|
|
Features []string `json:"features"`
|
|
SortOrder int `json:"sort_order"`
|
|
Status *int `json:"status"`
|
|
}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "参数错误: " + err.Error()})
|
|
return
|
|
}
|
|
plan, err := ctrl.svc.Create(req)
|
|
if err != nil {
|
|
c.JSON(http.StatusOK, gin.H{"code": 1, "message": utils.InternalMsg(err)})
|
|
return
|
|
}
|
|
utils.LogAction(ctrl.db, c, "create", "plan", plan.ID, plan.Name, "", "", "创建定价计划")
|
|
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "创建成功", "data": plan})
|
|
}
|
|
|
|
// Update 更新计划 // Logan.Li
|
|
func (ctrl *PlanController) Update(c *gin.Context) {
|
|
id, _ := strconv.ParseUint(c.Param("id"), 10, 32)
|
|
var req struct {
|
|
Name string `json:"name"`
|
|
Price *float64 `json:"price"`
|
|
Currency string `json:"currency"`
|
|
Duration *int `json:"duration"`
|
|
MaxDevices *int `json:"max_devices"`
|
|
Features []string `json:"features"`
|
|
SortOrder *int `json:"sort_order"`
|
|
Status *int `json:"status"`
|
|
}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "参数错误"})
|
|
return
|
|
}
|
|
plan, err := ctrl.svc.Update(uint(id), req)
|
|
if err != nil {
|
|
c.JSON(http.StatusOK, gin.H{"code": 1, "message": utils.InternalMsg(err)})
|
|
return
|
|
}
|
|
utils.LogAction(ctrl.db, c, "update", "plan", plan.ID, plan.Name, "", "", "更新定价计划")
|
|
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "更新成功", "data": plan})
|
|
}
|
|
|
|
// Delete 删除计划 // Logan.Li
|
|
func (ctrl *PlanController) Delete(c *gin.Context) {
|
|
id, _ := strconv.ParseUint(c.Param("id"), 10, 32)
|
|
if err := ctrl.svc.Delete(uint(id)); err != nil {
|
|
c.JSON(http.StatusOK, gin.H{"code": 1, "message": utils.InternalMsg(err)})
|
|
return
|
|
}
|
|
utils.LogAction(ctrl.db, c, "delete", "plan", uint(id), "", "", "", "删除定价计划")
|
|
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "删除成功"})
|
|
}
|
|
|
|
// ToggleStatus 切换上下架 // Logan.Li
|
|
func (ctrl *PlanController) ToggleStatus(c *gin.Context) {
|
|
id, _ := strconv.ParseUint(c.Param("id"), 10, 32)
|
|
var req struct {
|
|
Status int `json:"status"`
|
|
}
|
|
c.ShouldBindJSON(&req)
|
|
if err := ctrl.svc.ToggleStatus(uint(id), req.Status); err != nil {
|
|
c.JSON(http.StatusOK, gin.H{"code": 1, "message": utils.InternalMsg(err)})
|
|
return
|
|
}
|
|
utils.LogAction(ctrl.db, c, "toggle_status", "plan", uint(id), "", "", "", "切换计划状态")
|
|
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "状态更新成功"})
|
|
}
|