From 8ba0952cc56721311079d10a752c7400cbeec386 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Tue, 6 Jun 2023 17:18:01 +0800 Subject: [PATCH] fix --- model/fsproductmodel_gen.go | 1 + model/self_fsproductmodel.go | 17 +++++ product/internal/logic/getproductlistlogic.go | 1 - .../logic/getsuccessrecommandlogic.go | 65 ++++++++++++++++++- 4 files changed, 80 insertions(+), 4 deletions(-) diff --git a/model/fsproductmodel_gen.go b/model/fsproductmodel_gen.go index 86bc2bb1..e9edc4d9 100755 --- a/model/fsproductmodel_gen.go +++ b/model/fsproductmodel_gen.go @@ -29,6 +29,7 @@ type ( Update(ctx context.Context, data *FsProduct) error Delete(ctx context.Context, id int64) error 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) } defaultFsProductModel struct { diff --git a/model/self_fsproductmodel.go b/model/self_fsproductmodel.go index 9384d052..8655bdae 100755 --- a/model/self_fsproductmodel.go +++ b/model/self_fsproductmodel.go @@ -24,3 +24,20 @@ func (m *defaultFsProductModel) GetProductListByConditions(ctx context.Context, } 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` = ?", + 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) + if err != nil { + return nil, err + } + return +} diff --git a/product/internal/logic/getproductlistlogic.go b/product/internal/logic/getproductlistlogic.go index d5ca2886..97fd6732 100644 --- a/product/internal/logic/getproductlistlogic.go +++ b/product/internal/logic/getproductlistlogic.go @@ -37,7 +37,6 @@ func NewGetProductListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Ge // 获取产品列表 func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq, loginInfo auth.UserInfo) (resp *types.Response) { resp = &types.Response{} - loginInfo.UserId = 83 //校验前台登录情况 if loginInfo.UserId == 0 { resp.Set(constants.CODE_UNAUTH, "please sign in") diff --git a/product/internal/logic/getsuccessrecommandlogic.go b/product/internal/logic/getsuccessrecommandlogic.go index e72b4a39..1fb4d3fd 100644 --- a/product/internal/logic/getsuccessrecommandlogic.go +++ b/product/internal/logic/getsuccessrecommandlogic.go @@ -2,10 +2,16 @@ package logic import ( "context" - "fusenapi/utils/auth" - + "errors" + "fmt" + "fusenapi/constants" + "fusenapi/model" "fusenapi/product/internal/svc" "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" ) @@ -25,7 +31,60 @@ func NewGetSuccessRecommandLogic(ctx context.Context, svcCtx *svc.ServiceContext } func (l *GetSuccessRecommandLogic) GetSuccessRecommand(req *types.GetSuccessRecommandReq, loginInfo auth.UserInfo) (resp *types.Response) { - // 必须返回response, 前端需要的是内部约定的Code码, 处理相关的逻辑. 例子(eg): resp.Set(501, "error") resp = &types.Response{} + //校验前台登录情况 + if loginInfo.UserId == 0 { + resp.Set(constants.CODE_UNAUTH, "please sign in") + return + } + //获取用户信息 + userModel := model.NewFsUserModel(l.svcCtx.MysqlConn) + userInfo, err := userModel.FindOne(l.ctx, loginInfo.UserId) + if err != nil && errors.Is(err, sqlx.ErrNotFound) { + logx.Error(err) + resp.Set(constants.CODE_SERVICE_ERR, "failed to get user info") + return + } + if userInfo == nil { + resp.Set(constants.CODE_UNAUTH, "failed to get user info") + return + } + if req.Num == 0 { + req.Num = 8 + } + if req.Size > 0 { + req.Size = image.GetCurrentSize(req.Size) + } + //获取所有产品的ids + productModel := model.NewFsProductModel(l.svcCtx.MysqlConn) + productList, err := productModel.GetAllProductList(l.ctx, 0, 1, "sort-asc") + if err != nil { + logx.Error(err) + resp.Set(constants.CODE_SERVICE_ERR, "failed to get product list") + return + } + //没有推荐产品就返回 + if len(productList) == 0 { + resp.Set(constants.CODE_OK, "success") + return + } + productIds := make([]string, 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]) + } + productIds = tmpProductIds + } + return resp }