增加产品推荐接口
This commit is contained in:
parent
4312d978fb
commit
cf852c5a87
|
@ -13,8 +13,8 @@ type (
|
|||
// and implement the added methods in customFsProductModel.
|
||||
FsProductModel interface {
|
||||
fsProductModel
|
||||
GetProductListByConditions(ctx context.Context, productType int, isDel int, isShelf int, sort string) ([]FsProduct, error)
|
||||
GetAllProductList(ctx context.Context, isDel int, isShelf int, sort string) (resp []FsProduct, err error)
|
||||
GetProductListByConditions(ctx context.Context, productType int, sort string) ([]FsProduct, error)
|
||||
GetRandomProductList(ctx context.Context, limit int) (resp []FsProduct, err error)
|
||||
}
|
||||
|
||||
customFsProductModel struct {
|
||||
|
@ -28,7 +28,7 @@ func NewFsProductModel(conn sqlx.SqlConn) FsProductModel {
|
|||
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` = ?",
|
||||
fsProductRows, m.table)
|
||||
switch sort {
|
||||
|
@ -39,24 +39,16 @@ func (m *defaultFsProductModel) GetProductListByConditions(ctx context.Context,
|
|||
default:
|
||||
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 {
|
||||
return nil, err
|
||||
}
|
||||
return
|
||||
}
|
||||
func (m *defaultFsProductModel) GetAllProductList(ctx context.Context, isDel int, isShelf int, sort string) (resp []FsProduct, err error) {
|
||||
query := fmt.Sprintf("select %s from %s where `is_del` =? and `is_shelf` = ?",
|
||||
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` = ? order by RAND() limit ?",
|
||||
fsProductRows, m.table)
|
||||
switch sort {
|
||||
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)
|
||||
err = m.conn.QueryRowsCtx(ctx, &resp, query, 0, 1, limit)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -73,7 +73,7 @@ func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq, login
|
|||
}
|
||||
//查询符合的产品列表
|
||||
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 {
|
||||
logx.Error(err)
|
||||
resp.Set(constants.CODE_SERVICE_ERR, "failed to get product list")
|
||||
|
|
|
@ -10,10 +10,10 @@ import (
|
|||
"fusenapi/product/internal/types"
|
||||
"fusenapi/utils/auth"
|
||||
"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/stores/sqlx"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
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) {
|
||||
resp = &types.Response{}
|
||||
loginInfo.UserId = 83
|
||||
//校验前台登录情况
|
||||
if loginInfo.UserId == 0 {
|
||||
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")
|
||||
return
|
||||
}
|
||||
if req.Num == 0 {
|
||||
if req.Num == 0 || req.Num > 500 {
|
||||
req.Num = 8
|
||||
}
|
||||
if req.Size > 0 {
|
||||
req.Size = image.GetCurrentSize(req.Size)
|
||||
}
|
||||
//获取所有产品的ids
|
||||
//随机取8个产品
|
||||
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 {
|
||||
logx.Error(err)
|
||||
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")
|
||||
return
|
||||
}
|
||||
productIds := make([]string, 0, len(productList))
|
||||
list := make([]types.GetSuccessRecommandRsp, 0, len(productList))
|
||||
for _, v := range productList {
|
||||
productIds = append(productIds, fmt.Sprintf("%d", v.Id))
|
||||
}
|
||||
//随机取8个
|
||||
if len(productIds) > int(req.Num) {
|
||||
//打乱顺序
|
||||
indexArr := rand.Perm(len(productIds))
|
||||
tmpProductIds := make([]string, 0, int(req.Num))
|
||||
for k, v := range indexArr {
|
||||
if k == 8 {
|
||||
break
|
||||
}
|
||||
tmpProductIds = append(tmpProductIds, productIds[v])
|
||||
data := types.GetSuccessRecommandRsp{
|
||||
Title: v.Title,
|
||||
Cover: v.Cover,
|
||||
CoverImg: v.CoverImg,
|
||||
Sn: v.Sn,
|
||||
Id: v.Id,
|
||||
SkuId: 0, //???????
|
||||
CoverDefault: v.CoverImg,
|
||||
}
|
||||
productIds = tmpProductIds
|
||||
if req.Size > 0 {
|
||||
coverImgSlice := strings.Split(v.CoverImg, ".")
|
||||
coverSlice := strings.Split(v.Cover, ".")
|
||||
if req.Size > 200 {
|
||||
data.Cover = fmt.Sprintf("%s_%d.%s", coverSlice[0], req.Size, coverSlice[1])
|
||||
data.CoverImg = fmt.Sprintf("%s_%d.%s", coverImgSlice[0], req.Size, coverImgSlice[1])
|
||||
}
|
||||
//千人千面
|
||||
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])
|
||||
}
|
||||
}
|
||||
}
|
||||
list = append(list, data)
|
||||
}
|
||||
|
||||
return resp
|
||||
resp.SetWithData(constants.CODE_OK, "success", list)
|
||||
return
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user