347 lines
8.1 KiB
Go
347 lines
8.1 KiB
Go
package controller
|
|
|
|
import (
|
|
"github.com/codinl/go-logger"
|
|
"github.com/gin-gonic/gin"
|
|
"mh-server/lib/auth"
|
|
"mh-server/lib/status"
|
|
"mh-server/model"
|
|
)
|
|
|
|
// StoreList 门店列表
|
|
// @Summary 门店列表
|
|
// @Tags 首页
|
|
// @Produce json
|
|
// @Accept json
|
|
// @Param request body model.StoreListReq true "门店列表模型"
|
|
// @Success 200 {object} model.StoreListResp
|
|
// @Router /api/v1/store/list [post]
|
|
func StoreList(c *gin.Context) {
|
|
req := struct {
|
|
//GameCardId uint64 `json:"game_card_id"`
|
|
UseType uint32 `json:"use_type"`
|
|
GameCardList []model.CardInfo `json:"game_card_list"`
|
|
}{}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
logger.Error(err)
|
|
RespJson(c, status.BadRequest, nil)
|
|
return
|
|
}
|
|
|
|
stores, err := model.GetStoreList(req.GameCardList)
|
|
if err != nil {
|
|
logger.Error("err:", err)
|
|
RespJson(c, status.InternalServerError, nil)
|
|
return
|
|
}
|
|
RespOK(c, stores)
|
|
return
|
|
}
|
|
|
|
// DisplayStoreList 用户门店列表
|
|
func DisplayStoreList(c *gin.Context) {
|
|
req := struct {
|
|
DisplayType uint32 `json:"display_type"` // 0-所有 1-共享卡
|
|
}{}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
logger.Error(err)
|
|
RespJson(c, status.BadRequest, nil)
|
|
return
|
|
}
|
|
|
|
stores, err := model.GetDisplayStoreList(req.DisplayType)
|
|
if err != nil {
|
|
logger.Error("err:", err)
|
|
RespJson(c, status.InternalServerError, nil)
|
|
return
|
|
}
|
|
RespOK(c, stores)
|
|
}
|
|
|
|
// StoreInfo 门店详情
|
|
func StoreInfo(c *gin.Context) {
|
|
req := struct {
|
|
StoreId uint32 `json:"store_id"`
|
|
}{}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
logger.Error(err)
|
|
RespJson(c, status.BadRequest, nil)
|
|
return
|
|
}
|
|
store := model.Store{}
|
|
store.ID = req.StoreId
|
|
err := store.Info()
|
|
if err != nil {
|
|
logger.Error("err:", err)
|
|
return
|
|
}
|
|
RespOK(c, store)
|
|
return
|
|
}
|
|
|
|
// ShoppingCartAdd 租赁订单购物车-添加商品
|
|
func ShoppingCartAdd(c *gin.Context) {
|
|
req := &struct {
|
|
GameCardId uint32 `json:"game_card_id"`
|
|
AddType uint32 `json:"add_type"` // 1-加 2-减
|
|
}{}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
logger.Error(err)
|
|
RespJson(c, status.BadRequest, nil)
|
|
return
|
|
}
|
|
//fmt.Println("GameCardId:",req.GameCardId)
|
|
//fmt.Println("AddType:",req.AddType)
|
|
|
|
if req.GameCardId == 0 {
|
|
logger.Error("GameCardId is 0")
|
|
RespJson(c, status.BadRequest, nil)
|
|
return
|
|
}
|
|
uc := auth.GetCurrentUser(c)
|
|
if uc == nil {
|
|
RespJson(c, status.Unauthorized, nil)
|
|
return
|
|
}
|
|
|
|
// 原租卡购物车逻辑保持不变
|
|
var shoppingCart model.ShoppingCart
|
|
err := model.NewShoppingCartQuerySet(model.DB).UidEq(uint64(uc.Uid)).GameCardIdEq(uint64(req.GameCardId)).One(&shoppingCart)
|
|
if err != nil && err != model.RecordNotFound {
|
|
logger.Error("err:", err)
|
|
RespJson(c, status.InternalServerError, nil)
|
|
return
|
|
}
|
|
if err == model.RecordNotFound {
|
|
shoppingCart = model.ShoppingCart{
|
|
Uid: uint64(uc.Uid),
|
|
GameCardId: uint64(req.GameCardId),
|
|
Count: 1,
|
|
}
|
|
err := model.DB.Create(&shoppingCart).Error
|
|
if err != nil {
|
|
logger.Error("err:", err)
|
|
RespJson(c, status.InternalServerError, nil)
|
|
return
|
|
}
|
|
RespOK(c, shoppingCart)
|
|
return
|
|
}
|
|
|
|
if req.AddType == 1 {
|
|
shoppingCart.Count += 1
|
|
} else if req.AddType == 2 {
|
|
shoppingCart.Count -= 1
|
|
}
|
|
if shoppingCart.Count < 0 {
|
|
shoppingCart.Count = 0
|
|
}
|
|
_, err = model.NewShoppingCartQuerySet(model.DB).IDEq(shoppingCart.ID).GetUpdater().SetCount(shoppingCart.Count).UpdateNum()
|
|
if err != nil {
|
|
logger.Error("err:", err)
|
|
RespJson(c, status.InternalServerError, nil)
|
|
return
|
|
}
|
|
|
|
RespOK(c, shoppingCart)
|
|
return
|
|
}
|
|
|
|
// ShoppingCartList 租赁订单购物车-列表
|
|
func ShoppingCartList(c *gin.Context) {
|
|
req := &model.ShoppingCartListReq{}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
logger.Error(err)
|
|
RespJson(c, status.BadRequest, nil)
|
|
return
|
|
}
|
|
uc := auth.GetCurrentUser(c)
|
|
if uc == nil {
|
|
RespJson(c, status.Unauthorized, nil)
|
|
return
|
|
}
|
|
|
|
req.Uid = uc.Uid
|
|
list, err := req.List()
|
|
if err != nil {
|
|
logger.Error("err:", err)
|
|
RespJson(c, status.InternalServerError, nil)
|
|
return
|
|
}
|
|
|
|
RespOK(c, list)
|
|
return
|
|
}
|
|
|
|
// ShoppingCartDel 租赁订单购物车-删除商品
|
|
func ShoppingCartDel(c *gin.Context) {
|
|
req := &struct {
|
|
ShoppingCartIds []uint32 `json:"shopping_cart_ids"`
|
|
}{}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
logger.Error(err)
|
|
RespJson(c, status.BadRequest, nil)
|
|
return
|
|
}
|
|
uc := auth.GetCurrentUser(c)
|
|
if uc == nil {
|
|
RespJson(c, status.Unauthorized, nil)
|
|
return
|
|
}
|
|
|
|
// 原租卡购物车删除逻辑
|
|
err := model.NewShoppingCartQuerySet(model.DB).UidEq(uint64(uc.Uid)).IDIn(req.ShoppingCartIds...).Delete()
|
|
if err != nil {
|
|
logger.Error("err:", err)
|
|
RespJson(c, status.InternalServerError, nil)
|
|
return
|
|
}
|
|
|
|
RespOK(c, nil)
|
|
return
|
|
}
|
|
|
|
// MallCartAdd 商城购物车-添加商品
|
|
// @Summary 商城购物车-添加商品
|
|
// @Tags 商城购物车
|
|
// @Produce json
|
|
// @Accept json
|
|
// @Param request body model.MallCartAddReq true "添加商品请求模型"
|
|
// @Success 200 {object} common.Response
|
|
// @Router /api/v1/mall_cart/add [post]
|
|
func MallCartAdd(c *gin.Context) {
|
|
req := &model.MallCartAddReq{}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
logger.Error(err)
|
|
RespJson(c, status.BadRequest, nil)
|
|
return
|
|
}
|
|
//fmt.Println("GameCardId:",req.GameCardId)
|
|
//fmt.Println("AddType:",req.AddType)
|
|
|
|
if req.GoodsId == 0 {
|
|
logger.Error("GoodsId is 0")
|
|
RespJson(c, status.BadRequest, nil)
|
|
return
|
|
}
|
|
uc := auth.GetCurrentUser(c)
|
|
if uc == nil {
|
|
RespJson(c, status.Unauthorized, nil)
|
|
return
|
|
}
|
|
|
|
// 商城购物车
|
|
handleMallCart(c, uc.Uid, req.GoodsId, req.AddType)
|
|
return
|
|
|
|
}
|
|
|
|
// MallCartList 商城购物车-列表
|
|
// @Summary 商城购物车-列表
|
|
// @Tags 商城购物车
|
|
// @Produce json
|
|
// @Accept json
|
|
// @Param request body model.MallCartListReq true "购物车列表请求模型"
|
|
// @Success 200 {object} model.MallCartListResp
|
|
// @Router /api/v1/mall_cart/list [post]
|
|
func MallCartList(c *gin.Context) {
|
|
req := &model.MallCartListReq{}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
logger.Error(err)
|
|
RespJson(c, status.BadRequest, nil)
|
|
return
|
|
}
|
|
uc := auth.GetCurrentUser(c)
|
|
if uc == nil {
|
|
RespJson(c, status.Unauthorized, nil)
|
|
return
|
|
}
|
|
|
|
// 商城购物车逻辑
|
|
list, err := model.MallCartList(uc.Uid, req.PageNum, req.PageSize)
|
|
if err != nil {
|
|
logger.Error("err:", err)
|
|
RespJson(c, status.InternalServerError, nil)
|
|
return
|
|
}
|
|
RespOK(c, list)
|
|
return
|
|
}
|
|
|
|
// MallCartDel 商城购物车-删除商品
|
|
// @Summary 商城购物车-删除商品
|
|
// @Tags 商城购物车
|
|
// @Produce json
|
|
// @Accept json
|
|
// @Param request body model.MallCartDel true "删除商品请求模型"
|
|
// @Success 200 {object} common.Response
|
|
// @Router /api/v1/mall_cart/del [post]
|
|
func MallCartDel(c *gin.Context) {
|
|
req := &model.MallCartDel{}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
logger.Error(err)
|
|
RespJson(c, status.BadRequest, nil)
|
|
return
|
|
}
|
|
uc := auth.GetCurrentUser(c)
|
|
if uc == nil {
|
|
RespJson(c, status.Unauthorized, nil)
|
|
return
|
|
}
|
|
|
|
// 商城购物车删除
|
|
err := model.NewMallCartQuerySet(model.DB).UidEq(uint64(uc.Uid)).IDIn(req.MallCartIds...).Delete()
|
|
if err != nil {
|
|
logger.Error("err:", err)
|
|
RespJson(c, status.InternalServerError, nil)
|
|
return
|
|
}
|
|
RespOK(c, nil)
|
|
return
|
|
}
|
|
|
|
// 商城购物车逻辑
|
|
func handleMallCart(c *gin.Context, uid uint32, goodsId uint32, addType uint32) {
|
|
var mallCart model.MallCart
|
|
err := model.NewMallCartQuerySet(model.DB).UidEq(uint64(uid)).GoodsIdEq(goodsId).One(&mallCart)
|
|
if err != nil && err != model.RecordNotFound {
|
|
logger.Error("err:", err)
|
|
RespJson(c, status.InternalServerError, nil)
|
|
return
|
|
}
|
|
|
|
if err == model.RecordNotFound {
|
|
mallCart = model.MallCart{
|
|
Uid: uint64(uid),
|
|
GoodsId: goodsId,
|
|
Count: 1,
|
|
}
|
|
if err := model.DB.Create(&mallCart).Error; err != nil {
|
|
logger.Error("err:", err)
|
|
RespJson(c, status.InternalServerError, nil)
|
|
return
|
|
}
|
|
RespOK(c, mallCart)
|
|
return
|
|
}
|
|
|
|
if addType == 1 {
|
|
mallCart.Count += 1
|
|
} else if addType == 2 {
|
|
mallCart.Count -= 1
|
|
}
|
|
|
|
if mallCart.Count < 0 {
|
|
mallCart.Count = 0
|
|
}
|
|
|
|
_, err = model.NewMallCartQuerySet(model.DB).IDEq(mallCart.ID).GetUpdater().SetCount(mallCart.Count).UpdateNum()
|
|
if err != nil {
|
|
logger.Error("err:", err)
|
|
RespJson(c, status.InternalServerError, nil)
|
|
return
|
|
}
|
|
|
|
RespOK(c, mallCart)
|
|
}
|