fix
This commit is contained in:
248
server/product/internal/logic/gettagproductlistlogic.go
Normal file
248
server/product/internal/logic/gettagproductlistlogic.go
Normal file
@@ -0,0 +1,248 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"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"
|
||||
)
|
||||
|
||||
type GetTagProductListLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewGetTagProductListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetTagProductListLogic {
|
||||
return &GetTagProductListLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *GetTagProductListLogic) GetTagProductList(req *types.GetTagProductListReq, userinfo *auth.UserInfo) (resp *basic.Response) {
|
||||
//获取合适尺寸
|
||||
if req.Size > 0 {
|
||||
req.Size = image.GetCurrentSize(req.Size)
|
||||
}
|
||||
//查询用户信息(不用判断存在)
|
||||
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")
|
||||
}
|
||||
//查询分类列表
|
||||
tStatus := int64(1)
|
||||
tReq := gmodel.GetAllTagByParamsReq{
|
||||
Status: &tStatus,
|
||||
OrderBy: "`sort` DESC",
|
||||
}
|
||||
//传入分类id
|
||||
if req.Cid > 0 {
|
||||
tReq.Ids = []int64{req.Cid}
|
||||
}
|
||||
tagList, err := l.svcCtx.AllModels.FsTags.GetAllTagByParams(l.ctx, tReq)
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get tag list")
|
||||
}
|
||||
if len(tagList) == 0 {
|
||||
return resp.SetStatusWithMessage(basic.CodeOK, "success", []interface{}{})
|
||||
}
|
||||
typeIds := make([]int64, 0, len(tagList))
|
||||
for _, v := range tagList {
|
||||
typeIds = append(typeIds, v.Id)
|
||||
}
|
||||
//查询符合的产品列表
|
||||
pIsDel := int64(0)
|
||||
pStatus := int64(1)
|
||||
pIsShelf := int64(1)
|
||||
pReq := gmodel.GetProductListByParamsReq{
|
||||
Type: typeIds,
|
||||
IsDel: &pIsDel,
|
||||
IsShelf: &pIsShelf,
|
||||
Status: &pStatus,
|
||||
OrderBy: "`sort` DESC",
|
||||
}
|
||||
//获取产品列表
|
||||
productList, err := l.svcCtx.AllModels.FsProduct.GetProductListByParams(l.ctx, pReq)
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product list")
|
||||
}
|
||||
productLen := len(productList)
|
||||
if productLen == 0 {
|
||||
return resp.SetStatusWithMessage(basic.CodeOK, "success")
|
||||
}
|
||||
//提取产品ids
|
||||
productIds := make([]int64, 0, productLen)
|
||||
for _, v := range productList {
|
||||
productIds = append(productIds, v.Id)
|
||||
}
|
||||
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")
|
||||
}
|
||||
//存储产品最小价格
|
||||
mapProductMinPrice := make(map[int64]int64)
|
||||
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)
|
||||
mapProductMinPrice[v.ProductId] = int64(priceSlice[0])
|
||||
}
|
||||
//获取模板
|
||||
productTemplatesV2, err := l.svcCtx.AllModels.FsProductTemplateV2.FindAllByProductIds(l.ctx, productIds)
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, "get product template_v2 err")
|
||||
}
|
||||
mapProductTemplate := make(map[int64]struct{})
|
||||
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")
|
||||
}
|
||||
mapProductSizeCount := make(map[int64]int64)
|
||||
for _, v := range productSizeCountList {
|
||||
mapProductSizeCount[v.ProductId] = v.Num
|
||||
}
|
||||
mapTagLevel := make(map[string]types.TagItem)
|
||||
minLevel := int64(0) //记录最小等级数字
|
||||
for _, tagInfo := range tagList {
|
||||
if (minLevel == 0 && *tagInfo.Level > 0) || (minLevel > *tagInfo.Level) {
|
||||
minLevel = *tagInfo.Level
|
||||
}
|
||||
productListRsp := l.getTagProductList(getTagProductListReq{
|
||||
TagInfo: tagInfo,
|
||||
ProductList: productList,
|
||||
MapProductMinPrice: mapProductMinPrice,
|
||||
MapProductTemplate: mapProductTemplate,
|
||||
MapProductSizeCount: mapProductSizeCount,
|
||||
Size: req.Size,
|
||||
User: user,
|
||||
})
|
||||
//加入分类
|
||||
tagTem := types.TagItem{
|
||||
TagProductList: productListRsp,
|
||||
TypeName: *tagInfo.Title,
|
||||
TypeId: tagInfo.Id,
|
||||
Level: *tagInfo.Level,
|
||||
BelongPrefix: *tagInfo.LevelPrefix,
|
||||
Description: *tagInfo.Description,
|
||||
ChildTagList: []types.TagItem{},
|
||||
}
|
||||
//当前tag保存入map
|
||||
mapTagLevel[*tagInfo.LevelPrefix] = tagTem
|
||||
}
|
||||
//组装等级从属关系
|
||||
for prefix, tagItem := range mapTagLevel {
|
||||
//查看有没有直接父级,有的话则把当前的append到父级的ChildTagList中
|
||||
prefixSlice := strings.Split(prefix, "/")
|
||||
//等于1表示自己是最上级
|
||||
if len(prefixSlice) == 1 {
|
||||
continue
|
||||
}
|
||||
parentPrefix := strings.Join(prefixSlice[:len(prefixSlice)-1], "/")
|
||||
if parent, ok := mapTagLevel[parentPrefix]; ok {
|
||||
parent.ChildTagList = append(parent.ChildTagList, tagItem)
|
||||
mapTagLevel[parentPrefix] = parent
|
||||
}
|
||||
}
|
||||
//最终值提取最高级别那一层出来
|
||||
finalSlice := make([]interface{}, 0, len(mapTagLevel))
|
||||
for _, v := range mapTagLevel {
|
||||
if v.Level != minLevel {
|
||||
continue
|
||||
}
|
||||
finalSlice = append(finalSlice, v)
|
||||
}
|
||||
return resp.SetStatusWithMessage(basic.CodeOK, "success", finalSlice)
|
||||
}
|
||||
|
||||
type getTagProductListReq struct {
|
||||
TagInfo gmodel.FsTags
|
||||
ProductList []gmodel.FsProduct
|
||||
MapProductMinPrice map[int64]int64
|
||||
MapProductTemplate map[int64]struct{}
|
||||
MapProductSizeCount map[int64]int64
|
||||
Size uint32
|
||||
User gmodel.FsUser
|
||||
}
|
||||
|
||||
// 获取对应tag的产品列表
|
||||
func (l *GetTagProductListLogic) getTagProductList(req getTagProductListReq) (productListRsp []types.TagProduct) {
|
||||
//默认给50个容量
|
||||
productListRsp = make([]types.TagProduct, 0, 50)
|
||||
for _, productInfo := range req.ProductList {
|
||||
//不属于一个类型则跳过
|
||||
if *productInfo.Type != req.TagInfo.Id {
|
||||
continue
|
||||
}
|
||||
minPrice, ok := req.MapProductMinPrice[productInfo.Id]
|
||||
_, tmpOk := req.MapProductTemplate[productInfo.Id]
|
||||
//无最小价格则不显示 || 没有模板也不显示
|
||||
if !ok || !tmpOk {
|
||||
continue
|
||||
}
|
||||
sizeNum := int64(0)
|
||||
if mapSizeNum, ok := req.MapProductSizeCount[productInfo.Id]; ok {
|
||||
sizeNum = mapSizeNum
|
||||
}
|
||||
item := types.TagProduct{
|
||||
ProductId: productInfo.Id,
|
||||
Sn: *productInfo.Sn,
|
||||
Title: *productInfo.Title,
|
||||
Intro: *productInfo.Intro,
|
||||
IsEnv: *productInfo.IsProtection,
|
||||
IsMicro: *productInfo.IsMicrowave,
|
||||
SizeNum: uint32(sizeNum),
|
||||
MiniPrice: minPrice,
|
||||
}
|
||||
//千人千面处理
|
||||
r := image.ThousandFaceImageFormatReq{
|
||||
Size: int(req.Size),
|
||||
IsThousandFace: 0,
|
||||
Cover: *productInfo.Cover,
|
||||
CoverImg: *productInfo.CoverImg,
|
||||
CoverDefault: *productInfo.CoverImg,
|
||||
ProductId: productInfo.Id,
|
||||
UserId: req.User.Id,
|
||||
}
|
||||
if req.User.Id != 0 {
|
||||
r.IsThousandFace = int(*req.User.IsThousandFace)
|
||||
}
|
||||
image.ThousandFaceImageFormat(&r)
|
||||
item.Cover = r.Cover
|
||||
item.CoverImg = r.CoverImg
|
||||
item.CoverDefault = r.CoverDefault
|
||||
//加入分类产品切片
|
||||
productListRsp = append(productListRsp, item)
|
||||
}
|
||||
return productListRsp
|
||||
}
|
||||
Reference in New Issue
Block a user