feat 添加数据库操作封装

This commit is contained in:
zhanghaogang 2024-08-03 17:07:10 +08:00
parent 96b8215fa6
commit 015781e484
7 changed files with 256 additions and 6 deletions

View File

@ -7,9 +7,14 @@ import (
) )
type Storage struct { type Storage struct {
ArticleApi
} }
type ArticleApi interface { type ArticleApi interface {
Upsert(article v2.Article) error
Find(query FindArticleQuery) (articles []v2.Article, err error)
List(query PageListQuery) (articles []v2.Article, total int64, err error)
Get(query GetArticleQuery) (article v2.Article, err error)
} }
type articleApi struct { type articleApi struct {
@ -23,3 +28,73 @@ func (a *articleApi) Upsert(article v2.Article) error {
DoUpdates: clause.AssignmentColumns([]string{"name", "english_name", "available", "updated_at", "cost_price", "sell_price", "rate", "remark"}), DoUpdates: clause.AssignmentColumns([]string{"name", "english_name", "available", "updated_at", "cost_price", "sell_price", "rate", "remark"}),
}).Create(&article).Error }).Create(&article).Error
} }
type FindArticleQuery struct {
ID uint `query:"id"`
Keyword string `query:"keyword"`
Brand string `query:"brand"`
Pid string `query:"pid"`
Available *bool `query:"available"`
}
func (l *FindArticleQuery) Scope(db *gorm.DB) *gorm.DB {
if l.ID != 0 {
db = db.Where("`id` = ?", l.ID)
}
if l.Keyword != "" {
db = db.Where("(`name` LIKE ? OR `english_name LIKE ? OR `remark` LIKE ? )", "%"+l.Keyword+"%", "%"+l.Keyword+"%", "%"+l.Keyword+"%")
}
if l.Brand != "" {
db = db.Where("`brand` = ?", l.Brand)
}
if l.Pid != "" {
db = db.Where("`pid` = ?", l.Pid)
}
if l.Available != nil {
db = db.Where("`available` = ?", *l.Available)
}
return db
}
func (a *articleApi) Find(query FindArticleQuery) (articles []v2.Article, err error) {
err = a.db.Scopes(query.Scope).Find(&articles).Error
return
}
func (a *articleApi) List(query PageListQuery) (articles []v2.Article, total int64, err error) {
err = a.db.Scopes(query.Scoper.Scope).Count(&total).Error
if err != nil {
return
}
err = a.db.Scopes(query.Scope).Find(&articles).Error
return
}
type GetArticleQuery struct {
ID uint `query:"id"`
Brand string `query:"brand"`
Pid string `query:"pid"`
History bool `query:"history"`
}
func (g *GetArticleQuery) Scope(db *gorm.DB) *gorm.DB {
db = db.Preload("Providers").Preload("Sellers")
if g.ID != 0 {
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.History {
db = db.Preload("Providers.HistoryPrice").Preload("Sellers.HistoryPrice")
}
return db
}
func (a *articleApi) Get(query GetArticleQuery) (article v2.Article, err error) {
err = a.db.Scopes(query.Scope).First(&article).Error
return
}

View File

@ -0,0 +1,62 @@
package storage
import (
v2 "gitea.timerzz.com/kedaya_haitao/common/structs/v2"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
type ProviderApi interface {
}
type providerApi struct {
db *gorm.DB
}
// ProviderArticleApi 管理供应商商品的接口
type ProviderArticleApi interface {
Get(query GetProviderArticleQuery) (article v2.ProviderArticle, err error)
}
type providerArticleApi struct {
db *gorm.DB
}
type GetProviderArticleQuery struct {
ID uint `query:"id"`
Brand string `query:"brand"`
Pid string `query:"pid"`
ProviderId string `query:"providerId"`
SkuId string `query:"skuId"`
}
func (g *GetProviderArticleQuery) Scope(db *gorm.DB) *gorm.DB {
if g.ID > 0 {
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.ProviderId != "" {
db = db.Where("provider_id=?", g.ProviderId)
}
if g.SkuId != "" {
db = db.Where("sku_id=?", g.SkuId)
}
return db
}
func (p *providerArticleApi) Get(query GetProviderArticleQuery) (article v2.ProviderArticle, err error) {
err = p.db.Scopes(query.Scope).First(&article).Error
return
}
func (p *providerArticleApi) Upsert(article v2.ProviderArticle) error {
return p.db.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "provider_id"}, {Name: "sku_id"}},
DoUpdates: clause.AssignmentColumns([]string{"cost"}),
}).Create(&article).Error
}

35
structs/storage/scope.go Normal file
View File

@ -0,0 +1,35 @@
package storage
import "gorm.io/gorm"
type Scoper interface {
Scope(db *gorm.DB) *gorm.DB
}
type PageQuery struct {
Page int `query:"page"`
Size int `query:"size"`
}
func (p *PageQuery) Scope(db *gorm.DB) *gorm.DB {
if p.Size <= 0 {
p.Size = 10
}
if p.Page <= 0 {
p.Page = 1
}
return db.Offset((p.Page - 1) * p.Size).Limit(p.Size)
}
type PageListQuery struct {
Scoper
PageQuery
}
func (p *PageListQuery) Scope(db *gorm.DB) *gorm.DB {
if p.Scoper != nil {
db = p.Scoper.Scope(db)
}
return p.PageQuery.Scope(db)
}

