fusenapi/model/gmodel/fs_shopping_cart_logic.go

103 lines
3.3 KiB
Go
Raw Normal View History

2023-09-13 08:06:05 +00:00
package gmodel
2023-09-14 06:11:22 +00:00
2023-09-15 09:58:45 +00:00
import (
"context"
)
type RelaFsShoppingCart struct {
FsShoppingCart
2023-09-18 07:39:12 +00:00
ShoppingCartProduct *RelaFsProduct `json:"shopping_cart_product" gorm:"foreignkey:product_id;references:id"`
2023-09-15 09:58:45 +00:00
ShoppingCartProductPriceList []*FsProductPrice `json:"shopping_cart_product_price_list" gorm:"foreignkey:product_id;references:product_id"`
ShoppingCartProductModel3dList []*FsProductModel3d `json:"shopping_cart_product_model3d_list" gorm:"foreignkey:product_id;references:product_id"`
}
2023-09-14 06:11:22 +00:00
// 获取单个
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 {
2023-09-15 06:19:14 +00:00
return s.db.WithContext(ctx).Model(&FsShoppingCart{}).Create(&data).Error
}
2023-09-15 02:39:08 +00:00
// 删除
func (s *FsShoppingCartModel) Delete(ctx context.Context, id, userId int64) error {
2023-09-15 06:19:14 +00:00
return s.db.WithContext(ctx).Model(&FsShoppingCart{}).Where("user_id = ? and id = ?", userId, id).Delete(&FsShoppingCart{}).Error
}
// 更新
func (s *FsShoppingCartModel) Update(ctx context.Context, id, userId int64, data *FsShoppingCart) error {
2023-09-15 06:19:14 +00:00
return s.db.WithContext(ctx).Model(&FsShoppingCart{}).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) {
2023-09-15 06:19:14 +00:00
err = s.db.WithContext(ctx).Model(&FsShoppingCart{}).Where("user_id = ?", userId).Limit(1).Count(&total).Error
2023-09-14 07:20:37 +00:00
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
}
2023-09-15 06:19:14 +00:00
db := s.db.WithContext(ctx).Model(&FsShoppingCart{}).Where("id in (?)", ids)
2023-09-14 06:11:22 +00:00
if len(fields) > 0 {
db = db.Select(fields[0])
}
if sort != "" {
db = db.Order(sort)
}
err = db.Find(&resp).Error
return resp, err
}
// 获取用户所有的购物车
2023-09-14 10:33:29 +00:00
type GetAllCartsByParamReq struct {
Ids []int64 //id集合
UserId int64 //用户id
Fields string //筛选的字段
Sort string //排序
Page int //当前页
Limit int //每页数量
}
func (s *FsShoppingCartModel) GetAllCartsByParam(ctx context.Context, req GetAllCartsByParamReq) (resp []FsShoppingCart, total int64, err error) {
2023-09-15 06:19:14 +00:00
db := s.db.WithContext(ctx).Model(&FsShoppingCart{})
2023-09-14 10:33:29 +00:00
if req.UserId > 0 {
db = db.Where("user_id = ?", req.UserId)
2023-09-14 06:11:22 +00:00
}
2023-09-14 10:33:29 +00:00
if len(req.Ids) > 0 {
db = db.Where("id in (?)", req.Ids)
2023-09-14 06:11:22 +00:00
}
2023-09-14 10:33:29 +00:00
if req.Fields != "" {
db = db.Select(req.Fields)
}
if req.Sort != "" {
db = db.Order(req.Sort)
}
//查询数量
if err = db.Limit(1).Count(&total).Error; err != nil {
return nil, 0, err
}
offset := (req.Page - 1) * req.Limit
err = db.Offset(offset).Limit(req.Limit).Find(&resp).Error
return resp, total, err
2023-09-14 06:11:22 +00:00
}