feat 添加第二版数据结构
This commit is contained in:
parent
c498a70be1
commit
96b8215fa6
25
structs/storage/article.go
Normal file
25
structs/storage/article.go
Normal file
@ -0,0 +1,25 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
v2 "gitea.timerzz.com/kedaya_haitao/common/structs/v2"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
type Storage struct {
|
||||
}
|
||||
|
||||
type ArticleApi interface {
|
||||
}
|
||||
|
||||
type articleApi struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
// Upsert 插入或者更新商品
|
||||
func (a *articleApi) Upsert(article v2.Article) error {
|
||||
return a.db.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"}),
|
||||
}).Create(&article).Error
|
||||
}
|
41
structs/v2/article.go
Normal file
41
structs/v2/article.go
Normal file
@ -0,0 +1,41 @@
|
||||
package v2
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// Article 商品
|
||||
type Article struct {
|
||||
ID uint `gorm:"primary_key" json:"id"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
UpdatedAt time.Time `json:"updatedAt"`
|
||||
DeletedAt gorm.DeletedAt `gorm:"index"`
|
||||
|
||||
Name string `gorm:"index" json:"name"`
|
||||
|
||||
EnglishName string `gorm:"index" json:"englishName"`
|
||||
// 货号
|
||||
Pid string `gorm:"index:article_pid_brand,unique" json:"pid"`
|
||||
// 品牌
|
||||
Brand string `gorm:"index:article_pid_brand,unique" json:"brand"`
|
||||
// 描述
|
||||
Desc string `json:"desc"`
|
||||
// 图片
|
||||
Image string `json:"image"`
|
||||
// 可以购买的
|
||||
Available bool `json:"available"`
|
||||
// 最低成本价(单位为分)
|
||||
CostPrice int `json:"costPrice"`
|
||||
// 供应商报价列表
|
||||
Providers []ProviderArticle `json:"providers" gorm:"foreignKey:ArticleID"`
|
||||
// 最低出售价 (单位为分)
|
||||
SellPrice int `json:"sellPrice"`
|
||||
// 销售商报价列表
|
||||
Sellers []SellerArticle `json:"sellers" gorm:"foreignKey:ArticleID"`
|
||||
// 利润率
|
||||
Rate float64 `json:"rate" gorm:"index"` //利润率
|
||||
//备注
|
||||
Remark string `json:"remark"`
|
||||
}
|
61
structs/v2/model_test.go
Normal file
61
structs/v2/model_test.go
Normal file
@ -0,0 +1,61 @@
|
||||
package v2
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
)
|
||||
|
||||
func TestModel(t *testing.T) {
|
||||
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%s sslmode=disable TimeZone=Asia/Shanghai", "192.168.31.55", "timerzz", "zhhg1997", "kedaya_dev", "5432")
|
||||
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
err = db.AutoMigrate(&Article{}, &ProviderArticle{}, &ProviderPrice{}, &SellerArticle{}, &SellerPrice{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var article = Article{
|
||||
Name: "test",
|
||||
EnglishName: "test",
|
||||
Pid: "1299999922",
|
||||
Brand: "col",
|
||||
Desc: "desc",
|
||||
Image: "http://123",
|
||||
Available: false,
|
||||
CostPrice: 123,
|
||||
Providers: []ProviderArticle{
|
||||
{
|
||||
Pid: "999",
|
||||
Brand: "col",
|
||||
Link: "http://123",
|
||||
ProviderId: "coach",
|
||||
SkuID: "sku999999",
|
||||
Cost: ProviderPrice{
|
||||
OriginalPrice: 000,
|
||||
FinalPrice: 0000,
|
||||
CalMark: "uuuuu",
|
||||
},
|
||||
HistoryPrice: []ProviderPrice{
|
||||
{
|
||||
OriginalPrice: 0000,
|
||||
FinalPrice: 000,
|
||||
CalMark: "uuuuu",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
SellPrice: 321,
|
||||
Sellers: []SellerArticle{},
|
||||
Rate: 123,
|
||||
Remark: "jjjjj",
|
||||
}
|
||||
db.Clauses(clause.OnConflict{
|
||||
Columns: []clause.Column{{Name: "pid"}, {Name: "brand"}},
|
||||
DoUpdates: clause.AssignmentColumns([]string{"name", "english_name", "available", "updated_at"}),
|
||||
}).Create(&article)
|
||||
}
|
41
structs/v2/provider.go
Normal file
41
structs/v2/provider.go
Normal file
@ -0,0 +1,41 @@
|
||||
package v2
|
||||
|
||||
import "time"
|
||||
|
||||
type ProviderId string
|
||||
|
||||
// ProviderArticle 供应商商品信息
|
||||
type ProviderArticle struct {
|
||||
ID uint `gorm:"primary_key" json:"-"`
|
||||
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
// 对应的商品ID
|
||||
ArticleID uint
|
||||
// 货号
|
||||
Pid string `gorm:"index:provider_pid_brand,unique" json:"pid"`
|
||||
// 品牌
|
||||
Brand string `gorm:"index:provider_pid_brand,unique" json:"brand"`
|
||||
// 供应商商品链接链接
|
||||
Link string `json:"link"`
|
||||
// 供应商id
|
||||
ProviderId ProviderId `gorm:"index:providerId_sku,unique" json:"providerId"`
|
||||
// 供应商的sku
|
||||
SkuID string `gorm:"index:providerId_sku,unique" json:"skuID"`
|
||||
// 当前成本价
|
||||
Cost ProviderPrice `json:"cost" gorm:"type:json;serializer:json"`
|
||||
// 历史成本价格
|
||||
HistoryPrice []ProviderPrice `json:"historyPrice"`
|
||||
}
|
||||
|
||||
// ProviderPrice 供应商成本价格
|
||||
type ProviderPrice struct {
|
||||
ID uint `gorm:"primary_key" json:"-"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
|
||||
ProviderArticleID uint `gorm:"index"`
|
||||
// 供应商原始价格,美元、人名币、加元,单位为分
|
||||
OriginalPrice int `json:"originalPrice"`
|
||||
// 计算优惠后最终价格,人名币,单位为分
|
||||
FinalPrice int `json:"finalPrice"` // 计算完优惠的最终的价格
|
||||
CalMark string `json:"calMark"` //计算优惠的过程
|
||||
}
|
40
structs/v2/seller.go
Normal file
40
structs/v2/seller.go
Normal file
@ -0,0 +1,40 @@
|
||||
package v2
|
||||
|
||||
import "time"
|
||||
|
||||
type SellerId string
|
||||
|
||||
// SellerArticle 销售商商品信息
|
||||
type SellerArticle struct {
|
||||
ID uint `gorm:"primary_key" json:"-"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
// 商品的ID
|
||||
ArticleID uint
|
||||
// 货号
|
||||
Pid string `gorm:"index:seller_pid_brand,unique" json:"pid"`
|
||||
// 品牌
|
||||
Brand string `gorm:"index:seller_pid_brand,unique" json:"brand"`
|
||||
// 销售商商品链接链接
|
||||
Link string `json:"link"`
|
||||
// 销售商id
|
||||
SellerId SellerId `gorm:"index:sellerId_sku,unique" json:"sellerId"`
|
||||
// 销售商的sku
|
||||
SkuID string `gorm:"index:sellerId_sku,unique" json:"skuID"`
|
||||
// 当前成本价
|
||||
Sell SellerPrice `json:"sell" gorm:"type:json;serializer:json"`
|
||||
// 历史出售价格
|
||||
HistoryPrice []SellerPrice `json:"historyPrice"`
|
||||
}
|
||||
|
||||
// SellerPrice 供应商成本价格
|
||||
type SellerPrice struct {
|
||||
ID uint `gorm:"primary_key" json:"-"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
SellerArticleID uint `gorm:"index"`
|
||||
// 出售最低价,单位为分
|
||||
OriginalPrice int `json:"originalPrice"`
|
||||
// 能拿到手的最终价格,单位为分
|
||||
FinalPrice int `json:"finalPrice"`
|
||||
//计算过程
|
||||
CalMark string `json:"calMark"`
|
||||
}
|
Loading…
Reference in New Issue
Block a user