package gmodel import ( "context" ) type RelaFsShoppingCart struct { FsShoppingCart ShoppingCartProduct *FsProduct `json:"shopping_cart_product" gorm:"foreignkey:product_id;references:id"` 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"` } // 获取单个 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 } // 创建 func (s *FsShoppingCartModel) Create(ctx context.Context, data *FsShoppingCart) error { return s.db.WithContext(ctx).Model(&FsShoppingCart{}).Create(&data).Error } // 删除 func (s *FsShoppingCartModel) Delete(ctx context.Context, id, userId int64) error { 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 { return s.db.WithContext(ctx).Model(&FsShoppingCart{}).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).Model(&FsShoppingCart{}).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).Model(&FsShoppingCart{}).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 } // 获取用户所有的购物车 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) { db := s.db.WithContext(ctx).Model(&FsShoppingCart{}) if req.UserId > 0 { db = db.Where("user_id = ?", req.UserId) } if len(req.Ids) > 0 { db = db.Where("id in (?)", req.Ids) } 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 }