package handler import ( "net/http" "github.com/gin-gonic/gin" "wx_service/internal/marketing/service" "wx_service/internal/middleware" "wx_service/internal/model" ) type DownloadHandler struct { svc *service.DownloadService } func NewDownloadHandler(svc *service.DownloadService) *DownloadHandler { return &DownloadHandler{svc: svc} } func (h *DownloadHandler) Create(c *gin.Context) { user := middleware.MustCurrentUser(c) var req service.CreateDownloadRequest if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, "请求参数错误")) return } dl, err := h.svc.Create(user.ID, req) if err != nil { if service.IsBadRequestError(err) { c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, err.Error())) return } c.JSON(http.StatusInternalServerError, model.Error(http.StatusInternalServerError, "服务器错误")) return } c.JSON(http.StatusOK, model.Success(dl)) } type adCallbackRequest struct { DownloadID uint `json:"download_id" binding:"required"` } func (h *DownloadHandler) AdCallback(c *gin.Context) { var req adCallbackRequest if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, "请求参数错误")) return } if err := h.svc.MarkAdCompleted(req.DownloadID); err != nil { if service.IsNotFoundError(err) { c.JSON(http.StatusNotFound, model.Error(http.StatusNotFound, "记录不存在")) return } c.JSON(http.StatusInternalServerError, model.Error(http.StatusInternalServerError, "服务器错误")) return } c.JSON(http.StatusOK, model.Success(nil)) } func (h *DownloadHandler) ListByUser(c *gin.Context) { user := middleware.MustCurrentUser(c) page := parseIntQuery(c, "page", 1) pageSize := parseIntQuery(c, "page_size", 20) resp, err := h.svc.ListByUser(user.ID, page, pageSize) if err != nil { c.JSON(http.StatusInternalServerError, model.Error(http.StatusInternalServerError, "服务器错误")) return } c.JSON(http.StatusOK, model.Success(resp)) } func (h *DownloadHandler) AdminStats(c *gin.Context) { stats, err := h.svc.GetStats() if err != nil { c.JSON(http.StatusInternalServerError, model.Error(http.StatusInternalServerError, "服务器错误")) return } c.JSON(http.StatusOK, model.Success(stats)) }