Commit bc3d33f5 authored by zhengyaoqiu's avatar zhengyaoqiu

feat(colorway): 商品依据权重转换

parent dc3ad202
package convert
import (
"git.chillcy.com/golang/chillcy/pkg/slice"
"git.chillcy.com/golang/chillcy/project/pc4/pkg/rpc/pb/pc4"
model "git.chillcy.com/golang/colorway/internal/pkg/database/mongo/product"
"git.chillcy.com/golang/colorway/internal/rpc/internal/svc/cache"
......@@ -18,6 +19,8 @@ func NewProduct(cache *cache.Cache) *Product {
func (receiver *Product) Convert(designerProducts []*pc4.DesignerProduct) model.Products {
var ps model.Products
for _, designerProduct := range designerProducts {
sourceIds := getSourceIds(designerProduct)
ws := receiver.getWeightSelect(sourceIds)
once := sync.Once{}
p := model.Product{}
styleNumber := designerProduct.GetDesignerId()
......@@ -40,24 +43,83 @@ func (receiver *Product) Convert(designerProducts []*pc4.DesignerProduct) model.
p.MadeIn = product.GetDetail().GetMadeIn()
p.HsCode = product.GetDetail().GetHsCode()
p.InfoSelect = product.GetId()
p.Category = getCategory(product.GetCategoriesV2())
for _, composition := range product.GetDetail().GetCompositions() {
p.Compositions = append(p.Compositions, model.ProductComposition{
Part: composition.GetPart(),
Material: composition.GetMaterial(),
Value: composition.GetValue(),
})
}
p.Fits = product.GetDetail().GetFits()
for _, g := range product.GetGenders() {
p.Genders = append(p.Genders, model.ProductGender(g))
}
p.Colors = buildProductColors(designerProduct.GetColorProducts(), designerProduct.GetDesignerId())
p.Models = product.GetDetail().GetModels()
})
if ws.Title.sourceId == product.GetSourceId() {
p.Title = model.ProductTitle{
En: product.GetTitleEn(),
Cn: product.GetTitleCn(),
}
}
if ws.Category.sourceId == product.GetSourceId() {
p.Category = getCategory(product.GetCategoriesV2())
}
if ws.Compositions.sourceId == product.GetSourceId() {
for _, composition := range product.GetDetail().GetCompositions() {
p.Compositions = append(p.Compositions, model.ProductComposition{
Part: composition.GetPart(),
Material: composition.GetMaterial(),
Value: composition.GetValue(),
})
}
}
if ws.Care.sourceId == product.GetSourceId() {
p.Care = product.GetDetail().GetCare()
}
if ws.Description.sourceId == product.GetSourceId() {
p.Description = product.GetDetail().GetDescription()
}
if ws.Fits.sourceId == product.GetSourceId() {
p.Fits = product.GetDetail().GetFits()
}
if ws.Models.sourceId == product.GetSourceId() {
p.Models = product.GetDetail().GetModels()
}
if ws.Season.sourceId == product.GetSourceId() {
p.Season = product.GetSeason()
}
}
}
p.Colors = buildProductColors(designerProduct.GetColorProducts(), designerProduct.GetDesignerId(), ws)
ps = append(ps, p)
}
return ps
}
func buildProductColors(colorProducts []*pc4.ColorProduct, styleNumber string) model.ProductColors {
func getCategory(category *pc4.CategoryV2) model.ProductCategory {
pc := model.ProductCategory{}
pc.Id = category.GetCategoryId()
pc.Name = category.GetCategoryName()
if category.GetChildren() == nil {
return pc
}
for _, c := range category.GetChildren() {
pc.Children = append(pc.Children, getCategory(c))
}
return pc
}
func buildProductColors(colorProducts []*pc4.ColorProduct, styleNumber string, ws weightSelect) model.ProductColors {
var pcs model.ProductColors
for _, colorProduct := range colorProducts {
pcs = append(pcs, buildProductColor(colorProduct, styleNumber))
pcs = append(pcs, buildProductColor(colorProduct, styleNumber, ws))
}
return pcs
}
func buildProductColor(colorProduct *pc4.ColorProduct, styleNumber string) model.ProductColor {
func buildProductColor(colorProduct *pc4.ColorProduct, styleNumber string, ws weightSelect) model.ProductColor {
pc := model.ProductColor{}
once := &sync.Once{}
for _, productAndSkus := range colorProduct.GetProductAndSkusList() {
......@@ -71,6 +133,11 @@ func buildProductColor(colorProduct *pc4.ColorProduct, styleNumber string) model
pc.InfoSelect = product.GetId()
pc.Images = product.GetColor().GetImages()
})
if ws.Image.sourceId == product.GetSourceId() {
pc.Images = product.GetColor().GetImages()
}
for _, sku := range productAndSkus.GetSkus() {
pc.Skus = append(pc.Skus, model.ProductColorSku{
Pc3SkuId: sku.GetId(),
......@@ -88,3 +155,50 @@ func buildProductColor(colorProduct *pc4.ColorProduct, styleNumber string) model
}
return pc
}
func (receiver *Product) 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
}
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