2024-08-03 14:12:44 +08:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
v2 "gitea.timerzz.com/kedaya_haitao/common/structs/v2"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"gorm.io/gorm/clause"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ArticleApi interface {
|
2024-08-03 17:07:10 +08:00
|
|
|
Upsert(article v2.Article) error
|
2024-08-31 12:46:18 +08:00
|
|
|
Find(query *FindArticleQuery) (articles []v2.Article, err error)
|
2024-08-03 17:07:10 +08:00
|
|
|
List(query PageListQuery) (articles []v2.Article, total int64, err error)
|
2024-08-31 12:46:18 +08:00
|
|
|
Get(query *GetArticleQuery) (article v2.Article, err error)
|
2024-09-01 17:10:18 +08:00
|
|
|
AutoMigrate() error
|
2024-08-03 14:12:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type articleApi struct {
|
|
|
|
db *gorm.DB
|
|
|
|
}
|
|
|
|
|
2024-08-27 19:35:21 +08:00
|
|
|
func NewArticleApi(db *gorm.DB) ArticleApi {
|
|
|
|
return &articleApi{
|
|
|
|
db: db,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-03 14:12:44 +08:00
|
|
|
// Upsert 插入或者更新商品
|
|
|
|
func (a *articleApi) Upsert(article v2.Article) error {
|
|
|
|
return a.db.Clauses(clause.OnConflict{
|
|
|
|
Columns: []clause.Column{{Name: "pid"}, {Name: "brand"}},
|
2024-08-31 12:39:40 +08:00
|
|
|
DoUpdates: clause.AssignmentColumns([]string{"name", "english_name", "available", "updated_at", "cost_price", "sell_price", "rate", "remark", "exclude"}),
|
2024-08-03 14:12:44 +08:00
|
|
|
}).Create(&article).Error
|
|
|
|
}
|
2024-08-03 17:07:10 +08:00
|
|
|
|
2024-08-31 12:39:40 +08:00
|
|
|
// ******************Find和List
|
|
|
|
|
2024-08-03 17:07:10 +08:00
|
|
|
type FindArticleQuery struct {
|
2024-08-31 12:39:40 +08:00
|
|
|
ID uint `query:"id"`
|
|
|
|
Keyword string `query:"keyword"`
|
|
|
|
Brand v2.Brand `query:"brand"`
|
|
|
|
Pid string `query:"pid"`
|
|
|
|
Available *bool `query:"available"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewFindArticleQuery() *FindArticleQuery {
|
|
|
|
return &FindArticleQuery{}
|
|
|
|
}
|
|
|
|
func (f *FindArticleQuery) SetID(id uint) *FindArticleQuery {
|
|
|
|
f.ID = id
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
func (f *FindArticleQuery) SetKeyword(keyword string) *FindArticleQuery {
|
|
|
|
f.Keyword = keyword
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
func (f *FindArticleQuery) SetBrand(brand v2.Brand) *FindArticleQuery {
|
|
|
|
f.Brand = brand
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
func (f *FindArticleQuery) SetPid(pid string) *FindArticleQuery {
|
|
|
|
f.Pid = pid
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
func (f *FindArticleQuery) SetAvailable(available bool) *FindArticleQuery {
|
|
|
|
f.Available = &available
|
|
|
|
return f
|
2024-08-03 17:07:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *FindArticleQuery) Scope(db *gorm.DB) *gorm.DB {
|
|
|
|
if l.ID != 0 {
|
2024-09-01 16:49:35 +08:00
|
|
|
db = db.Where("`id`=?", l.ID)
|
2024-08-03 17:07:10 +08:00
|
|
|
}
|
|
|
|
if l.Keyword != "" {
|
2024-09-01 16:49:35 +08:00
|
|
|
db = db.Where("(`name` ilike ? OR `english_name ilike ? OR `remark` ilike ? )", "%"+l.Keyword+"%", "%"+l.Keyword+"%", "%"+l.Keyword+"%")
|
2024-08-03 17:07:10 +08:00
|
|
|
}
|
|
|
|
if l.Brand != "" {
|
2024-09-01 16:49:35 +08:00
|
|
|
db = db.Where("`brand`=?", l.Brand)
|
2024-08-03 17:07:10 +08:00
|
|
|
}
|
|
|
|
if l.Pid != "" {
|
2024-09-01 16:49:35 +08:00
|
|
|
db = db.Where("`pid`=?", l.Pid)
|
2024-08-03 17:07:10 +08:00
|
|
|
}
|
|
|
|
if l.Available != nil {
|
2024-09-01 16:49:35 +08:00
|
|
|
db = db.Where("`available`=?", *l.Available)
|
2024-08-03 17:07:10 +08:00
|
|
|
}
|
|
|
|
return db
|
|
|
|
}
|
|
|
|
|
2024-08-31 12:46:18 +08:00
|
|
|
func (a *articleApi) Find(query *FindArticleQuery) (articles []v2.Article, err error) {
|
2024-08-03 17:07:10 +08:00
|
|
|
err = a.db.Scopes(query.Scope).Find(&articles).Error
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *articleApi) List(query PageListQuery) (articles []v2.Article, total int64, err error) {
|
2024-08-26 17:51:14 +08:00
|
|
|
err = a.db.Scopes(query.Scoper.Scope).Model(&v2.Article{}).Count(&total).Error
|
2024-08-03 17:07:10 +08:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = a.db.Scopes(query.Scope).Find(&articles).Error
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-08-31 12:39:40 +08:00
|
|
|
//**************GET
|
|
|
|
|
2024-08-03 17:07:10 +08:00
|
|
|
type GetArticleQuery struct {
|
2024-08-31 12:39:40 +08:00
|
|
|
ID uint `query:"id"`
|
|
|
|
Brand v2.Brand `query:"brand"`
|
|
|
|
Pid string `query:"pid"`
|
|
|
|
History bool `query:"history"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *GetArticleQuery) SetID(id uint) *GetArticleQuery {
|
|
|
|
g.ID = id
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *GetArticleQuery) SetBrand(brand v2.Brand) *GetArticleQuery {
|
|
|
|
g.Brand = brand
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
func (g *GetArticleQuery) SetPid(pid string) *GetArticleQuery {
|
|
|
|
g.Pid = pid
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
func (g *GetArticleQuery) SetHistory(history bool) *GetArticleQuery {
|
|
|
|
g.History = history
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewGetArticleQuery() *GetArticleQuery {
|
|
|
|
return &GetArticleQuery{}
|
2024-08-03 17:07:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *GetArticleQuery) Scope(db *gorm.DB) *gorm.DB {
|
|
|
|
db = db.Preload("Providers").Preload("Sellers")
|
|
|
|
if g.ID != 0 {
|
2024-09-01 16:49:35 +08:00
|
|
|
db = db.Where("`id`=?", g.ID)
|
2024-08-03 17:07:10 +08:00
|
|
|
}
|
|
|
|
if g.Brand != "" {
|
2024-09-01 16:49:35 +08:00
|
|
|
db = db.Where("`brand`=?", g.Brand)
|
2024-08-03 17:07:10 +08:00
|
|
|
}
|
|
|
|
if g.Pid != "" {
|
2024-09-01 16:49:35 +08:00
|
|
|
db = db.Where("`pid`=?", g.Pid)
|
2024-08-03 17:07:10 +08:00
|
|
|
}
|
|
|
|
if g.History {
|
|
|
|
db = db.Preload("Providers.HistoryPrice").Preload("Sellers.HistoryPrice")
|
|
|
|
}
|
|
|
|
return db
|
|
|
|
}
|
|
|
|
|
2024-08-31 12:46:18 +08:00
|
|
|
func (a *articleApi) Get(query *GetArticleQuery) (article v2.Article, err error) {
|
2024-08-03 17:07:10 +08:00
|
|
|
err = a.db.Scopes(query.Scope).First(&article).Error
|
|
|
|
return
|
|
|
|
}
|
2024-09-01 17:10:18 +08:00
|
|
|
|
|
|
|
func (a *articleApi) AutoMigrate() error {
|
|
|
|
return a.db.AutoMigrate(&v2.Article{})
|
|
|
|
}
|