购物车按添加时间排序

master
datang 3 years ago
parent 681913e9fb
commit 6f23ce0d5a

@ -53,112 +53,78 @@ type subShoppingChild struct {
SecKill SecKillDetail `json:"sec_kill"`
}
// QueryShoppingTrolleyGoodsList @Title 购物车列表
func QueryShoppingTrolleyGoodsList(c *gin.Context) {
var p queryShoppingTrolleyListParam
if err := tools.ParseParams(&p, c); err != nil {
back.Fail(c, err.Error())
return
}
//p.UserID = 1
if p.UserID == 0 {
back.Suc(c, "", make([]shoppingTrolleyResp, 0, 0))
return
}
var u user.Information
dbc.DB.First(&u, "id = ?", p.UserID)
var trolleyList []shopping_trolley.Information
dbc.DB.Order("id desc").Find(&trolleyList, "user_id = ?", p.UserID)
// 先把商品跟sku信息拿出来
// 查询购物车商品
subShoppingChildList := make([]subShoppingChild, 0, 0)
for _, v := range trolleyList {
var mainPhoto goods.MainPhoto
dbc.DB.Select("url").First(&mainPhoto, "goods_id = ? AND is_master = 1", v.GoodsID)
var goodsInfo goods.Information
dbc.DB.First(&goodsInfo, v.GoodsID)
var sku goods.Sku
dbc.DB.First(&sku, v.SkuID)
inventory := sku.Inventory
salesVolume := sku.SalesVolume
discountPrice := sku.DiscountPrice
commission := sku.GetSelfProfit(u.Level)
valid := true
//rate1 := decimal.Zero
//rate2 := decimal.Zero
//rate3 := decimal.Zero
//if u.Level == 1 {
// rate1 = decimal.NewFromFloat32(0.4)
// rate2 = decimal.NewFromFloat32(0.1)
//}
//if u.Level == 2 {
// rate1 = decimal.NewFromFloat32(0.4)
// rate2 = decimal.NewFromFloat32(0.1)
// rate3 = decimal.NewFromFloat(0.2)
//}
//base := sku.GetBase()
//commission1 := base.Mul(rate1).Round(2)
//commission2 := base.Mul(rate2).Round(2)
//commission3 := base.Mul(rate3).Round(2)
//commission = commission1.Add(commission2).Add(commission3)
//
//if u.Level == 10 {
// commission = sku.GetBase().Mul(decimal.NewFromFloat(define.Coefficient))
//}
if v.Quantity > inventory {
dbc.DB.Preload("Brand").Preload("Goods").Preload("Sku").Order("created_at desc").Find(&trolleyList, "user_id = ?", p.UserID)
//fmt.Printf("%v\n", trolleyList)
recookGoodsInfoModel := &goods2.RecookGoodsInfoModel{}
valid := true
for _, t := range trolleyList {
if t.Quantity > t.Sku.Inventory {
valid = false
}
recookGoodsInfoModel := &goods2.RecookGoodsInfoModel{}
subShoppingChildList = append(subShoppingChildList, subShoppingChild{
ShoppingTrolleyID: v.ID,
GoodsID: goodsInfo.ID,
BrandID: goodsInfo.BrandID,
GoodsName: goodsInfo.GoodsName,
Description: goodsInfo.Description,
SkuName: sku.Name,
Quantity: v.Quantity,
Inventory: inventory,
SalesVolume: salesVolume,
MainPhotoURL: mainPhoto.URL,
ShoppingTrolleyID: t.ID,
GoodsID: t.GoodsID,
BrandID: t.BrandID,
GoodsName: t.Goods.GoodsName,
Description: t.Goods.Description,
SkuName: t.Sku.Name,
Quantity: t.Quantity,
Inventory: t.Sku.Inventory,
SalesVolume: t.Sku.SalesVolume,
MainPhotoURL: t.Sku.PicURL,
PromotionName: "",
OriginalPrice: sku.OriginalPrice,
Price: discountPrice,
Commission: commission,
OriginalPrice: t.Sku.OriginalPrice,
Price: t.Sku.DiscountPrice,
Commission: t.Sku.Commission,
Valid: valid,
Promotion: nil,
IsImport: goodsInfo.IsImport,
Storehouse: goodsInfo.Storehouse,
IsFerme: goodsInfo.IsFerme,
Ferme: sku.PurchasePrice.Mul(decimal.NewFromFloat(1.2)).Mul(decimal.NewFromFloat(0.091)).Truncate(2),
HasCoin: recookGoodsInfoModel.HasCoin(goodsInfo.Storehouse),
HasBalance: recookGoodsInfoModel.HasBalance(goodsInfo.Storehouse),
PublishStatus: goodsInfo.PublishStatus,
IsImport: t.Goods.IsImport,
Storehouse: t.Goods.Storehouse,
IsFerme: t.Goods.IsFerme,
Ferme: t.Sku.PurchasePrice.Mul(decimal.NewFromFloat(1.2)).Mul(decimal.NewFromFloat(0.091)).Truncate(2),
HasCoin: recookGoodsInfoModel.HasCoin(t.Goods.Storehouse),
HasBalance: recookGoodsInfoModel.HasBalance(t.Goods.Storehouse),
PublishStatus: t.Goods.PublishStatus,
CountryIcon: "",
GysId: goodsInfo.VendorID,
GysId: t.Goods.VendorID,
})
}
brandIdMap := map[uint]uint{}
for _, v := range subShoppingChildList {
brandIdMap[v.BrandID] = 1
}
var brandList []goods.Brand
for k := range brandIdMap {
for _, k := range subShoppingChildList {
var brand goods.Brand
dbc.DB.Select("id,name,logo_url").First(&brand, k)
brandList = append(brandList, brand)
var flag = true
dbc.DB.Select("id,name,logo_url").First(&brand, k.BrandID)
for _, i2 := range brandList { //不添加已有brand
if i2.ID == brand.ID {
flag = false
break
}
}
if flag {
brandList = append(brandList, brand)
}
}
//fmt.Printf("%v\n", brandList)
// 把商品按品归类
list := make([]shoppingTrolleyResp, 0, 0)
for _, v1 := range brandList {
var children []subShoppingChild
for i2, v2 := range subShoppingChildList {
@ -173,6 +139,5 @@ func QueryShoppingTrolleyGoodsList(c *gin.Context) {
Children: children,
})
}
back.Suc(c, "操作成功", list)
}

@ -1,15 +1,21 @@
package shopping_trolley
import "github.com/golangkit/formatime"
import (
"github.com/golangkit/formatime"
"recook/internal/model/goods"
)
type Information struct {
ID uint `gorm:"column:id;primary_key" json:"id"`
UserID uint `gorm:"column:user_id" json:"-"`
BrandID uint `gorm:"column:brand_id" json:"-"`
GoodsID uint `gorm:"column:goods_id" json:"goodsId"`
SkuID uint `gorm:"column:sku_id" json:"skuId"`
Quantity uint `gorm:"column:quantity" json:"quantity"`
CreatedAt formatime.Second `gorm:"column:created_at" json:"-"`
ID uint `gorm:"column:id;primary_key" json:"id"`
UserID uint `gorm:"column:user_id" json:"-"`
BrandID uint `gorm:"column:brand_id" json:"-"`
Brand goods.Brand `gorm:"foreignKey:BrandID"`
GoodsID uint `gorm:"column:goods_id" json:"goodsId"`
Goods goods.Information `gorm:"foreignKey:GoodsID"`
SkuID uint `gorm:"column:sku_id" json:"skuId"`
Sku goods.Sku `gorm:"foreignKey:SkuID"`
Quantity uint `gorm:"column:quantity" json:"quantity"`
CreatedAt formatime.Second `gorm:"column:created_at" json:"-"`
}
// TableName sets the insert table name for this struct type

Loading…
Cancel
Save