fusenapi/model/gmodel/fs_shopping_cart_logic.go

69 lines
2.0 KiB
Go
Raw Normal View History

2023-09-13 08:06:05 +00:00
package gmodel
2023-09-14 06:11:22 +00:00
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
}
// 获取用户购物车指定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
}
2023-09-14 07:20:37 +00:00
// 创建
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
2023-09-14 07:20:37 +00:00
}
// 获取用户购物车数量
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
}
2023-09-14 06:11:22 +00:00
// 获取多个
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
}