feat 屏蔽商品
All checks were successful
Build image / build (push) Successful in 1m0s

This commit is contained in:
timerzz 2024-12-03 11:03:36 +08:00
parent 4e6ef40b72
commit 944c9a0a81
3 changed files with 82 additions and 2 deletions

2
go.mod
View File

@ -3,7 +3,7 @@ module gitea.timerzz.com/kedaya_haitao/article
go 1.22.5 go 1.22.5
require ( 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/gofiber/fiber/v3 v3.0.0-beta.3
github.com/golang/glog v1.2.2 github.com/golang/glog v1.2.2
) )

2
go.sum
View File

@ -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 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-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 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M=
github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY= 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= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=

View File

@ -3,12 +3,15 @@ package service
import ( import (
"fmt" "fmt"
"strconv" "strconv"
"strings"
"gitea.timerzz.com/kedaya_haitao/common/pkg/web" "gitea.timerzz.com/kedaya_haitao/common/pkg/web"
"gitea.timerzz.com/kedaya_haitao/common/structs/storage" "gitea.timerzz.com/kedaya_haitao/common/structs/storage"
"gitea.timerzz.com/kedaya_haitao/common/structs/utils" "gitea.timerzz.com/kedaya_haitao/common/structs/utils"
v2 "gitea.timerzz.com/kedaya_haitao/common/structs/v2" v2 "gitea.timerzz.com/kedaya_haitao/common/structs/v2"
"github.com/gofiber/fiber/v3" "github.com/gofiber/fiber/v3"
"github.com/samber/lo"
"gorm.io/gorm"
) )
type Article struct { type Article struct {
@ -24,13 +27,18 @@ func NewArticle(storage *storage.Storage) *Article {
func (s *Article) Registry(r fiber.Router) { func (s *Article) Registry(r fiber.Router) {
api := r.Group("/api/v2") api := r.Group("/api/v2")
api.Get("articles", s.List) 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.Get("articles/dict/brand", s.BrandDict)
api.Patch("articles", s.Update) api.Patch("articles", s.Update)
api.Get("articles/provider/history/:id", s.ProviderHistory) api.Get("articles/provider/history/:id", s.ProviderHistory)
api.Get("articles/seller/history/:id", s.SellerHistory) api.Get("articles/seller/history/:id", s.SellerHistory)
api.Patch("articles/provider", s.UpdateProviderArticle) api.Patch("articles/provider", s.UpdateProviderArticle)
api.Patch("articles/seller", s.UpdateSellerArticle) 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 { 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 { if err := c.Bind().Query(&q); err != nil {
return err return err
} }
q.SetBan(false)
var query = storage.NewPageListQuery(&q) var query = storage.NewPageListQuery(&q)
if err := c.Bind().Query(query); err != nil { if err := c.Bind().Query(query); err != nil {
return err return err
@ -137,3 +146,72 @@ func (s *Article) UpdateSellerArticle(c fiber.Ctx) error {
} }
return c.JSON(web.NewResponse(article)) 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)))
}