创建批次时插入一条记录
This commit is contained in:
@@ -37,7 +37,7 @@ type PigBatchService interface {
|
||||
TransferPigsAcrossBatches(sourceBatchID uint, destBatchID uint, fromPenID uint, toPenID uint, quantity uint) error
|
||||
|
||||
// CreatePigBatch 创建一个新的猪批次。
|
||||
CreatePigBatch(batch *models.PigBatch) (*models.PigBatch, error)
|
||||
CreatePigBatch(operatorID uint, batch *models.PigBatch) (*models.PigBatch, error)
|
||||
|
||||
// GetPigBatch 根据ID获取单个猪批次的详细信息。
|
||||
GetPigBatch(id uint) (*models.PigBatch, error)
|
||||
|
||||
@@ -16,29 +16,67 @@ import (
|
||||
// pigBatchService 是 PigBatchService 接口的具体实现。
|
||||
// 它作为猪群领域的主服务,封装了所有业务逻辑。
|
||||
type pigBatchService struct {
|
||||
pigBatchRepo repository.PigBatchRepository // 猪批次仓库
|
||||
uow repository.UnitOfWork // 工作单元,用于管理事务
|
||||
transferSvc PenTransferManager // 调栏子服务
|
||||
pigBatchRepo repository.PigBatchRepository // 猪批次仓库
|
||||
pigBatchLogRepo repository.PigBatchLogRepository // 猪批次日志仓库
|
||||
uow repository.UnitOfWork // 工作单元,用于管理事务
|
||||
transferSvc PenTransferManager // 调栏子服务
|
||||
}
|
||||
|
||||
// NewPigBatchService 是 pigBatchService 的构造函数。
|
||||
// 它通过依赖注入的方式,创建并返回一个 PigBatchService 接口的实例。
|
||||
func NewPigBatchService(
|
||||
pigBatchRepo repository.PigBatchRepository,
|
||||
pigBatchLogRepo repository.PigBatchLogRepository,
|
||||
uow repository.UnitOfWork,
|
||||
transferSvc PenTransferManager,
|
||||
) PigBatchService {
|
||||
return &pigBatchService{
|
||||
pigBatchRepo: pigBatchRepo,
|
||||
uow: uow,
|
||||
transferSvc: transferSvc,
|
||||
pigBatchRepo: pigBatchRepo,
|
||||
pigBatchLogRepo: pigBatchLogRepo,
|
||||
uow: uow,
|
||||
transferSvc: transferSvc,
|
||||
}
|
||||
}
|
||||
|
||||
// CreatePigBatch 实现了创建猪批次的逻辑。
|
||||
func (s *pigBatchService) CreatePigBatch(batch *models.PigBatch) (*models.PigBatch, error) {
|
||||
// CreatePigBatch 实现了创建猪批次的逻辑,并同时创建初始批次日志。
|
||||
func (s *pigBatchService) CreatePigBatch(operatorID uint, batch *models.PigBatch) (*models.PigBatch, error) {
|
||||
// 业务规则可以在这里添加,例如检查批次号是否唯一等
|
||||
return s.pigBatchRepo.CreatePigBatch(batch)
|
||||
|
||||
var createdBatch *models.PigBatch
|
||||
err := s.uow.ExecuteInTransaction(func(tx *gorm.DB) error {
|
||||
// 1. 创建猪批次
|
||||
// 注意: 此处依赖一个假设存在的 pigBatchRepo.CreatePigBatchTx 方法
|
||||
var err error
|
||||
createdBatch, err = s.pigBatchRepo.CreatePigBatchTx(tx, batch)
|
||||
if err != nil {
|
||||
return fmt.Errorf("创建猪批次失败: %w", err)
|
||||
}
|
||||
|
||||
// 2. 创建初始批次日志
|
||||
initialLog := &models.PigBatchLog{
|
||||
PigBatchID: createdBatch.ID,
|
||||
HappenedAt: time.Now(),
|
||||
ChangeType: models.ChangeTypeCorrection, // 初始创建可视为一种校正
|
||||
ChangeCount: createdBatch.InitialCount,
|
||||
Reason: fmt.Sprintf("创建了新的猪批次 %s,初始数量 %d", createdBatch.BatchNumber, createdBatch.InitialCount),
|
||||
BeforeCount: 0, // 初始创建前数量为0
|
||||
AfterCount: int(createdBatch.InitialCount),
|
||||
OperatorID: 0, // 假设初始创建没有特定操作员ID,或需要从上下文传入
|
||||
}
|
||||
|
||||
// 3. 记录批次日志
|
||||
if err := s.pigBatchLogRepo.Create(tx, initialLog); err != nil {
|
||||
return fmt.Errorf("记录初始批次日志失败: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return createdBatch, nil
|
||||
}
|
||||
|
||||
// GetPigBatch 实现了获取单个猪批次的逻辑。
|
||||
|
||||
Reference in New Issue
Block a user