ListWeighingRecords
This commit is contained in:
@@ -22,6 +22,9 @@ type PigBatchRepository interface {
|
||||
|
||||
// ListWeighingBatches 支持分页和过滤的批次称重列表查询
|
||||
ListWeighingBatches(opts WeighingBatchListOptions, page, pageSize int) ([]models.WeighingBatch, int64, error)
|
||||
|
||||
// ListWeighingRecords 支持分页和过滤的单次称重记录列表查询
|
||||
ListWeighingRecords(opts WeighingRecordListOptions, page, pageSize int) ([]models.WeighingRecord, int64, error)
|
||||
}
|
||||
|
||||
// WeighingBatchListOptions 定义了查询批次称重记录时的可选参数
|
||||
@@ -32,6 +35,16 @@ type WeighingBatchListOptions struct {
|
||||
OrderBy string // 例如 "weighing_time asc"
|
||||
}
|
||||
|
||||
// WeighingRecordListOptions 定义了查询单次称重记录时的可选参数
|
||||
type WeighingRecordListOptions struct {
|
||||
WeighingBatchID *uint
|
||||
PenID *uint
|
||||
OperatorID *uint
|
||||
StartTime *time.Time // 基于 weighing_time 字段
|
||||
EndTime *time.Time // 基于 weighing_time 字段
|
||||
OrderBy string // 例如 "weighing_time asc"
|
||||
}
|
||||
|
||||
// gormPigBatchRepository 是 PigBatchRepository 的 GORM 实现
|
||||
type gormPigBatchRepository struct {
|
||||
db *gorm.DB
|
||||
@@ -150,3 +163,46 @@ func (r *gormPigBatchRepository) ListWeighingBatches(opts WeighingBatchListOptio
|
||||
|
||||
return results, total, err
|
||||
}
|
||||
|
||||
// ListWeighingRecords 实现了分页和过滤查询单次称重记录的功能
|
||||
func (r *gormPigBatchRepository) ListWeighingRecords(opts WeighingRecordListOptions, page, pageSize int) ([]models.WeighingRecord, int64, error) {
|
||||
if page <= 0 || pageSize <= 0 {
|
||||
return nil, 0, ErrInvalidPagination
|
||||
}
|
||||
|
||||
var results []models.WeighingRecord
|
||||
var total int64
|
||||
|
||||
query := r.db.Model(&models.WeighingRecord{})
|
||||
|
||||
if opts.WeighingBatchID != nil {
|
||||
query = query.Where("weighing_batch_id = ?", *opts.WeighingBatchID)
|
||||
}
|
||||
if opts.PenID != nil {
|
||||
query = query.Where("pen_id = ?", *opts.PenID)
|
||||
}
|
||||
if opts.OperatorID != nil {
|
||||
query = query.Where("operator_id = ?", *opts.OperatorID)
|
||||
}
|
||||
if opts.StartTime != nil {
|
||||
query = query.Where("weighing_time >= ?", *opts.StartTime)
|
||||
}
|
||||
if opts.EndTime != nil {
|
||||
query = query.Where("weighing_time <= ?", *opts.EndTime)
|
||||
}
|
||||
|
||||
if err := query.Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
orderBy := "weighing_time 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