fusenapi/server/product/internal/logic/homepagerecommendproductlistlogic.go

177 lines
6.3 KiB
Go
Raw Normal View History

2023-07-20 09:35:13 +00:00
package logic
import (
"errors"
2023-07-20 10:18:36 +00:00
"fmt"
2023-07-20 09:35:13 +00:00
"fusenapi/constants"
"fusenapi/model/gmodel"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"fusenapi/utils/format"
"fusenapi/utils/image"
"gorm.io/gorm"
"sort"
"strings"
"context"
"fusenapi/server/product/internal/svc"
"fusenapi/server/product/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
2023-07-20 09:47:28 +00:00
type HomePageRecommendProductListLogic struct {
2023-07-20 09:35:13 +00:00
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
2023-07-20 09:47:28 +00:00
func NewHomePageRecommendProductListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *HomePageRecommendProductListLogic {
return &HomePageRecommendProductListLogic{
2023-07-20 09:35:13 +00:00
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
2023-07-20 09:47:28 +00:00
func (l *HomePageRecommendProductListLogic) HomePageRecommendProductList(req *types.HomePageRecommendProductListReq, userinfo *auth.UserInfo) (resp *basic.Response) {
2023-07-20 09:35:13 +00:00
//查询用户信息(不用判断存在)
user, err := l.svcCtx.AllModels.FsUser.FindUserById(l.ctx, userinfo.UserId)
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "get user info err")
}
var (
recommendProductList []gmodel.FsProduct //产品列表select 字段需要看查询的地方)
productOptionalPartList []gmodel.GetGroupPartListByProductIdsRsp //产品配件列表
mapProductHaveOptionFitting = make(map[int64]struct{})
productPriceList []gmodel.GetPriceListByProductIdsRsp //产品价格列表select 字段需要看查询的地方)
mapProductMinPrice = make(map[int64]int64) //产品最小价格map
productTemplatesV2 []gmodel.FsProductTemplateV2 //产品模板列表select 字段需要看查询的地方)
productSizeCountList []gmodel.CountProductSizeByStatusRsp //产品尺寸数量列表select 字段需要看查询的地方)
mapProductSizeCount = make(map[int64]int64) //产品尺寸数量map
mapProductTemplate = make(map[int64]struct{}) //产品模板map
)
//获取列表推荐产品
recommendProductList, _, err = l.svcCtx.AllModels.FsProductRecommend.GetRecommendProductList(gmodel.GetRecommendProductListReq{
Ctx: l.ctx,
Page: 1,
Limit: 500, //设置最大500
Category: int64(constants.HOME_PAGE_RECOMMEND_CATEGORY),
})
if len(recommendProductList) == 0 {
return resp.SetStatusWithMessage(basic.CodeOK, "success", []interface{}{})
}
2023-07-20 10:18:36 +00:00
fmt.Println(recommendProductList)
2023-07-20 09:35:13 +00:00
productIds := make([]int64, 0, len(recommendProductList))
for _, product := range recommendProductList {
productIds = append(productIds, product.Id)
}
//获取商品可选配件
productOptionalPartList, err = l.svcCtx.AllModels.FsProductModel3d.GetGroupPartListByProductIds(l.ctx, productIds)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product part list")
}
//存储有配件的map
for _, partList := range productOptionalPartList {
partList.PartList = strings.Trim(partList.PartList, " ")
partList.PartList = strings.Trim(partList.PartList, ",")
if partList.PartList == "" {
continue
}
mapProductHaveOptionFitting[partList.ProductId] = struct{}{}
}
//获取产品价格列表
productPriceList, err = l.svcCtx.AllModels.FsProductPrice.GetSimplePriceListByProductIds(l.ctx, productIds)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product min price list")
}
//存储产品最小价格
for _, v := range productPriceList {
priceStrSlic := strings.Split(v.Price, ",")
priceSlice, err := format.StrSlicToIntSlice(priceStrSlic)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, err.Error())
}
if len(priceSlice) == 0 {
continue
}
sort.Ints(priceSlice)
if min, ok := mapProductMinPrice[v.ProductId]; ok {
if min > int64(priceSlice[0]) {
mapProductMinPrice[v.ProductId] = int64(priceSlice[0])
}
} else {
mapProductMinPrice[v.ProductId] = int64(priceSlice[0])
}
}
//获取模板(只是获取产品product_id)
productTemplatesV2, err = l.svcCtx.AllModels.FsProductTemplateV2.FindAllByProductIds(l.ctx, productIds, "product_id")
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "get product template_v2 err")
}
for _, v := range productTemplatesV2 {
mapProductTemplate[*v.ProductId] = struct{}{}
}
//获取产品尺寸数量
productSizeCountList, err = l.svcCtx.AllModels.FsProductSize.GetGroupProductSizeByStatus(l.ctx, productIds, 1)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "get product size count err")
}
for _, v := range productSizeCountList {
mapProductSizeCount[v.ProductId] = v.Num
}
//组装返回
2023-07-20 09:47:28 +00:00
listRsp := make([]types.HomePageRecommendProductListRsp, 0, len(recommendProductList))
2023-07-20 09:35:13 +00:00
for _, productInfo := range recommendProductList {
minPrice, ok := mapProductMinPrice[productInfo.Id]
_, tmpOk := mapProductTemplate[productInfo.Id]
//无最小价格则不显示 || 没有模板也不显示
if !ok || !tmpOk {
continue
}
sizeNum := int64(0)
if mapSizeNum, ok := mapProductSizeCount[productInfo.Id]; ok {
sizeNum = mapSizeNum
}
//有无可选配件
haveOptionalFitting := false
if _, ok = mapProductHaveOptionFitting[productInfo.Id]; ok {
haveOptionalFitting = true
}
2023-07-20 09:47:28 +00:00
item := types.HomePageRecommendProductListRsp{
2023-07-24 10:08:19 +00:00
Id: productInfo.Id,
2023-07-20 09:35:13 +00:00
Sn: *productInfo.Sn,
Title: *productInfo.Title,
SizeNum: uint32(sizeNum),
2023-07-24 02:30:08 +00:00
MinPrice: minPrice,
2023-07-20 09:35:13 +00:00
HaveOptionalFitting: haveOptionalFitting,
}
//千人千面处理
r := image.ThousandFaceImageFormatReq{
Size: int(req.Size),
IsThousandFace: 0,
Cover: *productInfo.Cover,
CoverImg: *productInfo.CoverImg,
CoverDefault: *productInfo.CoverImg,
ProductId: productInfo.Id,
UserId: user.Id,
}
if user.Id != 0 {
r.IsThousandFace = int(*user.IsThousandFace)
}
image.ThousandFaceImageFormat(&r)
item.Cover = r.Cover
item.CoverDefault = r.CoverDefault
//加入分类产品切片
listRsp = append(listRsp, item)
}
return resp.SetStatusWithMessage(basic.CodeOK, "success", listRsp)
}