This commit is contained in:
laodaming 2023-07-10 18:28:37 +08:00
parent 6b96a3ecbe
commit 852fca2afa
2 changed files with 22 additions and 8 deletions

View File

@ -24,12 +24,18 @@ func (s *FsProductSizeModel) GetAllByIds(ctx context.Context, ids []int64, sort
}
return
}
func (s *FsProductSizeModel) CountByStatus(ctx context.Context, status int) (total int64, err error) {
err = s.db.WithContext(ctx).Model(&FsProductSize{}).Where("`status` = ? limit 1", status).Count(&total).Error
if err != nil {
return 0, err
}
return
type CountProductSizeByStatusRsp struct {
ProductId int64 `json:"product_id"`
Num int64 `json:"num"`
}
func (s *FsProductSizeModel) GetGroupProductSizeByStatus(ctx context.Context,productIds []int64, status int) (resp []CountProductSizeByStatusRsp, err error) {
err = s.db.WithContext(ctx).Model(&FsProductSize{}).
Select("product_id,count(*) as num").
Where("`product_id` in (?) and `status` = ?",productIds ,status).
Group("product_id").
Find(&resp).Error
return resp,err
}
func (s *FsProductSizeModel) GetAllByProductIds(ctx context.Context, productIds []int64, sort string) (resp []FsProductSize, err error) {
if len(productIds) == 0 {

View File

@ -124,11 +124,15 @@ func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq, useri
}
//获取产品尺寸数量
productSizeModel := gmodel.NewFsProductSizeModel(l.svcCtx.MysqlConn)
productSizeCount, err := productSizeModel.CountByStatus(l.ctx, 1)
productSizeCountList, err := productSizeModel.GetGroupProductSizeByStatus(l.ctx, productIds,1)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "get product size count err")
}
mapProductSizeCount := make(map[int64]int64)
for _,v := range productSizeCountList{
mapProductSizeCount[v.ProductId] = v.Num
}
//拼接返回
itemList := make([]types.Items, 0, productLen)
for _, v := range productList {
@ -138,6 +142,10 @@ func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq, useri
if !ok || !tmpOk {
continue
}
sizeNum := int64(0)
if mapSizeNum,ok := mapProductSizeCount[v.Id];ok{
sizeNum = mapSizeNum
}
item := types.Items{
Id: v.Id,
Sn: *v.Sn,
@ -145,7 +153,7 @@ func (l *GetProductListLogic) GetProductList(req *types.GetProductListReq, useri
Intro: *v.Intro,
IsEnv: *v.IsProtection,
IsMicro: *v.IsMicrowave,
SizeNum: uint32(productSizeCount),
SizeNum: uint32(sizeNum),
MiniPrice: format.CentoDollar(minPrice),
}
//千人千面处理