2025-09-14 14:13:40 +08:00
|
|
|
|
package plan
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"git.huangwc.com/pig/pig-farm-controller/internal/app/controller"
|
|
|
|
|
|
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
|
|
|
|
|
"gorm.io/datatypes"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// PlanToResponse 将Plan模型转换为PlanResponse
|
|
|
|
|
|
func PlanToResponse(plan *models.Plan) *PlanResponse {
|
|
|
|
|
|
if plan == nil {
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
response := &PlanResponse{
|
|
|
|
|
|
ID: plan.ID,
|
|
|
|
|
|
Name: plan.Name,
|
|
|
|
|
|
Description: plan.Description,
|
|
|
|
|
|
ExecutionType: plan.ExecutionType,
|
|
|
|
|
|
CronExpression: plan.CronExpression,
|
|
|
|
|
|
ContentType: plan.ContentType,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 转换子计划
|
|
|
|
|
|
if plan.ContentType == models.PlanContentTypeSubPlans {
|
|
|
|
|
|
response.SubPlans = make([]SubPlanResponse, len(plan.SubPlans))
|
|
|
|
|
|
for i, subPlan := range plan.SubPlans {
|
|
|
|
|
|
response.SubPlans[i] = SubPlanToResponse(&subPlan)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 转换任务
|
|
|
|
|
|
if plan.ContentType == models.PlanContentTypeTasks {
|
|
|
|
|
|
response.Tasks = make([]TaskResponse, len(plan.Tasks))
|
|
|
|
|
|
for i, task := range plan.Tasks {
|
|
|
|
|
|
response.Tasks[i] = TaskToResponse(&task)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return response
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-14 15:47:08 +08:00
|
|
|
|
// PlanFromCreateRequest 将CreatePlanRequest转换为Plan模型,并进行业务规则验证
|
|
|
|
|
|
func PlanFromCreateRequest(req *CreatePlanRequest) (*models.Plan, error) {
|
2025-09-14 14:13:40 +08:00
|
|
|
|
if req == nil {
|
2025-09-14 15:47:08 +08:00
|
|
|
|
return nil, nil
|
2025-09-14 14:13:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
plan := &models.Plan{
|
|
|
|
|
|
Name: req.Name,
|
|
|
|
|
|
Description: req.Description,
|
|
|
|
|
|
ExecutionType: req.ExecutionType,
|
|
|
|
|
|
CronExpression: req.CronExpression,
|
|
|
|
|
|
ContentType: req.ContentType,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 处理子计划 (通过ID引用)
|
|
|
|
|
|
if req.ContentType == models.PlanContentTypeSubPlans && req.SubPlanIDs != nil {
|
|
|
|
|
|
plan.SubPlans = make([]models.SubPlan, len(req.SubPlanIDs))
|
|
|
|
|
|
for i, childPlanID := range req.SubPlanIDs {
|
|
|
|
|
|
plan.SubPlans[i] = models.SubPlan{
|
|
|
|
|
|
ChildPlanID: childPlanID,
|
2025-09-14 15:47:08 +08:00
|
|
|
|
ExecutionOrder: i, // 默认执行顺序, ReorderSteps会再次确认
|
2025-09-14 14:13:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 处理任务
|
|
|
|
|
|
if req.ContentType == models.PlanContentTypeTasks && req.Tasks != nil {
|
|
|
|
|
|
plan.Tasks = make([]models.Task, len(req.Tasks))
|
|
|
|
|
|
for i, taskReq := range req.Tasks {
|
2025-09-14 15:47:08 +08:00
|
|
|
|
// 使用来自请求的ExecutionOrder
|
2025-09-14 14:13:40 +08:00
|
|
|
|
plan.Tasks[i] = TaskFromRequest(&taskReq)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-14 15:47:08 +08:00
|
|
|
|
// 1. 首先,执行重复性验证
|
|
|
|
|
|
if err := plan.ValidateExecutionOrder(); err != nil {
|
|
|
|
|
|
// 如果检测到重复,立即返回错误
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 2. 然后,调用方法来修复顺序断层
|
|
|
|
|
|
plan.ReorderSteps()
|
|
|
|
|
|
|
|
|
|
|
|
return plan, nil
|
2025-09-14 14:13:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-14 15:47:08 +08:00
|
|
|
|
// PlanFromUpdateRequest 将UpdatePlanRequest转换为Plan模型,并进行业务规则验证
|
|
|
|
|
|
func PlanFromUpdateRequest(req *UpdatePlanRequest) (*models.Plan, error) {
|
2025-09-14 14:13:40 +08:00
|
|
|
|
if req == nil {
|
2025-09-14 15:47:08 +08:00
|
|
|
|
return nil, nil
|
2025-09-14 14:13:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
plan := &models.Plan{
|
|
|
|
|
|
Name: req.Name,
|
|
|
|
|
|
Description: req.Description,
|
|
|
|
|
|
ExecutionType: req.ExecutionType,
|
|
|
|
|
|
CronExpression: req.CronExpression,
|
|
|
|
|
|
ContentType: req.ContentType,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 处理子计划 (通过ID引用)
|
|
|
|
|
|
if req.ContentType == models.PlanContentTypeSubPlans && req.SubPlanIDs != nil {
|
|
|
|
|
|
plan.SubPlans = make([]models.SubPlan, len(req.SubPlanIDs))
|
|
|
|
|
|
for i, childPlanID := range req.SubPlanIDs {
|
|
|
|
|
|
plan.SubPlans[i] = models.SubPlan{
|
|
|
|
|
|
ChildPlanID: childPlanID,
|
2025-09-14 15:47:08 +08:00
|
|
|
|
ExecutionOrder: i, // 默认执行顺序, ReorderSteps会再次确认
|
2025-09-14 14:13:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 处理任务
|
|
|
|
|
|
if req.ContentType == models.PlanContentTypeTasks && req.Tasks != nil {
|
|
|
|
|
|
plan.Tasks = make([]models.Task, len(req.Tasks))
|
|
|
|
|
|
for i, taskReq := range req.Tasks {
|
2025-09-14 15:47:08 +08:00
|
|
|
|
// 使用来自请求的ExecutionOrder
|
2025-09-14 14:13:40 +08:00
|
|
|
|
plan.Tasks[i] = TaskFromRequest(&taskReq)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-14 15:47:08 +08:00
|
|
|
|
// 1. 首先,执行重复性验证
|
|
|
|
|
|
if err := plan.ValidateExecutionOrder(); err != nil {
|
|
|
|
|
|
// 如果检测到重复,立即返回错误
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 2. 然后,调用方法来修复顺序断层
|
|
|
|
|
|
plan.ReorderSteps()
|
|
|
|
|
|
|
|
|
|
|
|
return plan, nil
|
2025-09-14 14:13:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// SubPlanToResponse 将SubPlan模型转换为SubPlanResponse
|
|
|
|
|
|
func SubPlanToResponse(subPlan *models.SubPlan) SubPlanResponse {
|
|
|
|
|
|
if subPlan == nil {
|
|
|
|
|
|
return SubPlanResponse{}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
response := SubPlanResponse{
|
|
|
|
|
|
ID: subPlan.ID,
|
|
|
|
|
|
ParentPlanID: subPlan.ParentPlanID,
|
|
|
|
|
|
ChildPlanID: subPlan.ChildPlanID,
|
|
|
|
|
|
ExecutionOrder: subPlan.ExecutionOrder,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 如果有完整的子计划数据,也进行转换
|
|
|
|
|
|
if subPlan.ChildPlan != nil {
|
|
|
|
|
|
response.ChildPlan = PlanToResponse(subPlan.ChildPlan)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return response
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// TaskToResponse 将Task模型转换为TaskResponse
|
|
|
|
|
|
func TaskToResponse(task *models.Task) TaskResponse {
|
|
|
|
|
|
if task == nil {
|
|
|
|
|
|
return TaskResponse{}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return TaskResponse{
|
|
|
|
|
|
ID: task.ID,
|
|
|
|
|
|
PlanID: task.PlanID,
|
|
|
|
|
|
Name: task.Name,
|
|
|
|
|
|
Description: task.Description,
|
|
|
|
|
|
ExecutionOrder: task.ExecutionOrder,
|
|
|
|
|
|
Type: task.Type,
|
|
|
|
|
|
Parameters: controller.Properties(task.Parameters),
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// TaskFromRequest 将TaskRequest转换为Task模型
|
|
|
|
|
|
func TaskFromRequest(req *TaskRequest) models.Task {
|
|
|
|
|
|
if req == nil {
|
|
|
|
|
|
return models.Task{}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return models.Task{
|
|
|
|
|
|
Name: req.Name,
|
|
|
|
|
|
Description: req.Description,
|
|
|
|
|
|
ExecutionOrder: req.ExecutionOrder,
|
|
|
|
|
|
Type: req.Type,
|
|
|
|
|
|
Parameters: datatypes.JSON(req.Parameters),
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|