提取修改猪群数量逻辑

This commit is contained in:
2025-10-06 15:08:32 +08:00
parent 59b6977367
commit 91e18c432c
8 changed files with 66 additions and 54 deletions

View File

@@ -96,7 +96,7 @@ func (r *gormExecutionLogRepository) CreateTaskExecutionLogsInBatch(logs []*mode
if len(logs) == 0 {
return nil
}
// GORM 的 Create 传入一个切片指针会执行批量插入。
// GORM 的 CreateTx 传入一个切片指针会执行批量插入。
return r.db.Create(&logs).Error
}

View File

@@ -9,8 +9,8 @@ import (
// PigBatchLogRepository 定义了与猪批次日志相关的数据库操作接口。
type PigBatchLogRepository interface {
// Create 在指定的事务中创建一条新的猪批次日志。
Create(tx *gorm.DB, log *models.PigBatchLog) error
// CreateTx 在指定的事务中创建一条新的猪批次日志。
CreateTx(tx *gorm.DB, log *models.PigBatchLog) error
// GetLogsByBatchIDAndDateRangeTx 在指定的事务中,获取指定批次在特定时间范围内的所有日志记录。
GetLogsByBatchIDAndDateRangeTx(tx *gorm.DB, batchID uint, startDate, endDate time.Time) ([]*models.PigBatchLog, error)
@@ -30,7 +30,7 @@ func NewGormPigBatchLogRepository(db *gorm.DB) PigBatchLogRepository {
}
// Create 实现了创建猪批次日志的逻辑。
func (r *gormPigBatchLogRepository) Create(tx *gorm.DB, log *models.PigBatchLog) error {
func (r *gormPigBatchLogRepository) CreateTx(tx *gorm.DB, log *models.PigBatchLog) error {
return tx.Create(log).Error
}

View File

@@ -870,7 +870,7 @@ func TestPlanRepository_Create(t *testing.T) {
type testCase struct {
name string
setupDB func(db *gorm.DB) // 准备数据库的初始状态
inputPlan *models.Plan // 传入 Create 方法的计划对象
inputPlan *models.Plan // 传入 CreateTx 方法的计划对象
expectedError error // 期望的错误类型
verifyDB func(t *testing.T, db *gorm.DB, createdPlan *models.Plan) // 验证数据库状态
}
@@ -1040,7 +1040,7 @@ func TestPlanRepository_Create(t *testing.T) {
{Name: "Task 2", ExecutionOrder: 1}, // 重复的顺序
},
},
expectedError: fmt.Errorf("任务执行顺序重复: %d", 1), // 假设 Create 方法会返回此错误
expectedError: fmt.Errorf("任务执行顺序重复: %d", 1), // 假设 CreateTx 方法会返回此错误
verifyDB: func(t *testing.T, db *gorm.DB, createdPlan *models.Plan) {
var count int64
db.Model(&models.Plan{}).Where("name = ?", "重复任务顺序计划").Count(&count)
@@ -1061,7 +1061,7 @@ func TestPlanRepository_Create(t *testing.T) {
{ChildPlanID: 2, ExecutionOrder: 1}, // 重复的顺序
},
},
expectedError: fmt.Errorf("子计划执行顺序重复: %d", 1), // 假设 Create 方法会返回此错误
expectedError: fmt.Errorf("子计划执行顺序重复: %d", 1), // 假设 CreateTx 方法会返回此错误
verifyDB: func(t *testing.T, db *gorm.DB, createdPlan *models.Plan) {
var count int64
db.Model(&models.Plan{}).Where("name = ?", "重复子计划顺序计划").Count(&count)
@@ -1078,7 +1078,7 @@ func TestPlanRepository_Create(t *testing.T) {
// 准备数据库状态
tc.setupDB(db)
// 执行 Create 操作
// 执行 CreateTx 操作
err := repo.CreatePlan(tc.inputPlan)
// 断言错误