修改infra.repository包
This commit is contained in:
@@ -1,9 +1,12 @@
|
||||
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"
|
||||
)
|
||||
|
||||
@@ -31,40 +34,44 @@ type PigSaleListOptions struct {
|
||||
// 领域服务通过此接口与数据层交互,实现解耦。
|
||||
type PigTradeRepository interface {
|
||||
// CreatePigSaleTx 在数据库中创建一条猪只销售记录。
|
||||
CreatePigSaleTx(tx *gorm.DB, sale *models.PigSale) error
|
||||
CreatePigSaleTx(ctx context.Context, tx *gorm.DB, sale *models.PigSale) error
|
||||
|
||||
// CreatePigPurchaseTx 在数据库中创建一条猪只采购记录。
|
||||
CreatePigPurchaseTx(tx *gorm.DB, purchase *models.PigPurchase) error
|
||||
CreatePigPurchaseTx(ctx context.Context, tx *gorm.DB, purchase *models.PigPurchase) error
|
||||
|
||||
// ListPigPurchases 支持分页和过滤的猪只采购记录列表查询
|
||||
ListPigPurchases(opts PigPurchaseListOptions, page, pageSize int) ([]models.PigPurchase, int64, error)
|
||||
ListPigPurchases(ctx context.Context, opts PigPurchaseListOptions, page, pageSize int) ([]models.PigPurchase, int64, error)
|
||||
|
||||
// ListPigSales 支持分页和过滤的猪只销售记录列表查询
|
||||
ListPigSales(opts PigSaleListOptions, page, pageSize int) ([]models.PigSale, int64, error)
|
||||
ListPigSales(ctx context.Context, opts PigSaleListOptions, page, pageSize int) ([]models.PigSale, int64, error)
|
||||
}
|
||||
|
||||
// gormPigTradeRepository 是 PigTradeRepository 接口的 GORM 实现。
|
||||
type gormPigTradeRepository struct {
|
||||
db *gorm.DB
|
||||
ctx context.Context
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
// NewGormPigTradeRepository 创建一个新的 PigTradeRepository GORM 实现实例。
|
||||
func NewGormPigTradeRepository(db *gorm.DB) PigTradeRepository {
|
||||
return &gormPigTradeRepository{db: db}
|
||||
func NewGormPigTradeRepository(ctx context.Context, db *gorm.DB) PigTradeRepository {
|
||||
return &gormPigTradeRepository{ctx: ctx, db: db}
|
||||
}
|
||||
|
||||
// CreatePigSaleTx 实现了在数据库中创建猪只销售记录的逻辑。
|
||||
func (r *gormPigTradeRepository) CreatePigSaleTx(tx *gorm.DB, sale *models.PigSale) error {
|
||||
return tx.Create(sale).Error
|
||||
func (r *gormPigTradeRepository) CreatePigSaleTx(ctx context.Context, tx *gorm.DB, sale *models.PigSale) error {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "CreatePigSaleTx")
|
||||
return tx.WithContext(repoCtx).Create(sale).Error
|
||||
}
|
||||
|
||||
// CreatePigPurchaseTx 实现了在数据库中创建猪只采购记录的逻辑。
|
||||
func (r *gormPigTradeRepository) CreatePigPurchaseTx(tx *gorm.DB, purchase *models.PigPurchase) error {
|
||||
return tx.Create(purchase).Error
|
||||
func (r *gormPigTradeRepository) CreatePigPurchaseTx(ctx context.Context, tx *gorm.DB, purchase *models.PigPurchase) error {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "CreatePigPurchaseTx")
|
||||
return tx.WithContext(repoCtx).Create(purchase).Error
|
||||
}
|
||||
|
||||
// ListPigPurchases 实现了分页和过滤查询猪只采购记录的功能
|
||||
func (r *gormPigTradeRepository) ListPigPurchases(opts PigPurchaseListOptions, page, pageSize int) ([]models.PigPurchase, int64, error) {
|
||||
func (r *gormPigTradeRepository) ListPigPurchases(ctx context.Context, opts PigPurchaseListOptions, page, pageSize int) ([]models.PigPurchase, int64, error) {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "ListPigPurchases")
|
||||
if page <= 0 || pageSize <= 0 {
|
||||
return nil, 0, ErrInvalidPagination
|
||||
}
|
||||
@@ -72,7 +79,7 @@ func (r *gormPigTradeRepository) ListPigPurchases(opts PigPurchaseListOptions, p
|
||||
var results []models.PigPurchase
|
||||
var total int64
|
||||
|
||||
query := r.db.Model(&models.PigPurchase{})
|
||||
query := r.db.WithContext(repoCtx).Model(&models.PigPurchase{})
|
||||
|
||||
if opts.PigBatchID != nil {
|
||||
query = query.Where("pig_batch_id = ?", *opts.PigBatchID)
|
||||
@@ -107,7 +114,8 @@ func (r *gormPigTradeRepository) ListPigPurchases(opts PigPurchaseListOptions, p
|
||||
}
|
||||
|
||||
// ListPigSales 实现了分页和过滤查询猪只销售记录的功能
|
||||
func (r *gormPigTradeRepository) ListPigSales(opts PigSaleListOptions, page, pageSize int) ([]models.PigSale, int64, error) {
|
||||
func (r *gormPigTradeRepository) ListPigSales(ctx context.Context, opts PigSaleListOptions, page, pageSize int) ([]models.PigSale, int64, error) {
|
||||
repoCtx := logs.AddFuncName(ctx, r.ctx, "ListPigSales")
|
||||
if page <= 0 || pageSize <= 0 {
|
||||
return nil, 0, ErrInvalidPagination
|
||||
}
|
||||
@@ -115,7 +123,7 @@ func (r *gormPigTradeRepository) ListPigSales(opts PigSaleListOptions, page, pag
|
||||
var results []models.PigSale
|
||||
var total int64
|
||||
|
||||
query := r.db.Model(&models.PigSale{})
|
||||
query := r.db.WithContext(repoCtx).Model(&models.PigSale{})
|
||||
|
||||
if opts.PigBatchID != nil {
|
||||
query = query.Where("pig_batch_id = ?", *opts.PigBatchID)
|
||||
|
||||
Reference in New Issue
Block a user