From 96b8215fa66b30fe882652981dcb3bcdc0ec81f8 Mon Sep 17 00:00:00 2001 From: timerzz Date: Sat, 3 Aug 2024 14:12:44 +0800 Subject: [PATCH] =?UTF-8?q?feat=20=E6=B7=BB=E5=8A=A0=E7=AC=AC=E4=BA=8C?= =?UTF-8?q?=E7=89=88=E6=95=B0=E6=8D=AE=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- structs/storage/article.go | 25 ++++++++++++++++ structs/v2/article.go | 41 +++++++++++++++++++++++++ structs/v2/model_test.go | 61 ++++++++++++++++++++++++++++++++++++++ structs/v2/provider.go | 41 +++++++++++++++++++++++++ structs/v2/seller.go | 40 +++++++++++++++++++++++++ 5 files changed, 208 insertions(+) create mode 100644 structs/storage/article.go create mode 100644 structs/v2/article.go create mode 100644 structs/v2/model_test.go create mode 100644 structs/v2/provider.go create mode 100644 structs/v2/seller.go diff --git a/structs/storage/article.go b/structs/storage/article.go new file mode 100644 index 0000000..8364795 --- /dev/null +++ b/structs/storage/article.go @@ -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 +} diff --git a/structs/v2/article.go b/structs/v2/article.go new file mode 100644 index 0000000..798ba49 --- /dev/null +++ b/structs/v2/article.go @@ -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"` +} diff --git a/structs/v2/model_test.go b/structs/v2/model_test.go new file mode 100644 index 0000000..2fc30b0 --- /dev/null +++ b/structs/v2/model_test.go @@ -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) +} diff --git a/structs/v2/provider.go b/structs/v2/provider.go new file mode 100644 index 0000000..75507f0 --- /dev/null +++ b/structs/v2/provider.go @@ -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"` //计算优惠的过程 +} diff --git a/structs/v2/seller.go b/structs/v2/seller.go new file mode 100644 index 0000000..6cd8eb7 --- /dev/null +++ b/structs/v2/seller.go @@ -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"` +}