实现Detail接口
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
package feed
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"git.huangwc.com/pig/pig-farm-controller/internal/controller"
|
||||
@@ -165,7 +166,87 @@ type FeedingPlanStep struct {
|
||||
|
||||
// Detail 获取饲料计划列细节
|
||||
func (c *Controller) Detail(ctx *gin.Context) {
|
||||
// TODO: 实现获取饲料计划列表的逻辑
|
||||
|
||||
controller.SendSuccessResponse(ctx, "success", &DetailResponse{})
|
||||
// 获取路径参数中的计划ID
|
||||
planIDStr := ctx.Param("id")
|
||||
planID, err := strconv.ParseUint(planIDStr, 10, 32)
|
||||
if err != nil {
|
||||
controller.SendErrorResponse(ctx, controller.InvalidParameterCode, "无效的计划ID")
|
||||
return
|
||||
}
|
||||
|
||||
// 从仓库中获取计划详情
|
||||
plan, err := c.feedPlanRepo.FindFeedingPlanByID(uint(planID))
|
||||
if err != nil {
|
||||
c.logger.Error("获取计划详情失败: " + err.Error())
|
||||
controller.SendErrorResponse(ctx, controller.InternalServerErrorCode, "获取计划详情失败")
|
||||
return
|
||||
}
|
||||
|
||||
// 转换为响应结构体
|
||||
resp := c.convertToDetailResponse(plan)
|
||||
|
||||
controller.SendSuccessResponse(ctx, "success", resp)
|
||||
}
|
||||
|
||||
// convertToDetailResponse 将数据库模型转换为响应结构体
|
||||
func (c *Controller) convertToDetailResponse(plan *model.FeedingPlan) *DetailResponse {
|
||||
resp := &DetailResponse{
|
||||
ID: plan.ID,
|
||||
Name: plan.Name,
|
||||
Description: plan.Description,
|
||||
Type: plan.Type,
|
||||
Enabled: plan.Enabled,
|
||||
ScheduleCron: plan.ScheduleCron,
|
||||
ExecutionLimit: plan.ExecutionLimit,
|
||||
ParentID: plan.ParentID,
|
||||
OrderInParent: plan.OrderInParent,
|
||||
IsMaster: plan.IsMaster,
|
||||
CreatedAt: plan.CreatedAt,
|
||||
UpdatedAt: plan.UpdatedAt,
|
||||
DeletedAt: plan.DeletedAt,
|
||||
Steps: make([]FeedingPlanStep, len(plan.Steps)),
|
||||
SubPlans: make([]DetailResponse, len(plan.SubPlans)),
|
||||
}
|
||||
|
||||
// 转换步骤
|
||||
for i, step := range plan.Steps {
|
||||
resp.Steps[i] = FeedingPlanStep{
|
||||
ID: step.ID,
|
||||
PlanID: step.PlanID,
|
||||
StepOrder: step.StepOrder,
|
||||
DeviceID: step.DeviceID,
|
||||
TargetValue: step.TargetValue,
|
||||
Action: step.Action,
|
||||
ScheduleCron: step.ScheduleCron,
|
||||
ExecutionLimit: step.ExecutionLimit,
|
||||
CreatedAt: step.CreatedAt,
|
||||
UpdatedAt: step.UpdatedAt,
|
||||
DeletedAt: step.DeletedAt,
|
||||
}
|
||||
}
|
||||
|
||||
// 转换子计划
|
||||
for i, subPlan := range plan.SubPlans {
|
||||
// 递归转换子计划
|
||||
subPlanModel := &model.FeedingPlan{
|
||||
ID: subPlan.ID,
|
||||
Name: subPlan.Name,
|
||||
Description: subPlan.Description,
|
||||
Type: subPlan.Type,
|
||||
Enabled: subPlan.Enabled,
|
||||
ScheduleCron: subPlan.ScheduleCron,
|
||||
ExecutionLimit: subPlan.ExecutionLimit,
|
||||
ParentID: subPlan.ParentID,
|
||||
OrderInParent: subPlan.OrderInParent,
|
||||
IsMaster: subPlan.IsMaster,
|
||||
CreatedAt: subPlan.CreatedAt,
|
||||
UpdatedAt: subPlan.UpdatedAt,
|
||||
DeletedAt: subPlan.DeletedAt,
|
||||
Steps: subPlan.Steps,
|
||||
SubPlans: subPlan.SubPlans,
|
||||
}
|
||||
resp.SubPlans[i] = *c.convertToDetailResponse(subPlanModel)
|
||||
}
|
||||
|
||||
return resp
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user