Files
pig-farm-controller/internal/app/controller/plan/plan_controller.go

207 lines
9.8 KiB
Go
Raw Normal View History

2025-09-13 14:09:22 +08:00
package plan
import (
"git.huangwc.com/pig/pig-farm-controller/internal/app/controller"
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
2025-09-14 14:00:42 +08:00
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
2025-09-13 14:09:22 +08:00
"git.huangwc.com/pig/pig-farm-controller/internal/infra/repository"
"github.com/gin-gonic/gin"
)
// --- 请求和响应 DTO 定义 ---
// CreatePlanRequest 定义创建计划请求的结构体
type CreatePlanRequest struct {
2025-09-14 14:00:42 +08:00
Name string `json:"name" binding:"required" example:"猪舍温度控制计划"`
Description string `json:"description" example:"根据温度自动调节风扇和加热器"`
ExecutionType models.PlanExecutionType `json:"execution_type" binding:"required" example:"automatic"`
CronExpression string `json:"cron_expression" example:"0 0 6 * * *"`
ContentType models.PlanContentType `json:"content_type" binding:"required" example:"tasks"`
SubPlanIDs []uint `json:"sub_plan_ids,omitempty"`
Tasks []TaskRequest `json:"tasks,omitempty"`
2025-09-13 14:09:22 +08:00
}
// PlanResponse 定义计划详情响应的结构体
type PlanResponse struct {
2025-09-14 14:00:42 +08:00
ID uint `json:"id" example:"1"`
Name string `json:"name" example:"猪舍温度控制计划"`
Description string `json:"description" example:"根据温度自动调节风扇和加热器"`
ExecutionType models.PlanExecutionType `json:"execution_type" example:"automatic"`
CronExpression string `json:"cron_expression" example:"0 0 6 * * *"`
ContentType models.PlanContentType `json:"content_type" example:"tasks"`
SubPlans []SubPlanResponse `json:"sub_plans,omitempty"`
Tasks []TaskResponse `json:"tasks,omitempty"`
2025-09-13 14:09:22 +08:00
}
// ListPlansResponse 定义获取计划列表响应的结构体
type ListPlansResponse struct {
Plans []PlanResponse `json:"plans"`
Total int `json:"total" example:"100"`
}
// UpdatePlanRequest 定义更新计划请求的结构体
type UpdatePlanRequest struct {
2025-09-14 14:00:42 +08:00
Name string `json:"name" example:"猪舍温度控制计划V2"`
Description string `json:"description" example:"更新后的描述"`
ExecutionType models.PlanExecutionType `json:"execution_type" example:"automatic"`
CronExpression string `json:"cron_expression" example:"0 0 6 * * *"`
ContentType models.PlanContentType `json:"content_type" example:"tasks"`
SubPlanIDs []uint `json:"sub_plan_ids,omitempty"`
Tasks []TaskRequest `json:"tasks,omitempty"`
}
// SubPlanResponse 定义子计划响应结构体
type SubPlanResponse struct {
ID uint `json:"id" example:"1"`
ParentPlanID uint `json:"parent_plan_id" example:"1"`
ChildPlanID uint `json:"child_plan_id" example:"2"`
ExecutionOrder int `json:"execution_order" example:"1"`
ChildPlan *PlanResponse `json:"child_plan,omitempty"`
}
// TaskRequest 定义任务请求结构体
type TaskRequest struct {
Name string `json:"name" example:"打开风扇"`
Description string `json:"description" example:"打开1号风扇"`
ExecutionOrder int `json:"execution_order" example:"1"`
Type models.TaskType `json:"type" example:"waiting"`
Parameters controller.Properties `json:"parameters,omitempty"`
}
// TaskResponse 定义任务响应结构体
type TaskResponse struct {
ID uint `json:"id" example:"1"`
PlanID uint `json:"plan_id" example:"1"`
Name string `json:"name" example:"打开风扇"`
Description string `json:"description" example:"打开1号风扇"`
ExecutionOrder int `json:"execution_order" example:"1"`
Type models.TaskType `json:"type" example:"waiting"`
Parameters controller.Properties `json:"parameters,omitempty"`
2025-09-13 14:09:22 +08:00
}
// --- Controller 定义 ---
// Controller 定义了计划相关的控制器
type Controller struct {
logger *logs.Logger
planRepo repository.PlanRepository
}
// NewController 创建一个新的 Controller 实例
func NewController(logger *logs.Logger, planRepo repository.PlanRepository) *Controller {
return &Controller{
logger: logger,
planRepo: planRepo,
}
}
// --- 接口方法实现 ---
// CreatePlan godoc
// @Summary 创建计划
// @Description 创建一个新的计划,包括其基本信息和所有关联的子计划/任务。
// @Tags 计划管理
// @Accept json
// @Produce json
// @Param plan body CreatePlanRequest true "计划信息"
// @Success 200 {object} controller.Response{data=plan.PlanResponse} "业务码为201代表创建成功"
// @Failure 200 {object} controller.Response "业务失败具体错误码和信息见响应体例如400, 500"
// @Router /plans [post]
func (pc *Controller) CreatePlan(c *gin.Context) {
// 占位符:此处应调用服务层或仓库层来创建计划
pc.logger.Infof("收到创建计划请求 (占位符)")
2025-09-14 13:23:16 +08:00
controller.SendResponse(c, controller.CodeCreated, "创建计划接口占位符", PlanResponse{ID: 0, Name: "占位计划"})
2025-09-13 14:09:22 +08:00
}
// GetPlan godoc
// @Summary 获取计划详情
// @Description 根据计划ID获取单个计划的详细信息。
// @Tags 计划管理
// @Produce json
// @Param id path int true "计划ID"
// @Success 200 {object} controller.Response{data=plan.PlanResponse} "业务码为200代表成功获取"
// @Failure 200 {object} controller.Response "业务失败具体错误码和信息见响应体例如400, 404, 500"
// @Router /plans/{id} [get]
func (pc *Controller) GetPlan(c *gin.Context) {
// 占位符:此处应调用服务层或仓库层来获取计划
pc.logger.Infof("收到获取计划 (占位符)")
2025-09-14 13:23:16 +08:00
controller.SendResponse(c, controller.CodeSuccess, "获取计划接口占位符", ListPlansResponse{Plans: []PlanResponse{}, Total: 0})
2025-09-13 14:09:22 +08:00
}
// ListPlans godoc
// @Summary 获取计划列表
// @Description 获取所有计划的列表
// @Tags 计划管理
// @Produce json
// @Success 200 {object} controller.Response{data=plan.ListPlansResponse} "业务码为200代表成功获取列表"
// @Failure 200 {object} controller.Response "业务失败具体错误码和信息见响应体例如400, 500"
// @Router /plans [get]
func (pc *Controller) ListPlans(c *gin.Context) {
// 占位符:此处应调用服务层或仓库层来获取计划列表
pc.logger.Infof("收到获取计划列表请求 (占位符)")
2025-09-14 13:23:16 +08:00
controller.SendResponse(c, controller.CodeSuccess, "获取计划列表接口占位符", ListPlansResponse{Plans: []PlanResponse{}, Total: 0})
2025-09-13 14:09:22 +08:00
}
// UpdatePlan godoc
// @Summary 更新计划
// @Description 根据计划ID更新计划的详细信息。
// @Tags 计划管理
// @Accept json
// @Produce json
// @Param id path int true "计划ID"
// @Param plan body UpdatePlanRequest true "更新后的计划信息"
// @Success 200 {object} controller.Response{data=plan.PlanResponse} "业务码为200代表更新成功"
// @Failure 200 {object} controller.Response "业务失败具体错误码和信息见响应体例如400, 404, 500"
// @Router /plans/{id} [put]
func (pc *Controller) UpdatePlan(c *gin.Context) {
// 占位符:此处应调用服务层或仓库层来更新计划
pc.logger.Infof("收到更新计划请求 (占位符)")
2025-09-14 13:23:16 +08:00
controller.SendResponse(c, controller.CodeSuccess, "更新计划接口占位符", PlanResponse{ID: 0, Name: "占位计划"})
2025-09-13 14:09:22 +08:00
}
// DeletePlan godoc
// @Summary 删除计划
// @Description 根据计划ID删除计划。
// @Tags 计划管理
// @Produce json
// @Param id path int true "计划ID"
// @Success 200 {object} controller.Response "业务码为200代表删除成功"
// @Failure 200 {object} controller.Response "业务失败具体错误码和信息见响应体例如400, 404, 500"
// @Router /plans/{id} [delete]
func (pc *Controller) DeletePlan(c *gin.Context) {
// 占位符:此处应调用服务层或仓库层来删除计划
pc.logger.Infof("收到删除计划请求 (占位符)")
2025-09-14 13:23:16 +08:00
controller.SendResponse(c, controller.CodeSuccess, "删除计划接口占位符", nil)
2025-09-13 14:09:22 +08:00
}
// StartPlan godoc
// @Summary 启动计划
// @Description 根据计划ID启动一个计划的执行。
// @Tags 计划管理
// @Produce json
// @Param id path int true "计划ID"
// @Success 200 {object} controller.Response "业务码为200代表成功启动计划"
// @Failure 200 {object} controller.Response "业务失败具体错误码和信息见响应体例如400, 404, 500"
// @Router /plans/{id}/start [post]
func (pc *Controller) StartPlan(c *gin.Context) {
// 占位符:此处应调用服务层或仓库层来启动计划
pc.logger.Infof("收到启动计划请求 (占位符)")
2025-09-14 13:23:16 +08:00
controller.SendResponse(c, controller.CodeSuccess, "启动计划接口占位符", nil)
2025-09-13 14:09:22 +08:00
}
// StopPlan godoc
// @Summary 停止计划
// @Description 根据计划ID停止一个正在执行的计划。
// @Tags 计划管理
// @Produce json
// @Param id path int true "计划ID"
// @Success 200 {object} controller.Response "业务码为200代表成功停止计划"
// @Failure 200 {object} controller.Response "业务失败具体错误码和信息见响应体例如400, 404, 500"
// @Router /plans/{id}/stop [post]
func (pc *Controller) StopPlan(c *gin.Context) {
// 占位符:此处应调用服务层或仓库层来停止计划
pc.logger.Infof("收到停止计划请求 (占位符)")
2025-09-14 13:23:16 +08:00
controller.SendResponse(c, controller.CodeSuccess, "停止计划接口占位符", nil)
2025-09-13 14:09:22 +08:00
}