85 lines
2.6 KiB
Go
85 lines
2.6 KiB
Go
// Package repository 提供数据访问层实现
|
|
// 包含各种数据实体的仓库接口和实现
|
|
package repository
|
|
|
|
import (
|
|
"git.huangwc.com/pig/pig-farm-controller/internal/model"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// OperationHistoryRepo 操作历史仓库接口
|
|
type OperationHistoryRepo interface {
|
|
// Create 创建操作历史记录
|
|
Create(history *model.OperationHistory) error
|
|
|
|
// FindByUserID 根据用户ID查找操作历史记录
|
|
FindByUserID(userID uint) ([]*model.OperationHistory, error)
|
|
|
|
// FindByID 根据ID查找操作历史记录
|
|
FindByID(id uint) (*model.OperationHistory, error)
|
|
|
|
// List 获取操作历史记录列表(分页)
|
|
List(offset, limit int) ([]*model.OperationHistory, error)
|
|
|
|
// ListByUserID 根据用户ID获取操作历史记录列表(分页)
|
|
ListByUserID(userID uint, offset, limit int) ([]*model.OperationHistory, error)
|
|
}
|
|
|
|
// operationHistoryRepo 操作历史仓库实现
|
|
type operationHistoryRepo struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewOperationHistoryRepo 创建操作历史仓库实例
|
|
func NewOperationHistoryRepo(db *gorm.DB) OperationHistoryRepo {
|
|
return &operationHistoryRepo{
|
|
db: db,
|
|
}
|
|
}
|
|
|
|
// Create 创建操作历史记录
|
|
func (r *operationHistoryRepo) Create(history *model.OperationHistory) error {
|
|
result := r.db.Create(history)
|
|
return result.Error
|
|
}
|
|
|
|
// FindByUserID 根据用户ID查找操作历史记录
|
|
func (r *operationHistoryRepo) FindByUserID(userID uint) ([]*model.OperationHistory, error) {
|
|
var histories []*model.OperationHistory
|
|
result := r.db.Where("user_id = ?", userID).Order("created_at DESC").Find(&histories)
|
|
if result.Error != nil {
|
|
return nil, result.Error
|
|
}
|
|
return histories, nil
|
|
}
|
|
|
|
// FindByID 根据ID查找操作历史记录
|
|
func (r *operationHistoryRepo) FindByID(id uint) (*model.OperationHistory, error) {
|
|
var history model.OperationHistory
|
|
result := r.db.First(&history, id)
|
|
if result.Error != nil {
|
|
return nil, result.Error
|
|
}
|
|
return &history, nil
|
|
}
|
|
|
|
// List 获取操作历史记录列表(分页)
|
|
func (r *operationHistoryRepo) List(offset, limit int) ([]*model.OperationHistory, error) {
|
|
var histories []*model.OperationHistory
|
|
result := r.db.Offset(offset).Limit(limit).Order("created_at DESC").Find(&histories)
|
|
if result.Error != nil {
|
|
return nil, result.Error
|
|
}
|
|
return histories, nil
|
|
}
|
|
|
|
// ListByUserID 根据用户ID获取操作历史记录列表(分页)
|
|
func (r *operationHistoryRepo) ListByUserID(userID uint, offset, limit int) ([]*model.OperationHistory, error) {
|
|
var histories []*model.OperationHistory
|
|
result := r.db.Where("user_id = ?", userID).Offset(offset).Limit(limit).Order("created_at DESC").Find(&histories)
|
|
if result.Error != nil {
|
|
return nil, result.Error
|
|
}
|
|
return histories, nil
|
|
}
|