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 +}