移动位置
This commit is contained in:
@@ -1,26 +0,0 @@
|
|||||||
package repository
|
|
||||||
|
|
||||||
import (
|
|
||||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
|
||||||
"gorm.io/gorm"
|
|
||||||
)
|
|
||||||
|
|
||||||
// MedicationLogRepository 定义了与群体用药日志模型相关的数据库操作接口。
|
|
||||||
type MedicationLogRepository interface {
|
|
||||||
CreateMedicationLog(log *models.MedicationLog) error
|
|
||||||
}
|
|
||||||
|
|
||||||
// gormMedicationLogRepository 是 MedicationLogRepository 接口的 GORM 实现。
|
|
||||||
type gormMedicationLogRepository struct {
|
|
||||||
db *gorm.DB
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewGormMedicationLogRepository 创建一个新的 MedicationLogRepository GORM 实现实例。
|
|
||||||
func NewGormMedicationLogRepository(db *gorm.DB) MedicationLogRepository {
|
|
||||||
return &gormMedicationLogRepository{db: db}
|
|
||||||
}
|
|
||||||
|
|
||||||
// CreateMedicationLog 创建一条新的群体用药日志记录
|
|
||||||
func (r *gormMedicationLogRepository) CreateMedicationLog(log *models.MedicationLog) error {
|
|
||||||
return r.db.Create(log).Error
|
|
||||||
}
|
|
||||||
@@ -13,28 +13,34 @@ type MedicationLogListOptions struct {
|
|||||||
MedicationID *uint
|
MedicationID *uint
|
||||||
Reason *models.MedicationReasonType
|
Reason *models.MedicationReasonType
|
||||||
OperatorID *uint
|
OperatorID *uint
|
||||||
StartTime *time.Time // 基于 happened_at 字段
|
StartTime *time.Time
|
||||||
EndTime *time.Time // 基于 happened_at 字段
|
EndTime *time.Time
|
||||||
OrderBy string // 例如 "happened_at asc"
|
OrderBy string // 例如 "happened_at desc"
|
||||||
}
|
}
|
||||||
|
|
||||||
// MedicationRepository 定义了与药品相关的数据库操作接口
|
// MedicationLogRepository 定义了与群体用药日志模型相关的数据库操作接口。
|
||||||
type MedicationRepository interface {
|
type MedicationLogRepository interface {
|
||||||
|
CreateMedicationLog(log *models.MedicationLog) error
|
||||||
ListMedicationLogs(opts MedicationLogListOptions, page, pageSize int) ([]models.MedicationLog, int64, error)
|
ListMedicationLogs(opts MedicationLogListOptions, page, pageSize int) ([]models.MedicationLog, int64, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// gormMedicationRepository 是 MedicationRepository 的 GORM 实现
|
// gormMedicationLogRepository 是 MedicationLogRepository 接口的 GORM 实现。
|
||||||
type gormMedicationRepository struct {
|
type gormMedicationLogRepository struct {
|
||||||
db *gorm.DB
|
db *gorm.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewGormMedicationRepository 创建一个新的 MedicationRepository GORM 实现
|
// NewGormMedicationLogRepository 创建一个新的 MedicationLogRepository GORM 实现实例。
|
||||||
func NewGormMedicationRepository(db *gorm.DB) MedicationRepository {
|
func NewGormMedicationLogRepository(db *gorm.DB) MedicationLogRepository {
|
||||||
return &gormMedicationRepository{db: db}
|
return &gormMedicationLogRepository{db: db}
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateMedicationLog 创建一条新的群体用药日志记录
|
||||||
|
func (r *gormMedicationLogRepository) CreateMedicationLog(log *models.MedicationLog) error {
|
||||||
|
return r.db.Create(log).Error
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListMedicationLogs 实现了分页和过滤查询用药记录的功能
|
// ListMedicationLogs 实现了分页和过滤查询用药记录的功能
|
||||||
func (r *gormMedicationRepository) ListMedicationLogs(opts MedicationLogListOptions, page, pageSize int) ([]models.MedicationLog, int64, error) {
|
func (r *gormMedicationLogRepository) ListMedicationLogs(opts MedicationLogListOptions, page, pageSize int) ([]models.MedicationLog, int64, error) {
|
||||||
if page <= 0 || pageSize <= 0 {
|
if page <= 0 || pageSize <= 0 {
|
||||||
return nil, 0, ErrInvalidPagination
|
return nil, 0, ErrInvalidPagination
|
||||||
}
|
}
|
||||||
@@ -71,7 +77,7 @@ func (r *gormMedicationRepository) ListMedicationLogs(opts MedicationLogListOpti
|
|||||||
if opts.OrderBy != "" {
|
if opts.OrderBy != "" {
|
||||||
orderBy = opts.OrderBy
|
orderBy = opts.OrderBy
|
||||||
}
|
}
|
||||||
query = query.Order(orderBy).Preload("Medication")
|
query = query.Order(orderBy)
|
||||||
|
|
||||||
offset := (page - 1) * pageSize
|
offset := (page - 1) * pageSize
|
||||||
err := query.Limit(pageSize).Offset(offset).Find(&results).Error
|
err := query.Limit(pageSize).Offset(offset).Find(&results).Error
|
||||||
Reference in New Issue
Block a user