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