diff --git a/go.mod b/go.mod index 482e1fd..f7dce82 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module gitea.timerzz.com/kedaya_haitao/article go 1.22.5 require ( - gitea.timerzz.com/kedaya_haitao/common v0.0.0-20241202091018-277d73739be8 + gitea.timerzz.com/kedaya_haitao/common v0.0.0-20241202121425-6def3252d85c github.com/gofiber/fiber/v3 v3.0.0-beta.3 github.com/golang/glog v1.2.2 ) diff --git a/go.sum b/go.sum index ba8cb0a..468dab7 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ gitea.timerzz.com/kedaya_haitao/common v0.0.0-20241202091018-277d73739be8 h1:AJo3Y3icJb8wcjeSnx6SjkHFdBKYm5lFscEuo6O4dDM= gitea.timerzz.com/kedaya_haitao/common v0.0.0-20241202091018-277d73739be8/go.mod h1:BIz+IMGznPiyLnV1+Ntw1zf8rEIcbymmGq+EfvDsSgE= +gitea.timerzz.com/kedaya_haitao/common v0.0.0-20241202121425-6def3252d85c h1:WELIrLlcOud0cutzPiZnroF3Z39Surcae7W8sx2XZi0= +gitea.timerzz.com/kedaya_haitao/common v0.0.0-20241202121425-6def3252d85c/go.mod h1:BIz+IMGznPiyLnV1+Ntw1zf8rEIcbymmGq+EfvDsSgE= github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M= github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= diff --git a/service/article_svc.go b/service/article_svc.go index 7234416..dc386c4 100644 --- a/service/article_svc.go +++ b/service/article_svc.go @@ -3,12 +3,15 @@ package service import ( "fmt" "strconv" + "strings" "gitea.timerzz.com/kedaya_haitao/common/pkg/web" "gitea.timerzz.com/kedaya_haitao/common/structs/storage" "gitea.timerzz.com/kedaya_haitao/common/structs/utils" v2 "gitea.timerzz.com/kedaya_haitao/common/structs/v2" "github.com/gofiber/fiber/v3" + "github.com/samber/lo" + "gorm.io/gorm" ) type Article struct { @@ -24,13 +27,18 @@ func NewArticle(storage *storage.Storage) *Article { func (s *Article) Registry(r fiber.Router) { api := r.Group("/api/v2") api.Get("articles", s.List) - api.Get("articles/:id", s.Get) + api.Get("articles/u/:id", s.Get) api.Get("articles/dict/brand", s.BrandDict) api.Patch("articles", s.Update) api.Get("articles/provider/history/:id", s.ProviderHistory) api.Get("articles/seller/history/:id", s.SellerHistory) api.Patch("articles/provider", s.UpdateProviderArticle) api.Patch("articles/seller", s.UpdateSellerArticle) + + //屏蔽相关操作 + api.Get("articles/ban", s.ListBanArticle) + api.Post("articles/ban/:ids", s.BanArticle) + api.Delete("articles/ban/:ids", s.LiftBanArticle) } func (s *Article) List(c fiber.Ctx) error { @@ -38,6 +46,7 @@ func (s *Article) List(c fiber.Ctx) error { if err := c.Bind().Query(&q); err != nil { return err } + q.SetBan(false) var query = storage.NewPageListQuery(&q) if err := c.Bind().Query(query); err != nil { return err @@ -137,3 +146,72 @@ func (s *Article) UpdateSellerArticle(c fiber.Ctx) error { } return c.JSON(web.NewResponse(article)) } + +// BanArticle 禁用商品 +func (s *Article) BanArticle(c fiber.Ctx) error { + ids := c.Params("ids") + if ids == "" { + return fiber.NewError(fiber.StatusBadRequest, "ids is required") + } + idList := lo.Map(strings.Split(ids, ","), func(item string, index int) int { + id, _ := strconv.Atoi(item) + return id + }) + + err := s.storage.DB().Transaction(func(tx *gorm.DB) (err error) { + if err = tx.Model(&v2.Article{}).Select("ban", "exclude").Where("id IN ?", idList).Updates(map[string]interface{}{"ban": true, "exclude": true}).Error; err != nil { + return err + } + if err = tx.Model(&v2.ProviderArticle{}).Where("article_id IN ?", idList).Update("exclude", true).Error; err != nil { + return err + } + return tx.Model(&v2.SellerArticle{}).Where("article_id IN ?", idList).Update("exclude", true).Error + }) + if err != nil { + return err + } + return c.JSON(web.NewResponse("ok")) +} + +// 解禁商品 +func (s *Article) LiftBanArticle(c fiber.Ctx) error { + ids := c.Params("ids") + if ids == "" { + return fiber.NewError(fiber.StatusBadRequest, "ids is required") + } + idList := lo.Map(strings.Split(ids, ","), func(item string, index int) int { + id, _ := strconv.Atoi(item) + return id + }) + + err := s.storage.DB().Transaction(func(tx *gorm.DB) (err error) { + if err = tx.Model(&v2.Article{}).Select("ban", "exclude").Where("id IN ?", idList).Updates(map[string]interface{}{"ban": false, "exclude": false}).Error; err != nil { + return err + } + if err = tx.Model(&v2.ProviderArticle{}).Where("article_id IN ?", idList).Update("exclude", false).Error; err != nil { + return err + } + return tx.Model(&v2.SellerArticle{}).Where("article_id IN ?", idList).Update("exclude", false).Error + }) + if err != nil { + return err + } + return c.JSON(web.NewResponse("ok")) +} + +func (s *Article) ListBanArticle(c fiber.Ctx) error { + var q storage.FindArticleQuery + if err := c.Bind().Query(&q); err != nil { + return err + } + q.SetBan(true) + var query = storage.NewPageListQuery(&q) + if err := c.Bind().Query(query); err != nil { + return err + } + articles, total, err := s.storage.Article().List(*query, q.SortScope) + if err != nil { + return err + } + return c.JSON(web.NewResponse(web.NewListResponse(total, articles))) +}