This commit is contained in:
laodaming 2023-07-13 11:36:02 +08:00
parent 0f96eaac30
commit e4925c2194
2 changed files with 20 additions and 8 deletions

View File

@ -44,7 +44,7 @@ func (p *FsProductModel) GetProductListByIdsWithoutStatus(ctx context.Context, p
}
return
}
func (p *FsProductModel) GetProductListByTypeIds(ctx context.Context, productTypes []int64, sort string) (resp []FsProduct, err error) {
func (p *FsProductModel) GetProductListByTypeIds(ctx context.Context, productTypes []int64, page int, limit int, sort string) (resp []FsProduct, total int64, err error) {
if len(productTypes) == 0 {
return
}
@ -55,11 +55,13 @@ func (p *FsProductModel) GetProductListByTypeIds(ctx context.Context, productTyp
case "sort-desc":
db = db.Order("`sort` DESC")
}
err = db.Find(&resp).Error
err = db.Count(&total).Error
if err != nil {
return nil, err
return nil, 0, err
}
return
offset := (page - 1) * limit
err = db.Offset(offset).Limit(limit).Find(&resp).Error
return resp, total, err
}
func (p *FsProductModel) GetRandomProductList(ctx context.Context, limit int) (resp []FsProduct, err error) {
err = p.db.WithContext(ctx).Model(&FsProduct{}).

View File

@ -15,6 +15,7 @@ import (
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/core/stores/sqlc"
"gorm.io/gorm"
"math"
"sort"
"strings"
)
@ -44,13 +45,14 @@ func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq, useri
}
return resp.SetStatusWithMessage(basic.CodeOK, "success", demo)
}
if req.Page <= 0 {
req.Page = 1
if req.Page <= 0 && req.Page > 10000 {
req.Page = constants.DEFAULT_PAGE
}
//获取合适尺寸
if req.Size > 0 {
req.Size = image.GetCurrentSize(req.Size)
}
pageSize := constants.DEFAULT_PAGE_SIZE
//查询用户信息(不用判断存在)
userModel := gmodel.NewFsUserModel(l.svcCtx.MysqlConn)
user, err := userModel.FindUserById(l.ctx, userinfo.UserId)
@ -60,7 +62,7 @@ func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq, useri
}
//查询符合的产品列表
productModel := gmodel.NewFsProductModel(l.svcCtx.MysqlConn)
productList, err := productModel.GetProductListByTypeIds(l.ctx, []int64{req.Cid}, "sort-desc")
productList, total, err := productModel.GetProductListByTypeIds(l.ctx, []int64{req.Cid}, int(req.Page), pageSize, "sort-desc")
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get product list")
@ -172,6 +174,14 @@ func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq, useri
return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetProductListRsp{
Ob: types.Ob{
Items: itemList,
}, TypeName: *tagInfo.Title, Description: *tagInfo.Description,
Meta: types.Meta{
TotalCount: total,
PageCount: int64(math.Ceil(float64(total) / float64(pageSize))),
CurrentPage: int(req.Page),
PerPage: pageSize,
},
},
TypeName: *tagInfo.Title,
Description: *tagInfo.Description,
})
}