You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
85 lines
2.2 KiB
85 lines
2.2 KiB
package shopping_trolley
|
|
|
|
import (
|
|
"fmt"
|
|
"recook/internal/back"
|
|
"recook/internal/dbc"
|
|
"recook/internal/model/goods"
|
|
"recook/internal/model/promotion"
|
|
"recook/internal/model/shopping_trolley"
|
|
"recook/tools"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type updateQuantityParam struct {
|
|
UserID uint `json:"userId"`
|
|
ShoppingTrolleyId uint `json:"shoppingTrolleyId" validate:"min=1"`
|
|
Quantity uint `json:"quantity" validate:"min=1"`
|
|
}
|
|
|
|
/*更新购物车商品数量 库存超过时如果是增加数量则禁止操作,可以减数量*/
|
|
func UpdateGoodsQuantity(c *gin.Context) {
|
|
var p updateQuantityParam
|
|
|
|
if err := tools.ParseParams(&p, c); err != nil {
|
|
back.Fail(c, err.Error())
|
|
return
|
|
}
|
|
|
|
var info shopping_trolley.Information
|
|
dbc.DB.First(&info, p.ShoppingTrolleyId)
|
|
if info.UserID != p.UserID {
|
|
back.Fail(c, "非法调用")
|
|
return
|
|
}
|
|
|
|
var sku goods.Sku
|
|
dbc.DB.First(&sku, info.SkuID)
|
|
inventory := sku.Inventory
|
|
// 判断商品在不在活动里
|
|
var promotionSku promotion.Sku
|
|
{
|
|
var promotionGoods promotion.Goods
|
|
dbc.DB.First(&(promotionGoods), "goods_id = ? AND promotion_start_date = ?", sku.GoodsID, time.Now().Format("2006-01-02"))
|
|
if promotionGoods.ID == 0 {
|
|
tomorrow := time.Now().Add(24 * time.Hour)
|
|
dbc.DB.First(&(promotionGoods), "goods_id = ? AND promotion_start_date = ?", sku.GoodsID, tomorrow.Format("2006-01-02"))
|
|
}
|
|
if promotionGoods.ID > 0 {
|
|
dbc.DB.First(&promotionSku, "promotion_goods_id = ? AND sku_id = ?", promotionGoods.ID, sku.ID)
|
|
inventory = promotionSku.Inventory
|
|
}
|
|
}
|
|
quan := p.Quantity
|
|
if quan > inventory {
|
|
back.Fail(c, "超出库存")
|
|
return
|
|
}
|
|
|
|
if p.Quantity > sku.Inventory {
|
|
if p.Quantity < info.Quantity { // 说明在减库存
|
|
dbc.DB.Model(&info).Updates(shopping_trolley.Information{Quantity: p.Quantity})
|
|
back.Suc(c, "仅剩"+fmt.Sprintf("%d", sku.Inventory)+"件", nil)
|
|
} else {
|
|
back.Fail(c, "超出库存")
|
|
}
|
|
} else {
|
|
if p.Quantity >= 1 {
|
|
quan := p.Quantity
|
|
if p.Quantity > 50 {
|
|
quan = 50
|
|
}
|
|
|
|
if err := dbc.DB.Model(&info).Updates(shopping_trolley.Information{Quantity: quan}).Error; err != nil {
|
|
back.Err(c, err.Error())
|
|
return
|
|
}
|
|
back.Suc(c, "", nil)
|
|
} else {
|
|
back.Fail(c, "不能再少啦!")
|
|
}
|
|
}
|
|
}
|