ListPigSickLogs
This commit is contained in:
@@ -2,11 +2,24 @@ package repository
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// PigSickLogListOptions 定义了查询病猪日志时的可选参数
|
||||
type PigSickLogListOptions struct {
|
||||
PigBatchID *uint
|
||||
PenID *uint
|
||||
Reason *models.PigBatchSickPigReasonType
|
||||
TreatmentLocation *models.PigBatchSickPigTreatmentLocation
|
||||
OperatorID *uint
|
||||
StartTime *time.Time // 基于 happened_at 字段
|
||||
EndTime *time.Time // 基于 happened_at 字段
|
||||
OrderBy string // 例如 "happened_at desc"
|
||||
}
|
||||
|
||||
// PigSickLogRepository 定义了与病猪日志模型相关的数据库操作接口。
|
||||
type PigSickLogRepository interface {
|
||||
// CreatePigSickLog 创建一条新的病猪日志记录
|
||||
@@ -15,6 +28,9 @@ type PigSickLogRepository interface {
|
||||
|
||||
// GetLastLogByBatchTx 在事务中获取指定批次和猪栏的最新一条 PigSickLog 记录
|
||||
GetLastLogByBatchTx(tx *gorm.DB, batchID uint) (*models.PigSickLog, error)
|
||||
|
||||
// ListPigSickLogs 支持分页和过滤的病猪日志列表查询
|
||||
ListPigSickLogs(opts PigSickLogListOptions, page, pageSize int) ([]models.PigSickLog, int64, error)
|
||||
}
|
||||
|
||||
// gormPigSickLogRepository 是 PigSickLogRepository 接口的 GORM 实现。
|
||||
@@ -51,3 +67,52 @@ func (r *gormPigSickLogRepository) GetLastLogByBatchTx(tx *gorm.DB, batchID uint
|
||||
}
|
||||
return &lastLog, nil
|
||||
}
|
||||
|
||||
// ListPigSickLogs 实现了分页和过滤查询病猪日志的功能
|
||||
func (r *gormPigSickLogRepository) ListPigSickLogs(opts PigSickLogListOptions, page, pageSize int) ([]models.PigSickLog, int64, error) {
|
||||
if page <= 0 || pageSize <= 0 {
|
||||
return nil, 0, ErrInvalidPagination
|
||||
}
|
||||
|
||||
var results []models.PigSickLog
|
||||
var total int64
|
||||
|
||||
query := r.db.Model(&models.PigSickLog{})
|
||||
|
||||
if opts.PigBatchID != nil {
|
||||
query = query.Where("pig_batch_id = ?", *opts.PigBatchID)
|
||||
}
|
||||
if opts.PenID != nil {
|
||||
query = query.Where("pen_id = ?", *opts.PenID)
|
||||
}
|
||||
if opts.Reason != nil {
|
||||
query = query.Where("reason = ?", *opts.Reason)
|
||||
}
|
||||
if opts.TreatmentLocation != nil {
|
||||
query = query.Where("treatment_location = ?", *opts.TreatmentLocation)
|
||||
}
|
||||
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