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