Add internal/repository/food.go
This commit is contained in:
105
internal/repository/food.go
Normal file
105
internal/repository/food.go
Normal file
@@ -0,0 +1,105 @@
|
|||||||
|
package repository
|
||||||
|
|
||||||
|
import (
|
||||||
|
"gotest/internal/model"
|
||||||
|
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
// FoodRepository 外卖数据访问层
|
||||||
|
type FoodRepository struct {
|
||||||
|
db *gorm.DB
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewFoodRepository 创建外卖仓库实例
|
||||||
|
func NewFoodRepository(db *gorm.DB) *FoodRepository {
|
||||||
|
return &FoodRepository{db: db}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListCategories 获取餐厅分类列表
|
||||||
|
func (r *FoodRepository) ListCategories() ([]model.RestaurantCategory, error) {
|
||||||
|
var categories []model.RestaurantCategory
|
||||||
|
if err := r.db.Order("sort ASC, id ASC").Find(&categories).Error; err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return categories, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListRestaurants 获取餐厅列表,支持分类过滤和关键词搜索
|
||||||
|
func (r *FoodRepository) ListRestaurants(categoryID uint, keyword string, sort string, page, pageSize int) ([]model.Restaurant, int64, error) {
|
||||||
|
var restaurants []model.Restaurant
|
||||||
|
var total int64
|
||||||
|
|
||||||
|
query := r.db.Where("status = 1")
|
||||||
|
if categoryID > 0 {
|
||||||
|
query = query.Where("category_id = ?", categoryID)
|
||||||
|
}
|
||||||
|
if keyword != "" {
|
||||||
|
query = query.Where("name LIKE ? OR tags LIKE ?", "%"+keyword+"%", "%"+keyword+"%")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := query.Model(&model.Restaurant{}).Count(&total).Error; err != nil {
|
||||||
|
return nil, 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
switch sort {
|
||||||
|
case "rating":
|
||||||
|
query = query.Order("rating DESC")
|
||||||
|
case "sales":
|
||||||
|
query = query.Order("sales DESC")
|
||||||
|
case "delivery_time":
|
||||||
|
query = query.Order("delivery_time ASC")
|
||||||
|
default:
|
||||||
|
query = query.Order("sort ASC, id DESC")
|
||||||
|
}
|
||||||
|
|
||||||
|
if page > 0 && pageSize > 0 {
|
||||||
|
offset := (page - 1) * pageSize
|
||||||
|
query = query.Offset(offset).Limit(pageSize)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := query.Find(&restaurants).Error; err != nil {
|
||||||
|
return nil, 0, err
|
||||||
|
}
|
||||||
|
return restaurants, total, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetRestaurant 根据 ID 获取餐厅详情
|
||||||
|
func (r *FoodRepository) GetRestaurant(id uint) (*model.Restaurant, error) {
|
||||||
|
var restaurant model.Restaurant
|
||||||
|
if err := r.db.First(&restaurant, id).Error; err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &restaurant, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListFeaturedRestaurants 获取推荐餐厅(按销量)
|
||||||
|
func (r *FoodRepository) ListFeaturedRestaurants(limit int) ([]model.Restaurant, error) {
|
||||||
|
var restaurants []model.Restaurant
|
||||||
|
if err := r.db.Where("status = 1").Order("sales DESC").Limit(limit).Find(&restaurants).Error; err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return restaurants, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListMenuCategories 获取餐厅的菜品分类
|
||||||
|
func (r *FoodRepository) ListMenuCategories(restaurantID uint) ([]model.MenuCategory, error) {
|
||||||
|
var categories []model.MenuCategory
|
||||||
|
if err := r.db.Where("restaurant_id = ?", restaurantID).Order("sort ASC, id ASC").Find(&categories).Error; err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return categories, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListMenuItems 获取餐厅的菜品列表,可按菜品分类过滤
|
||||||
|
func (r *FoodRepository) ListMenuItems(restaurantID uint, categoryID uint) ([]model.MenuItem, error) {
|
||||||
|
var items []model.MenuItem
|
||||||
|
query := r.db.Where("restaurant_id = ? AND status = 1", restaurantID)
|
||||||
|
if categoryID > 0 {
|
||||||
|
query = query.Where("category_id = ?", categoryID)
|
||||||
|
}
|
||||||
|
if err := query.Order("sort ASC, id ASC").Find(&items).Error; err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return items, nil
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user