From 3e060c364fe2a69f2db18824729a21d05b92c34e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8C=83=E4=BF=8A=E6=88=90?= Date: Wed, 20 Sep 2023 18:05:53 +0800 Subject: [PATCH] =?UTF-8?q?=E5=95=86=E5=93=81=E5=88=97=E8=A1=A8=E5=B1=95?= =?UTF-8?q?=E7=A4=BA=E4=BB=B7=E6=A0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/mall.go | 17 +++++++++++++++++ model/mall.go | 43 ++++++++++++++++++++++++++----------------- 2 files changed, 43 insertions(+), 17 deletions(-) diff --git a/controller/mall.go b/controller/mall.go index 4d856bc..91144b8 100644 --- a/controller/mall.go +++ b/controller/mall.go @@ -27,6 +27,23 @@ func MallGoodsList(c *gin.Context) { RespJson(c, status.InternalServerError, nil) return } + var ids []uint32 + for _, goods := range list { + ids = append(ids, goods.GoodsId) + } + + if len(ids) > 0 { + cm := model.GetGoodsFirstSkuCombo(ids) + if cm != nil { + for j, combo := range cm { + for i, goods := range list { + if combo.GoodsId == goods.GoodsId { + list[i].Combo = &cm[j] + } + } + } + } + } ret := map[string]interface{}{ "list": list, diff --git a/model/mall.go b/model/mall.go index 49fec7a..7afd7d6 100644 --- a/model/mall.go +++ b/model/mall.go @@ -78,9 +78,10 @@ type Goods struct { VersionId uint64 `json:"version_id"` // 乐观锁 ShowDiscount int8 `json:"show_discount"` // - GoodsCat *GoodsCat `json:"goods_cat" gorm:"-"` - GoodsDiscount *GoodsDiscount `json:"goods_discount" gorm:"-"` - Attributes []GoodsAttribute `json:"attributes" gorm:"-"` + GoodsCat *GoodsCat `json:"goods_cat" gorm:"-"` + GoodsDiscount *GoodsDiscount `json:"goods_discount" gorm:"-"` + Attributes []GoodsAttribute `json:"attributes" gorm:"-"` + Combo *GoodsAttributeCombo `json:"combo" gorm:"-"` } // gen:qs @@ -1106,18 +1107,26 @@ func ParaToSubstance(c *gin.Context, sign string) (string, error) { return signStr, nil } -//paramMap := make(map[string]string, 0) +func GetGoodsFirstSkuCombo(goodsId []uint32) []GoodsAttributeCombo { + var list []GoodsAttributeCombo + //查询每个商品按sort字端排序的最大的一个sku的价格 + err := DB. + Raw(` +SELECT b.* +FROM (SELECT id + FROM goods_attribute d + INNER JOIN (SELECT goods_id, MAX(sort) AS max_sort FROM goods_attribute GROUP BY goods_id) c + ON c.goods_id = d.goods_id AND c.max_sort = d.sort) AS a + INNER JOIN goods_attribute_combo b ON a.id = b.goods_attribute_id +WHERE b.goods_id IN (?) +GROUP BY b.goods_id + `, goodsId). + Scan(&list). + Error + if err != nil { + logger.Error("get goods first sku failed", err) + return nil + } -//appId := c.Param("app_id") -//bankTrxNo := c.Param("bank_trx_no") -//bankWayCode := c.Param("bank_way_code") -//buyerId := c.Param("buyer_id") -//billDetail := c.Param("buyer_trade_fund_bill_detail") -//notifyType := c.Param("notify_type") -//payModeCode := c.Param("pay_mode_code") -//paySuccessTime := c.Param("pay_success_time") -//payWayCode := c.Param("pay_way_code") -//platTrxNo := c.Param("plat_trx_no") -//settleAmount := c.Param("settle_amount") -//settleAppId := c.Param("settle_app_id") -//signType := c.Param("sign_type") + return list +}