package handler import ( "errors" "github.com/gin-gonic/gin" bizgift "kra/internal/biz/gift" "kra/internal/service/dto" service "kra/internal/service/gift" ) type Gift struct{ s *service.Service } func NewGift(s *service.Service) *Gift { return &Gift{s: s} } func (h *Gift) List(c *gin.Context) { in, ok := bindQuery[dto.GiftListRequest](c) if !ok { return } v, total, e := h.s.List(c.Request.Context(), in) if e != nil { businessFail(c, e) return } OKWithData(c, gin.H{"list": v, "total": total}) } func (h *Gift) PublicList(c *gin.Context) { enabled := true in := &dto.GiftListRequest{LiveListRequest: dto.LiveListRequest{Page: 1, PageSize: 100, OrderBy: "sort_asc"}, Enabled: &enabled} v, _, e := h.s.List(c.Request.Context(), in) if e != nil { businessFail(c, e) return } OKWithData(c, gin.H{"list": v}) } func (h *Gift) Get(c *gin.Context) { id, ok := businessID(c) if !ok { return } v, e := h.s.Get(c.Request.Context(), id) if e != nil { businessFail(c, e) return } OKWithData(c, v) } func (h *Gift) Create(c *gin.Context) { in, ok := bindJSON[dto.GiftRequest](c) if !ok { return } v, e := h.s.Save(c.Request.Context(), 0, in) if e != nil { businessFail(c, e) return } OKWithData(c, v) } func (h *Gift) Update(c *gin.Context) { id, ok := businessID(c) if !ok { return } in, ok := bindJSON[dto.GiftRequest](c) if !ok { return } v, e := h.s.Save(c.Request.Context(), id, in) if e != nil { businessFail(c, e) return } OKWithData(c, v) } func (h *Gift) Delete(c *gin.Context) { id, ok := businessID(c) if !ok { return } if e := h.s.Delete(c.Request.Context(), id); e != nil { businessFail(c, e) return } OK(c) } func (h *Gift) Status(c *gin.Context) { id, ok := businessID(c) if !ok { return } in, ok := bindJSON[dto.LiveStatusRequest](c) if !ok { return } if e := h.s.SetEnabled(c.Request.Context(), id, in.Enabled); e != nil { businessFail(c, e) return } OK(c) } func (h *Gift) Sort(c *gin.Context) { in, ok := bindJSON[dto.LiveSortRequest](c) if !ok { return } if e := h.s.SaveSort(c.Request.Context(), in.Values); e != nil { businessFail(c, e) return } OK(c) } func (h *Gift) Sends(c *gin.Context) { in, ok := bindQuery[dto.GiftSendListRequest](c) if !ok { return } v, total, sum, e := h.s.ListSends(c.Request.Context(), in) if e != nil { businessFail(c, e) return } OKWithData(c, gin.H{"list": v, "total": total, "summary": sum}) } func (h *Gift) Send(c *gin.Context) { id, ok := businessID(c) if !ok { return } in, ok := bindJSON[dto.GiftSendRequest](c) if !ok { return } claims := Claims(c) if claims == nil { NoAuth(c, "请登录") return } v, e := h.s.Send(c.Request.Context(), claims.ID, id, in) if e != nil { var insufficient *bizgift.InsufficientBalanceError if errors.As(e, &insufficient) { Write(c, CodeError, gin.H{ "reason": "insufficient_balance", "required": insufficient.Required, "balance": insufficient.Balance, "missing": insufficient.Missing, }, e.Error()) return } businessFail(c, e) return } OKWithData(c, v) }