diff --git a/model/gmodel/fs_product_size_logic.go b/model/gmodel/fs_product_size_logic.go index b1ce6705..6483cf64 100755 --- a/model/gmodel/fs_product_size_logic.go +++ b/model/gmodel/fs_product_size_logic.go @@ -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 { diff --git a/server/product/internal/logic/getproductlistlogic.go b/server/product/internal/logic/getproductlistlogic.go index 5d680cf0..30b966bd 100644 --- a/server/product/internal/logic/getproductlistlogic.go +++ b/server/product/internal/logic/getproductlistlogic.go @@ -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), } //千人千面处理