package handler import ( "strconv" "affiliate_dash/internal/middleware" "affiliate_dash/internal/model" "affiliate_dash/internal/pkg/response" "affiliate_dash/internal/service" "github.com/gin-gonic/gin" ) type OrderHandler struct { svc *service.OrderService } func NewOrderHandler(svc *service.OrderService) *OrderHandler { return &OrderHandler{svc: svc} } func (h *OrderHandler) List(c *gin.Context) { page, _ := strconv.Atoi(c.DefaultQuery("page", "1")) size, _ := strconv.Atoi(c.DefaultQuery("size", "20")) q := service.OrderListQuery{ Page: page, Size: size, Status: c.Query("status"), } // 分销商只能看自己的订单 if middleware.GetRole(c) == model.RoleDistributor { id := middleware.GetUserID(c) q.DistributorID = &id } else if d := c.Query("distributor_id"); d != "" { id, _ := strconv.ParseUint(d, 10, 64) uid := uint(id) q.DistributorID = &uid } list, total, err := h.svc.List(q) if err != nil { response.ServerError(c, err.Error()) return } response.Page(c, list, total, page, size) } type createOrderReq struct { SkinID uint `json:"skin_id" binding:"required"` BuyerName string `json:"buyer_name"` Remark string `json:"remark"` } func (h *OrderHandler) Create(c *gin.Context) { var req createOrderReq if err := c.ShouldBindJSON(&req); err != nil { response.BadRequest(c, "参数错误") return } distributorID := middleware.GetUserID(c) // 管理员可指定分销商 if middleware.GetRole(c) == model.RoleAdmin { if d := c.Query("distributor_id"); d != "" { id, _ := strconv.ParseUint(d, 10, 64) distributorID = uint(id) } } order, err := h.svc.Create(service.CreateOrderInput{ SkinID: req.SkinID, DistributorID: distributorID, BuyerName: req.BuyerName, Remark: req.Remark, }) if err != nil { response.BadRequest(c, err.Error()) return } response.OK(c, order) } type orderStatusReq struct { Status string `json:"status" binding:"required"` } func (h *OrderHandler) UpdateStatus(c *gin.Context) { id, _ := strconv.ParseUint(c.Param("id"), 10, 64) var req orderStatusReq if err := c.ShouldBindJSON(&req); err != nil { response.BadRequest(c, "参数错误") return } if err := h.svc.UpdateStatus(uint(id), req.Status); err != nil { response.BadRequest(c, err.Error()) return } response.OK(c, nil) } func (h *OrderHandler) Dashboard(c *gin.Context) { stats, err := h.svc.Dashboard() if err != nil { response.ServerError(c, err.Error()) return } response.OK(c, stats) }