service/vendor/app/mix/controller/fill_media.go

72 lines
1.5 KiB
Go

package controller
import "service/dbstruct"
func FillMedia(obj interface{}) {
switch obj.(type) {
case []*dbstruct.Product:
fillMediaProducts(obj.([]*dbstruct.Product))
}
}
// todo
func getImageByIds(ids []int64) map[int64]*dbstruct.Image {
return map[int64]*dbstruct.Image{
1: {W: 11111111},
2: {W: 22222222},
}
}
// todo
func getVideoByIds(ids []int64) map[int64]*dbstruct.Video {
return map[int64]*dbstruct.Video{
1: {Dur: 1111111},
2: {Dur: 2222222},
}
}
// todo
func transToCImage(image *dbstruct.Image) *dbstruct.ToCImage {
return &dbstruct.ToCImage{
Id: image.Id,
W: image.W,
H: image.H,
Fmt: image.Fmt,
Urls: []string{"todo"},
}
}
// todo
func transToCVideo(video *dbstruct.Video) *dbstruct.ToCVideo {
return &dbstruct.ToCVideo{
Id: video.Id,
Dur: video.Dur,
CoverUrls: []string{"todo"},
Urls: []string{"todo"},
}
}
func fillMediaProducts(list []*dbstruct.Product) {
imageIds := make([]int64, 0)
videoIds := make([]int64, 0)
for _, v := range list {
imageIds = append(imageIds, v.ImageIds...)
videoIds = append(videoIds, v.VideoIds...)
}
imageMap := getImageByIds(imageIds)
videoMap := getVideoByIds(videoIds)
for _, v := range list {
for _, imgId := range v.ImageIds {
if img, ok := imageMap[imgId]; ok {
v.Images = append(v.Images, transToCImage(img))
}
}
for _, vdoId := range v.VideoIds {
if vdo, ok := videoMap[vdoId]; ok {
v.Videos = append(v.Videos, transToCVideo(vdo))
}
}
}
}