package order import ( "backend/internal/dto" orderservice "backend/internal/service/order" "backend/utils" "net/http" "strconv" "github.com/gin-gonic/gin" "gorm.io/gorm" ) // OrderController 订单控制器 // Logan.Li type OrderController struct { svc *orderservice.OrderService db *gorm.DB } // NewOrderController 创建控制器实例 // Logan.Li func NewOrderController(db *gorm.DB) *OrderController { return &OrderController{svc: orderservice.NewOrderService(db), db: db} } // List 订单列表(管理后台) // Logan.Li func (ctrl *OrderController) List(c *gin.Context) { page, _ := strconv.Atoi(c.DefaultQuery("page", "1")) pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "10")) status := c.Query("status") productID, _ := strconv.ParseUint(c.Query("product_id"), 10, 32) orders, total, err := ctrl.svc.List(page, pageSize, status, 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": orders, "total": total, "page": page, "page_size": pageSize, "total_pages": (total + int64(pageSize) - 1) / int64(pageSize), }}) } // GetDetail 订单详情 // Logan.Li func (ctrl *OrderController) GetDetail(c *gin.Context) { id, _ := strconv.ParseUint(c.Param("id"), 10, 32) order, err := ctrl.svc.GetByID(uint(id)) if err != nil { c.JSON(http.StatusOK, gin.H{"code": 404, "message": err.Error()}) return } c.JSON(http.StatusOK, gin.H{"code": 0, "data": order}) } // CreateOrder 客户端下单(公开接口) // Logan.Li func (ctrl *OrderController) CreateOrder(c *gin.Context) { var req struct { CustomerEmail string `json:"customer_email" binding:"required,email"` CustomerName string `json:"customer_name"` ProductID uint `json:"product_id" binding:"required"` PlanID uint `json:"plan_id" binding:"required"` DeviceID string `json:"device_id"` ReturnURL string `json:"return_url"` CancelURL string `json:"cancel_url"` } if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "参数错误: " + err.Error()}) return } resp, err := ctrl.svc.CreateOrder(req, utils.GetClientIP(c), c.GetHeader("User-Agent")) if err != nil { c.JSON(http.StatusOK, gin.H{"code": 2001, "message": err.Error()}) return } c.JSON(http.StatusOK, gin.H{"code": 0, "message": "订单创建成功", "data": resp}) } // PaymentWebhook 支付回调(公开接口,供支付渠道调用) // Logan.Li func (ctrl *OrderController) PaymentWebhook(c *gin.Context) { var req struct { EventType string `json:"event_type" binding:"required"` OrderNo string `json:"order_no" binding:"required"` GatewayOrderNo string `json:"gateway_order_no"` Status string `json:"status" binding:"required"` Amount float64 `json:"amount"` Currency string `json:"currency"` CustomerEmail string `json:"customer_email"` Gateway string `json:"gateway" binding:"required"` Signature string `json:"signature"` Payload string `json:"payload"` } if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "Invalid payload"}) return } if err := ctrl.svc.PaymentWebhook(req, utils.GetClientIP(c)); err != nil { c.JSON(http.StatusOK, gin.H{"code": 1, "message": err.Error()}) return } c.JSON(http.StatusOK, gin.H{"code": 0, "message": "Webhook processed"}) } // ManualGrant 手动发放 License(管理后台) // Logan.Li func (ctrl *OrderController) ManualGrant(c *gin.Context) { var req struct { CustomerEmail string `json:"customer_email" binding:"required,email"` CustomerName string `json:"customer_name"` ProductID uint `json:"product_id" binding:"required"` PlanID uint `json:"plan_id" binding:"required"` DeviceID string `json:"device_id"` Notes string `json:"notes"` } if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "参数错误: " + err.Error()}) return } userID, _ := c.Get("user_id") username, _ := c.Get("username") data, err := ctrl.svc.ManualGrant(req, userID.(uint), username.(string), utils.GetClientIP(c)) if err != nil { c.JSON(http.StatusOK, gin.H{"code": 1, "message": err.Error()}) return } c.JSON(http.StatusOK, gin.H{"code": 0, "message": "License 发放成功", "data": data}) } // CheckDuplicatePurchase 防重复购买检测(公开接口) // Logan.Li func (ctrl *OrderController) CheckDuplicatePurchase(c *gin.Context) { var req struct { CustomerEmail string `json:"customer_email" binding:"required,email"` ProductID uint `json:"product_id" binding:"required"` } if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "参数错误"}) return } // 通过 CheckPurchaseStatus 间接判断:有记录且有效则不允许重复购买 // Logan.Li status, _ := ctrl.svc.CheckPurchaseStatus(req.CustomerEmail, req.ProductID) result := dto.DuplicatePurchaseCheckResponse{AllowPurchase: true} if status.HasActiveLicense { result.AllowPurchase = false result.Reason = status.Message if result.Reason == "" { result.Reason = "您已拥有该产品的有效授权" } result.ExistingLicenseKey = status.LicenseKey result.ExistingPlanType = status.PlanType result.CanUpgrade = true } c.JSON(http.StatusOK, gin.H{"code": 0, "data": result}) } // CheckPurchaseStatus 查询购买状态(公开接口) // Logan.Li func (ctrl *OrderController) CheckPurchaseStatus(c *gin.Context) { var req struct { CustomerEmail string `json:"customer_email" binding:"required,email"` ProductID uint `json:"product_id" binding:"required"` } if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "参数错误"}) return } resp, _ := ctrl.svc.CheckPurchaseStatus(req.CustomerEmail, req.ProductID) c.JSON(http.StatusOK, gin.H{"code": 0, "data": resp}) }