Commit e7f4c5d6 authored by zhengyaoqiu's avatar zhengyaoqiu

feat(colorway): PC3商品转换

parent 53636eac
......@@ -7,11 +7,12 @@ type ColorSpu struct {
CompleteSpuStyleNumber string `gorm:"uniqueIndex:style; size:50; not null"`
ColorCode string `gorm:"uniqueIndex:style; size:50; not null"`
Color Color `gorm:"embedded;embeddedPrefix:color_"`
InfoSelect string `gorm:"not null; size:24"` // 商品信息默认选择哪个商品
// 外键
// 多对一
Skus []Sku `gorm:"foreignKey:ColorSpuStyleNumber,ColorSpuColorCode; references:CompleteSpuStyleNumber,ColorCode; constraint:OnUpdate:CASCADE,OnDelete:CASCADE"`
Images []Image `gorm:"foreignKey:ColorSpuStyleNumber,ColorSpuColorCode; references:CompleteSpuStyleNumber,ColorCode; constraint:OnUpdate:CASCADE,OnDelete:CASCADE"`
Skus Skus `gorm:"foreignKey:ColorSpuStyleNumber,ColorSpuColorCode; references:CompleteSpuStyleNumber,ColorCode; constraint:OnUpdate:CASCADE,OnDelete:CASCADE"`
Images Images `gorm:"foreignKey:ColorSpuStyleNumber,ColorSpuColorCode; references:CompleteSpuStyleNumber,ColorCode; constraint:OnUpdate:CASCADE,OnDelete:CASCADE"`
}
type ColorSpus []ColorSpu
......
......@@ -12,13 +12,12 @@ type CompleteSpu struct {
Care string `gorm:"type:text"`
MadeIn string `gorm:"size:50"`
HsCode string `gorm:"size:50"`
InfoSelect string `gorm:"not null; size:24"` // 商品信息默认选择哪个商品
// 外键
// 多对一
ColorSpus ColorSpus `gorm:"foreignKey:CompleteSpuStyleNumber; references:StyleNumber; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
Products Products `gorm:"foreignKey:CompleteSpuStyleNumber; references:StyleNumber; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
Compositions Compositions `gorm:"foreignKey:CompleteSpuStyleNumber; references:StyleNumber; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
Skus Skus `gorm:"foreignKey:ColorSpuStyleNumber; references:StyleNumber; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
Models Models `gorm:"foreignKey:CompleteSpuStyleNumber; references:StyleNumber; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
Fits Fits `gorm:"foreignKey:CompleteSpuStyleNumber; references:StyleNumber; constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
// 多对多
......
......@@ -5,10 +5,11 @@ import "gorm.io/gorm"
type Sku struct {
gorm.Model
Pc3SkuId string `gorm:"uniqueIndex; not null; size:24"`
Pc3ProductId string `gorm:"not null; size:24"`
ColorSpuStyleNumber string `gorm:"size:50; not null"`
ColorSpuColorCode string `gorm:"size:50; not null"`
Source string `gorm:"size:50; not null"`
Site string `gorm:"size:50; not null"`
SourceId string `gorm:"size:50; not null"`
SiteId string `gorm:"size:50; not null"`
Size string `gorm:"size:50; not null"`
Stock int `gorm:"not null"`
Price float64 `gorm:"type:decimal(10,2); not null"`
......
......@@ -32,11 +32,7 @@ func (l *UploadLogic) Upload(in *colorway.ProductUploadRequest) (*colorway.Produ
if err != nil {
return nil, err
}
sourceItems := l.svcCtx.Cache.Source.GetSourceItems()
for _, designerProduct := range productList.GetDesignerProducts() {
designerProduct.GetDesignerId()
}
log.Println(productList, sourceItems)
completeSpus := l.svcCtx.SpuConvert.Convert(productList.GetDesignerProducts())
log.Println(completeSpus)
return &colorway.ProductUploadResponse{}, nil
}
package convert
import (
"git.chillcy.com/golang/chillcy/project/pc4/pkg/rpc/pb/pc4"
"git.chillcy.com/golang/colorway/internal/pkg/database/model"
)
type Result struct {
CompleteSpus model.CompleteSpus
ColorSpus model.ColorSpus
}
type Product struct{}
func (receiver Product) Convert(designerProducts []*pc4.DesignerProduct) {
for _, designerProduct := range designerProducts {
designerProduct.GetDesignerId()
designerProduct.GetColorProducts()
}
}
package convert
import (
"git.chillcy.com/golang/chillcy/pkg/slice"
"git.chillcy.com/golang/chillcy/project/pc4/pkg/rpc/pb/pc4"
"git.chillcy.com/golang/colorway/internal/pkg/database/model"
"git.chillcy.com/golang/colorway/internal/rpc/internal/svc/cache"
"sync"
)
type Spu struct {
cache *cache.Cache
}
func NewSpu(cache *cache.Cache) *Spu {
return &Spu{cache: cache}
}
func (receiver *Spu) Convert(designerProducts []*pc4.DesignerProduct) model.CompleteSpus {
var completeSpus model.CompleteSpus
for _, designerProduct := range designerProducts {
completeSpus = append(completeSpus, receiver.buildCompleteSpu(designerProduct))
}
return completeSpus
}
type weight struct {
sourceId string
weight int
}
type weightSelect struct {
Image weight
Title weight
Category weight
Compositions weight
Care weight
Description weight
Fits weight
Models weight
Season weight
}
func (receiver *Spu) buildCompleteSpu(designerProduct *pc4.DesignerProduct) model.CompleteSpu {
sourceIds := getSourceIds(designerProduct)
ws := receiver.getWeightSelect(sourceIds)
completeSpu := model.CompleteSpu{}
completeSpu.StyleNumber = designerProduct.GetDesignerId()
once := sync.Once{}
for _, colorProduct := range designerProduct.GetColorProducts() {
for _, productAndSkus := range colorProduct.GetProductAndSkusList() {
product := productAndSkus.GetProduct()
once.Do(func() { // 默认商品数据选择
completeSpu.InfoSelect = product.GetId()
completeSpu.Title = model.Title{
En: product.GetTitleEn(),
Cn: product.GetTitleCn(),
}
completeSpu.Description = product.GetDetail().GetDescription()
completeSpu.Season = product.GetSeason()
completeSpu.BrandId = product.GetBrandId()
completeSpu.Care = product.GetDetail().GetCare()
completeSpu.MadeIn = product.GetDetail().GetMadeIn()
completeSpu.HsCode = product.GetDetail().GetHsCode()
completeSpu.Compositions = buildCompositions(product.GetDetail().GetCompositions(), product.GetDesignerId())
completeSpu.Models = buildModels(product.GetDetail().GetModels(), product.GetDesignerId())
completeSpu.Fits = buildFits(product.GetDetail().GetFits(), product.GetDesignerId())
for _, g := range product.GetGenders() {
completeSpu.Genders = append(completeSpu.Genders, model.Gender{
Code: g,
})
}
completeSpu.Categories = buildCategories(product.GetCategoryIds())
})
// 分权重商品信息选择覆盖默认
if product.GetSourceId() == ws.Title.sourceId {
completeSpu.Title = model.Title{
En: product.GetTitleEn(),
Cn: product.GetTitleCn(),
}
}
if product.GetSourceId() == ws.Description.sourceId {
completeSpu.Description = product.GetDetail().GetDescription()
}
if product.GetSourceId() == ws.Season.sourceId {
completeSpu.Season = product.GetSeason()
}
if product.GetSourceId() == ws.Care.sourceId {
completeSpu.Care = product.GetDetail().GetCare()
}
if product.GetSourceId() == ws.Compositions.sourceId {
completeSpu.Compositions = buildCompositions(product.GetDetail().GetCompositions(), product.GetDesignerId())
}
if product.GetSourceId() == ws.Models.sourceId {
completeSpu.Models = buildModels(product.GetDetail().GetModels(), product.GetDesignerId())
}
if product.GetSourceId() == ws.Fits.sourceId {
completeSpu.Fits = buildFits(product.GetDetail().GetFits(), product.GetDesignerId())
}
if product.GetSourceId() == ws.Category.sourceId {
completeSpu.Categories = buildCategories(product.GetCategoryIds())
}
}
}
completeSpu.ColorSpus = buildColorSpus(designerProduct.GetColorProducts(), designerProduct.GetDesignerId(), ws)
return completeSpu
}
func buildCategories(cs []string) model.Categories {
var categories model.Categories
for _, c := range cs {
categories = append(categories, model.Category{
Code: c,
Name: "",
ParentCode: "",
})
}
return categories
}
func buildFits(fs []string, styleNumber string) model.Fits {
var fits model.Fits
for _, f := range fs {
fits = append(fits, model.Fit{
CompleteSpuStyleNumber: styleNumber,
Content: f,
})
}
return fits
}
func buildModels(ms []string, styleNumber string) model.Models {
var models model.Models
for _, m := range ms {
models = append(models, model.Model{
CompleteSpuStyleNumber: styleNumber,
Content: m,
})
}
return models
}
func buildCompositions(cs []*pc4.Composition, styleNumber string) model.Compositions {
var compositions model.Compositions
for _, c := range cs {
compositions = append(compositions, model.Composition{
CompleteSpuStyleNumber: styleNumber,
Part: c.GetPart(),
Material: c.GetMaterial(),
Value: c.GetValue(),
})
}
return compositions
}
func buildColorSpus(colorProducts []*pc4.ColorProduct, styleNumber string, ws weightSelect) model.ColorSpus {
var colorSpus model.ColorSpus
for _, colorProduct := range colorProducts {
colorSpu := buildColorSpu(colorProduct, styleNumber, ws)
colorSpus = append(colorSpus, colorSpu)
}
return colorSpus
}
func buildColorSpu(colorProduct *pc4.ColorProduct, styleNumber string, ws weightSelect) model.ColorSpu {
colorSpu := model.ColorSpu{}
colorSpu.CompleteSpuStyleNumber = styleNumber
colorSpu.ColorCode = colorProduct.GetColorNumber()
once := sync.Once{}
for _, productAndSkus := range colorProduct.GetProductAndSkusList() {
product := productAndSkus.GetProduct()
once.Do(func() {
colorSpu.InfoSelect = product.GetId()
colorSpu.Color = model.Color{
Id: product.GetColor().GetId(),
Name: product.GetColor().GetName(),
BasicId: product.GetColor().GetBasicColorId(),
BasicName: product.GetColor().GetBasicColorName(),
}
colorSpu.Images = getImages(product.GetColor().GetImages(), styleNumber, colorProduct.GetColorNumber())
})
if product.GetSourceId() == ws.Image.sourceId {
colorSpu.Images = getImages(product.GetColor().GetImages(), styleNumber, colorProduct.GetColorNumber())
}
}
colorSpu.Skus = buildSkus(colorProduct.GetProductAndSkusList(), styleNumber, colorProduct.GetColorNumber())
return colorSpu
}
func getImages(imageUrls []string, styleNumber, colorCode string) model.Images {
var images model.Images
for _, imageUrl := range imageUrls {
images = append(images, model.Image{
ColorSpuStyleNumber: styleNumber,
ColorSpuColorCode: colorCode,
Url: imageUrl,
})
}
return images
}
func buildSkus(productAndSkusList []*pc4.ProductAndSkus, styleNumber, colorCode string) model.Skus {
var skus model.Skus
for _, productAndSkus := range productAndSkusList {
for _, pSku := range productAndSkus.GetSkus() {
sku := model.Sku{}
sku.Pc3SkuId = pSku.GetId()
sku.Pc3ProductId = productAndSkus.GetProduct().GetId()
sku.ColorSpuStyleNumber = styleNumber
sku.ColorSpuColorCode = colorCode
sku.SourceId = productAndSkus.GetProduct().GetSourceId()
sku.SiteId = productAndSkus.GetProduct().GetSiteId()
sku.Size = pSku.GetRawSize()
sku.Stock = int(pSku.GetStock())
sku.Price = pSku.GetPriceDetail().GetNow()
sku.Currency = pSku.GetPriceDetail().GetCurrency()
skus = append(skus, sku)
}
}
return skus
}
func getSourceIds(designerProduct *pc4.DesignerProduct) []string {
var sourceIds []string
for _, colorProduct := range designerProduct.GetColorProducts() {
for _, productAndSkus := range colorProduct.GetProductAndSkusList() {
sourceIds = append(sourceIds, productAndSkus.GetProduct().GetSourceId())
}
}
return sourceIds
}
func (receiver *Spu) getWeightSelect(sourceIds []string) weightSelect {
sourceItems := receiver.cache.Source.GetSourceItems()
ws := weightSelect{}
for _, item := range sourceItems {
if !slice.Contains(sourceIds, item.Id) { // 不在所需要合并商品中的货源跳过
continue
}
if ws.Image.weight < item.CombineWeight.Image {
ws.Image.sourceId = item.Id
ws.Image.weight = item.CombineWeight.Image
}
if ws.Title.weight < item.CombineWeight.Title {
ws.Title.sourceId = item.Id
ws.Title.weight = item.CombineWeight.Title
}
if ws.Category.weight < item.CombineWeight.Category {
ws.Category.sourceId = item.Id
ws.Category.weight = item.CombineWeight.Category
}
if ws.Compositions.weight < item.CombineWeight.Compositions {
ws.Compositions.sourceId = item.Id
ws.Compositions.weight = item.CombineWeight.Compositions
}
if ws.Care.weight < item.CombineWeight.Care {
ws.Care.sourceId = item.Id
ws.Care.weight = item.CombineWeight.Care
}
if ws.Description.weight < item.CombineWeight.Description {
ws.Description.sourceId = item.Id
ws.Description.weight = item.CombineWeight.Description
}
if ws.Fits.weight < item.CombineWeight.Fits {
ws.Fits.sourceId = item.Id
ws.Fits.weight = item.CombineWeight.Fits
}
if ws.Models.weight < item.CombineWeight.Models {
ws.Models.sourceId = item.Id
ws.Models.weight = item.CombineWeight.Models
}
if ws.Season.weight < item.CombineWeight.Season {
ws.Season.sourceId = item.Id
ws.Season.weight = item.CombineWeight.Season
}
}
return ws
}
......@@ -7,6 +7,7 @@ import (
"git.chillcy.com/golang/colorway/internal/pkg/database"
"git.chillcy.com/golang/colorway/internal/rpc/internal/config"
"git.chillcy.com/golang/colorway/internal/rpc/internal/svc/cache"
"git.chillcy.com/golang/colorway/internal/rpc/internal/svc/convert"
"github.com/zeromicro/go-zero/zrpc"
)
......@@ -15,6 +16,7 @@ type ServiceContext struct {
DatabaseManager *database.Manager
Pc4RpcService Pc4RpcService
Cache *cache.Cache
SpuConvert *convert.Spu
}
func NewServiceContext(c config.Config) *ServiceContext {
......@@ -41,6 +43,7 @@ func NewServiceContext(c config.Config) *ServiceContext {
Source: svc.Pc4RpcService.Source,
})
svc.Cache = cache.GetCache()
svc.SpuConvert = convert.NewSpu(svc.Cache)
return svc
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment