Compare commits
	
		
			7 Commits
		
	
	
		
			32e260ed73
			...
			01b11b6e42
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 01b11b6e42 | |||
| cacb9a9b1d | |||
| 16bc3b08bc | |||
| d169f9b9d1 | |||
| fcea68c7c7 | |||
| 2b431377c1 | |||
| 986bdf15a6 | 
							
								
								
									
										266
									
								
								docs/docs.go
									
									
									
									
									
								
							
							
						
						
									
										266
									
								
								docs/docs.go
									
									
									
									
									
								
							@@ -162,6 +162,211 @@ const docTemplate = `{
 | 
				
			|||||||
                }
 | 
					                }
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        },
 | 
					        },
 | 
				
			||||||
 | 
					        "/plans": {
 | 
				
			||||||
 | 
					            "get": {
 | 
				
			||||||
 | 
					                "description": "获取所有计划的列表",
 | 
				
			||||||
 | 
					                "produces": [
 | 
				
			||||||
 | 
					                    "application/json"
 | 
				
			||||||
 | 
					                ],
 | 
				
			||||||
 | 
					                "tags": [
 | 
				
			||||||
 | 
					                    "计划管理"
 | 
				
			||||||
 | 
					                ],
 | 
				
			||||||
 | 
					                "summary": "获取计划列表",
 | 
				
			||||||
 | 
					                "responses": {
 | 
				
			||||||
 | 
					                    "200": {
 | 
				
			||||||
 | 
					                        "description": "业务失败,具体错误码和信息见响应体(例如400, 500)",
 | 
				
			||||||
 | 
					                        "schema": {
 | 
				
			||||||
 | 
					                            "$ref": "#/definitions/controller.Response"
 | 
				
			||||||
 | 
					                        }
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					            "post": {
 | 
				
			||||||
 | 
					                "description": "创建一个新的计划,包括其基本信息和所有关联的子计划/任务。",
 | 
				
			||||||
 | 
					                "consumes": [
 | 
				
			||||||
 | 
					                    "application/json"
 | 
				
			||||||
 | 
					                ],
 | 
				
			||||||
 | 
					                "produces": [
 | 
				
			||||||
 | 
					                    "application/json"
 | 
				
			||||||
 | 
					                ],
 | 
				
			||||||
 | 
					                "tags": [
 | 
				
			||||||
 | 
					                    "计划管理"
 | 
				
			||||||
 | 
					                ],
 | 
				
			||||||
 | 
					                "summary": "创建计划",
 | 
				
			||||||
 | 
					                "parameters": [
 | 
				
			||||||
 | 
					                    {
 | 
				
			||||||
 | 
					                        "description": "计划信息",
 | 
				
			||||||
 | 
					                        "name": "plan",
 | 
				
			||||||
 | 
					                        "in": "body",
 | 
				
			||||||
 | 
					                        "required": true,
 | 
				
			||||||
 | 
					                        "schema": {
 | 
				
			||||||
 | 
					                            "$ref": "#/definitions/plan.CreatePlanRequest"
 | 
				
			||||||
 | 
					                        }
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                ],
 | 
				
			||||||
 | 
					                "responses": {
 | 
				
			||||||
 | 
					                    "200": {
 | 
				
			||||||
 | 
					                        "description": "业务失败,具体错误码和信息见响应体(例如400, 500)",
 | 
				
			||||||
 | 
					                        "schema": {
 | 
				
			||||||
 | 
					                            "$ref": "#/definitions/controller.Response"
 | 
				
			||||||
 | 
					                        }
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        "/plans/{id}": {
 | 
				
			||||||
 | 
					            "get": {
 | 
				
			||||||
 | 
					                "description": "根据计划ID获取单个计划的详细信息。",
 | 
				
			||||||
 | 
					                "produces": [
 | 
				
			||||||
 | 
					                    "application/json"
 | 
				
			||||||
 | 
					                ],
 | 
				
			||||||
 | 
					                "tags": [
 | 
				
			||||||
 | 
					                    "计划管理"
 | 
				
			||||||
 | 
					                ],
 | 
				
			||||||
 | 
					                "summary": "获取计划详情",
 | 
				
			||||||
 | 
					                "parameters": [
 | 
				
			||||||
 | 
					                    {
 | 
				
			||||||
 | 
					                        "type": "integer",
 | 
				
			||||||
 | 
					                        "description": "计划ID",
 | 
				
			||||||
 | 
					                        "name": "id",
 | 
				
			||||||
 | 
					                        "in": "path",
 | 
				
			||||||
 | 
					                        "required": true
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                ],
 | 
				
			||||||
 | 
					                "responses": {
 | 
				
			||||||
 | 
					                    "200": {
 | 
				
			||||||
 | 
					                        "description": "业务失败,具体错误码和信息见响应体(例如400, 404, 500)",
 | 
				
			||||||
 | 
					                        "schema": {
 | 
				
			||||||
 | 
					                            "$ref": "#/definitions/controller.Response"
 | 
				
			||||||
 | 
					                        }
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					            "put": {
 | 
				
			||||||
 | 
					                "description": "根据计划ID更新计划的详细信息。",
 | 
				
			||||||
 | 
					                "consumes": [
 | 
				
			||||||
 | 
					                    "application/json"
 | 
				
			||||||
 | 
					                ],
 | 
				
			||||||
 | 
					                "produces": [
 | 
				
			||||||
 | 
					                    "application/json"
 | 
				
			||||||
 | 
					                ],
 | 
				
			||||||
 | 
					                "tags": [
 | 
				
			||||||
 | 
					                    "计划管理"
 | 
				
			||||||
 | 
					                ],
 | 
				
			||||||
 | 
					                "summary": "更新计划",
 | 
				
			||||||
 | 
					                "parameters": [
 | 
				
			||||||
 | 
					                    {
 | 
				
			||||||
 | 
					                        "type": "integer",
 | 
				
			||||||
 | 
					                        "description": "计划ID",
 | 
				
			||||||
 | 
					                        "name": "id",
 | 
				
			||||||
 | 
					                        "in": "path",
 | 
				
			||||||
 | 
					                        "required": true
 | 
				
			||||||
 | 
					                    },
 | 
				
			||||||
 | 
					                    {
 | 
				
			||||||
 | 
					                        "description": "更新后的计划信息",
 | 
				
			||||||
 | 
					                        "name": "plan",
 | 
				
			||||||
 | 
					                        "in": "body",
 | 
				
			||||||
 | 
					                        "required": true,
 | 
				
			||||||
 | 
					                        "schema": {
 | 
				
			||||||
 | 
					                            "$ref": "#/definitions/plan.UpdatePlanRequest"
 | 
				
			||||||
 | 
					                        }
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                ],
 | 
				
			||||||
 | 
					                "responses": {
 | 
				
			||||||
 | 
					                    "200": {
 | 
				
			||||||
 | 
					                        "description": "业务失败,具体错误码和信息见响应体(例如400, 404, 500)",
 | 
				
			||||||
 | 
					                        "schema": {
 | 
				
			||||||
 | 
					                            "$ref": "#/definitions/controller.Response"
 | 
				
			||||||
 | 
					                        }
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					            "delete": {
 | 
				
			||||||
 | 
					                "description": "根据计划ID删除计划。",
 | 
				
			||||||
 | 
					                "produces": [
 | 
				
			||||||
 | 
					                    "application/json"
 | 
				
			||||||
 | 
					                ],
 | 
				
			||||||
 | 
					                "tags": [
 | 
				
			||||||
 | 
					                    "计划管理"
 | 
				
			||||||
 | 
					                ],
 | 
				
			||||||
 | 
					                "summary": "删除计划",
 | 
				
			||||||
 | 
					                "parameters": [
 | 
				
			||||||
 | 
					                    {
 | 
				
			||||||
 | 
					                        "type": "integer",
 | 
				
			||||||
 | 
					                        "description": "计划ID",
 | 
				
			||||||
 | 
					                        "name": "id",
 | 
				
			||||||
 | 
					                        "in": "path",
 | 
				
			||||||
 | 
					                        "required": true
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                ],
 | 
				
			||||||
 | 
					                "responses": {
 | 
				
			||||||
 | 
					                    "200": {
 | 
				
			||||||
 | 
					                        "description": "业务失败,具体错误码和信息见响应体(例如400, 404, 500)",
 | 
				
			||||||
 | 
					                        "schema": {
 | 
				
			||||||
 | 
					                            "$ref": "#/definitions/controller.Response"
 | 
				
			||||||
 | 
					                        }
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        "/plans/{id}/start": {
 | 
				
			||||||
 | 
					            "post": {
 | 
				
			||||||
 | 
					                "description": "根据计划ID启动一个计划的执行。",
 | 
				
			||||||
 | 
					                "produces": [
 | 
				
			||||||
 | 
					                    "application/json"
 | 
				
			||||||
 | 
					                ],
 | 
				
			||||||
 | 
					                "tags": [
 | 
				
			||||||
 | 
					                    "计划管理"
 | 
				
			||||||
 | 
					                ],
 | 
				
			||||||
 | 
					                "summary": "启动计划",
 | 
				
			||||||
 | 
					                "parameters": [
 | 
				
			||||||
 | 
					                    {
 | 
				
			||||||
 | 
					                        "type": "integer",
 | 
				
			||||||
 | 
					                        "description": "计划ID",
 | 
				
			||||||
 | 
					                        "name": "id",
 | 
				
			||||||
 | 
					                        "in": "path",
 | 
				
			||||||
 | 
					                        "required": true
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                ],
 | 
				
			||||||
 | 
					                "responses": {
 | 
				
			||||||
 | 
					                    "200": {
 | 
				
			||||||
 | 
					                        "description": "业务失败,具体错误码和信息见响应体(例如400, 404, 500)",
 | 
				
			||||||
 | 
					                        "schema": {
 | 
				
			||||||
 | 
					                            "$ref": "#/definitions/controller.Response"
 | 
				
			||||||
 | 
					                        }
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        "/plans/{id}/stop": {
 | 
				
			||||||
 | 
					            "post": {
 | 
				
			||||||
 | 
					                "description": "根据计划ID停止一个正在执行的计划。",
 | 
				
			||||||
 | 
					                "produces": [
 | 
				
			||||||
 | 
					                    "application/json"
 | 
				
			||||||
 | 
					                ],
 | 
				
			||||||
 | 
					                "tags": [
 | 
				
			||||||
 | 
					                    "计划管理"
 | 
				
			||||||
 | 
					                ],
 | 
				
			||||||
 | 
					                "summary": "停止计划",
 | 
				
			||||||
 | 
					                "parameters": [
 | 
				
			||||||
 | 
					                    {
 | 
				
			||||||
 | 
					                        "type": "integer",
 | 
				
			||||||
 | 
					                        "description": "计划ID",
 | 
				
			||||||
 | 
					                        "name": "id",
 | 
				
			||||||
 | 
					                        "in": "path",
 | 
				
			||||||
 | 
					                        "required": true
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                ],
 | 
				
			||||||
 | 
					                "responses": {
 | 
				
			||||||
 | 
					                    "200": {
 | 
				
			||||||
 | 
					                        "description": "业务失败,具体错误码和信息见响应体(例如400, 404, 500)",
 | 
				
			||||||
 | 
					                        "schema": {
 | 
				
			||||||
 | 
					                            "$ref": "#/definitions/controller.Response"
 | 
				
			||||||
 | 
					                        }
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
        "/users": {
 | 
					        "/users": {
 | 
				
			||||||
            "post": {
 | 
					            "post": {
 | 
				
			||||||
                "description": "根据用户名和密码创建一个新的系统用户。",
 | 
					                "description": "根据用户名和密码创建一个新的系统用户。",
 | 
				
			||||||
@@ -345,6 +550,67 @@ const docTemplate = `{
 | 
				
			|||||||
                "DeviceTypeDevice"
 | 
					                "DeviceTypeDevice"
 | 
				
			||||||
            ]
 | 
					            ]
 | 
				
			||||||
        },
 | 
					        },
 | 
				
			||||||
 | 
					        "plan.CreatePlanRequest": {
 | 
				
			||||||
 | 
					            "type": "object",
 | 
				
			||||||
 | 
					            "required": [
 | 
				
			||||||
 | 
					                "name"
 | 
				
			||||||
 | 
					            ],
 | 
				
			||||||
 | 
					            "properties": {
 | 
				
			||||||
 | 
					                "description": {
 | 
				
			||||||
 | 
					                    "type": "string",
 | 
				
			||||||
 | 
					                    "example": "根据温度自动调节风扇和加热器"
 | 
				
			||||||
 | 
					                },
 | 
				
			||||||
 | 
					                "name": {
 | 
				
			||||||
 | 
					                    "type": "string",
 | 
				
			||||||
 | 
					                    "example": "猪舍温度控制计划"
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        "plan.ListPlansResponse": {
 | 
				
			||||||
 | 
					            "type": "object",
 | 
				
			||||||
 | 
					            "properties": {
 | 
				
			||||||
 | 
					                "plans": {
 | 
				
			||||||
 | 
					                    "type": "array",
 | 
				
			||||||
 | 
					                    "items": {
 | 
				
			||||||
 | 
					                        "$ref": "#/definitions/plan.PlanResponse"
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                },
 | 
				
			||||||
 | 
					                "total": {
 | 
				
			||||||
 | 
					                    "type": "integer",
 | 
				
			||||||
 | 
					                    "example": 100
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        "plan.PlanResponse": {
 | 
				
			||||||
 | 
					            "type": "object",
 | 
				
			||||||
 | 
					            "properties": {
 | 
				
			||||||
 | 
					                "description": {
 | 
				
			||||||
 | 
					                    "type": "string",
 | 
				
			||||||
 | 
					                    "example": "根据温度自动调节风扇和加热器"
 | 
				
			||||||
 | 
					                },
 | 
				
			||||||
 | 
					                "id": {
 | 
				
			||||||
 | 
					                    "type": "integer",
 | 
				
			||||||
 | 
					                    "example": 1
 | 
				
			||||||
 | 
					                },
 | 
				
			||||||
 | 
					                "name": {
 | 
				
			||||||
 | 
					                    "type": "string",
 | 
				
			||||||
 | 
					                    "example": "猪舍温度控制计划"
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        "plan.UpdatePlanRequest": {
 | 
				
			||||||
 | 
					            "type": "object",
 | 
				
			||||||
 | 
					            "properties": {
 | 
				
			||||||
 | 
					                "description": {
 | 
				
			||||||
 | 
					                    "type": "string",
 | 
				
			||||||
 | 
					                    "example": "更新后的描述"
 | 
				
			||||||
 | 
					                },
 | 
				
			||||||
 | 
					                "name": {
 | 
				
			||||||
 | 
					                    "type": "string",
 | 
				
			||||||
 | 
					                    "example": "猪舍温度控制计划V2"
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
        "user.CreateUserRequest": {
 | 
					        "user.CreateUserRequest": {
 | 
				
			||||||
            "type": "object",
 | 
					            "type": "object",
 | 
				
			||||||
            "required": [
 | 
					            "required": [
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -151,6 +151,211 @@
 | 
				
			|||||||
                }
 | 
					                }
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        },
 | 
					        },
 | 
				
			||||||
 | 
					        "/plans": {
 | 
				
			||||||
 | 
					            "get": {
 | 
				
			||||||
 | 
					                "description": "获取所有计划的列表",
 | 
				
			||||||
 | 
					                "produces": [
 | 
				
			||||||
 | 
					                    "application/json"
 | 
				
			||||||
 | 
					                ],
 | 
				
			||||||
 | 
					                "tags": [
 | 
				
			||||||
 | 
					                    "计划管理"
 | 
				
			||||||
 | 
					                ],
 | 
				
			||||||
 | 
					                "summary": "获取计划列表",
 | 
				
			||||||
 | 
					                "responses": {
 | 
				
			||||||
 | 
					                    "200": {
 | 
				
			||||||
 | 
					                        "description": "业务失败,具体错误码和信息见响应体(例如400, 500)",
 | 
				
			||||||
 | 
					                        "schema": {
 | 
				
			||||||
 | 
					                            "$ref": "#/definitions/controller.Response"
 | 
				
			||||||
 | 
					                        }
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					            "post": {
 | 
				
			||||||
 | 
					                "description": "创建一个新的计划,包括其基本信息和所有关联的子计划/任务。",
 | 
				
			||||||
 | 
					                "consumes": [
 | 
				
			||||||
 | 
					                    "application/json"
 | 
				
			||||||
 | 
					                ],
 | 
				
			||||||
 | 
					                "produces": [
 | 
				
			||||||
 | 
					                    "application/json"
 | 
				
			||||||
 | 
					                ],
 | 
				
			||||||
 | 
					                "tags": [
 | 
				
			||||||
 | 
					                    "计划管理"
 | 
				
			||||||
 | 
					                ],
 | 
				
			||||||
 | 
					                "summary": "创建计划",
 | 
				
			||||||
 | 
					                "parameters": [
 | 
				
			||||||
 | 
					                    {
 | 
				
			||||||
 | 
					                        "description": "计划信息",
 | 
				
			||||||
 | 
					                        "name": "plan",
 | 
				
			||||||
 | 
					                        "in": "body",
 | 
				
			||||||
 | 
					                        "required": true,
 | 
				
			||||||
 | 
					                        "schema": {
 | 
				
			||||||
 | 
					                            "$ref": "#/definitions/plan.CreatePlanRequest"
 | 
				
			||||||
 | 
					                        }
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                ],
 | 
				
			||||||
 | 
					                "responses": {
 | 
				
			||||||
 | 
					                    "200": {
 | 
				
			||||||
 | 
					                        "description": "业务失败,具体错误码和信息见响应体(例如400, 500)",
 | 
				
			||||||
 | 
					                        "schema": {
 | 
				
			||||||
 | 
					                            "$ref": "#/definitions/controller.Response"
 | 
				
			||||||
 | 
					                        }
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        "/plans/{id}": {
 | 
				
			||||||
 | 
					            "get": {
 | 
				
			||||||
 | 
					                "description": "根据计划ID获取单个计划的详细信息。",
 | 
				
			||||||
 | 
					                "produces": [
 | 
				
			||||||
 | 
					                    "application/json"
 | 
				
			||||||
 | 
					                ],
 | 
				
			||||||
 | 
					                "tags": [
 | 
				
			||||||
 | 
					                    "计划管理"
 | 
				
			||||||
 | 
					                ],
 | 
				
			||||||
 | 
					                "summary": "获取计划详情",
 | 
				
			||||||
 | 
					                "parameters": [
 | 
				
			||||||
 | 
					                    {
 | 
				
			||||||
 | 
					                        "type": "integer",
 | 
				
			||||||
 | 
					                        "description": "计划ID",
 | 
				
			||||||
 | 
					                        "name": "id",
 | 
				
			||||||
 | 
					                        "in": "path",
 | 
				
			||||||
 | 
					                        "required": true
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                ],
 | 
				
			||||||
 | 
					                "responses": {
 | 
				
			||||||
 | 
					                    "200": {
 | 
				
			||||||
 | 
					                        "description": "业务失败,具体错误码和信息见响应体(例如400, 404, 500)",
 | 
				
			||||||
 | 
					                        "schema": {
 | 
				
			||||||
 | 
					                            "$ref": "#/definitions/controller.Response"
 | 
				
			||||||
 | 
					                        }
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					            "put": {
 | 
				
			||||||
 | 
					                "description": "根据计划ID更新计划的详细信息。",
 | 
				
			||||||
 | 
					                "consumes": [
 | 
				
			||||||
 | 
					                    "application/json"
 | 
				
			||||||
 | 
					                ],
 | 
				
			||||||
 | 
					                "produces": [
 | 
				
			||||||
 | 
					                    "application/json"
 | 
				
			||||||
 | 
					                ],
 | 
				
			||||||
 | 
					                "tags": [
 | 
				
			||||||
 | 
					                    "计划管理"
 | 
				
			||||||
 | 
					                ],
 | 
				
			||||||
 | 
					                "summary": "更新计划",
 | 
				
			||||||
 | 
					                "parameters": [
 | 
				
			||||||
 | 
					                    {
 | 
				
			||||||
 | 
					                        "type": "integer",
 | 
				
			||||||
 | 
					                        "description": "计划ID",
 | 
				
			||||||
 | 
					                        "name": "id",
 | 
				
			||||||
 | 
					                        "in": "path",
 | 
				
			||||||
 | 
					                        "required": true
 | 
				
			||||||
 | 
					                    },
 | 
				
			||||||
 | 
					                    {
 | 
				
			||||||
 | 
					                        "description": "更新后的计划信息",
 | 
				
			||||||
 | 
					                        "name": "plan",
 | 
				
			||||||
 | 
					                        "in": "body",
 | 
				
			||||||
 | 
					                        "required": true,
 | 
				
			||||||
 | 
					                        "schema": {
 | 
				
			||||||
 | 
					                            "$ref": "#/definitions/plan.UpdatePlanRequest"
 | 
				
			||||||
 | 
					                        }
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                ],
 | 
				
			||||||
 | 
					                "responses": {
 | 
				
			||||||
 | 
					                    "200": {
 | 
				
			||||||
 | 
					                        "description": "业务失败,具体错误码和信息见响应体(例如400, 404, 500)",
 | 
				
			||||||
 | 
					                        "schema": {
 | 
				
			||||||
 | 
					                            "$ref": "#/definitions/controller.Response"
 | 
				
			||||||
 | 
					                        }
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					            "delete": {
 | 
				
			||||||
 | 
					                "description": "根据计划ID删除计划。",
 | 
				
			||||||
 | 
					                "produces": [
 | 
				
			||||||
 | 
					                    "application/json"
 | 
				
			||||||
 | 
					                ],
 | 
				
			||||||
 | 
					                "tags": [
 | 
				
			||||||
 | 
					                    "计划管理"
 | 
				
			||||||
 | 
					                ],
 | 
				
			||||||
 | 
					                "summary": "删除计划",
 | 
				
			||||||
 | 
					                "parameters": [
 | 
				
			||||||
 | 
					                    {
 | 
				
			||||||
 | 
					                        "type": "integer",
 | 
				
			||||||
 | 
					                        "description": "计划ID",
 | 
				
			||||||
 | 
					                        "name": "id",
 | 
				
			||||||
 | 
					                        "in": "path",
 | 
				
			||||||
 | 
					                        "required": true
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                ],
 | 
				
			||||||
 | 
					                "responses": {
 | 
				
			||||||
 | 
					                    "200": {
 | 
				
			||||||
 | 
					                        "description": "业务失败,具体错误码和信息见响应体(例如400, 404, 500)",
 | 
				
			||||||
 | 
					                        "schema": {
 | 
				
			||||||
 | 
					                            "$ref": "#/definitions/controller.Response"
 | 
				
			||||||
 | 
					                        }
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        "/plans/{id}/start": {
 | 
				
			||||||
 | 
					            "post": {
 | 
				
			||||||
 | 
					                "description": "根据计划ID启动一个计划的执行。",
 | 
				
			||||||
 | 
					                "produces": [
 | 
				
			||||||
 | 
					                    "application/json"
 | 
				
			||||||
 | 
					                ],
 | 
				
			||||||
 | 
					                "tags": [
 | 
				
			||||||
 | 
					                    "计划管理"
 | 
				
			||||||
 | 
					                ],
 | 
				
			||||||
 | 
					                "summary": "启动计划",
 | 
				
			||||||
 | 
					                "parameters": [
 | 
				
			||||||
 | 
					                    {
 | 
				
			||||||
 | 
					                        "type": "integer",
 | 
				
			||||||
 | 
					                        "description": "计划ID",
 | 
				
			||||||
 | 
					                        "name": "id",
 | 
				
			||||||
 | 
					                        "in": "path",
 | 
				
			||||||
 | 
					                        "required": true
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                ],
 | 
				
			||||||
 | 
					                "responses": {
 | 
				
			||||||
 | 
					                    "200": {
 | 
				
			||||||
 | 
					                        "description": "业务失败,具体错误码和信息见响应体(例如400, 404, 500)",
 | 
				
			||||||
 | 
					                        "schema": {
 | 
				
			||||||
 | 
					                            "$ref": "#/definitions/controller.Response"
 | 
				
			||||||
 | 
					                        }
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        "/plans/{id}/stop": {
 | 
				
			||||||
 | 
					            "post": {
 | 
				
			||||||
 | 
					                "description": "根据计划ID停止一个正在执行的计划。",
 | 
				
			||||||
 | 
					                "produces": [
 | 
				
			||||||
 | 
					                    "application/json"
 | 
				
			||||||
 | 
					                ],
 | 
				
			||||||
 | 
					                "tags": [
 | 
				
			||||||
 | 
					                    "计划管理"
 | 
				
			||||||
 | 
					                ],
 | 
				
			||||||
 | 
					                "summary": "停止计划",
 | 
				
			||||||
 | 
					                "parameters": [
 | 
				
			||||||
 | 
					                    {
 | 
				
			||||||
 | 
					                        "type": "integer",
 | 
				
			||||||
 | 
					                        "description": "计划ID",
 | 
				
			||||||
 | 
					                        "name": "id",
 | 
				
			||||||
 | 
					                        "in": "path",
 | 
				
			||||||
 | 
					                        "required": true
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                ],
 | 
				
			||||||
 | 
					                "responses": {
 | 
				
			||||||
 | 
					                    "200": {
 | 
				
			||||||
 | 
					                        "description": "业务失败,具体错误码和信息见响应体(例如400, 404, 500)",
 | 
				
			||||||
 | 
					                        "schema": {
 | 
				
			||||||
 | 
					                            "$ref": "#/definitions/controller.Response"
 | 
				
			||||||
 | 
					                        }
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
        "/users": {
 | 
					        "/users": {
 | 
				
			||||||
            "post": {
 | 
					            "post": {
 | 
				
			||||||
                "description": "根据用户名和密码创建一个新的系统用户。",
 | 
					                "description": "根据用户名和密码创建一个新的系统用户。",
 | 
				
			||||||
@@ -334,6 +539,67 @@
 | 
				
			|||||||
                "DeviceTypeDevice"
 | 
					                "DeviceTypeDevice"
 | 
				
			||||||
            ]
 | 
					            ]
 | 
				
			||||||
        },
 | 
					        },
 | 
				
			||||||
 | 
					        "plan.CreatePlanRequest": {
 | 
				
			||||||
 | 
					            "type": "object",
 | 
				
			||||||
 | 
					            "required": [
 | 
				
			||||||
 | 
					                "name"
 | 
				
			||||||
 | 
					            ],
 | 
				
			||||||
 | 
					            "properties": {
 | 
				
			||||||
 | 
					                "description": {
 | 
				
			||||||
 | 
					                    "type": "string",
 | 
				
			||||||
 | 
					                    "example": "根据温度自动调节风扇和加热器"
 | 
				
			||||||
 | 
					                },
 | 
				
			||||||
 | 
					                "name": {
 | 
				
			||||||
 | 
					                    "type": "string",
 | 
				
			||||||
 | 
					                    "example": "猪舍温度控制计划"
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        "plan.ListPlansResponse": {
 | 
				
			||||||
 | 
					            "type": "object",
 | 
				
			||||||
 | 
					            "properties": {
 | 
				
			||||||
 | 
					                "plans": {
 | 
				
			||||||
 | 
					                    "type": "array",
 | 
				
			||||||
 | 
					                    "items": {
 | 
				
			||||||
 | 
					                        "$ref": "#/definitions/plan.PlanResponse"
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                },
 | 
				
			||||||
 | 
					                "total": {
 | 
				
			||||||
 | 
					                    "type": "integer",
 | 
				
			||||||
 | 
					                    "example": 100
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        "plan.PlanResponse": {
 | 
				
			||||||
 | 
					            "type": "object",
 | 
				
			||||||
 | 
					            "properties": {
 | 
				
			||||||
 | 
					                "description": {
 | 
				
			||||||
 | 
					                    "type": "string",
 | 
				
			||||||
 | 
					                    "example": "根据温度自动调节风扇和加热器"
 | 
				
			||||||
 | 
					                },
 | 
				
			||||||
 | 
					                "id": {
 | 
				
			||||||
 | 
					                    "type": "integer",
 | 
				
			||||||
 | 
					                    "example": 1
 | 
				
			||||||
 | 
					                },
 | 
				
			||||||
 | 
					                "name": {
 | 
				
			||||||
 | 
					                    "type": "string",
 | 
				
			||||||
 | 
					                    "example": "猪舍温度控制计划"
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        "plan.UpdatePlanRequest": {
 | 
				
			||||||
 | 
					            "type": "object",
 | 
				
			||||||
 | 
					            "properties": {
 | 
				
			||||||
 | 
					                "description": {
 | 
				
			||||||
 | 
					                    "type": "string",
 | 
				
			||||||
 | 
					                    "example": "更新后的描述"
 | 
				
			||||||
 | 
					                },
 | 
				
			||||||
 | 
					                "name": {
 | 
				
			||||||
 | 
					                    "type": "string",
 | 
				
			||||||
 | 
					                    "example": "猪舍温度控制计划V2"
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
        "user.CreateUserRequest": {
 | 
					        "user.CreateUserRequest": {
 | 
				
			||||||
            "type": "object",
 | 
					            "type": "object",
 | 
				
			||||||
            "required": [
 | 
					            "required": [
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -79,6 +79,48 @@ definitions:
 | 
				
			|||||||
    x-enum-varnames:
 | 
					    x-enum-varnames:
 | 
				
			||||||
    - DeviceTypeAreaController
 | 
					    - DeviceTypeAreaController
 | 
				
			||||||
    - DeviceTypeDevice
 | 
					    - DeviceTypeDevice
 | 
				
			||||||
 | 
					  plan.CreatePlanRequest:
 | 
				
			||||||
 | 
					    properties:
 | 
				
			||||||
 | 
					      description:
 | 
				
			||||||
 | 
					        example: 根据温度自动调节风扇和加热器
 | 
				
			||||||
 | 
					        type: string
 | 
				
			||||||
 | 
					      name:
 | 
				
			||||||
 | 
					        example: 猪舍温度控制计划
 | 
				
			||||||
 | 
					        type: string
 | 
				
			||||||
 | 
					    required:
 | 
				
			||||||
 | 
					    - name
 | 
				
			||||||
 | 
					    type: object
 | 
				
			||||||
 | 
					  plan.ListPlansResponse:
 | 
				
			||||||
 | 
					    properties:
 | 
				
			||||||
 | 
					      plans:
 | 
				
			||||||
 | 
					        items:
 | 
				
			||||||
 | 
					          $ref: '#/definitions/plan.PlanResponse'
 | 
				
			||||||
 | 
					        type: array
 | 
				
			||||||
 | 
					      total:
 | 
				
			||||||
 | 
					        example: 100
 | 
				
			||||||
 | 
					        type: integer
 | 
				
			||||||
 | 
					    type: object
 | 
				
			||||||
 | 
					  plan.PlanResponse:
 | 
				
			||||||
 | 
					    properties:
 | 
				
			||||||
 | 
					      description:
 | 
				
			||||||
 | 
					        example: 根据温度自动调节风扇和加热器
 | 
				
			||||||
 | 
					        type: string
 | 
				
			||||||
 | 
					      id:
 | 
				
			||||||
 | 
					        example: 1
 | 
				
			||||||
 | 
					        type: integer
 | 
				
			||||||
 | 
					      name:
 | 
				
			||||||
 | 
					        example: 猪舍温度控制计划
 | 
				
			||||||
 | 
					        type: string
 | 
				
			||||||
 | 
					    type: object
 | 
				
			||||||
 | 
					  plan.UpdatePlanRequest:
 | 
				
			||||||
 | 
					    properties:
 | 
				
			||||||
 | 
					      description:
 | 
				
			||||||
 | 
					        example: 更新后的描述
 | 
				
			||||||
 | 
					        type: string
 | 
				
			||||||
 | 
					      name:
 | 
				
			||||||
 | 
					        example: 猪舍温度控制计划V2
 | 
				
			||||||
 | 
					        type: string
 | 
				
			||||||
 | 
					    type: object
 | 
				
			||||||
  user.CreateUserRequest:
 | 
					  user.CreateUserRequest:
 | 
				
			||||||
    properties:
 | 
					    properties:
 | 
				
			||||||
      password:
 | 
					      password:
 | 
				
			||||||
@@ -225,6 +267,141 @@ paths:
 | 
				
			|||||||
      summary: 更新设备信息
 | 
					      summary: 更新设备信息
 | 
				
			||||||
      tags:
 | 
					      tags:
 | 
				
			||||||
      - 设备管理
 | 
					      - 设备管理
 | 
				
			||||||
 | 
					  /plans:
 | 
				
			||||||
 | 
					    get:
 | 
				
			||||||
 | 
					      description: 获取所有计划的列表
 | 
				
			||||||
 | 
					      produces:
 | 
				
			||||||
 | 
					      - application/json
 | 
				
			||||||
 | 
					      responses:
 | 
				
			||||||
 | 
					        "200":
 | 
				
			||||||
 | 
					          description: 业务失败,具体错误码和信息见响应体(例如400, 500)
 | 
				
			||||||
 | 
					          schema:
 | 
				
			||||||
 | 
					            $ref: '#/definitions/controller.Response'
 | 
				
			||||||
 | 
					      summary: 获取计划列表
 | 
				
			||||||
 | 
					      tags:
 | 
				
			||||||
 | 
					      - 计划管理
 | 
				
			||||||
 | 
					    post:
 | 
				
			||||||
 | 
					      consumes:
 | 
				
			||||||
 | 
					      - application/json
 | 
				
			||||||
 | 
					      description: 创建一个新的计划,包括其基本信息和所有关联的子计划/任务。
 | 
				
			||||||
 | 
					      parameters:
 | 
				
			||||||
 | 
					      - description: 计划信息
 | 
				
			||||||
 | 
					        in: body
 | 
				
			||||||
 | 
					        name: plan
 | 
				
			||||||
 | 
					        required: true
 | 
				
			||||||
 | 
					        schema:
 | 
				
			||||||
 | 
					          $ref: '#/definitions/plan.CreatePlanRequest'
 | 
				
			||||||
 | 
					      produces:
 | 
				
			||||||
 | 
					      - application/json
 | 
				
			||||||
 | 
					      responses:
 | 
				
			||||||
 | 
					        "200":
 | 
				
			||||||
 | 
					          description: 业务失败,具体错误码和信息见响应体(例如400, 500)
 | 
				
			||||||
 | 
					          schema:
 | 
				
			||||||
 | 
					            $ref: '#/definitions/controller.Response'
 | 
				
			||||||
 | 
					      summary: 创建计划
 | 
				
			||||||
 | 
					      tags:
 | 
				
			||||||
 | 
					      - 计划管理
 | 
				
			||||||
 | 
					  /plans/{id}:
 | 
				
			||||||
 | 
					    delete:
 | 
				
			||||||
 | 
					      description: 根据计划ID删除计划。
 | 
				
			||||||
 | 
					      parameters:
 | 
				
			||||||
 | 
					      - description: 计划ID
 | 
				
			||||||
 | 
					        in: path
 | 
				
			||||||
 | 
					        name: id
 | 
				
			||||||
 | 
					        required: true
 | 
				
			||||||
 | 
					        type: integer
 | 
				
			||||||
 | 
					      produces:
 | 
				
			||||||
 | 
					      - application/json
 | 
				
			||||||
 | 
					      responses:
 | 
				
			||||||
 | 
					        "200":
 | 
				
			||||||
 | 
					          description: 业务失败,具体错误码和信息见响应体(例如400, 404, 500)
 | 
				
			||||||
 | 
					          schema:
 | 
				
			||||||
 | 
					            $ref: '#/definitions/controller.Response'
 | 
				
			||||||
 | 
					      summary: 删除计划
 | 
				
			||||||
 | 
					      tags:
 | 
				
			||||||
 | 
					      - 计划管理
 | 
				
			||||||
 | 
					    get:
 | 
				
			||||||
 | 
					      description: 根据计划ID获取单个计划的详细信息。
 | 
				
			||||||
 | 
					      parameters:
 | 
				
			||||||
 | 
					      - description: 计划ID
 | 
				
			||||||
 | 
					        in: path
 | 
				
			||||||
 | 
					        name: id
 | 
				
			||||||
 | 
					        required: true
 | 
				
			||||||
 | 
					        type: integer
 | 
				
			||||||
 | 
					      produces:
 | 
				
			||||||
 | 
					      - application/json
 | 
				
			||||||
 | 
					      responses:
 | 
				
			||||||
 | 
					        "200":
 | 
				
			||||||
 | 
					          description: 业务失败,具体错误码和信息见响应体(例如400, 404, 500)
 | 
				
			||||||
 | 
					          schema:
 | 
				
			||||||
 | 
					            $ref: '#/definitions/controller.Response'
 | 
				
			||||||
 | 
					      summary: 获取计划详情
 | 
				
			||||||
 | 
					      tags:
 | 
				
			||||||
 | 
					      - 计划管理
 | 
				
			||||||
 | 
					    put:
 | 
				
			||||||
 | 
					      consumes:
 | 
				
			||||||
 | 
					      - application/json
 | 
				
			||||||
 | 
					      description: 根据计划ID更新计划的详细信息。
 | 
				
			||||||
 | 
					      parameters:
 | 
				
			||||||
 | 
					      - description: 计划ID
 | 
				
			||||||
 | 
					        in: path
 | 
				
			||||||
 | 
					        name: id
 | 
				
			||||||
 | 
					        required: true
 | 
				
			||||||
 | 
					        type: integer
 | 
				
			||||||
 | 
					      - description: 更新后的计划信息
 | 
				
			||||||
 | 
					        in: body
 | 
				
			||||||
 | 
					        name: plan
 | 
				
			||||||
 | 
					        required: true
 | 
				
			||||||
 | 
					        schema:
 | 
				
			||||||
 | 
					          $ref: '#/definitions/plan.UpdatePlanRequest'
 | 
				
			||||||
 | 
					      produces:
 | 
				
			||||||
 | 
					      - application/json
 | 
				
			||||||
 | 
					      responses:
 | 
				
			||||||
 | 
					        "200":
 | 
				
			||||||
 | 
					          description: 业务失败,具体错误码和信息见响应体(例如400, 404, 500)
 | 
				
			||||||
 | 
					          schema:
 | 
				
			||||||
 | 
					            $ref: '#/definitions/controller.Response'
 | 
				
			||||||
 | 
					      summary: 更新计划
 | 
				
			||||||
 | 
					      tags:
 | 
				
			||||||
 | 
					      - 计划管理
 | 
				
			||||||
 | 
					  /plans/{id}/start:
 | 
				
			||||||
 | 
					    post:
 | 
				
			||||||
 | 
					      description: 根据计划ID启动一个计划的执行。
 | 
				
			||||||
 | 
					      parameters:
 | 
				
			||||||
 | 
					      - description: 计划ID
 | 
				
			||||||
 | 
					        in: path
 | 
				
			||||||
 | 
					        name: id
 | 
				
			||||||
 | 
					        required: true
 | 
				
			||||||
 | 
					        type: integer
 | 
				
			||||||
 | 
					      produces:
 | 
				
			||||||
 | 
					      - application/json
 | 
				
			||||||
 | 
					      responses:
 | 
				
			||||||
 | 
					        "200":
 | 
				
			||||||
 | 
					          description: 业务失败,具体错误码和信息见响应体(例如400, 404, 500)
 | 
				
			||||||
 | 
					          schema:
 | 
				
			||||||
 | 
					            $ref: '#/definitions/controller.Response'
 | 
				
			||||||
 | 
					      summary: 启动计划
 | 
				
			||||||
 | 
					      tags:
 | 
				
			||||||
 | 
					      - 计划管理
 | 
				
			||||||
 | 
					  /plans/{id}/stop:
 | 
				
			||||||
 | 
					    post:
 | 
				
			||||||
 | 
					      description: 根据计划ID停止一个正在执行的计划。
 | 
				
			||||||
 | 
					      parameters:
 | 
				
			||||||
 | 
					      - description: 计划ID
 | 
				
			||||||
 | 
					        in: path
 | 
				
			||||||
 | 
					        name: id
 | 
				
			||||||
 | 
					        required: true
 | 
				
			||||||
 | 
					        type: integer
 | 
				
			||||||
 | 
					      produces:
 | 
				
			||||||
 | 
					      - application/json
 | 
				
			||||||
 | 
					      responses:
 | 
				
			||||||
 | 
					        "200":
 | 
				
			||||||
 | 
					          description: 业务失败,具体错误码和信息见响应体(例如400, 404, 500)
 | 
				
			||||||
 | 
					          schema:
 | 
				
			||||||
 | 
					            $ref: '#/definitions/controller.Response'
 | 
				
			||||||
 | 
					      summary: 停止计划
 | 
				
			||||||
 | 
					      tags:
 | 
				
			||||||
 | 
					      - 计划管理
 | 
				
			||||||
  /users:
 | 
					  /users:
 | 
				
			||||||
    post:
 | 
					    post:
 | 
				
			||||||
      consumes:
 | 
					      consumes:
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -17,6 +17,7 @@ import (
 | 
				
			|||||||
	"time"
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"git.huangwc.com/pig/pig-farm-controller/internal/app/controller/device"
 | 
						"git.huangwc.com/pig/pig-farm-controller/internal/app/controller/device"
 | 
				
			||||||
 | 
						"git.huangwc.com/pig/pig-farm-controller/internal/app/controller/plan"
 | 
				
			||||||
	"git.huangwc.com/pig/pig-farm-controller/internal/app/controller/user"
 | 
						"git.huangwc.com/pig/pig-farm-controller/internal/app/controller/user"
 | 
				
			||||||
	"git.huangwc.com/pig/pig-farm-controller/internal/app/service/token"
 | 
						"git.huangwc.com/pig/pig-farm-controller/internal/app/service/token"
 | 
				
			||||||
	"git.huangwc.com/pig/pig-farm-controller/internal/infra/config"
 | 
						"git.huangwc.com/pig/pig-farm-controller/internal/infra/config"
 | 
				
			||||||
@@ -39,11 +40,12 @@ type API struct {
 | 
				
			|||||||
	config           config.ServerConfig       // API 服务器的配置,使用 infra/config 包中的 ServerConfig
 | 
						config           config.ServerConfig       // API 服务器的配置,使用 infra/config 包中的 ServerConfig
 | 
				
			||||||
	userController   *user.Controller          // 用户控制器实例
 | 
						userController   *user.Controller          // 用户控制器实例
 | 
				
			||||||
	deviceController *device.Controller        // 设备控制器实例
 | 
						deviceController *device.Controller        // 设备控制器实例
 | 
				
			||||||
 | 
						planController   *plan.Controller          // 计划控制器实例
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// NewAPI 创建并返回一个新的 API 实例
 | 
					// NewAPI 创建并返回一个新的 API 实例
 | 
				
			||||||
// 负责初始化 Gin 引擎、设置全局中间件,并注入所有必要的依赖。
 | 
					// 负责初始化 Gin 引擎、设置全局中间件,并注入所有必要的依赖。
 | 
				
			||||||
func NewAPI(cfg config.ServerConfig, logger *logs.Logger, userRepo repository.UserRepository, deviceRepository repository.DeviceRepository, tokenService token.TokenService) *API {
 | 
					func NewAPI(cfg config.ServerConfig, logger *logs.Logger, userRepo repository.UserRepository, deviceRepository repository.DeviceRepository, planRepository repository.PlanRepository, tokenService token.TokenService) *API {
 | 
				
			||||||
	// 设置 Gin 模式,例如 gin.ReleaseMode (生产模式) 或 gin.DebugMode (开发模式)
 | 
						// 设置 Gin 模式,例如 gin.ReleaseMode (生产模式) 或 gin.DebugMode (开发模式)
 | 
				
			||||||
	// 从配置中获取 Gin 模式
 | 
						// 从配置中获取 Gin 模式
 | 
				
			||||||
	gin.SetMode(cfg.Mode)
 | 
						gin.SetMode(cfg.Mode)
 | 
				
			||||||
@@ -67,6 +69,8 @@ func NewAPI(cfg config.ServerConfig, logger *logs.Logger, userRepo repository.Us
 | 
				
			|||||||
		userController: user.NewController(userRepo, logger, tokenService),
 | 
							userController: user.NewController(userRepo, logger, tokenService),
 | 
				
			||||||
		// 在 NewAPI 中初始化设备控制器,并将其作为 API 结构体的成员
 | 
							// 在 NewAPI 中初始化设备控制器,并将其作为 API 结构体的成员
 | 
				
			||||||
		deviceController: device.NewController(deviceRepository, logger),
 | 
							deviceController: device.NewController(deviceRepository, logger),
 | 
				
			||||||
 | 
							// 在 NewAPI 中初始化计划控制器,并将其作为 API 结构体的成员
 | 
				
			||||||
 | 
							planController: plan.NewController(logger, planRepository),
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	api.setupRoutes() // 设置所有路由
 | 
						api.setupRoutes() // 设置所有路由
 | 
				
			||||||
@@ -95,6 +99,18 @@ func (a *API) setupRoutes() {
 | 
				
			|||||||
			deviceGroup.PUT("/:id", a.deviceController.UpdateDevice)
 | 
								deviceGroup.PUT("/:id", a.deviceController.UpdateDevice)
 | 
				
			||||||
			deviceGroup.DELETE("/:id", a.deviceController.DeleteDevice)
 | 
								deviceGroup.DELETE("/:id", a.deviceController.DeleteDevice)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// 计划相关路由组
 | 
				
			||||||
 | 
							planGroup := v1.Group("/plans")
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								planGroup.POST("", a.planController.CreatePlan)
 | 
				
			||||||
 | 
								planGroup.GET("", a.planController.ListPlans)
 | 
				
			||||||
 | 
								planGroup.GET("/:id", a.planController.GetPlan)
 | 
				
			||||||
 | 
								planGroup.PUT("/:id", a.planController.UpdatePlan)
 | 
				
			||||||
 | 
								planGroup.DELETE("/:id", a.planController.DeletePlan)
 | 
				
			||||||
 | 
								planGroup.POST("/:id/start", a.planController.StartPlan)
 | 
				
			||||||
 | 
								planGroup.POST("/:id/stop", a.planController.StopPlan)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// 添加 Swagger UI 路由
 | 
						// 添加 Swagger UI 路由
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										168
									
								
								internal/app/controller/plan/plan_controller.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										168
									
								
								internal/app/controller/plan/plan_controller.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,168 @@
 | 
				
			|||||||
 | 
					package plan
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"net/http"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"git.huangwc.com/pig/pig-farm-controller/internal/app/controller"
 | 
				
			||||||
 | 
						"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
 | 
				
			||||||
 | 
						"git.huangwc.com/pig/pig-farm-controller/internal/infra/repository"
 | 
				
			||||||
 | 
						"github.com/gin-gonic/gin"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// --- 请求和响应 DTO 定义 ---
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// CreatePlanRequest 定义创建计划请求的结构体
 | 
				
			||||||
 | 
					type CreatePlanRequest struct {
 | 
				
			||||||
 | 
						Name        string `json:"name" binding:"required" example:"猪舍温度控制计划"`
 | 
				
			||||||
 | 
						Description string `json:"description" example:"根据温度自动调节风扇和加热器"`
 | 
				
			||||||
 | 
						// 更多计划基本信息字段
 | 
				
			||||||
 | 
						// SubPlans 或 Tasks 可以在这里嵌套定义,以支持一次性创建
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// PlanResponse 定义计划详情响应的结构体
 | 
				
			||||||
 | 
					type PlanResponse struct {
 | 
				
			||||||
 | 
						ID          uint   `json:"id" example:"1"`
 | 
				
			||||||
 | 
						Name        string `json:"name" example:"猪舍温度控制计划"`
 | 
				
			||||||
 | 
						Description string `json:"description" example:"根据温度自动调节风扇和加热器"`
 | 
				
			||||||
 | 
						// 更多计划基本信息字段
 | 
				
			||||||
 | 
						// SubPlans 或 Tasks 也可以在这里返回
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// ListPlansResponse 定义获取计划列表响应的结构体
 | 
				
			||||||
 | 
					type ListPlansResponse struct {
 | 
				
			||||||
 | 
						Plans []PlanResponse `json:"plans"`
 | 
				
			||||||
 | 
						Total int            `json:"total" example:"100"`
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// UpdatePlanRequest 定义更新计划请求的结构体
 | 
				
			||||||
 | 
					type UpdatePlanRequest struct {
 | 
				
			||||||
 | 
						Name        string `json:"name" example:"猪舍温度控制计划V2"`
 | 
				
			||||||
 | 
						Description string `json:"description" example:"更新后的描述"`
 | 
				
			||||||
 | 
						// 更多可更新字段
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// --- 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("收到创建计划请求 (占位符)")
 | 
				
			||||||
 | 
						controller.SendResponse(c, http.StatusCreated, "创建计划接口占位符", PlanResponse{ID: 0, Name: "占位计划"})
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 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("收到获取计划 (占位符)")
 | 
				
			||||||
 | 
						controller.SendResponse(c, http.StatusOK, "获取计划接口占位符", ListPlansResponse{Plans: []PlanResponse{}, Total: 0})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 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("收到获取计划列表请求 (占位符)")
 | 
				
			||||||
 | 
						controller.SendResponse(c, http.StatusOK, "获取计划列表接口占位符", ListPlansResponse{Plans: []PlanResponse{}, Total: 0})
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 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("收到更新计划请求 (占位符)")
 | 
				
			||||||
 | 
						controller.SendResponse(c, http.StatusOK, "更新计划接口占位符", PlanResponse{ID: 0, Name: "占位计划"})
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 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("收到删除计划请求 (占位符)")
 | 
				
			||||||
 | 
						controller.SendResponse(c, http.StatusOK, "删除计划接口占位符", nil)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 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("收到启动计划请求 (占位符)")
 | 
				
			||||||
 | 
						controller.SendResponse(c, http.StatusOK, "启动计划接口占位符", nil)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 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("收到停止计划请求 (占位符)")
 | 
				
			||||||
 | 
						controller.SendResponse(c, http.StatusOK, "停止计划接口占位符", nil)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -55,10 +55,13 @@ func NewApplication(configPath string) (*Application, error) {
 | 
				
			|||||||
	// 7. 初始化设备仓库
 | 
						// 7. 初始化设备仓库
 | 
				
			||||||
	deviceRepo := repository.NewGormDeviceRepository(storage.GetDB())
 | 
						deviceRepo := repository.NewGormDeviceRepository(storage.GetDB())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// 8. 初始化 API 服务器
 | 
						// 8. 初始化计划仓库
 | 
				
			||||||
	apiServer := api.NewAPI(cfg.Server, logger, userRepo, deviceRepo, tokenService)
 | 
						planRepo := repository.NewGormPlanRepository(storage.GetDB())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// 9. 组装 Application 对象
 | 
						// 9. 初始化 API 服务器
 | 
				
			||||||
 | 
						apiServer := api.NewAPI(cfg.Server, logger, userRepo, deviceRepo, planRepo, tokenService)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 10. 组装 Application 对象
 | 
				
			||||||
	app := &Application{
 | 
						app := &Application{
 | 
				
			||||||
		Config:   cfg,
 | 
							Config:   cfg,
 | 
				
			||||||
		Logger:   logger,
 | 
							Logger:   logger,
 | 
				
			||||||
@@ -121,15 +124,7 @@ func initStorage(cfg config.DatabaseConfig, logger *logs.Logger) (database.Stora
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// 执行数据库迁移
 | 
						// 执行数据库迁移
 | 
				
			||||||
	// 这里需要添加所有需要自动迁移的模型
 | 
						if err := storage.Migrate(models.GetAllModels()...); err != nil {
 | 
				
			||||||
	var dbModels = []interface{}{
 | 
					 | 
				
			||||||
		&models.User{},
 | 
					 | 
				
			||||||
		&models.Device{},
 | 
					 | 
				
			||||||
		&models.Plan{},
 | 
					 | 
				
			||||||
		&models.SubPlan{},
 | 
					 | 
				
			||||||
		&models.Task{},
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	if err := storage.Migrate(dbModels...); err != nil {
 | 
					 | 
				
			||||||
		return nil, fmt.Errorf("数据库迁移失败: %w", err)
 | 
							return nil, fmt.Errorf("数据库迁移失败: %w", err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										13
									
								
								internal/infra/models/models.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								internal/infra/models/models.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,13 @@
 | 
				
			|||||||
 | 
					package models
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// GetAllModels 返回一个包含所有数据库模型实例的切片。
 | 
				
			||||||
 | 
					// 这个函数用于在数据库初始化时自动迁移所有的表结构。
 | 
				
			||||||
 | 
					func GetAllModels() []interface{} {
 | 
				
			||||||
 | 
						return []interface{}{
 | 
				
			||||||
 | 
							&User{},
 | 
				
			||||||
 | 
							&Device{},
 | 
				
			||||||
 | 
							&Plan{},
 | 
				
			||||||
 | 
							&SubPlan{},
 | 
				
			||||||
 | 
							&Task{},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -60,7 +60,8 @@ type SubPlan struct {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	ParentPlanID   uint  `gorm:"not null;index" json:"parent_plan_id"` // 父计划的ID
 | 
						ParentPlanID   uint  `gorm:"not null;index" json:"parent_plan_id"` // 父计划的ID
 | 
				
			||||||
	ChildPlanID    uint  `gorm:"not null;index" json:"child_plan_id"`  // 子计划的ID (它本身也是一个 Plan)
 | 
						ChildPlanID    uint  `gorm:"not null;index" json:"child_plan_id"`  // 子计划的ID (它本身也是一个 Plan)
 | 
				
			||||||
	Order        int  `gorm:"not null" json:"order"`                // 在父计划中的执行顺序
 | 
						ExecutionOrder int   `gorm:"not null" json:"execution_order"`      // 在父计划中的执行顺序
 | 
				
			||||||
 | 
						ChildPlan      *Plan `gorm:"-" json:"child_plan"`                  // 完整子计划数据,仅内存中
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// TableName 自定义 GORM 使用的数据库表名
 | 
					// TableName 自定义 GORM 使用的数据库表名
 | 
				
			||||||
@@ -75,7 +76,7 @@ type Task struct {
 | 
				
			|||||||
	PlanID         uint           `gorm:"not null;index" json:"plan_id"` // 此任务所属计划的ID
 | 
						PlanID         uint           `gorm:"not null;index" json:"plan_id"` // 此任务所属计划的ID
 | 
				
			||||||
	Name           string         `gorm:"not null" json:"name"`
 | 
						Name           string         `gorm:"not null" json:"name"`
 | 
				
			||||||
	Description    string         `json:"description"`
 | 
						Description    string         `json:"description"`
 | 
				
			||||||
	Order       int            `gorm:"not null" json:"order"` // 在计划中的执行顺序
 | 
						ExecutionOrder int            `gorm:"not null" json:"execution_order"` // 在计划中的执行顺序
 | 
				
			||||||
	Type           TaskType       `gorm:"not null" json:"type"`            // 任务的类型,对应 task 包中的具体动作
 | 
						Type           TaskType       `gorm:"not null" json:"type"`            // 任务的类型,对应 task 包中的具体动作
 | 
				
			||||||
	Parameters     datatypes.JSON `json:"parameters"`                      // 任务特定参数的JSON (例如: 设备ID, 值)
 | 
						Parameters     datatypes.JSON `json:"parameters"`                      // 任务特定参数的JSON (例如: 设备ID, 值)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -12,12 +12,12 @@ import (
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// setupTestDB 是一个共享的辅助函数,用于为集成测试创建一个干净的、内存中的 SQLite 数据库实例。
 | 
					// setupTestDB 是一个共享的辅助函数,用于为集成测试创建一个干净的、内存中的 SQLite 数据库实例。
 | 
				
			||||||
func setupTestDB(t *testing.T) *gorm.DB {
 | 
					func setupTestDB(t *testing.T) *gorm.DB {
 | 
				
			||||||
	// "file::memory:?cache=shared" 是 GORM 连接内存 SQLite 的标准方式,确保在同一测试中的不同连接可以访问相同的数据。
 | 
						// "file::memory:?cache=shared" 是 GORM 连接内存 SQLite 的标准方式,确保在同一测试中的不同连接可以访问相同的数据,而我们显然不需要这个
 | 
				
			||||||
	db, err := gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{})
 | 
						db, err := gorm.Open(sqlite.Open("file::memory:"), &gorm.Config{})
 | 
				
			||||||
	assert.NoError(t, err, "连接内存数据库时发生错误")
 | 
						assert.NoError(t, err, "连接内存数据库时发生错误")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// 自动迁移所有需要的表结构
 | 
						// 自动迁移所有需要的表结构
 | 
				
			||||||
	err = db.AutoMigrate(&models.User{}, &models.Device{}, &models.SubPlan{}, &models.Task{}, &models.Plan{})
 | 
						err = db.AutoMigrate(models.GetAllModels()...)
 | 
				
			||||||
	assert.NoError(t, err, "数据库迁移时发生错误")
 | 
						assert.NoError(t, err, "数据库迁移时发生错误")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return db
 | 
						return db
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,6 +1,8 @@
 | 
				
			|||||||
package repository
 | 
					package repository
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
 | 
						"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
 | 
				
			||||||
	"gorm.io/gorm"
 | 
						"gorm.io/gorm"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
@@ -12,6 +14,8 @@ type PlanRepository interface {
 | 
				
			|||||||
	ListBasicPlans() ([]models.Plan, error)
 | 
						ListBasicPlans() ([]models.Plan, error)
 | 
				
			||||||
	// GetBasicPlanByID 根据ID获取计划的基本信息,不包含子计划和任务详情
 | 
						// GetBasicPlanByID 根据ID获取计划的基本信息,不包含子计划和任务详情
 | 
				
			||||||
	GetBasicPlanByID(id uint) (*models.Plan, error)
 | 
						GetBasicPlanByID(id uint) (*models.Plan, error)
 | 
				
			||||||
 | 
						// GetPlanByID 根据ID获取计划,包含子计划和任务详情
 | 
				
			||||||
 | 
						GetPlanByID(id uint) (*models.Plan, error)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// gormPlanRepository 是 PlanRepository 的 GORM 实现
 | 
					// gormPlanRepository 是 PlanRepository 的 GORM 实现
 | 
				
			||||||
@@ -47,3 +51,48 @@ func (r *gormPlanRepository) GetBasicPlanByID(id uint) (*models.Plan, error) {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
	return &plan, nil
 | 
						return &plan, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// GetPlanByID 根据ID获取计划,包含子计划和任务详情
 | 
				
			||||||
 | 
					func (r *gormPlanRepository) GetPlanByID(id uint) (*models.Plan, error) {
 | 
				
			||||||
 | 
						var plan models.Plan
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 先获取基本计划信息
 | 
				
			||||||
 | 
						result := r.db.First(&plan, id)
 | 
				
			||||||
 | 
						if result.Error != nil {
 | 
				
			||||||
 | 
							return nil, result.Error
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 根据内容类型加载关联数据
 | 
				
			||||||
 | 
						switch plan.ContentType {
 | 
				
			||||||
 | 
						case models.PlanContentTypeSubPlans:
 | 
				
			||||||
 | 
							// 加载子计划引用
 | 
				
			||||||
 | 
							var subPlans []models.SubPlan
 | 
				
			||||||
 | 
							result = r.db.Where("parent_plan_id = ?", plan.ID).Order("execution_order").Find(&subPlans)
 | 
				
			||||||
 | 
							if result.Error != nil {
 | 
				
			||||||
 | 
								return nil, result.Error
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// 递归加载每个子计划的完整信息
 | 
				
			||||||
 | 
							for i := range subPlans {
 | 
				
			||||||
 | 
								childPlan, err := r.GetPlanByID(subPlans[i].ChildPlanID)
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									return nil, err
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								subPlans[i].ChildPlan = childPlan
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							plan.SubPlans = subPlans
 | 
				
			||||||
 | 
						case models.PlanContentTypeTasks:
 | 
				
			||||||
 | 
							// 加载任务
 | 
				
			||||||
 | 
							result = r.db.Preload("Tasks", func(taskDB *gorm.DB) *gorm.DB {
 | 
				
			||||||
 | 
								return taskDB.Order("execution_order")
 | 
				
			||||||
 | 
							}).First(&plan, id)
 | 
				
			||||||
 | 
							if result.Error != nil {
 | 
				
			||||||
 | 
								return nil, result.Error
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						default:
 | 
				
			||||||
 | 
							return nil, fmt.Errorf("未知的计划内容类型: %v; 计划ID: %v", plan.ContentType, plan.ID)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return &plan, nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,7 +2,9 @@ package repository_test
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"errors"
 | 
						"errors"
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
	"testing"
 | 
						"testing"
 | 
				
			||||||
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
 | 
						"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
 | 
				
			||||||
	"git.huangwc.com/pig/pig-farm-controller/internal/infra/repository"
 | 
						"git.huangwc.com/pig/pig-farm-controller/internal/infra/repository"
 | 
				
			||||||
@@ -134,3 +136,265 @@ func TestGetBasicPlanByID(t *testing.T) {
 | 
				
			|||||||
		})
 | 
							})
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// TestGetPlanByID 测试 GetPlanByID 方法,确保它能根据ID正确返回计划的完整信息,包括关联数据。
 | 
				
			||||||
 | 
					func TestGetPlanByID(t *testing.T) {
 | 
				
			||||||
 | 
						type testCase struct {
 | 
				
			||||||
 | 
							name          string
 | 
				
			||||||
 | 
							setupData     func(db *gorm.DB) // 用于在测试前插入数据
 | 
				
			||||||
 | 
							planID        uint
 | 
				
			||||||
 | 
							expectedPlan  *models.Plan
 | 
				
			||||||
 | 
							expectedError string
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						testCases := []testCase{
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								name: "PlanNotFound",
 | 
				
			||||||
 | 
								setupData: func(db *gorm.DB) {
 | 
				
			||||||
 | 
									// 不插入任何数据
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
								planID:        999,
 | 
				
			||||||
 | 
								expectedPlan:  nil,
 | 
				
			||||||
 | 
								expectedError: "record not found",
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								name: "PlanWithTasks",
 | 
				
			||||||
 | 
								setupData: func(db *gorm.DB) {
 | 
				
			||||||
 | 
									// 使用硬编码的ID创建计划,使测试可预测
 | 
				
			||||||
 | 
									plan := models.Plan{
 | 
				
			||||||
 | 
										Model:       gorm.Model{ID: 1},
 | 
				
			||||||
 | 
										Name:        "Plan A",
 | 
				
			||||||
 | 
										ContentType: models.PlanContentTypeTasks,
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
									db.Create(&plan)
 | 
				
			||||||
 | 
									// 创建任务,它们的ID将由数据库自动生成
 | 
				
			||||||
 | 
									db.Create(&models.Task{PlanID: 1, Name: "Task 2", ExecutionOrder: 1})
 | 
				
			||||||
 | 
									db.Create(&models.Task{PlanID: 1, Name: "Task 1", ExecutionOrder: 2})
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
								planID: 1,
 | 
				
			||||||
 | 
								expectedPlan: &models.Plan{
 | 
				
			||||||
 | 
									Model:       gorm.Model{ID: 1},
 | 
				
			||||||
 | 
									Name:        "Plan A",
 | 
				
			||||||
 | 
									ContentType: models.PlanContentTypeTasks,
 | 
				
			||||||
 | 
									Tasks: []models.Task{
 | 
				
			||||||
 | 
										// 期望按 "order" 字段升序排序
 | 
				
			||||||
 | 
										{PlanID: 1, Name: "Task 2", ExecutionOrder: 1},
 | 
				
			||||||
 | 
										{PlanID: 1, Name: "Task 1", ExecutionOrder: 2},
 | 
				
			||||||
 | 
									},
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
								expectedError: "",
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								name: "PlanWithMultiLevelSubPlans",
 | 
				
			||||||
 | 
								setupData: func(db *gorm.DB) {
 | 
				
			||||||
 | 
									// 创建一个三层结构的计划
 | 
				
			||||||
 | 
									db.Create(&models.Plan{Model: gorm.Model{ID: 20}, Name: "Grandparent Plan", ContentType: models.PlanContentTypeSubPlans})
 | 
				
			||||||
 | 
									db.Create(&models.Plan{Model: gorm.Model{ID: 21}, Name: "Parent Plan", ContentType: models.PlanContentTypeSubPlans})
 | 
				
			||||||
 | 
									db.Create(&models.Plan{Model: gorm.Model{ID: 22}, Name: "Child Plan With Tasks", ContentType: models.PlanContentTypeTasks})
 | 
				
			||||||
 | 
									db.Create(&models.Task{PlanID: 22, Name: "Grandchild's Task", ExecutionOrder: 1})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									// 创建关联关系
 | 
				
			||||||
 | 
									db.Create(&models.SubPlan{ParentPlanID: 20, ChildPlanID: 21, ExecutionOrder: 1})
 | 
				
			||||||
 | 
									db.Create(&models.SubPlan{ParentPlanID: 21, ChildPlanID: 22, ExecutionOrder: 1})
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
								planID: 20,
 | 
				
			||||||
 | 
								expectedPlan: &models.Plan{
 | 
				
			||||||
 | 
									Model:       gorm.Model{ID: 20},
 | 
				
			||||||
 | 
									Name:        "Grandparent Plan",
 | 
				
			||||||
 | 
									ContentType: models.PlanContentTypeSubPlans,
 | 
				
			||||||
 | 
									SubPlans: []models.SubPlan{
 | 
				
			||||||
 | 
										{
 | 
				
			||||||
 | 
											ParentPlanID:   20,
 | 
				
			||||||
 | 
											ChildPlanID:    21,
 | 
				
			||||||
 | 
											ExecutionOrder: 1,
 | 
				
			||||||
 | 
											ChildPlan: &models.Plan{
 | 
				
			||||||
 | 
												Model:       gorm.Model{ID: 21},
 | 
				
			||||||
 | 
												Name:        "Parent Plan",
 | 
				
			||||||
 | 
												ContentType: models.PlanContentTypeSubPlans,
 | 
				
			||||||
 | 
												SubPlans: []models.SubPlan{
 | 
				
			||||||
 | 
													{
 | 
				
			||||||
 | 
														ParentPlanID:   21,
 | 
				
			||||||
 | 
														ChildPlanID:    22,
 | 
				
			||||||
 | 
														ExecutionOrder: 1,
 | 
				
			||||||
 | 
														ChildPlan: &models.Plan{
 | 
				
			||||||
 | 
															Model:       gorm.Model{ID: 22},
 | 
				
			||||||
 | 
															Name:        "Child Plan With Tasks",
 | 
				
			||||||
 | 
															ContentType: models.PlanContentTypeTasks,
 | 
				
			||||||
 | 
															Tasks: []models.Task{
 | 
				
			||||||
 | 
																{PlanID: 22, Name: "Grandchild's Task", ExecutionOrder: 1},
 | 
				
			||||||
 | 
															},
 | 
				
			||||||
 | 
														},
 | 
				
			||||||
 | 
													},
 | 
				
			||||||
 | 
												},
 | 
				
			||||||
 | 
											},
 | 
				
			||||||
 | 
										},
 | 
				
			||||||
 | 
									},
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
								expectedError: "",
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								name: "UnknownContentType",
 | 
				
			||||||
 | 
								setupData: func(db *gorm.DB) {
 | 
				
			||||||
 | 
									db.Create(&models.Plan{
 | 
				
			||||||
 | 
										Model:       gorm.Model{ID: 30},
 | 
				
			||||||
 | 
										Name:        "Unknown Type Plan",
 | 
				
			||||||
 | 
										ContentType: "INVALID_TYPE",
 | 
				
			||||||
 | 
									})
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
								planID:        30,
 | 
				
			||||||
 | 
								expectedPlan:  nil,
 | 
				
			||||||
 | 
								expectedError: fmt.Sprintf("未知的计划内容类型: INVALID_TYPE; 计划ID: 30"),
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							// 新增场景:测试空的关联数据
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								name: "PlanWithTasksType_ButNoTasks",
 | 
				
			||||||
 | 
								setupData: func(db *gorm.DB) {
 | 
				
			||||||
 | 
									db.Create(&models.Plan{
 | 
				
			||||||
 | 
										Model:       gorm.Model{ID: 50},
 | 
				
			||||||
 | 
										Name:        "Plan with empty tasks",
 | 
				
			||||||
 | 
										ContentType: models.PlanContentTypeTasks,
 | 
				
			||||||
 | 
									})
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
								planID: 50,
 | 
				
			||||||
 | 
								expectedPlan: &models.Plan{
 | 
				
			||||||
 | 
									Model:       gorm.Model{ID: 50},
 | 
				
			||||||
 | 
									Name:        "Plan with empty tasks",
 | 
				
			||||||
 | 
									ContentType: models.PlanContentTypeTasks,
 | 
				
			||||||
 | 
									Tasks:       []models.Task{}, // 期望一个空的切片
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
								expectedError: "",
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							// 新增场景:测试复杂的同级排序
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								name: "PlanWithSubPlans_ComplexSorting",
 | 
				
			||||||
 | 
								setupData: func(db *gorm.DB) {
 | 
				
			||||||
 | 
									db.Create(&models.Plan{Model: gorm.Model{ID: 60}, Name: "Main Plan For Sorting", ContentType: models.PlanContentTypeSubPlans})
 | 
				
			||||||
 | 
									db.Create(&models.Plan{Model: gorm.Model{ID: 61}, Name: "SubPlan C", ContentType: models.PlanContentTypeTasks})
 | 
				
			||||||
 | 
									db.Create(&models.Plan{Model: gorm.Model{ID: 62}, Name: "SubPlan A", ContentType: models.PlanContentTypeTasks})
 | 
				
			||||||
 | 
									db.Create(&models.Plan{Model: gorm.Model{ID: 63}, Name: "SubPlan B", ContentType: models.PlanContentTypeTasks})
 | 
				
			||||||
 | 
									// 故意打乱顺序插入
 | 
				
			||||||
 | 
									db.Create(&models.SubPlan{ParentPlanID: 60, ChildPlanID: 61, ExecutionOrder: 3})
 | 
				
			||||||
 | 
									db.Create(&models.SubPlan{ParentPlanID: 60, ChildPlanID: 62, ExecutionOrder: 1})
 | 
				
			||||||
 | 
									db.Create(&models.SubPlan{ParentPlanID: 60, ChildPlanID: 63, ExecutionOrder: 2})
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
								planID: 60,
 | 
				
			||||||
 | 
								expectedPlan: &models.Plan{
 | 
				
			||||||
 | 
									Model:       gorm.Model{ID: 60},
 | 
				
			||||||
 | 
									Name:        "Main Plan For Sorting",
 | 
				
			||||||
 | 
									ContentType: models.PlanContentTypeSubPlans,
 | 
				
			||||||
 | 
									SubPlans: []models.SubPlan{
 | 
				
			||||||
 | 
										{ParentPlanID: 60, ChildPlanID: 62, ExecutionOrder: 1, ChildPlan: &models.Plan{Model: gorm.Model{ID: 62}, Name: "SubPlan A", ContentType: models.PlanContentTypeTasks, Tasks: []models.Task{}}},
 | 
				
			||||||
 | 
										{ParentPlanID: 60, ChildPlanID: 63, ExecutionOrder: 2, ChildPlan: &models.Plan{Model: gorm.Model{ID: 63}, Name: "SubPlan B", ContentType: models.PlanContentTypeTasks, Tasks: []models.Task{}}},
 | 
				
			||||||
 | 
										{ParentPlanID: 60, ChildPlanID: 61, ExecutionOrder: 3, ChildPlan: &models.Plan{Model: gorm.Model{ID: 61}, Name: "SubPlan C", ContentType: models.PlanContentTypeTasks, Tasks: []models.Task{}}},
 | 
				
			||||||
 | 
									},
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
								expectedError: "",
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
							// 新增场景:测试混合内容的子计划树
 | 
				
			||||||
 | 
							{
 | 
				
			||||||
 | 
								name: "PlanWithSubPlans_MixedContentTypes",
 | 
				
			||||||
 | 
								setupData: func(db *gorm.DB) {
 | 
				
			||||||
 | 
									db.Create(&models.Plan{Model: gorm.Model{ID: 70}, Name: "Mixed Main Plan", ContentType: models.PlanContentTypeSubPlans})
 | 
				
			||||||
 | 
									db.Create(&models.Plan{Model: gorm.Model{ID: 71}, Name: "Child with SubPlans", ContentType: models.PlanContentTypeSubPlans})
 | 
				
			||||||
 | 
									db.Create(&models.Plan{Model: gorm.Model{ID: 72}, Name: "Grandchild with Tasks", ContentType: models.PlanContentTypeTasks})
 | 
				
			||||||
 | 
									db.Create(&models.Task{PlanID: 72, Name: "Grandchild's Task", ExecutionOrder: 1})
 | 
				
			||||||
 | 
									db.Create(&models.Plan{Model: gorm.Model{ID: 73}, Name: "Child with Tasks", ContentType: models.PlanContentTypeTasks})
 | 
				
			||||||
 | 
									db.Create(&models.Task{PlanID: 73, Name: "Child's Task", ExecutionOrder: 1})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									// 创建关联
 | 
				
			||||||
 | 
									db.Create(&models.SubPlan{ParentPlanID: 70, ChildPlanID: 71, ExecutionOrder: 1}) // Main -> Child with SubPlans
 | 
				
			||||||
 | 
									db.Create(&models.SubPlan{ParentPlanID: 70, ChildPlanID: 73, ExecutionOrder: 2}) // Main -> Child with Tasks
 | 
				
			||||||
 | 
									db.Create(&models.SubPlan{ParentPlanID: 71, ChildPlanID: 72, ExecutionOrder: 1}) // Child with SubPlans -> Grandchild
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
								planID: 70,
 | 
				
			||||||
 | 
								expectedPlan: &models.Plan{
 | 
				
			||||||
 | 
									Model:       gorm.Model{ID: 70},
 | 
				
			||||||
 | 
									Name:        "Mixed Main Plan",
 | 
				
			||||||
 | 
									ContentType: models.PlanContentTypeSubPlans,
 | 
				
			||||||
 | 
									SubPlans: []models.SubPlan{
 | 
				
			||||||
 | 
										{
 | 
				
			||||||
 | 
											ParentPlanID: 70, ChildPlanID: 71, ExecutionOrder: 1,
 | 
				
			||||||
 | 
											ChildPlan: &models.Plan{
 | 
				
			||||||
 | 
												Model: gorm.Model{ID: 71}, Name: "Child with SubPlans", ContentType: models.PlanContentTypeSubPlans,
 | 
				
			||||||
 | 
												SubPlans: []models.SubPlan{
 | 
				
			||||||
 | 
													{ParentPlanID: 71, ChildPlanID: 72, ExecutionOrder: 1, ChildPlan: &models.Plan{
 | 
				
			||||||
 | 
														Model: gorm.Model{ID: 72}, Name: "Grandchild with Tasks", ContentType: models.PlanContentTypeTasks,
 | 
				
			||||||
 | 
														Tasks: []models.Task{{PlanID: 72, Name: "Grandchild's Task", ExecutionOrder: 1}},
 | 
				
			||||||
 | 
													}},
 | 
				
			||||||
 | 
												},
 | 
				
			||||||
 | 
											},
 | 
				
			||||||
 | 
										},
 | 
				
			||||||
 | 
										{
 | 
				
			||||||
 | 
											ParentPlanID: 70, ChildPlanID: 73, ExecutionOrder: 2,
 | 
				
			||||||
 | 
											ChildPlan: &models.Plan{
 | 
				
			||||||
 | 
												Model: gorm.Model{ID: 73}, Name: "Child with Tasks", ContentType: models.PlanContentTypeTasks,
 | 
				
			||||||
 | 
												Tasks: []models.Task{{PlanID: 73, Name: "Child's Task", ExecutionOrder: 1}},
 | 
				
			||||||
 | 
											},
 | 
				
			||||||
 | 
										},
 | 
				
			||||||
 | 
									},
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
								expectedError: "",
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for _, tc := range testCases {
 | 
				
			||||||
 | 
							t.Run(tc.name, func(t *testing.T) {
 | 
				
			||||||
 | 
								db := setupTestDB(t)
 | 
				
			||||||
 | 
								// 使用 defer 确保数据库连接在测试结束后关闭
 | 
				
			||||||
 | 
								sqlDB, _ := db.DB()
 | 
				
			||||||
 | 
								defer sqlDB.Close()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								tc.setupData(db)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								repo := repository.NewGormPlanRepository(db)
 | 
				
			||||||
 | 
								plan, err := repo.GetPlanByID(tc.planID)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								if tc.expectedError != "" {
 | 
				
			||||||
 | 
									assert.Error(t, err)
 | 
				
			||||||
 | 
									assert.Contains(t, err.Error(), tc.expectedError)
 | 
				
			||||||
 | 
									assert.Nil(t, plan)
 | 
				
			||||||
 | 
								} else {
 | 
				
			||||||
 | 
									assert.NoError(t, err)
 | 
				
			||||||
 | 
									assert.NotNil(t, plan)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									// 在比较之前,清理实际结果和期望结果中所有不确定的、由数据库自动生成的字段
 | 
				
			||||||
 | 
									cleanPlanForComparison(plan)
 | 
				
			||||||
 | 
									cleanPlanForComparison(tc.expectedPlan)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									assert.Equal(t, tc.expectedPlan, plan)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							})
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// cleanPlanForComparison 递归地重置 Plan 对象及其关联对象中由数据库自动生成的字段(如ID和时间戳),
 | 
				
			||||||
 | 
					// 以便在测试中断言它们与期望值相等。
 | 
				
			||||||
 | 
					func cleanPlanForComparison(p *models.Plan) {
 | 
				
			||||||
 | 
						if p == nil {
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 重置 Plan 自身的时间戳
 | 
				
			||||||
 | 
						p.CreatedAt = time.Time{}
 | 
				
			||||||
 | 
						p.UpdatedAt = time.Time{}
 | 
				
			||||||
 | 
						p.DeletedAt = gorm.DeletedAt{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 重置所有 Task 的自动生成字段
 | 
				
			||||||
 | 
						for i := range p.Tasks {
 | 
				
			||||||
 | 
							p.Tasks[i].ID = 0 // ID是自动生成的,必须重置为0才能与期望值匹配
 | 
				
			||||||
 | 
							p.Tasks[i].CreatedAt = time.Time{}
 | 
				
			||||||
 | 
							p.Tasks[i].UpdatedAt = time.Time{}
 | 
				
			||||||
 | 
							p.Tasks[i].DeletedAt = gorm.DeletedAt{}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// 重置所有 SubPlan 的自动生成字段,并递归清理子计划
 | 
				
			||||||
 | 
						for i := range p.SubPlans {
 | 
				
			||||||
 | 
							p.SubPlans[i].ID = 0 // SubPlan 连接记录的ID也是自动生成的
 | 
				
			||||||
 | 
							p.SubPlans[i].CreatedAt = time.Time{}
 | 
				
			||||||
 | 
							p.SubPlans[i].UpdatedAt = time.Time{}
 | 
				
			||||||
 | 
							p.SubPlans[i].DeletedAt = gorm.DeletedAt{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// 递归调用以清理嵌套的子计划
 | 
				
			||||||
 | 
							cleanPlanForComparison(p.SubPlans[i].ChildPlan)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user