Files
pig-farm-controller/internal/infra/repository/pig_pen_repository.go

111 lines
4.4 KiB
Go
Raw Normal View History

2025-10-05 17:42:27 +08:00
package repository
import (
2025-11-05 23:00:07 +08:00
"context"
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
2025-10-05 17:42:27 +08:00
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
2025-11-05 23:00:07 +08:00
2025-10-05 17:42:27 +08:00
"gorm.io/gorm"
)
// PigPenRepository 定义了与猪栏模型相关的数据库操作接口。
type PigPenRepository interface {
2025-11-05 23:00:07 +08:00
CreatePen(ctx context.Context, pen *models.Pen) error
2025-10-05 17:42:27 +08:00
// GetPenByID 根据ID获取单个猪栏 (非事务性)
2025-11-05 23:00:07 +08:00
GetPenByID(ctx context.Context, id uint) (*models.Pen, error)
2025-10-05 17:42:27 +08:00
// GetPenByIDTx 根据ID获取单个猪栏 (事务性)
2025-11-05 23:00:07 +08:00
GetPenByIDTx(ctx context.Context, tx *gorm.DB, id uint) (*models.Pen, error)
ListPens(ctx context.Context) ([]models.Pen, error)
2025-10-05 17:42:27 +08:00
// UpdatePen 更新一个猪栏,返回受影响的行数和错误
2025-11-05 23:00:07 +08:00
UpdatePen(ctx context.Context, pen *models.Pen) (int64, error)
2025-10-05 17:42:27 +08:00
// DeletePen 根据ID删除一个猪栏返回受影响的行数和错误
2025-11-05 23:00:07 +08:00
DeletePen(ctx context.Context, id uint) (int64, error)
2025-10-05 18:00:50 +08:00
// GetPensByBatchIDTx 根据批次ID获取所有关联的猪栏 (事务性)
2025-11-05 23:00:07 +08:00
GetPensByBatchIDTx(ctx context.Context, tx *gorm.DB, batchID uint) ([]*models.Pen, error)
2025-10-05 18:00:50 +08:00
// UpdatePenFieldsTx 更新猪栏的指定字段 (事务性)
2025-11-05 23:00:07 +08:00
UpdatePenFieldsTx(ctx context.Context, tx *gorm.DB, penID uint, updates map[string]interface{}) error
2025-10-05 17:42:27 +08:00
}
2025-10-05 18:00:50 +08:00
// gormPigPenRepository 是 PigPenRepository 接口的 GORM 实现。
type gormPigPenRepository struct {
2025-11-05 23:00:07 +08:00
ctx context.Context
db *gorm.DB
2025-10-05 17:42:27 +08:00
}
2025-10-05 18:00:50 +08:00
// NewGormPigPenRepository 创建一个新的 PigPenRepository GORM 实现实例。
2025-11-05 23:00:07 +08:00
func NewGormPigPenRepository(ctx context.Context, db *gorm.DB) PigPenRepository {
return &gormPigPenRepository{ctx: ctx, db: db}
2025-10-05 17:42:27 +08:00
}
// CreatePen 创建一个新的猪栏
2025-11-05 23:00:07 +08:00
func (r *gormPigPenRepository) CreatePen(ctx context.Context, pen *models.Pen) error {
repoCtx := logs.AddFuncName(ctx, r.ctx, "CreatePen")
return r.db.WithContext(repoCtx).Create(pen).Error
2025-10-05 17:42:27 +08:00
}
// GetPenByID 根据ID获取单个猪栏 (非事务性)
2025-11-05 23:00:07 +08:00
func (r *gormPigPenRepository) GetPenByID(ctx context.Context, id uint) (*models.Pen, error) {
repoCtx := logs.AddFuncName(ctx, r.ctx, "GetPenByID")
return r.GetPenByIDTx(repoCtx, r.db, id) // 非Tx方法直接调用Tx方法
2025-10-05 18:00:50 +08:00
}
// GetPenByIDTx 在指定的事务中通过ID获取单个猪栏信息。
2025-11-05 23:00:07 +08:00
func (r *gormPigPenRepository) GetPenByIDTx(ctx context.Context, tx *gorm.DB, id uint) (*models.Pen, error) {
repoCtx := logs.AddFuncName(ctx, r.ctx, "GetPenByIDTx")
2025-10-05 17:42:27 +08:00
var pen models.Pen
2025-11-05 23:00:07 +08:00
if err := tx.WithContext(repoCtx).First(&pen, id).Error; err != nil {
2025-10-05 17:42:27 +08:00
return nil, err
}
return &pen, nil
}
// ListPens 列出所有猪栏
2025-11-05 23:00:07 +08:00
func (r *gormPigPenRepository) ListPens(ctx context.Context) ([]models.Pen, error) {
repoCtx := logs.AddFuncName(ctx, r.ctx, "ListPens")
2025-10-05 17:42:27 +08:00
var pens []models.Pen
2025-11-05 23:00:07 +08:00
if err := r.db.WithContext(repoCtx).Find(&pens).Error; err != nil {
2025-10-05 17:42:27 +08:00
return nil, err
}
return pens, nil
}
// UpdatePen 更新一个猪栏,返回受影响的行数和错误
2025-11-05 23:00:07 +08:00
func (r *gormPigPenRepository) UpdatePen(ctx context.Context, pen *models.Pen) (int64, error) {
repoCtx := logs.AddFuncName(ctx, r.ctx, "UpdatePen")
result := r.db.WithContext(repoCtx).Model(&models.Pen{}).Where("id = ?", pen.ID).Updates(pen)
2025-10-05 17:42:27 +08:00
if result.Error != nil {
return 0, result.Error
}
return result.RowsAffected, nil
}
// DeletePen 根据ID删除一个猪栏返回受影响的行数和错误
2025-11-05 23:00:07 +08:00
func (r *gormPigPenRepository) DeletePen(ctx context.Context, id uint) (int64, error) {
repoCtx := logs.AddFuncName(ctx, r.ctx, "DeletePen")
result := r.db.WithContext(repoCtx).Delete(&models.Pen{}, id)
2025-10-05 17:42:27 +08:00
if result.Error != nil {
return 0, result.Error
}
return result.RowsAffected, nil
}
2025-10-05 18:00:50 +08:00
// GetPensByBatchIDTx 在指定的事务中,获取一个猪群当前关联的所有猪栏。
2025-11-05 23:00:07 +08:00
func (r *gormPigPenRepository) GetPensByBatchIDTx(ctx context.Context, tx *gorm.DB, batchID uint) ([]*models.Pen, error) {
repoCtx := logs.AddFuncName(ctx, r.ctx, "GetPensByBatchIDTx")
2025-10-05 18:00:50 +08:00
var pens []*models.Pen
2025-10-05 17:42:27 +08:00
// 注意PigBatchID 是指针类型,需要处理 nil 值
2025-11-05 23:00:07 +08:00
result := tx.WithContext(repoCtx).Where("pig_batch_id = ?", batchID).Find(&pens)
2025-10-05 17:42:27 +08:00
if result.Error != nil {
return nil, result.Error
}
return pens, nil
}
2025-10-05 18:00:50 +08:00
// UpdatePenFieldsTx 在指定的事务中,更新一个猪栏的指定字段。
2025-11-05 23:00:07 +08:00
func (r *gormPigPenRepository) UpdatePenFieldsTx(ctx context.Context, tx *gorm.DB, penID uint, updates map[string]interface{}) error {
repoCtx := logs.AddFuncName(ctx, r.ctx, "UpdatePenFieldsTx")
result := tx.WithContext(repoCtx).Model(&models.Pen{}).Where("id = ?", penID).Updates(updates)
2025-10-05 17:42:27 +08:00
return result.Error
}