146 lines
4.3 KiB
Go
146 lines
4.3 KiB
Go
package dto
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
|
)
|
|
|
|
// NewPlanFromCreateRequest 将 CreatePlanRequest 转换为 models.Plan
|
|
func NewPlanFromCreateRequest(req *CreatePlanRequest) (*models.Plan, error) {
|
|
plan := &models.Plan{
|
|
Name: req.Name,
|
|
Description: req.Description,
|
|
ExecutionType: req.ExecutionType,
|
|
ExecuteNum: req.ExecuteNum,
|
|
CronExpression: req.CronExpression,
|
|
Status: models.PlanStatusDisabled, // 默认创建时为禁用状态
|
|
}
|
|
|
|
if len(req.SubPlanIDs) > 0 {
|
|
plan.ContentType = models.PlanContentTypeSubPlans
|
|
for _, subPlanID := range req.SubPlanIDs {
|
|
plan.SubPlans = append(plan.SubPlans, models.SubPlan{
|
|
ChildPlanID: subPlanID,
|
|
})
|
|
}
|
|
} else if len(req.Tasks) > 0 {
|
|
plan.ContentType = models.PlanContentTypeTasks
|
|
for _, taskReq := range req.Tasks {
|
|
parametersJSON, err := json.Marshal(taskReq.Parameters)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("序列化任务参数失败: %w", err)
|
|
}
|
|
plan.Tasks = append(plan.Tasks, models.Task{
|
|
Name: taskReq.Name,
|
|
Description: taskReq.Description,
|
|
ExecutionOrder: taskReq.ExecutionOrder,
|
|
Type: taskReq.Type,
|
|
Parameters: parametersJSON,
|
|
})
|
|
}
|
|
} else {
|
|
return nil, errors.New("计划必须包含子计划或任务")
|
|
}
|
|
|
|
return plan, nil
|
|
}
|
|
|
|
// NewPlanFromUpdateRequest 将 UpdatePlanRequest 转换为 models.Plan
|
|
func NewPlanFromUpdateRequest(req *UpdatePlanRequest) (*models.Plan, error) {
|
|
plan := &models.Plan{
|
|
Name: req.Name,
|
|
Description: req.Description,
|
|
ExecutionType: req.ExecutionType,
|
|
ExecuteNum: req.ExecuteNum,
|
|
CronExpression: req.CronExpression,
|
|
}
|
|
|
|
if len(req.SubPlanIDs) > 0 {
|
|
plan.ContentType = models.PlanContentTypeSubPlans
|
|
for _, subPlanID := range req.SubPlanIDs {
|
|
plan.SubPlans = append(plan.SubPlans, models.SubPlan{
|
|
ChildPlanID: subPlanID,
|
|
})
|
|
}
|
|
} else if len(req.Tasks) > 0 {
|
|
plan.ContentType = models.PlanContentTypeTasks
|
|
for _, taskReq := range req.Tasks {
|
|
parametersJSON, err := json.Marshal(taskReq.Parameters)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("序列化任务参数失败: %w", err)
|
|
}
|
|
plan.Tasks = append(plan.Tasks, models.Task{
|
|
Name: taskReq.Name,
|
|
Description: taskReq.Description,
|
|
ExecutionOrder: taskReq.ExecutionOrder,
|
|
Type: taskReq.Type,
|
|
Parameters: parametersJSON,
|
|
})
|
|
}
|
|
} else {
|
|
return nil, errors.New("计划必须包含子计划或任务")
|
|
}
|
|
|
|
return plan, nil
|
|
}
|
|
|
|
// NewPlanToResponse 将 models.Plan 转换为 PlanResponse
|
|
func NewPlanToResponse(plan *models.Plan) (*PlanResponse, error) {
|
|
if plan == nil {
|
|
return nil, nil
|
|
}
|
|
|
|
resp := &PlanResponse{
|
|
ID: plan.ID,
|
|
Name: plan.Name,
|
|
Description: plan.Description,
|
|
ExecutionType: plan.ExecutionType,
|
|
Status: plan.Status,
|
|
ExecuteNum: plan.ExecuteNum,
|
|
ExecuteCount: plan.ExecuteCount,
|
|
CronExpression: plan.CronExpression,
|
|
ContentType: plan.ContentType,
|
|
}
|
|
|
|
if plan.ContentType == models.PlanContentTypeSubPlans && len(plan.SubPlans) > 0 {
|
|
resp.SubPlans = make([]SubPlanResponse, 0, len(plan.SubPlans))
|
|
for _, sp := range plan.SubPlans {
|
|
childPlanResp, err := NewPlanToResponse(sp.ChildPlan) // 递归调用
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
resp.SubPlans = append(resp.SubPlans, SubPlanResponse{
|
|
ID: sp.ID,
|
|
ParentPlanID: sp.ParentPlanID,
|
|
ChildPlanID: sp.ChildPlanID,
|
|
ExecutionOrder: sp.ExecutionOrder,
|
|
ChildPlan: childPlanResp,
|
|
})
|
|
}
|
|
} else if plan.ContentType == models.PlanContentTypeTasks && len(plan.Tasks) > 0 {
|
|
resp.Tasks = make([]TaskResponse, 0, len(plan.Tasks))
|
|
for _, task := range plan.Tasks {
|
|
var parameters map[string]interface{}
|
|
if len(task.Parameters) > 0 && string(task.Parameters) != "null" {
|
|
if err := json.Unmarshal(task.Parameters, ¶meters); err != nil {
|
|
return nil, fmt.Errorf("解析任务参数失败 (ID: %d): %w", task.ID, err)
|
|
}
|
|
}
|
|
resp.Tasks = append(resp.Tasks, TaskResponse{
|
|
ID: task.ID,
|
|
PlanID: task.PlanID,
|
|
Name: task.Name,
|
|
Description: task.Description,
|
|
ExecutionOrder: task.ExecutionOrder,
|
|
Type: task.Type,
|
|
Parameters: parameters,
|
|
})
|
|
}
|
|
}
|
|
|
|
return resp, nil
|
|
}
|