fusenapi/model/gmodel/fs_shopping_cart_logic.go
2023-09-14 15:20:37 +08:00

54 lines
1.5 KiB
Go

package gmodel
import "context"
// 获取单个
func (s *FsShoppingCartModel) FindOne(ctx context.Context, id int64, fields ...string) (resp *FsShoppingCart, err error) {
db := s.db.WithContext(ctx).Where("id = ?", 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) CountUserCart(ctx context.Context, userId int64) (total int64, err error) {
err = s.db.WithContext(ctx).Where("user_id = ?", userId).Limit(1).Count(&total).Error
return total, err
}
// 获取多个
func (s *FsShoppingCartModel) GetAllByIds(ctx context.Context, ids []int64, sort string, fields ...string) (resp []FsShoppingCart, err error) {
if len(ids) == 0 {
return
}
db := s.db.WithContext(ctx).Where("id in (?)", ids)
if len(fields) > 0 {
db = db.Select(fields[0])
}
if sort != "" {
db = db.Order(sort)
}
err = db.Find(&resp).Error
return resp, err
}
// 获取用户所有的购物车
func (s *FsShoppingCartModel) GetUserAllCarts(ctx context.Context, userId int64, sort string, fields ...string) (resp []FsShoppingCart, err error) {
db := s.db.WithContext(ctx).Where("user_id = ?", userId)
if len(fields) > 0 {
db = db.Select(fields[0])
}
if sort != "" {
db = db.Order(sort)
}
err = db.Find(&resp).Error
return resp, err
}