2025-10-03 23:42:14 +08:00
|
|
|
|
package repository
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
|
|
|
|
|
"gorm.io/gorm"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// PigBatchRepository 定义了与猪批次相关的数据库操作接口
|
|
|
|
|
|
type PigBatchRepository interface {
|
|
|
|
|
|
CreatePigBatch(batch *models.PigBatch) (*models.PigBatch, error)
|
|
|
|
|
|
GetPigBatchByID(id uint) (*models.PigBatch, error)
|
2025-10-05 17:30:39 +08:00
|
|
|
|
GetPigBatchByIDTx(tx *gorm.DB, id uint) (*models.PigBatch, error)
|
2025-10-04 00:58:29 +08:00
|
|
|
|
// UpdatePigBatch 更新一个猪批次,返回更新后的批次、受影响的行数和错误
|
|
|
|
|
|
UpdatePigBatch(batch *models.PigBatch) (*models.PigBatch, int64, error)
|
|
|
|
|
|
// DeletePigBatch 根据ID删除一个猪批次,返回受影响的行数和错误
|
|
|
|
|
|
DeletePigBatch(id uint) (int64, error)
|
2025-10-03 23:42:14 +08:00
|
|
|
|
ListPigBatches(isActive *bool) ([]*models.PigBatch, error)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// gormPigBatchRepository 是 PigBatchRepository 的 GORM 实现
|
|
|
|
|
|
type gormPigBatchRepository struct {
|
|
|
|
|
|
db *gorm.DB
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// NewGormPigBatchRepository 创建一个新的 PigBatchRepository GORM 实现实例
|
|
|
|
|
|
func NewGormPigBatchRepository(db *gorm.DB) PigBatchRepository {
|
|
|
|
|
|
return &gormPigBatchRepository{db: db}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// CreatePigBatch 创建一个新的猪批次
|
|
|
|
|
|
func (r *gormPigBatchRepository) CreatePigBatch(batch *models.PigBatch) (*models.PigBatch, error) {
|
|
|
|
|
|
if err := r.db.Create(batch).Error; err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
return batch, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// GetPigBatchByID 根据ID获取单个猪批次
|
|
|
|
|
|
func (r *gormPigBatchRepository) GetPigBatchByID(id uint) (*models.PigBatch, error) {
|
|
|
|
|
|
var batch models.PigBatch
|
|
|
|
|
|
if err := r.db.First(&batch, id).Error; err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
return &batch, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// UpdatePigBatch 更新一个猪批次
|
2025-10-04 00:58:29 +08:00
|
|
|
|
func (r *gormPigBatchRepository) UpdatePigBatch(batch *models.PigBatch) (*models.PigBatch, int64, error) {
|
2025-10-03 23:42:14 +08:00
|
|
|
|
result := r.db.Model(&models.PigBatch{}).Where("id = ?", batch.ID).Updates(batch)
|
|
|
|
|
|
if result.Error != nil {
|
2025-10-04 00:58:29 +08:00
|
|
|
|
return nil, 0, result.Error
|
2025-10-03 23:42:14 +08:00
|
|
|
|
}
|
2025-10-04 00:58:29 +08:00
|
|
|
|
// 返回更新后的批次、受影响的行数和错误
|
|
|
|
|
|
return batch, result.RowsAffected, nil
|
2025-10-03 23:42:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// DeletePigBatch 根据ID删除一个猪批次 (GORM 会执行软删除)
|
2025-10-04 00:58:29 +08:00
|
|
|
|
func (r *gormPigBatchRepository) DeletePigBatch(id uint) (int64, error) {
|
2025-10-03 23:42:14 +08:00
|
|
|
|
result := r.db.Delete(&models.PigBatch{}, id)
|
|
|
|
|
|
if result.Error != nil {
|
2025-10-04 00:58:29 +08:00
|
|
|
|
return 0, result.Error
|
2025-10-03 23:42:14 +08:00
|
|
|
|
}
|
2025-10-04 00:58:29 +08:00
|
|
|
|
// 返回受影响的行数和错误
|
|
|
|
|
|
return result.RowsAffected, nil
|
2025-10-03 23:42:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ListPigBatches 批量查询猪批次,支持根据 IsActive 筛选
|
|
|
|
|
|
func (r *gormPigBatchRepository) ListPigBatches(isActive *bool) ([]*models.PigBatch, error) {
|
|
|
|
|
|
var batches []*models.PigBatch
|
|
|
|
|
|
query := r.db.Model(&models.PigBatch{})
|
|
|
|
|
|
|
|
|
|
|
|
if isActive != nil {
|
|
|
|
|
|
if *isActive {
|
|
|
|
|
|
// 查询活跃的批次:状态不是已出售或已归档
|
|
|
|
|
|
query = query.Where("status NOT IN (?) ", []models.PigBatchStatus{models.BatchStatusSold, models.BatchStatusArchived})
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// 查询非活跃的批次:状态是已出售或已归档
|
|
|
|
|
|
query = query.Where("status IN (?) ", []models.PigBatchStatus{models.BatchStatusSold, models.BatchStatusArchived})
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if err := query.Find(&batches).Error; err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
return batches, nil
|
|
|
|
|
|
}
|
2025-10-05 17:30:39 +08:00
|
|
|
|
|
|
|
|
|
|
// GetPigBatchByIDTx 在指定的事务中,通过ID获取单个猪批次
|
|
|
|
|
|
func (r *gormPigBatchRepository) GetPigBatchByIDTx(tx *gorm.DB, id uint) (*models.PigBatch, error) {
|
|
|
|
|
|
var batch models.PigBatch
|
|
|
|
|
|
if err := tx.First(&batch, id).Error; err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
return &batch, nil
|
|
|
|
|
|
}
|