修改infra.repository包

This commit is contained in:
2025-11-05 23:00:07 +08:00
parent 97aea66f7c
commit 10b123ab93
25 changed files with 877 additions and 608 deletions

View File

@@ -1,30 +1,33 @@
package repository
import (
"context"
"time"
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
"gorm.io/gorm"
)
// PigBatchRepository 定义了与猪批次相关的数据库操作接口
type PigBatchRepository interface {
CreatePigBatch(batch *models.PigBatch) (*models.PigBatch, error)
CreatePigBatchTx(tx *gorm.DB, batch *models.PigBatch) (*models.PigBatch, error)
GetPigBatchByID(id uint) (*models.PigBatch, error)
GetPigBatchByIDTx(tx *gorm.DB, id uint) (*models.PigBatch, error)
CreatePigBatch(ctx context.Context, batch *models.PigBatch) (*models.PigBatch, error)
CreatePigBatchTx(ctx context.Context, tx *gorm.DB, batch *models.PigBatch) (*models.PigBatch, error)
GetPigBatchByID(ctx context.Context, id uint) (*models.PigBatch, error)
GetPigBatchByIDTx(ctx context.Context, tx *gorm.DB, id uint) (*models.PigBatch, error)
// UpdatePigBatch 更新一个猪批次,返回更新后的批次、受影响的行数和错误
UpdatePigBatch(batch *models.PigBatch) (*models.PigBatch, int64, error)
UpdatePigBatch(ctx context.Context, batch *models.PigBatch) (*models.PigBatch, int64, error)
// DeletePigBatch 根据ID删除一个猪批次返回受影响的行数和错误
DeletePigBatch(id uint) (int64, error)
DeletePigBatchTx(tx *gorm.DB, id uint) (int64, error)
ListPigBatches(isActive *bool) ([]*models.PigBatch, error)
DeletePigBatch(ctx context.Context, id uint) (int64, error)
DeletePigBatchTx(ctx context.Context, tx *gorm.DB, id uint) (int64, error)
ListPigBatches(ctx context.Context, isActive *bool) ([]*models.PigBatch, error)
// ListWeighingBatches 支持分页和过滤的批次称重列表查询
ListWeighingBatches(opts WeighingBatchListOptions, page, pageSize int) ([]models.WeighingBatch, int64, error)
ListWeighingBatches(ctx context.Context, opts WeighingBatchListOptions, page, pageSize int) ([]models.WeighingBatch, int64, error)
// ListWeighingRecords 支持分页和过滤的单次称重记录列表查询
ListWeighingRecords(opts WeighingRecordListOptions, page, pageSize int) ([]models.WeighingRecord, int64, error)
ListWeighingRecords(ctx context.Context, opts WeighingRecordListOptions, page, pageSize int) ([]models.WeighingRecord, int64, error)
}
// WeighingBatchListOptions 定义了查询批次称重记录时的可选参数
@@ -47,35 +50,40 @@ type WeighingRecordListOptions struct {
// gormPigBatchRepository 是 PigBatchRepository 的 GORM 实现
type gormPigBatchRepository struct {
db *gorm.DB
ctx context.Context
db *gorm.DB
}
// NewGormPigBatchRepository 创建一个新的 PigBatchRepository GORM 实现实例
func NewGormPigBatchRepository(db *gorm.DB) PigBatchRepository {
return &gormPigBatchRepository{db: db}
func NewGormPigBatchRepository(ctx context.Context, db *gorm.DB) PigBatchRepository {
return &gormPigBatchRepository{ctx: ctx, db: db}
}
// CreatePigBatch 创建一个新的猪批次
func (r *gormPigBatchRepository) CreatePigBatch(batch *models.PigBatch) (*models.PigBatch, error) {
return r.CreatePigBatchTx(r.db, batch)
func (r *gormPigBatchRepository) CreatePigBatch(ctx context.Context, batch *models.PigBatch) (*models.PigBatch, error) {
repoCtx := logs.AddFuncName(ctx, r.ctx, "CreatePigBatch")
return r.CreatePigBatchTx(repoCtx, r.db, batch)
}
// CreatePigBatchTx 在指定的事务中,创建一个新的猪批次
func (r *gormPigBatchRepository) CreatePigBatchTx(tx *gorm.DB, batch *models.PigBatch) (*models.PigBatch, error) {
if err := tx.Create(batch).Error; err != nil {
func (r *gormPigBatchRepository) CreatePigBatchTx(ctx context.Context, tx *gorm.DB, batch *models.PigBatch) (*models.PigBatch, error) {
repoCtx := logs.AddFuncName(ctx, r.ctx, "CreatePigBatchTx")
if err := tx.WithContext(repoCtx).Create(batch).Error; err != nil {
return nil, err
}
return batch, nil
}
// GetPigBatchByID 根据ID获取单个猪批次
func (r *gormPigBatchRepository) GetPigBatchByID(id uint) (*models.PigBatch, error) {
return r.GetPigBatchByIDTx(r.db, id)
func (r *gormPigBatchRepository) GetPigBatchByID(ctx context.Context, id uint) (*models.PigBatch, error) {
repoCtx := logs.AddFuncName(ctx, r.ctx, "GetPigBatchByID")
return r.GetPigBatchByIDTx(repoCtx, r.db, id)
}
// UpdatePigBatch 更新一个猪批次
func (r *gormPigBatchRepository) UpdatePigBatch(batch *models.PigBatch) (*models.PigBatch, int64, error) {
result := r.db.Model(&models.PigBatch{}).Where("id = ?", batch.ID).Updates(batch)
func (r *gormPigBatchRepository) UpdatePigBatch(ctx context.Context, batch *models.PigBatch) (*models.PigBatch, int64, error) {
repoCtx := logs.AddFuncName(ctx, r.ctx, "UpdatePigBatch")
result := r.db.WithContext(repoCtx).Model(&models.PigBatch{}).Where("id = ?", batch.ID).Updates(batch)
if result.Error != nil {
return nil, 0, result.Error
}
@@ -84,12 +92,14 @@ func (r *gormPigBatchRepository) UpdatePigBatch(batch *models.PigBatch) (*models
}
// DeletePigBatch 根据ID删除一个猪批次 (GORM 会执行软删除)
func (r *gormPigBatchRepository) DeletePigBatch(id uint) (int64, error) {
return r.DeletePigBatchTx(r.db, id)
func (r *gormPigBatchRepository) DeletePigBatch(ctx context.Context, id uint) (int64, error) {
repoCtx := logs.AddFuncName(ctx, r.ctx, "DeletePigBatch")
return r.DeletePigBatchTx(repoCtx, r.db, id)
}
func (r *gormPigBatchRepository) DeletePigBatchTx(tx *gorm.DB, id uint) (int64, error) {
result := tx.Delete(&models.PigBatch{}, id)
func (r *gormPigBatchRepository) DeletePigBatchTx(ctx context.Context, tx *gorm.DB, id uint) (int64, error) {
repoCtx := logs.AddFuncName(ctx, r.ctx, "DeletePigBatchTx")
result := tx.WithContext(repoCtx).Delete(&models.PigBatch{}, id)
if result.Error != nil {
return 0, result.Error
}
@@ -98,9 +108,10 @@ func (r *gormPigBatchRepository) DeletePigBatchTx(tx *gorm.DB, id uint) (int64,
}
// ListPigBatches 批量查询猪批次,支持根据 IsActive 筛选
func (r *gormPigBatchRepository) ListPigBatches(isActive *bool) ([]*models.PigBatch, error) {
func (r *gormPigBatchRepository) ListPigBatches(ctx context.Context, isActive *bool) ([]*models.PigBatch, error) {
repoCtx := logs.AddFuncName(ctx, r.ctx, "ListPigBatches")
var batches []*models.PigBatch
query := r.db.Model(&models.PigBatch{})
query := r.db.WithContext(repoCtx).Model(&models.PigBatch{})
if isActive != nil {
if *isActive {
@@ -119,16 +130,18 @@ func (r *gormPigBatchRepository) ListPigBatches(isActive *bool) ([]*models.PigBa
}
// GetPigBatchByIDTx 在指定的事务中通过ID获取单个猪批次
func (r *gormPigBatchRepository) GetPigBatchByIDTx(tx *gorm.DB, id uint) (*models.PigBatch, error) {
func (r *gormPigBatchRepository) GetPigBatchByIDTx(ctx context.Context, tx *gorm.DB, id uint) (*models.PigBatch, error) {
repoCtx := logs.AddFuncName(ctx, r.ctx, "GetPigBatchByIDTx")
var batch models.PigBatch
if err := tx.First(&batch, id).Error; err != nil {
if err := tx.WithContext(repoCtx).First(&batch, id).Error; err != nil {
return nil, err
}
return &batch, nil
}
// ListWeighingBatches 实现了分页和过滤查询批次称重记录的功能
func (r *gormPigBatchRepository) ListWeighingBatches(opts WeighingBatchListOptions, page, pageSize int) ([]models.WeighingBatch, int64, error) {
func (r *gormPigBatchRepository) ListWeighingBatches(ctx context.Context, opts WeighingBatchListOptions, page, pageSize int) ([]models.WeighingBatch, int64, error) {
repoCtx := logs.AddFuncName(ctx, r.ctx, "ListWeighingBatches")
if page <= 0 || pageSize <= 0 {
return nil, 0, ErrInvalidPagination
}
@@ -136,7 +149,7 @@ func (r *gormPigBatchRepository) ListWeighingBatches(opts WeighingBatchListOptio
var results []models.WeighingBatch
var total int64
query := r.db.Model(&models.WeighingBatch{})
query := r.db.WithContext(repoCtx).Model(&models.WeighingBatch{})
if opts.PigBatchID != nil {
query = query.Where("pig_batch_id = ?", *opts.PigBatchID)
@@ -165,7 +178,8 @@ func (r *gormPigBatchRepository) ListWeighingBatches(opts WeighingBatchListOptio
}
// ListWeighingRecords 实现了分页和过滤查询单次称重记录的功能
func (r *gormPigBatchRepository) ListWeighingRecords(opts WeighingRecordListOptions, page, pageSize int) ([]models.WeighingRecord, int64, error) {
func (r *gormPigBatchRepository) ListWeighingRecords(ctx context.Context, opts WeighingRecordListOptions, page, pageSize int) ([]models.WeighingRecord, int64, error) {
repoCtx := logs.AddFuncName(ctx, r.ctx, "ListWeighingRecords")
if page <= 0 || pageSize <= 0 {
return nil, 0, ErrInvalidPagination
}
@@ -173,7 +187,7 @@ func (r *gormPigBatchRepository) ListWeighingRecords(opts WeighingRecordListOpti
var results []models.WeighingRecord
var total int64
query := r.db.Model(&models.WeighingRecord{})
query := r.db.WithContext(repoCtx).Model(&models.WeighingRecord{})
if opts.WeighingBatchID != nil {
query = query.Where("weighing_batch_id = ?", *opts.WeighingBatchID)