62
structs/storage/seller.go Normal file
View File

@ -0,0 +1,62 @@
package storage
import (
v2 "gitea.timerzz.com/kedaya_haitao/common/structs/v2"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
type SellerApi interface {
}
type sellerApi struct {
db *gorm.DB
}
// SellerArticleApi 管理供应商商品的接口
type SellerArticleApi interface {
Get(query GetProviderArticleQuery) (article v2.ProviderArticle, err error)
}
type sellerArticleApi struct {
db *gorm.DB
}
type GetSellerArticleQuery struct {
ID uint `query:"id"`
Brand string `query:"brand"`
Pid string `query:"pid"`
SellerId string `query:"sellerId"`
SkuId string `query:"skuId"`
}
func (g *GetSellerArticleQuery) Scope(db *gorm.DB) *gorm.DB {
if g.ID > 0 {
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 != "" {
db = db.Where("seller_id=?", g.SellerId)
}
if g.SkuId != "" {
db = db.Where("sku_id=?", g.SkuId)
}
return db
}
func (p *sellerArticleApi) Get(query GetSellerArticleQuery) (article v2.SellerArticle, err error) {
err = p.db.Scopes(query.Scope).First(&article).Error
return
}
func (p *sellerArticleApi) Upsert(article v2.SellerArticle) error {
return p.db.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "seller_id"}, {Name: "sku_id"}},
DoUpdates: clause.AssignmentColumns([]string{"sell"}),
}).Create(&article).Error
}

View File

@ -6,6 +6,12 @@ import (
"gorm.io/gorm" "gorm.io/gorm"
) )
type Brand string
const (
Brand_Coach Brand = "coach"
)
// Article 商品 // Article 商品
type Article struct { type Article struct {
ID uint `gorm:"primary_key" json:"id"` ID uint `gorm:"primary_key" json:"id"`
@ -19,7 +25,7 @@ type Article struct {
// 货号 // 货号
Pid string `gorm:"index:article_pid_brand,unique" json:"pid"` Pid string `gorm:"index:article_pid_brand,unique" json:"pid"`
// 品牌 // 品牌
Brand string `gorm:"index:article_pid_brand,unique" json:"brand"` Brand Brand `gorm:"index:article_pid_brand,unique" json:"brand"`
// 描述 // 描述
Desc string `json:"desc"` Desc string `json:"desc"`
// 图片 // 图片

View File

@ -4,6 +4,12 @@ import "time"
type ProviderId string type ProviderId string
const (
ProviderId_CoachOutlet_US ProviderId = "coachOutlet_us"
ProviderId_CoachOutlet_CN ProviderId = "coachOutlet_cn"
ProviderId_Coach_CN ProviderId = "coach_cn"
)
// ProviderArticle 供应商商品信息 // ProviderArticle 供应商商品信息
type ProviderArticle struct { type ProviderArticle struct {
ID uint `gorm:"primary_key" json:"-"` ID uint `gorm:"primary_key" json:"-"`
@ -12,9 +18,9 @@ type ProviderArticle struct {
// 对应的商品ID // 对应的商品ID
ArticleID uint ArticleID uint
// 货号 // 货号
Pid string `gorm:"index:provider_pid_brand,unique" json:"pid"` Pid string `gorm:"index" json:"pid"`
// 品牌 // 品牌
Brand string `gorm:"index:provider_pid_brand,unique" json:"brand"` Brand Brand `gorm:"index" json:"brand"`
// 供应商商品链接链接 // 供应商商品链接链接
Link string `json:"link"` Link string `json:"link"`
// 供应商id // 供应商id

View File

@ -4,6 +4,10 @@ import "time"
type SellerId string type SellerId string
const (
SellerId_DW SellerId = "dewu"
)
// SellerArticle 销售商商品信息 // SellerArticle 销售商商品信息
type SellerArticle struct { type SellerArticle struct {
ID uint `gorm:"primary_key" json:"-"` ID uint `gorm:"primary_key" json:"-"`
@ -11,10 +15,10 @@ type SellerArticle struct {
// 商品的ID // 商品的ID
ArticleID uint ArticleID uint
// 货号 // 货号
Pid string `gorm:"index:seller_pid_brand,unique" json:"pid"` Pid string `gorm:"index" json:"pid"`
// 品牌 // 品牌
Brand string `gorm:"index:seller_pid_brand,unique" json:"brand"` Brand Brand `gorm:"index" json:"brand"`
// 销售商商品链接链接 // 销售商商品链接
Link string `json:"link"` Link string `json:"link"`
// 销售商id // 销售商id
SellerId SellerId `gorm:"index:sellerId_sku,unique" json:"sellerId"` SellerId SellerId `gorm:"index:sellerId_sku,unique" json:"sellerId"`