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 { storage *storage.Storage } func NewArticle(storage *storage.Storage) *Article { return &Article{ storage: storage, } } func (s *Article) Registry(r fiber.Router) { api := r.Group("/api/v2") api.Get("articles", s.List) 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 { var q storage.FindArticleQuery 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 } articles, total, err := s.storage.Article().List(*query, q.SortScope) if err != nil { return err } return c.JSON(web.NewResponse(web.NewListResponse(total, articles))) } func (s *Article) Get(c fiber.Ctx) error { id := c.Params("id") if id == "" { return fiber.NewError(fiber.StatusBadRequest, "id is required") } i, _ := strconv.Atoi(id) query := storage.NewGetArticleQuery().SetID(uint(i)) article, err := s.storage.Article().Get(query) if err != nil { return err } return c.JSON(web.NewResponse(article)) } func (s *Article) Update(c fiber.Ctx) error { var article v2.Article if err := c.Bind().JSON(&article); err != nil { return err } if article.ID == 0 { return fiber.NewError(fiber.StatusBadRequest, "id is required") } if err := s.storage.Article().Update(article); err != nil { return err } return c.JSON(web.NewResponse(article)) } func (s *Article) BrandDict(c fiber.Ctx) error { var dict = []utils.Dict{ { Key: string(v2.Brand_Coach), Title: fmt.Sprintf("蔻驰/%s", v2.Brand_Coach), Value: v2.Brand_Coach, }, } return c.JSON(web.NewResponse(dict)) } // 获取供应商历史价格 func (s *Article) ProviderHistory(c fiber.Ctx) error { i := c.Params("id") id, _ := strconv.Atoi(i) if id == 0 { return fiber.NewError(fiber.StatusBadRequest, "id is required") } prices, err := s.storage.ProviderArticle().ProviderPrice(uint(id)) if err != nil { return err } return c.JSON(web.NewResponse(prices)) } // 获取销售商历史价格 func (s *Article) SellerHistory(c fiber.Ctx) error { i := c.Params("id") id, _ := strconv.Atoi(i) if id == 0 { return fiber.NewError(fiber.StatusBadRequest, "id is required") } prices, err := s.storage.SellerArticle().SellerPrice(uint(id)) if err != nil { return err } return c.JSON(web.NewResponse(prices)) } func (s *Article) UpdateProviderArticle(c fiber.Ctx) error { var article v2.ProviderArticle if err := c.Bind().JSON(&article); err != nil { return err } if err := s.storage.ProviderArticle().Update(article); err != nil { return err } return c.JSON(web.NewResponse(article)) } func (s *Article) UpdateSellerArticle(c fiber.Ctx) error { var article v2.SellerArticle if err := c.Bind().JSON(&article); err != nil { return err } if err := s.storage.SellerArticle().Update(article); err != nil { return err } 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))) }