重构 #4
							
								
								
									
										170
									
								
								internal/app/controller/plan/converter.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										170
									
								
								internal/app/controller/plan/converter.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,170 @@ | |||||||
|  | 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 | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // PlanFromCreateRequest 将CreatePlanRequest转换为Plan模型 | ||||||
|  | func PlanFromCreateRequest(req *CreatePlanRequest) *models.Plan { | ||||||
|  | 	if req == nil { | ||||||
|  | 		return nil | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	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, | ||||||
|  | 				ExecutionOrder: i + 1, // 默认执行顺序 | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// 处理任务 | ||||||
|  | 	if req.ContentType == models.PlanContentTypeTasks && req.Tasks != nil { | ||||||
|  | 		plan.Tasks = make([]models.Task, len(req.Tasks)) | ||||||
|  | 		for i, taskReq := range req.Tasks { | ||||||
|  | 			plan.Tasks[i] = TaskFromRequest(&taskReq) | ||||||
|  | 			// 设置执行顺序 | ||||||
|  | 			plan.Tasks[i].ExecutionOrder = i + 1 | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	return plan | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // PlanFromUpdateRequest 将UpdatePlanRequest转换为Plan模型 | ||||||
|  | func PlanFromUpdateRequest(req *UpdatePlanRequest) *models.Plan { | ||||||
|  | 	if req == nil { | ||||||
|  | 		return nil | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	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, | ||||||
|  | 				ExecutionOrder: i + 1, // 默认执行顺序 | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	// 处理任务 | ||||||
|  | 	if req.ContentType == models.PlanContentTypeTasks && req.Tasks != nil { | ||||||
|  | 		plan.Tasks = make([]models.Task, len(req.Tasks)) | ||||||
|  | 		for i, taskReq := range req.Tasks { | ||||||
|  | 			plan.Tasks[i] = TaskFromRequest(&taskReq) | ||||||
|  | 			// 设置执行顺序 | ||||||
|  | 			plan.Tasks[i].ExecutionOrder = i + 1 | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	return plan | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // 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), | ||||||
|  | 	} | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user