1.注册饲喂计划相关路由
2. 实现create接口
This commit is contained in:
@@ -29,6 +29,100 @@ func NewController(feedPlanRepo repository.FeedPlanRepo) *Controller {
|
||||
}
|
||||
}
|
||||
|
||||
// CreateRequest 创建计划请求结构体
|
||||
type CreateRequest struct {
|
||||
// Name 计划名称
|
||||
Name string `json:"name"`
|
||||
|
||||
// Description 计划描述
|
||||
Description string `json:"description"`
|
||||
|
||||
// Type 计划类型(手动触发/自动触发)
|
||||
Type model.FeedingPlanType `json:"type"`
|
||||
|
||||
// Enabled 是否启用
|
||||
Enabled bool `json:"enabled"`
|
||||
|
||||
// ScheduleCron 定时任务表达式(仅当Type为auto时有效)
|
||||
ScheduleCron *string `json:"scheduleCron"`
|
||||
|
||||
// ExecutionLimit 执行次数限制(0表示无限制,仅当Type为auto时有效)
|
||||
ExecutionLimit int `json:"executionLimit"`
|
||||
|
||||
// ParentID 父计划ID(用于支持子计划结构)
|
||||
ParentID *uint `json:"parentID"`
|
||||
|
||||
// OrderInParent 在父计划中的执行顺序
|
||||
OrderInParent *int `json:"orderInParent"`
|
||||
|
||||
// IsMaster 是否为主计划(主计划可以包含子计划)
|
||||
IsMaster bool `json:"isMaster"`
|
||||
|
||||
// Steps 计划步骤列表
|
||||
Steps []FeedingPlanStep `json:"steps"`
|
||||
|
||||
// SubPlans 子计划列表
|
||||
SubPlans []CreateRequest `json:"subPlans"`
|
||||
}
|
||||
|
||||
// Create 创建饲料计划
|
||||
func (c *Controller) Create(ctx *gin.Context) {
|
||||
var req CreateRequest
|
||||
if err := ctx.ShouldBindJSON(&req); err != nil {
|
||||
controller.SendErrorResponse(ctx, controller.InvalidParameterCode, "请求参数错误: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// 转换请求结构体为模型
|
||||
plan := c.convertToCreateModel(&req)
|
||||
|
||||
// 调用仓库创建计划
|
||||
if err := c.feedPlanRepo.CreateFeedingPlan(plan); err != nil {
|
||||
c.logger.Error("创建计划失败: " + err.Error())
|
||||
controller.SendErrorResponse(ctx, controller.InternalServerErrorCode, "创建计划失败")
|
||||
return
|
||||
}
|
||||
|
||||
controller.SendSuccessResponse(ctx, "创建计划成功", nil)
|
||||
}
|
||||
|
||||
// convertToCreateModel 将创建请求结构体转换为数据库模型
|
||||
func (c *Controller) convertToCreateModel(req *CreateRequest) *model.FeedingPlan {
|
||||
plan := &model.FeedingPlan{
|
||||
Name: req.Name,
|
||||
Description: req.Description,
|
||||
Type: req.Type,
|
||||
Enabled: req.Enabled,
|
||||
ScheduleCron: req.ScheduleCron,
|
||||
ExecutionLimit: req.ExecutionLimit,
|
||||
ParentID: req.ParentID,
|
||||
OrderInParent: req.OrderInParent,
|
||||
}
|
||||
|
||||
// 转换步骤
|
||||
plan.Steps = make([]model.FeedingPlanStep, len(req.Steps))
|
||||
for i, step := range req.Steps {
|
||||
plan.Steps[i] = model.FeedingPlanStep{
|
||||
// ID在创建时不需要设置
|
||||
// PlanID会在创建过程中自动设置
|
||||
StepOrder: step.StepOrder,
|
||||
DeviceID: step.DeviceID,
|
||||
TargetValue: step.TargetValue,
|
||||
Action: step.Action,
|
||||
ScheduleCron: step.ScheduleCron,
|
||||
ExecutionLimit: step.ExecutionLimit,
|
||||
}
|
||||
}
|
||||
|
||||
// 转换子计划
|
||||
plan.SubPlans = make([]model.FeedingPlan, len(req.SubPlans))
|
||||
for i, subReq := range req.SubPlans {
|
||||
plan.SubPlans[i] = *c.convertToCreateModel(&subReq)
|
||||
}
|
||||
|
||||
return plan
|
||||
}
|
||||
|
||||
// Delete 删除饲料计划
|
||||
func (c *Controller) Delete(ctx *gin.Context) {
|
||||
// 获取路径参数中的计划ID
|
||||
@@ -100,18 +194,41 @@ func (c *Controller) ListPlans(ctx *gin.Context) {
|
||||
|
||||
// UpdateRequest 更新计划请求结构体
|
||||
type UpdateRequest struct {
|
||||
ID uint `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
Type model.FeedingPlanType `json:"type"`
|
||||
Enabled bool `json:"enabled"`
|
||||
ScheduleCron *string `json:"scheduleCron"`
|
||||
ExecutionLimit int `json:"executionLimit"`
|
||||
ParentID *uint `json:"parentID"`
|
||||
OrderInParent *int `json:"orderInParent"`
|
||||
IsMaster bool `json:"isMaster"`
|
||||
Steps []FeedingPlanStep `json:"steps"`
|
||||
SubPlans []UpdateRequest `json:"subPlans"`
|
||||
// ID 计划ID
|
||||
ID uint `json:"id"`
|
||||
|
||||
// Name 计划名称
|
||||
Name string `json:"name"`
|
||||
|
||||
// Description 计划描述
|
||||
Description string `json:"description"`
|
||||
|
||||
// Type 计划类型(手动触发/自动触发)
|
||||
Type model.FeedingPlanType `json:"type"`
|
||||
|
||||
// Enabled 是否启用
|
||||
Enabled bool `json:"enabled"`
|
||||
|
||||
// ScheduleCron 定时任务表达式(仅当Type为auto时有效)
|
||||
ScheduleCron *string `json:"scheduleCron"`
|
||||
|
||||
// ExecutionLimit 执行次数限制(0表示无限制,仅当Type为auto时有效)
|
||||
ExecutionLimit int `json:"executionLimit"`
|
||||
|
||||
// ParentID 父计划ID(用于支持子计划结构)
|
||||
ParentID *uint `json:"parentID"`
|
||||
|
||||
// OrderInParent 在父计划中的执行顺序
|
||||
OrderInParent *int `json:"orderInParent"`
|
||||
|
||||
// IsMaster 是否为主计划(主计划可以包含子计划)
|
||||
IsMaster bool `json:"isMaster"`
|
||||
|
||||
// Steps 计划步骤列表
|
||||
Steps []FeedingPlanStep `json:"steps"`
|
||||
|
||||
// SubPlans 子计划列表
|
||||
SubPlans []UpdateRequest `json:"subPlans"`
|
||||
}
|
||||
|
||||
// DetailResponse 喂料计划主表
|
||||
@@ -293,20 +410,7 @@ func (c *Controller) convertToDetailResponse(plan *model.FeedingPlan) *DetailRes
|
||||
// 转换子计划
|
||||
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,
|
||||
Steps: subPlan.Steps,
|
||||
SubPlans: subPlan.SubPlans,
|
||||
}
|
||||
resp.SubPlans[i] = *c.convertToDetailResponse(subPlanModel)
|
||||
resp.SubPlans[i] = *c.convertToDetailResponse(&subPlan)
|
||||
}
|
||||
|
||||
return resp
|
||||
|
||||
Reference in New Issue
Block a user