feat 修改article upsert
This commit is contained in:
parent
8dfcdb4086
commit
c4bc3e368a
@ -1,6 +1,8 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
v2 "gitea.timerzz.com/kedaya_haitao/common/structs/v2"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
@ -26,10 +28,19 @@ func NewArticleApi(db *gorm.DB) ArticleApi {
|
||||
|
||||
// Upsert 插入或者更新商品
|
||||
func (a *articleApi) Upsert(article v2.Article) error {
|
||||
return a.db.Clauses(clause.OnConflict{
|
||||
return a.db.Transaction(func(tx *gorm.DB) error {
|
||||
if err := tx.Clauses(clause.OnConflict{
|
||||
Columns: []clause.Column{{Name: "pid"}, {Name: "brand"}},
|
||||
DoUpdates: clause.AssignmentColumns([]string{"name", "english_name", "available", "updated_at", "cost_price", "sell_price", "rate", "remark", "exclude"}),
|
||||
}).Create(&article).Error
|
||||
}).Create(&article).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Save(&article.Providers).Error; err != nil {
|
||||
return fmt.Errorf("failed to save providers: %v", err)
|
||||
}
|
||||
return tx.Save(&article.Sellers).Error
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
// ******************Find和List
|
||||
|
@ -15,39 +15,40 @@ func GetDevDB() (*gorm.DB, error) {
|
||||
}
|
||||
|
||||
var article = v2.Article{
|
||||
Name: "test1",
|
||||
EnglishName: "test1",
|
||||
Name: "test2",
|
||||
EnglishName: "test2",
|
||||
Pid: "111",
|
||||
Brand: "col",
|
||||
Desc: "desc",
|
||||
Image: "http://123",
|
||||
Available: false,
|
||||
CostPrice: 123,
|
||||
CostPrice: 321,
|
||||
Providers: []v2.ProviderArticle{
|
||||
{
|
||||
ID: 1,
|
||||
Pid: "111",
|
||||
Brand: "col1",
|
||||
Brand: "col",
|
||||
Link: "http://123",
|
||||
ProviderId: "coach2",
|
||||
SkuID: "sku1",
|
||||
SkuID: "sku2",
|
||||
Cost: v2.ProviderPrice{
|
||||
OriginalPrice: 111,
|
||||
FinalPrice: 111,
|
||||
CalMark: "uuuuu",
|
||||
OriginalPrice: 222,
|
||||
FinalPrice: 222,
|
||||
CalMark: "aaa",
|
||||
},
|
||||
HistoryPrice: []v2.ProviderPrice{
|
||||
{
|
||||
OriginalPrice: 111,
|
||||
FinalPrice: 111,
|
||||
OriginalPrice: 222,
|
||||
FinalPrice: 222,
|
||||
CalMark: "uuuuu",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
SellPrice: 111,
|
||||
SellPrice: 222,
|
||||
Sellers: []v2.SellerArticle{},
|
||||
Rate: 111,
|
||||
Remark: "111",
|
||||
Rate: 222,
|
||||
Remark: "222",
|
||||
}
|
||||
|
||||
func TestUpsetArticle(t *testing.T) {
|
||||
|
@ -102,5 +102,5 @@ func (p *providerArticleApi) BatchUpdate(articles []v2.ProviderArticle) error {
|
||||
}
|
||||
|
||||
func (p *providerArticleApi) AutoMigrate() error {
|
||||
return p.db.AutoMigrate(&v2.ProviderArticle{})
|
||||
return p.db.AutoMigrate(&v2.ProviderArticle{}, &v2.ProviderPrice{})
|
||||
}
|
||||
|
@ -98,5 +98,5 @@ func (p *sellerArticleApi) BatchUpdate(articles []v2.SellerArticle) error {
|
||||
}
|
||||
|
||||
func (p *sellerArticleApi) AutoMigrate() error {
|
||||
return p.db.AutoMigrate(&v2.SellerArticle{})
|
||||
return p.db.AutoMigrate(&v2.SellerArticle{}, &v2.SellerPrice{})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user