ListPigBatchLogs
This commit is contained in:
@@ -1,12 +1,22 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"time" // 引入 time 包
|
||||
"time"
|
||||
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// PigBatchLogListOptions 定义了查询猪批次日志时的可选参数
|
||||
type PigBatchLogListOptions struct {
|
||||
PigBatchID *uint
|
||||
ChangeType *models.LogChangeType
|
||||
OperatorID *uint
|
||||
StartTime *time.Time // 基于 happened_at 字段
|
||||
EndTime *time.Time // 基于 happened_at 字段
|
||||
OrderBy string // 例如 "happened_at asc"
|
||||
}
|
||||
|
||||
// PigBatchLogRepository 定义了与猪批次日志相关的数据库操作接口。
|
||||
type PigBatchLogRepository interface {
|
||||
// CreateTx 在指定的事务中创建一条新的猪批次日志。
|
||||
@@ -17,6 +27,9 @@ type PigBatchLogRepository interface {
|
||||
|
||||
// GetLastLogByBatchIDTx 在指定的事务中,获取某批次的最后一条日志记录。
|
||||
GetLastLogByBatchIDTx(tx *gorm.DB, batchID uint) (*models.PigBatchLog, error)
|
||||
|
||||
// List 支持分页和过滤的列表查询
|
||||
List(opts PigBatchLogListOptions, page, pageSize int) ([]models.PigBatchLog, int64, error)
|
||||
}
|
||||
|
||||
// gormPigBatchLogRepository 是 PigBatchLogRepository 的 GORM 实现。
|
||||
@@ -29,7 +42,7 @@ func NewGormPigBatchLogRepository(db *gorm.DB) PigBatchLogRepository {
|
||||
return &gormPigBatchLogRepository{db: db}
|
||||
}
|
||||
|
||||
// Create 实现了创建猪批次日志的逻辑。
|
||||
// CreateTx 实现了在事务中创建猪批次日志的逻辑。
|
||||
func (r *gormPigBatchLogRepository) CreateTx(tx *gorm.DB, log *models.PigBatchLog) error {
|
||||
return tx.Create(log).Error
|
||||
}
|
||||
@@ -53,3 +66,46 @@ func (r *gormPigBatchLogRepository) GetLastLogByBatchIDTx(tx *gorm.DB, batchID u
|
||||
}
|
||||
return &log, nil
|
||||
}
|
||||
|
||||
// List 实现了分页和过滤查询猪批次日志的功能
|
||||
func (r *gormPigBatchLogRepository) List(opts PigBatchLogListOptions, page, pageSize int) ([]models.PigBatchLog, int64, error) {
|
||||
if page <= 0 || pageSize <= 0 {
|
||||
return nil, 0, ErrInvalidPagination
|
||||
}
|
||||
|
||||
var results []models.PigBatchLog
|
||||
var total int64
|
||||
|
||||
query := r.db.Model(&models.PigBatchLog{})
|
||||
|
||||
if opts.PigBatchID != nil {
|
||||
query = query.Where("pig_batch_id = ?", *opts.PigBatchID)
|
||||
}
|
||||
if opts.ChangeType != nil {
|
||||
query = query.Where("change_type = ?", *opts.ChangeType)
|
||||
}
|
||||
if opts.OperatorID != nil {
|
||||
query = query.Where("operator_id = ?", *opts.OperatorID)
|
||||
}
|
||||
if opts.StartTime != nil {
|
||||
query = query.Where("happened_at >= ?", *opts.StartTime)
|
||||
}
|
||||
if opts.EndTime != nil {
|
||||
query = query.Where("happened_at <= ?", *opts.EndTime)
|
||||
}
|
||||
|
||||
if err := query.Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
orderBy := "happened_at DESC"
|
||||
if opts.OrderBy != "" {
|
||||
orderBy = opts.OrderBy
|
||||
}
|
||||
query = query.Order(orderBy)
|
||||
|
||||
offset := (page - 1) * pageSize
|
||||
err := query.Limit(pageSize).Offset(offset).Find(&results).Error
|
||||
|
||||
return results, total, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user