增加产品推荐接口

This commit is contained in:
laodaming 2023-06-06 18:17:03 +08:00
parent 4312d978fb
commit cf852c5a87
3 changed files with 44 additions and 38 deletions

View File

@ -13,8 +13,8 @@ type (
// and implement the added methods in customFsProductModel. // and implement the added methods in customFsProductModel.
FsProductModel interface { FsProductModel interface {
fsProductModel fsProductModel
GetProductListByConditions(ctx context.Context, productType int, isDel int, isShelf int, sort string) ([]FsProduct, error) GetProductListByConditions(ctx context.Context, productType int, sort string) ([]FsProduct, error)
GetAllProductList(ctx context.Context, isDel int, isShelf int, sort string) (resp []FsProduct, err error) GetRandomProductList(ctx context.Context, limit int) (resp []FsProduct, err error)
} }
customFsProductModel struct { customFsProductModel struct {
@ -28,7 +28,7 @@ func NewFsProductModel(conn sqlx.SqlConn) FsProductModel {
defaultFsProductModel: newFsProductModel(conn), defaultFsProductModel: newFsProductModel(conn),
} }
} }
func (m *defaultFsProductModel) GetProductListByConditions(ctx context.Context, productType int, isDel int, isShelf int, sort string) (resp []FsProduct, err error) { func (m *defaultFsProductModel) GetProductListByConditions(ctx context.Context, productType int, sort string) (resp []FsProduct, err error) {
query := fmt.Sprintf("select %s from %s where `type` = ? and `is_del` =? and `is_shelf` = ?", query := fmt.Sprintf("select %s from %s where `type` = ? and `is_del` =? and `is_shelf` = ?",
fsProductRows, m.table) fsProductRows, m.table)
switch sort { switch sort {
@ -39,24 +39,16 @@ func (m *defaultFsProductModel) GetProductListByConditions(ctx context.Context,
default: default:
query = fmt.Sprintf("%s order by sort DESC", query) query = fmt.Sprintf("%s order by sort DESC", query)
} }
err = m.conn.QueryRowsCtx(ctx, &resp, query, productType, isDel, isShelf) err = m.conn.QueryRowsCtx(ctx, &resp, query, productType, 0, 1)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return return
} }
func (m *defaultFsProductModel) GetAllProductList(ctx context.Context, isDel int, isShelf int, sort string) (resp []FsProduct, err error) { func (m *defaultFsProductModel) GetRandomProductList(ctx context.Context, limit int) (resp []FsProduct, err error) {
query := fmt.Sprintf("select %s from %s where `is_del` =? and `is_shelf` = ?", query := fmt.Sprintf("select %s from %s where `is_del` =? and `is_shelf` = ? order by RAND() limit ?",
fsProductRows, m.table) fsProductRows, m.table)
switch sort { err = m.conn.QueryRowsCtx(ctx, &resp, query, 0, 1, limit)
case "sort-asc":
query = fmt.Sprintf("%s order by sort ASC", query)
case "sort-desc":
query = fmt.Sprintf("%s order by sort DESC", query)
default:
query = fmt.Sprintf("%s order by sort DESC", query)
}
err = m.conn.QueryRowsCtx(ctx, &resp, query, isDel, isShelf)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -73,7 +73,7 @@ func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq, login
} }
//查询符合的产品列表 //查询符合的产品列表
productModel := model.NewFsProductModel(l.svcCtx.MysqlConn) productModel := model.NewFsProductModel(l.svcCtx.MysqlConn)
productList, err := productModel.GetProductListByConditions(l.ctx, int(req.Cid), 0, 1, "sort-desc") productList, err := productModel.GetProductListByConditions(l.ctx, int(req.Cid), "sort-desc")
if err != nil { if err != nil {
logx.Error(err) logx.Error(err)
resp.Set(constants.CODE_SERVICE_ERR, "failed to get product list") resp.Set(constants.CODE_SERVICE_ERR, "failed to get product list")

View File

@ -10,10 +10,10 @@ import (
"fusenapi/product/internal/types" "fusenapi/product/internal/types"
"fusenapi/utils/auth" "fusenapi/utils/auth"
"fusenapi/utils/image" "fusenapi/utils/image"
"github.com/zeromicro/go-zero/core/stores/sqlx"
"math/rand"
"github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/core/stores/sqlx"
"strings"
"time"
) )
type GetSuccessRecommandLogic struct { type GetSuccessRecommandLogic struct {
@ -30,8 +30,10 @@ func NewGetSuccessRecommandLogic(ctx context.Context, svcCtx *svc.ServiceContext
} }
} }
// 获取推荐的产品列表
func (l *GetSuccessRecommandLogic) GetSuccessRecommand(req *types.GetSuccessRecommandReq, loginInfo auth.UserInfo) (resp *types.Response) { func (l *GetSuccessRecommandLogic) GetSuccessRecommand(req *types.GetSuccessRecommandReq, loginInfo auth.UserInfo) (resp *types.Response) {
resp = &types.Response{} resp = &types.Response{}
loginInfo.UserId = 83
//校验前台登录情况 //校验前台登录情况
if loginInfo.UserId == 0 { if loginInfo.UserId == 0 {
resp.Set(constants.CODE_UNAUTH, "please sign in") resp.Set(constants.CODE_UNAUTH, "please sign in")
@ -49,15 +51,15 @@ func (l *GetSuccessRecommandLogic) GetSuccessRecommand(req *types.GetSuccessReco
resp.Set(constants.CODE_UNAUTH, "failed to get user info") resp.Set(constants.CODE_UNAUTH, "failed to get user info")
return return
} }
if req.Num == 0 { if req.Num == 0 || req.Num > 500 {
req.Num = 8 req.Num = 8
} }
if req.Size > 0 { if req.Size > 0 {
req.Size = image.GetCurrentSize(req.Size) req.Size = image.GetCurrentSize(req.Size)
} }
//获取所有产品的ids //随机取8个产品
productModel := model.NewFsProductModel(l.svcCtx.MysqlConn) productModel := model.NewFsProductModel(l.svcCtx.MysqlConn)
productList, err := productModel.GetAllProductList(l.ctx, 0, 1, "sort-asc") productList, err := productModel.GetRandomProductList(l.ctx, int(req.Num))
if err != nil { if err != nil {
logx.Error(err) logx.Error(err)
resp.Set(constants.CODE_SERVICE_ERR, "failed to get product list") resp.Set(constants.CODE_SERVICE_ERR, "failed to get product list")
@ -68,23 +70,35 @@ func (l *GetSuccessRecommandLogic) GetSuccessRecommand(req *types.GetSuccessReco
resp.Set(constants.CODE_OK, "success") resp.Set(constants.CODE_OK, "success")
return return
} }
productIds := make([]string, 0, len(productList)) list := make([]types.GetSuccessRecommandRsp, 0, len(productList))
for _, v := range productList { for _, v := range productList {
productIds = append(productIds, fmt.Sprintf("%d", v.Id)) data := types.GetSuccessRecommandRsp{
Title: v.Title,
Cover: v.Cover,
CoverImg: v.CoverImg,
Sn: v.Sn,
Id: v.Id,
SkuId: 0, //???????
CoverDefault: v.CoverImg,
} }
//随机取8个 if req.Size > 0 {
if len(productIds) > int(req.Num) { coverImgSlice := strings.Split(v.CoverImg, ".")
//打乱顺序 coverSlice := strings.Split(v.Cover, ".")
indexArr := rand.Perm(len(productIds)) if req.Size > 200 {
tmpProductIds := make([]string, 0, int(req.Num)) data.Cover = fmt.Sprintf("%s_%d.%s", coverSlice[0], req.Size, coverSlice[1])
for k, v := range indexArr { data.CoverImg = fmt.Sprintf("%s_%d.%s", coverImgSlice[0], req.Size, coverImgSlice[1])
if k == 8 {
break
} }
tmpProductIds = append(tmpProductIds, productIds[v]) //千人千面
if userInfo.IsThousandFace == 1 {
data.Cover = ""
data.CoverImg = fmt.Sprintf("%s/test/%d/%d_%d.png?%d", constants.DOMAIN_RENDER_IMG_NAME, userInfo.Id, userInfo.Id, v.Id, time.Now().Unix())
if req.Size > 200 {
data.CoverDefault = fmt.Sprintf("%s_%d.%s", coverImgSlice[0], req.Size, coverImgSlice[1])
} }
productIds = tmpProductIds
} }
}
return resp list = append(list, data)
}
resp.SetWithData(constants.CODE_OK, "success", list)
return
} }