1. 增加重复顺序校验
2. 增加测试用例
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"gorm.io/datatypes"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
@@ -54,6 +56,29 @@ func (Plan) TableName() string {
|
||||
return "plans"
|
||||
}
|
||||
|
||||
// ValidateExecutionOrder 校验计划中的步骤或子计划顺序不能有重复的
|
||||
func (p Plan) ValidateExecutionOrder() error {
|
||||
orderMap := make(map[int]bool)
|
||||
|
||||
switch p.ContentType {
|
||||
case PlanContentTypeTasks:
|
||||
for _, task := range p.Tasks {
|
||||
if orderMap[task.ExecutionOrder] {
|
||||
return fmt.Errorf("任务执行顺序重复: %d", task.ExecutionOrder)
|
||||
}
|
||||
orderMap[task.ExecutionOrder] = true
|
||||
}
|
||||
case PlanContentTypeSubPlans:
|
||||
for _, subPlan := range p.SubPlans {
|
||||
if orderMap[subPlan.ExecutionOrder] {
|
||||
return fmt.Errorf("子计划执行顺序重复: %d", subPlan.ExecutionOrder)
|
||||
}
|
||||
orderMap[subPlan.ExecutionOrder] = true
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SubPlan 代表作为另一个计划一部分的子计划,具有执行顺序
|
||||
type SubPlan struct {
|
||||
gorm.Model
|
||||
|
||||
Reference in New Issue
Block a user