新增更新购物车购买数量接口
This commit is contained in:
parent
b71141807b
commit
0bb11f975d
|
@ -17,8 +17,6 @@ type FsShoppingCart struct {
|
|||
PurchaseQuantity *int64 `gorm:"default:0;" json:"purchase_quantity"` // 购买数量
|
||||
Snapshot *string `gorm:"default:'';" json:"snapshot"` //
|
||||
IsHighlyCustomized *int64 `gorm:"default:0;" json:"is_highly_customized"` // 是否高度定制 0非 1是(针对客人高度定制只能后台增加如购物车)
|
||||
Status *int64 `gorm:"default:0;" json:"status"` // 0未下单 1已下单
|
||||
IsEffective *int64 `gorm:"default:1;" json:"is_effective"` // 是否有效 0非 1是(针对对购物车下单,此前数据表更失效)
|
||||
Ctime *time.Time `gorm:"default:'0000-00-00 00:00:00';" json:"ctime"` //
|
||||
Utime *time.Time `gorm:"default:'0000-00-00 00:00:00';" json:"utime"` //
|
||||
}
|
||||
|
|
|
@ -12,11 +12,26 @@ func (s *FsShoppingCartModel) FindOne(ctx context.Context, id int64, fields ...s
|
|||
return resp, err
|
||||
}
|
||||
|
||||
// 获取用户购物车指定item
|
||||
func (s *FsShoppingCartModel) FineOneUserCart(ctx context.Context, id, userId int64, fields ...string) (resp *FsShoppingCart, err error) {
|
||||
db := s.db.WithContext(ctx).Where("user_id = ? and id = ?", userId, id)
|
||||
if len(fields) > 0 {
|
||||
db = db.Select(fields[0])
|
||||
}
|
||||
err = db.Take(&resp).Error
|
||||
return resp, err
|
||||
}
|
||||
|
||||
// 创建
|
||||
func (s *FsShoppingCartModel) Create(ctx context.Context, data *FsShoppingCart) error {
|
||||
return s.db.WithContext(ctx).Create(&data).Error
|
||||
}
|
||||
|
||||
// 更新
|
||||
func (s *FsShoppingCartModel) Update(ctx context.Context, id, userId int64, data *FsShoppingCart) error {
|
||||
return s.db.WithContext(ctx).Where("user_id = ? and id = ?", userId, id).Updates(&data).Error
|
||||
}
|
||||
|
||||
// 获取用户购物车数量
|
||||
func (s *FsShoppingCartModel) CountUserCart(ctx context.Context, userId int64) (total int64, err error) {
|
||||
err = s.db.WithContext(ctx).Where("user_id = ?", userId).Limit(1).Count(&total).Error
|
||||
|
|
|
@ -60,6 +60,7 @@ type AllModelsGen struct {
|
|||
FsOrderDetail *FsOrderDetailModel // fs_order_detail 订单详细表
|
||||
FsOrderDetailTemplate *FsOrderDetailTemplateModel // fs_order_detail_template 订单模板详细表
|
||||
FsOrderRemark *FsOrderRemarkModel // fs_order_remark 订单备注表
|
||||
FsOrdersTrade *FsOrdersTradeModel // fs_orders_trade 订单交易记录表
|
||||
FsPay *FsPayModel // fs_pay 支付记录
|
||||
FsPayEvent *FsPayEventModel // fs_pay_event 支付回调事件日志
|
||||
FsProduct *FsProductModel // fs_product 产品表
|
||||
|
@ -167,6 +168,7 @@ func NewAllModels(gdb *gorm.DB) *AllModelsGen {
|
|||
FsOrderDetail: NewFsOrderDetailModel(gdb),
|
||||
FsOrderDetailTemplate: NewFsOrderDetailTemplateModel(gdb),
|
||||
FsOrderRemark: NewFsOrderRemarkModel(gdb),
|
||||
FsOrdersTrade: NewFsOrdersTradeModel(gdb),
|
||||
FsPay: NewFsPayModel(gdb),
|
||||
FsPayEvent: NewFsPayEventModel(gdb),
|
||||
FsProduct: NewFsProductModel(gdb),
|
||||
|
|
|
@ -37,7 +37,7 @@ func NewAddToCartLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddToCa
|
|||
|
||||
func (l *AddToCartLogic) AddToCart(req *types.AddToCartReq, userinfo *auth.UserInfo) (resp *basic.Response) {
|
||||
if !userinfo.IsUser() {
|
||||
return resp.SetStatusWithMessage(basic.CodeUnAuth, "please sign in before add to shopping cart")
|
||||
return resp.SetStatusWithMessage(basic.CodeUnAuth, "please sign in")
|
||||
}
|
||||
//校验参数
|
||||
if err := l.AddToCartParamVerify(req); err != nil {
|
||||
|
|
|
@ -1,8 +1,11 @@
|
|||
package logic
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fusenapi/model/gmodel"
|
||||
"fusenapi/utils/auth"
|
||||
"fusenapi/utils/basic"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"context"
|
||||
|
||||
|
@ -31,10 +34,33 @@ func NewModifyCartPurchaseQuantityLogic(ctx context.Context, svcCtx *svc.Service
|
|||
// }
|
||||
|
||||
func (l *ModifyCartPurchaseQuantityLogic) ModifyCartPurchaseQuantity(req *types.ModifyCartPurchaseQuantityReq, userinfo *auth.UserInfo) (resp *basic.Response) {
|
||||
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
|
||||
// userinfo 传入值时, 一定不为null
|
||||
|
||||
return resp.SetStatus(basic.CodeOK)
|
||||
if !userinfo.IsUser() {
|
||||
return resp.SetStatusWithMessage(basic.CodeUnAuth, "please sign in")
|
||||
}
|
||||
if req.Id <= 0 {
|
||||
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "cart id is required")
|
||||
}
|
||||
if req.PurchaseQuantity <= 0 {
|
||||
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "purchase quantity can not less than 0 or equal to 0")
|
||||
}
|
||||
//查询购物车
|
||||
_, err := l.svcCtx.AllModels.FsShoppingCart.FineOneUserCart(l.ctx, req.Id, userinfo.UserId, "id")
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "the shopping cart is not exists")
|
||||
}
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "system err:failed to get shopping cart info ")
|
||||
}
|
||||
//修改数量
|
||||
err = l.svcCtx.AllModels.FsShoppingCart.Update(l.ctx, req.Id, userinfo.UserId, &gmodel.FsShoppingCart{
|
||||
PurchaseQuantity: &req.PurchaseQuantity,
|
||||
})
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "system err:failed to modify cart purchase quantity")
|
||||
}
|
||||
return resp.SetStatus(basic.CodeOK, "success")
|
||||
}
|
||||
|
||||
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
|
||||
|
|
|
@ -30,8 +30,8 @@ type DeleteCartReq struct {
|
|||
}
|
||||
|
||||
type ModifyCartPurchaseQuantityReq struct {
|
||||
Id int64 `json:"id"` //购物车id
|
||||
Quantity int64 `json:"quantity"` //数量
|
||||
Id int64 `json:"id"` //购物车id
|
||||
PurchaseQuantity int64 `json:"purchase_quantity"` //数量
|
||||
}
|
||||
|
||||
type GetCartsReq struct {
|
||||
|
|
|
@ -48,8 +48,8 @@ type DeleteCartReq {
|
|||
}
|
||||
//修改购物车购买数量
|
||||
type ModifyCartPurchaseQuantityReq {
|
||||
Id int64 `json:"id"` //购物车id
|
||||
Quantity int64 `json:"quantity"` //数量
|
||||
Id int64 `json:"id"` //购物车id
|
||||
PurchaseQuantity int64 `json:"purchase_quantity"` //数量
|
||||
}
|
||||
//获取购物车列表
|
||||
type GetCartsReq {
|
||||
|
|
Loading…
Reference in New Issue
Block a user