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

142 lines
4.1 KiB
Go
Raw Normal View History

2023-07-10 11:42:55 +00:00
package logic
import (
2023-07-11 03:28:57 +00:00
"context"
2023-07-10 11:42:55 +00:00
"errors"
2023-07-11 03:28:57 +00:00
"fusenapi/model/gmodel"
2023-07-10 11:42:55 +00:00
"fusenapi/utils/auth"
"fusenapi/utils/basic"
2023-07-11 03:28:57 +00:00
"fusenapi/utils/format"
2023-07-10 11:42:55 +00:00
"fusenapi/utils/image"
"gorm.io/gorm"
2023-07-11 03:28:57 +00:00
"strings"
2023-07-10 11:42:55 +00:00
"fusenapi/server/product/internal/svc"
"fusenapi/server/product/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type OtherProductListLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewOtherProductListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *OtherProductListLogic {
return &OtherProductListLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *OtherProductListLogic) OtherProductList(req *types.OtherProductListReq, userinfo *auth.UserInfo) (resp *basic.Response) {
2023-07-11 09:08:19 +00:00
if req.Num <= 0 || req.Num > 100 {
2023-07-10 11:42:55 +00:00
req.Num = 4
}
2023-07-11 09:08:19 +00:00
if req.Size > 0 {
2023-07-10 11:42:55 +00:00
req.Size = image.GetCurrentSize(req.Size)
}
2023-07-11 03:28:57 +00:00
//获取用户信息
2023-07-11 09:08:19 +00:00
user, err := l.svcCtx.AllModels.FsUser.FindUserById(l.ctx, userinfo.UserId)
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
2023-07-11 03:28:57 +00:00
logx.Error(err)
2023-07-11 09:08:19 +00:00
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get user info")
2023-07-11 03:28:57 +00:00
}
2023-07-11 09:08:19 +00:00
tagInfo, err := l.svcCtx.AllModels.FsTags.FindOne(l.ctx, req.Cid)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "the tag info is not exists")
2023-07-10 11:42:55 +00:00
}
logx.Error(err)
2023-07-11 09:08:19 +00:00
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get tage info")
2023-07-10 11:42:55 +00:00
}
2023-07-11 09:08:19 +00:00
if *tagInfo.Status != 1 {
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "tage info is not available")
2023-07-10 11:42:55 +00:00
}
if tagInfo.RecommendProduct == nil || *tagInfo.RecommendProduct == "" {
2023-07-11 09:08:19 +00:00
return resp.SetStatusWithMessage(basic.CodeOK, "success")
2023-07-10 11:42:55 +00:00
}
2023-07-11 09:08:19 +00:00
pids, err := format.StrSlicToInt64Slice(strings.Split(*tagInfo.RecommendProduct, ","))
if err != nil {
2023-07-11 03:28:57 +00:00
logx.Error(err)
2023-07-11 09:08:19 +00:00
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to parse recommend product")
2023-07-11 03:28:57 +00:00
}
2023-07-11 09:08:19 +00:00
list, err := l.getRandom(pids, int(req.Num))
if err != nil {
2023-07-11 03:28:57 +00:00
logx.Error(err)
2023-07-11 09:08:19 +00:00
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get random list")
2023-07-11 03:28:57 +00:00
}
2023-07-11 09:08:19 +00:00
if req.Size <= 0 {
return resp.SetStatusWithMessage(basic.CodeOK, "success,", list)
2023-07-11 03:28:57 +00:00
}
//千人前面处理
2023-07-11 09:08:19 +00:00
for _, v := range list {
2023-07-11 03:28:57 +00:00
r := &image.ThousandFaceImageFormatReq{
Size: int(req.Size),
2023-07-11 09:08:19 +00:00
IsThousandFace: 0,
2023-07-11 03:28:57 +00:00
Cover: v.Cover,
CoverImg: v.CoverImg,
CoverDefault: "",
ProductId: v.Id,
2023-07-11 09:08:19 +00:00
UserId: user.Id,
}
if user.Id > 0 {
r.IsThousandFace = int(*user.IsThousandFace)
2023-07-11 03:28:57 +00:00
}
image.ThousandFaceImageFormat(r)
v.Cover = r.Cover
v.CoverImg = r.CoverImg
v.CoverDefault = r.CoverDefault
}
2023-07-11 09:08:19 +00:00
return resp.SetStatusWithMessage(basic.CodeOK, "success,", list)
2023-07-10 11:42:55 +00:00
}
2023-07-11 09:08:19 +00:00
func (l *OtherProductListLogic) getRandom(productIds []int64, limit int) (result []types.OtherProductListRsp, err error) {
if len(productIds) == 0 {
2023-07-11 03:28:57 +00:00
return
}
//获取推荐产品信息
2023-07-11 09:08:19 +00:00
recommendProductList, err := l.svcCtx.AllModels.FsProduct.GetRandomProductListInIds(l.ctx, productIds, limit)
if err != nil {
2023-07-11 03:28:57 +00:00
return nil, err
}
2023-07-11 09:08:19 +00:00
if len(recommendProductList) == 0 {
2023-07-11 03:28:57 +00:00
return
}
mapProduct := make(map[int64]int)
2023-07-11 09:08:19 +00:00
newProductIds := make([]int64, 0, len(recommendProductList))
for k, v := range recommendProductList {
newProductIds = append(newProductIds, v.Id)
2023-07-11 03:28:57 +00:00
mapProduct[v.Id] = k
}
//查询最新的sku
reqStatus := int64(1)
2023-07-11 09:08:19 +00:00
productTemplateIds, err := l.svcCtx.AllModels.FsProductTemplateV2.GetProductTemplateListByParams(l.ctx, gmodel.GetProductTemplateListByParamsReq{
ProductIds: newProductIds,
GroupBy: "product_id",
OrderBy: "id DESC,sort DESC",
Fields: "product_id,max(id) as id",
Status: &reqStatus,
2023-07-11 03:28:57 +00:00
})
2023-07-11 09:08:19 +00:00
if err != nil {
return nil, err
2023-07-11 03:28:57 +00:00
}
2023-07-11 09:08:19 +00:00
for _, v := range productTemplateIds {
productIndex, ok := mapProduct[*v.ProductId]
if !ok {
2023-07-11 03:28:57 +00:00
continue
}
productInfo := recommendProductList[productIndex]
2023-07-11 09:08:19 +00:00
result = append(result, types.OtherProductListRsp{
2023-07-11 03:28:57 +00:00
Title: *productInfo.Title,
Cover: *productInfo.Cover,
CoverImg: *productInfo.CoverImg,
Sn: *productInfo.Sn,
Id: *v.ProductId,
SkuId: v.Id,
})
}
2023-07-11 09:08:19 +00:00
return result, nil
}