feat 添加seller Api

This commit is contained in:
timerzz 2024-08-27 16:39:06 +08:00
parent 68e2a21402
commit 1907a3538a
3 changed files with 81 additions and 32 deletions

View File

@ -1,63 +1,106 @@
package storage package storage
import ( import (
"fmt"
"strconv"
v2 "gitea.timerzz.com/kedaya_haitao/common/structs/v2" v2 "gitea.timerzz.com/kedaya_haitao/common/structs/v2"
"github.com/samber/lo"
"gorm.io/gorm" "gorm.io/gorm"
"gorm.io/gorm/clause" "gorm.io/gorm/clause"
) )
type SellerApi interface { type SellerApi interface {
Get(id uint) (seller v2.Seller, err error)
List(query PageListQuery) (sellers []v2.Seller, total int64, err error)
Create(seller v2.Seller) error
Update(seller v2.Seller) error
Delete(id uint) (seller v2.Seller, err error)
AutoMigrate() error
}
func NewSellerApi(db *gorm.DB) SellerApi {
return &sellerApi{db: db}
} }
type sellerApi struct { type sellerApi struct {
db *gorm.DB db *gorm.DB
} }
// SellerArticleApi 管理供应商商品的接口 type ListSellerQuery struct {
type SellerArticleApi interface {
Get(query GetProviderArticleQuery) (article v2.ProviderArticle, err error)
Upsert(article v2.SellerArticle) error
}
type sellerArticleApi struct {
db *gorm.DB
}
type GetSellerArticleQuery struct {
ID uint `query:"id"` ID uint `query:"id"`
Brand string `query:"brand"`
Pid string `query:"pid"`
SellerId string `query:"sellerId"` SellerId string `query:"sellerId"`
SkuId string `query:"skuId"` Keyword string `query:"keyword"`
} }
func (g *GetSellerArticleQuery) Scope(db *gorm.DB) *gorm.DB { func (g *ListSellerQuery) Scope(db *gorm.DB) *gorm.DB {
if g.ID > 0 { if g.ID > 0 {
db = db.Where("id=?", g.ID) db = db.Where("id=?", g.ID)
} }
if g.Brand != "" {
db = db.Where("brand=?", g.Brand)
}
if g.Pid != "" {
db = db.Where("pid=?", g.Pid)
}
if g.SellerId != "" { if g.SellerId != "" {
db = db.Where("seller_id = ?", g.SellerId) db = db.Where("seller_id = ?", g.SellerId)
} }
if g.SkuId != "" { if g.Keyword != "" {
db = db.Where("sku_id=?", g.SkuId) db = db.Where("name LIKE ?", "%"+g.Keyword+"%")
} }
return db return db
} }
func (p *sellerArticleApi) Get(query GetSellerArticleQuery) (article v2.SellerArticle, err error) { func (p *sellerApi) Get(id uint) (seller v2.Seller, err error) {
err = p.db.Scopes(query.Scope).First(&article).Error err = p.db.Where("id = ?", id).Preload("CalculateProcess").First(&seller).Error
return return
} }
func (p *sellerArticleApi) Upsert(article v2.SellerArticle) error { func (p *sellerApi) List(query PageListQuery) (sellers []v2.Seller, total int64, err error) {
return p.db.Clauses(clause.OnConflict{ err = p.db.Scopes(query.Scoper.Scope).Model(&v2.Seller{}).Count(&total).Error
Columns: []clause.Column{{Name: "seller_id"}, {Name: "sku_id"}}, if err != nil {
DoUpdates: clause.AssignmentColumns([]string{"sell"}), return
}).Create(&article).Error }
err = p.db.Scopes(query.Scope).Find(&sellers).Error
return
}
func (p *sellerApi) Create(seller v2.Seller) error {
if seller.SellerId == "" {
return fmt.Errorf("标识为空")
}
if seller.Name == "" {
return fmt.Errorf("名称为空")
}
return p.db.Create(&seller).Error
}
func (p *sellerApi) Update(seller v2.Seller) error {
if seller.SellerId == "" {
return fmt.Errorf("标识为空")
}
return p.db.Transaction(func(tx *gorm.DB) error {
ids := lo.Map(lo.Filter(seller.CalculateProcess, func(item v2.CalculateProcess, index int) bool { return item.ID > 0 }), func(item v2.CalculateProcess, index int) string {
return strconv.Itoa(int(item.ID))
})
// 删除已经没有的CalculateProcess
if err := tx.Not(ids).Where("owner_id = ? AND kind = ?", seller.ID, "seller").Delete(&v2.CalculateProcess{}).Error; err != nil {
return err
}
// 更新方案的name和描述
return tx.Session(&gorm.Session{FullSaveAssociations: true}).Select("updated_at", "name", "config", "mark", "CalculateProcess").
Where("id = ?", seller.ID).Updates(&seller).Error
})
}
func (p *sellerApi) Delete(id uint) (seller v2.Seller, err error) {
err = p.db.Transaction(func(tx *gorm.DB) error {
if err = tx.Where("owner_id = ? AND kind = ?", id, "seller").Delete(&v2.CalculateProcess{}).Error; err != nil {
}
return p.db.Clauses(clause.Returning{}).Where("id = ?", id).Delete(&seller).Error
})
return
}
func (p *sellerApi) AutoMigrate() error {
return p.db.AutoMigrate(&v2.Seller{}, &v2.CalculateProcess{})
} }

View File

@ -6,6 +6,8 @@ type Storage struct {
articleApi ArticleApi articleApi ArticleApi
providerApi ProviderApi providerApi ProviderApi
providerArticleApi ProviderArticleApi providerArticleApi ProviderArticleApi
sellerApi SellerApi
sellerArticleApi SellerArticleApi
} }
func NewStorage(db *gorm.DB) *Storage { func NewStorage(db *gorm.DB) *Storage {
@ -14,6 +16,7 @@ func NewStorage(db *gorm.DB) *Storage {
db: db, db: db,
}, },
providerApi: NewProviderApi(db), providerApi: NewProviderApi(db),
sellerApi: NewSellerApi(db),
} }
} }
@ -28,3 +31,6 @@ func (s *Storage) Provider() ProviderApi {
func (s *Storage) ProviderArticle() ProviderArticleApi { func (s *Storage) ProviderArticle() ProviderArticleApi {
return s.providerArticleApi return s.providerArticleApi
} }
func (s *Storage) Seller() SellerApi {
return s.sellerApi
}

View File

@ -22,7 +22,7 @@ type Seller struct {
} }
type SellerOption struct { type SellerOption struct {
Interval time.Duration `yaml:"interval" json:"interval"` //抓取间隔 Ticker string `yaml:"ticker" json:"ticker"` //定时抓取
} }
// SellerArticle 销售商商品信息 // SellerArticle 销售商商品信息