修复bug
This commit is contained in:
2
.swaggo
2
.swaggo
@@ -1,2 +0,0 @@
|
|||||||
replace encoding/json.RawMessage object
|
|
||||||
replace git_huangwc_com_pig_pig-farm-controller_internal_app_controller_device.DeviceResponse device.DeviceResponse
|
|
||||||
18
docs/docs.go
18
docs/docs.go
@@ -560,9 +560,6 @@ const docTemplate = `{
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"definitions": {
|
"definitions": {
|
||||||
"controller.Properties": {
|
|
||||||
"type": "object"
|
|
||||||
},
|
|
||||||
"controller.Response": {
|
"controller.Response": {
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"properties": {
|
"properties": {
|
||||||
@@ -596,7 +593,8 @@ const docTemplate = `{
|
|||||||
"type": "integer"
|
"type": "integer"
|
||||||
},
|
},
|
||||||
"properties": {
|
"properties": {
|
||||||
"$ref": "#/definitions/controller.Properties"
|
"type": "object",
|
||||||
|
"additionalProperties": true
|
||||||
},
|
},
|
||||||
"sub_type": {
|
"sub_type": {
|
||||||
"$ref": "#/definitions/models.DeviceSubType"
|
"$ref": "#/definitions/models.DeviceSubType"
|
||||||
@@ -623,7 +621,8 @@ const docTemplate = `{
|
|||||||
"type": "integer"
|
"type": "integer"
|
||||||
},
|
},
|
||||||
"properties": {
|
"properties": {
|
||||||
"$ref": "#/definitions/controller.Properties"
|
"type": "object",
|
||||||
|
"additionalProperties": true
|
||||||
},
|
},
|
||||||
"sub_type": {
|
"sub_type": {
|
||||||
"$ref": "#/definitions/models.DeviceSubType"
|
"$ref": "#/definitions/models.DeviceSubType"
|
||||||
@@ -652,7 +651,8 @@ const docTemplate = `{
|
|||||||
"type": "integer"
|
"type": "integer"
|
||||||
},
|
},
|
||||||
"properties": {
|
"properties": {
|
||||||
"$ref": "#/definitions/controller.Properties"
|
"type": "object",
|
||||||
|
"additionalProperties": true
|
||||||
},
|
},
|
||||||
"sub_type": {
|
"sub_type": {
|
||||||
"$ref": "#/definitions/models.DeviceSubType"
|
"$ref": "#/definitions/models.DeviceSubType"
|
||||||
@@ -952,7 +952,8 @@ const docTemplate = `{
|
|||||||
"example": "打开风扇"
|
"example": "打开风扇"
|
||||||
},
|
},
|
||||||
"parameters": {
|
"parameters": {
|
||||||
"$ref": "#/definitions/controller.Properties"
|
"type": "object",
|
||||||
|
"additionalProperties": true
|
||||||
},
|
},
|
||||||
"type": {
|
"type": {
|
||||||
"allOf": [
|
"allOf": [
|
||||||
@@ -984,7 +985,8 @@ const docTemplate = `{
|
|||||||
"example": "打开风扇"
|
"example": "打开风扇"
|
||||||
},
|
},
|
||||||
"parameters": {
|
"parameters": {
|
||||||
"$ref": "#/definitions/controller.Properties"
|
"type": "object",
|
||||||
|
"additionalProperties": true
|
||||||
},
|
},
|
||||||
"plan_id": {
|
"plan_id": {
|
||||||
"type": "integer",
|
"type": "integer",
|
||||||
|
|||||||
@@ -549,9 +549,6 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"definitions": {
|
"definitions": {
|
||||||
"controller.Properties": {
|
|
||||||
"type": "object"
|
|
||||||
},
|
|
||||||
"controller.Response": {
|
"controller.Response": {
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"properties": {
|
"properties": {
|
||||||
@@ -585,7 +582,8 @@
|
|||||||
"type": "integer"
|
"type": "integer"
|
||||||
},
|
},
|
||||||
"properties": {
|
"properties": {
|
||||||
"$ref": "#/definitions/controller.Properties"
|
"type": "object",
|
||||||
|
"additionalProperties": true
|
||||||
},
|
},
|
||||||
"sub_type": {
|
"sub_type": {
|
||||||
"$ref": "#/definitions/models.DeviceSubType"
|
"$ref": "#/definitions/models.DeviceSubType"
|
||||||
@@ -612,7 +610,8 @@
|
|||||||
"type": "integer"
|
"type": "integer"
|
||||||
},
|
},
|
||||||
"properties": {
|
"properties": {
|
||||||
"$ref": "#/definitions/controller.Properties"
|
"type": "object",
|
||||||
|
"additionalProperties": true
|
||||||
},
|
},
|
||||||
"sub_type": {
|
"sub_type": {
|
||||||
"$ref": "#/definitions/models.DeviceSubType"
|
"$ref": "#/definitions/models.DeviceSubType"
|
||||||
@@ -641,7 +640,8 @@
|
|||||||
"type": "integer"
|
"type": "integer"
|
||||||
},
|
},
|
||||||
"properties": {
|
"properties": {
|
||||||
"$ref": "#/definitions/controller.Properties"
|
"type": "object",
|
||||||
|
"additionalProperties": true
|
||||||
},
|
},
|
||||||
"sub_type": {
|
"sub_type": {
|
||||||
"$ref": "#/definitions/models.DeviceSubType"
|
"$ref": "#/definitions/models.DeviceSubType"
|
||||||
@@ -941,7 +941,8 @@
|
|||||||
"example": "打开风扇"
|
"example": "打开风扇"
|
||||||
},
|
},
|
||||||
"parameters": {
|
"parameters": {
|
||||||
"$ref": "#/definitions/controller.Properties"
|
"type": "object",
|
||||||
|
"additionalProperties": true
|
||||||
},
|
},
|
||||||
"type": {
|
"type": {
|
||||||
"allOf": [
|
"allOf": [
|
||||||
@@ -973,7 +974,8 @@
|
|||||||
"example": "打开风扇"
|
"example": "打开风扇"
|
||||||
},
|
},
|
||||||
"parameters": {
|
"parameters": {
|
||||||
"$ref": "#/definitions/controller.Properties"
|
"type": "object",
|
||||||
|
"additionalProperties": true
|
||||||
},
|
},
|
||||||
"plan_id": {
|
"plan_id": {
|
||||||
"type": "integer",
|
"type": "integer",
|
||||||
|
|||||||
@@ -1,6 +1,4 @@
|
|||||||
definitions:
|
definitions:
|
||||||
controller.Properties:
|
|
||||||
type: object
|
|
||||||
controller.Response:
|
controller.Response:
|
||||||
properties:
|
properties:
|
||||||
code:
|
code:
|
||||||
@@ -21,7 +19,8 @@ definitions:
|
|||||||
parent_id:
|
parent_id:
|
||||||
type: integer
|
type: integer
|
||||||
properties:
|
properties:
|
||||||
$ref: '#/definitions/controller.Properties'
|
additionalProperties: true
|
||||||
|
type: object
|
||||||
sub_type:
|
sub_type:
|
||||||
$ref: '#/definitions/models.DeviceSubType'
|
$ref: '#/definitions/models.DeviceSubType'
|
||||||
type:
|
type:
|
||||||
@@ -39,7 +38,8 @@ definitions:
|
|||||||
parent_id:
|
parent_id:
|
||||||
type: integer
|
type: integer
|
||||||
properties:
|
properties:
|
||||||
$ref: '#/definitions/controller.Properties'
|
additionalProperties: true
|
||||||
|
type: object
|
||||||
sub_type:
|
sub_type:
|
||||||
$ref: '#/definitions/models.DeviceSubType'
|
$ref: '#/definitions/models.DeviceSubType'
|
||||||
type:
|
type:
|
||||||
@@ -61,7 +61,8 @@ definitions:
|
|||||||
parent_id:
|
parent_id:
|
||||||
type: integer
|
type: integer
|
||||||
properties:
|
properties:
|
||||||
$ref: '#/definitions/controller.Properties'
|
additionalProperties: true
|
||||||
|
type: object
|
||||||
sub_type:
|
sub_type:
|
||||||
$ref: '#/definitions/models.DeviceSubType'
|
$ref: '#/definitions/models.DeviceSubType'
|
||||||
type:
|
type:
|
||||||
@@ -271,7 +272,8 @@ definitions:
|
|||||||
example: 打开风扇
|
example: 打开风扇
|
||||||
type: string
|
type: string
|
||||||
parameters:
|
parameters:
|
||||||
$ref: '#/definitions/controller.Properties'
|
additionalProperties: true
|
||||||
|
type: object
|
||||||
type:
|
type:
|
||||||
allOf:
|
allOf:
|
||||||
- $ref: '#/definitions/models.TaskType'
|
- $ref: '#/definitions/models.TaskType'
|
||||||
@@ -292,7 +294,8 @@ definitions:
|
|||||||
example: 打开风扇
|
example: 打开风扇
|
||||||
type: string
|
type: string
|
||||||
parameters:
|
parameters:
|
||||||
$ref: '#/definitions/controller.Properties'
|
additionalProperties: true
|
||||||
|
type: object
|
||||||
plan_id:
|
plan_id:
|
||||||
example: 1
|
example: 1
|
||||||
type: integer
|
type: integer
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
package device
|
package device
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@@ -11,7 +13,6 @@ import (
|
|||||||
"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"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"gorm.io/datatypes"
|
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -33,46 +34,54 @@ func NewController(repo repository.DeviceRepository, logger *logs.Logger) *Contr
|
|||||||
|
|
||||||
// CreateDeviceRequest 定义了创建设备时需要传入的参数
|
// CreateDeviceRequest 定义了创建设备时需要传入的参数
|
||||||
type CreateDeviceRequest struct {
|
type CreateDeviceRequest struct {
|
||||||
Name string `json:"name" binding:"required"`
|
Name string `json:"name" binding:"required"`
|
||||||
Type models.DeviceType `json:"type" binding:"required"`
|
Type models.DeviceType `json:"type" binding:"required"`
|
||||||
SubType models.DeviceSubType `json:"sub_type,omitempty"`
|
SubType models.DeviceSubType `json:"sub_type,omitempty"`
|
||||||
ParentID *uint `json:"parent_id,omitempty"`
|
ParentID *uint `json:"parent_id,omitempty"`
|
||||||
Location string `json:"location,omitempty"`
|
Location string `json:"location,omitempty"`
|
||||||
Properties controller.Properties `json:"properties,omitempty"`
|
Properties map[string]interface{} `json:"properties,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateDeviceRequest 定义了更新设备时需要传入的参数
|
// UpdateDeviceRequest 定义了更新设备时需要传入的参数
|
||||||
type UpdateDeviceRequest struct {
|
type UpdateDeviceRequest struct {
|
||||||
Name string `json:"name" binding:"required"`
|
Name string `json:"name" binding:"required"`
|
||||||
Type models.DeviceType `json:"type" binding:"required"`
|
Type models.DeviceType `json:"type" binding:"required"`
|
||||||
SubType models.DeviceSubType `json:"sub_type,omitempty"`
|
SubType models.DeviceSubType `json:"sub_type,omitempty"`
|
||||||
ParentID *uint `json:"parent_id,omitempty"`
|
ParentID *uint `json:"parent_id,omitempty"`
|
||||||
Location string `json:"location,omitempty"`
|
Location string `json:"location,omitempty"`
|
||||||
Properties controller.Properties `json:"properties,omitempty"`
|
Properties map[string]interface{} `json:"properties,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- Response DTOs ---
|
// --- Response DTOs ---
|
||||||
|
|
||||||
// DeviceResponse 定义了返回给客户端的单个设备信息的结构
|
// DeviceResponse 定义了返回给客户端的单个设备信息的结构
|
||||||
type DeviceResponse struct {
|
type DeviceResponse struct {
|
||||||
ID uint `json:"id"`
|
ID uint `json:"id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Type models.DeviceType `json:"type"`
|
Type models.DeviceType `json:"type"`
|
||||||
SubType models.DeviceSubType `json:"sub_type"`
|
SubType models.DeviceSubType `json:"sub_type"`
|
||||||
ParentID *uint `json:"parent_id"`
|
ParentID *uint `json:"parent_id"`
|
||||||
Location string `json:"location"`
|
Location string `json:"location"`
|
||||||
Properties controller.Properties `json:"properties"`
|
Properties map[string]interface{} `json:"properties"`
|
||||||
CreatedAt string `json:"created_at"`
|
CreatedAt string `json:"created_at"`
|
||||||
UpdatedAt string `json:"updated_at"`
|
UpdatedAt string `json:"updated_at"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- DTO 转换函数 ---
|
// --- DTO 转换函数 ---
|
||||||
|
|
||||||
// newDeviceResponse 从数据库模型创建一个新的设备响应 DTO
|
// newDeviceResponse 从数据库模型创建一个新的设备响应 DTO
|
||||||
func newDeviceResponse(device *models.Device) *DeviceResponse {
|
func newDeviceResponse(device *models.Device) (*DeviceResponse, error) {
|
||||||
if device == nil {
|
if device == nil {
|
||||||
return nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var props map[string]interface{}
|
||||||
|
if len(device.Properties) > 0 && string(device.Properties) != "null" {
|
||||||
|
if err := json.Unmarshal(device.Properties, &props); err != nil {
|
||||||
|
return nil, fmt.Errorf("解析设备属性失败 (ID: %d): %w", device.ID, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return &DeviceResponse{
|
return &DeviceResponse{
|
||||||
ID: device.ID,
|
ID: device.ID,
|
||||||
Name: device.Name,
|
Name: device.Name,
|
||||||
@@ -80,19 +89,23 @@ func newDeviceResponse(device *models.Device) *DeviceResponse {
|
|||||||
SubType: device.SubType,
|
SubType: device.SubType,
|
||||||
ParentID: device.ParentID,
|
ParentID: device.ParentID,
|
||||||
Location: device.Location,
|
Location: device.Location,
|
||||||
Properties: controller.Properties(device.Properties),
|
Properties: props,
|
||||||
CreatedAt: device.CreatedAt.Format(time.RFC3339),
|
CreatedAt: device.CreatedAt.Format(time.RFC3339),
|
||||||
UpdatedAt: device.UpdatedAt.Format(time.RFC3339),
|
UpdatedAt: device.UpdatedAt.Format(time.RFC3339),
|
||||||
}
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// newListDeviceResponse 从数据库模型切片创建一个新的设备列表响应 DTO 切片
|
// newListDeviceResponse 从数据库模型切片创建一个新的设备列表响应 DTO 切片
|
||||||
func newListDeviceResponse(devices []*models.Device) []*DeviceResponse {
|
func newListDeviceResponse(devices []*models.Device) ([]*DeviceResponse, error) {
|
||||||
list := make([]*DeviceResponse, 0, len(devices))
|
list := make([]*DeviceResponse, 0, len(devices))
|
||||||
for _, device := range devices {
|
for _, device := range devices {
|
||||||
list = append(list, newDeviceResponse(device))
|
resp, err := newDeviceResponse(device)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
list = append(list, resp)
|
||||||
}
|
}
|
||||||
return list
|
return list, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- Controller Methods ---
|
// --- Controller Methods ---
|
||||||
@@ -114,13 +127,20 @@ func (c *Controller) CreateDevice(ctx *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
propertiesJSON, err := json.Marshal(req.Properties)
|
||||||
|
if err != nil {
|
||||||
|
c.logger.Errorf("创建设备: 序列化属性失败: %v", err)
|
||||||
|
controller.SendErrorResponse(ctx, controller.CodeBadRequest, "属性字段格式错误")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
device := &models.Device{
|
device := &models.Device{
|
||||||
Name: req.Name,
|
Name: req.Name,
|
||||||
Type: req.Type,
|
Type: req.Type,
|
||||||
SubType: req.SubType,
|
SubType: req.SubType,
|
||||||
ParentID: req.ParentID,
|
ParentID: req.ParentID,
|
||||||
Location: req.Location,
|
Location: req.Location,
|
||||||
Properties: datatypes.JSON(req.Properties),
|
Properties: propertiesJSON,
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := c.repo.Create(device); err != nil {
|
if err := c.repo.Create(device); err != nil {
|
||||||
@@ -129,7 +149,14 @@ func (c *Controller) CreateDevice(ctx *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
controller.SendResponse(ctx, controller.CodeCreated, "设备创建成功", newDeviceResponse(device))
|
resp, err := newDeviceResponse(device)
|
||||||
|
if err != nil {
|
||||||
|
c.logger.Errorf("创建设备: 序列化响应失败: %v", err)
|
||||||
|
controller.SendErrorResponse(ctx, controller.CodeInternalError, "设备创建成功,但响应生成失败")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
controller.SendResponse(ctx, controller.CodeCreated, "设备创建成功", resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetDevice godoc
|
// GetDevice godoc
|
||||||
@@ -158,7 +185,14 @@ func (c *Controller) GetDevice(ctx *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
controller.SendResponse(ctx, controller.CodeSuccess, "获取设备信息成功", newDeviceResponse(device))
|
resp, err := newDeviceResponse(device)
|
||||||
|
if err != nil {
|
||||||
|
c.logger.Errorf("获取设备: 序列化响应失败: %v", err)
|
||||||
|
controller.SendErrorResponse(ctx, controller.CodeInternalError, "获取设备信息失败: 内部数据格式错误")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
controller.SendResponse(ctx, controller.CodeSuccess, "获取设备信息成功", resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListDevices godoc
|
// ListDevices godoc
|
||||||
@@ -176,7 +210,14 @@ func (c *Controller) ListDevices(ctx *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
controller.SendResponse(ctx, controller.CodeSuccess, "获取设备列表成功", newListDeviceResponse(devices))
|
resp, err := newListDeviceResponse(devices)
|
||||||
|
if err != nil {
|
||||||
|
c.logger.Errorf("获取设备列表: 序列化响应失败: %v", err)
|
||||||
|
controller.SendErrorResponse(ctx, controller.CodeInternalError, "获取设备列表失败: 内部数据格式错误")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
controller.SendResponse(ctx, controller.CodeSuccess, "获取设备列表成功", resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateDevice godoc
|
// UpdateDevice godoc
|
||||||
@@ -216,13 +257,20 @@ func (c *Controller) UpdateDevice(ctx *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
propertiesJSON, err := json.Marshal(req.Properties)
|
||||||
|
if err != nil {
|
||||||
|
c.logger.Errorf("更新设备: 序列化属性失败: %v", err)
|
||||||
|
controller.SendErrorResponse(ctx, controller.CodeBadRequest, "属性字段格式错误")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// 3. 更新从数据库中查出的现有设备对象的字段
|
// 3. 更新从数据库中查出的现有设备对象的字段
|
||||||
existingDevice.Name = req.Name
|
existingDevice.Name = req.Name
|
||||||
existingDevice.Type = req.Type
|
existingDevice.Type = req.Type
|
||||||
existingDevice.SubType = req.SubType
|
existingDevice.SubType = req.SubType
|
||||||
existingDevice.ParentID = req.ParentID
|
existingDevice.ParentID = req.ParentID
|
||||||
existingDevice.Location = req.Location
|
existingDevice.Location = req.Location
|
||||||
existingDevice.Properties = datatypes.JSON(req.Properties)
|
existingDevice.Properties = propertiesJSON
|
||||||
|
|
||||||
// 4. 将修改后的 existingDevice 对象保存回数据库
|
// 4. 将修改后的 existingDevice 对象保存回数据库
|
||||||
if err := c.repo.Update(existingDevice); err != nil {
|
if err := c.repo.Update(existingDevice); err != nil {
|
||||||
@@ -231,7 +279,14 @@ func (c *Controller) UpdateDevice(ctx *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
controller.SendResponse(ctx, controller.CodeSuccess, "设备更新成功", newDeviceResponse(existingDevice))
|
resp, err := newDeviceResponse(existingDevice)
|
||||||
|
if err != nil {
|
||||||
|
c.logger.Errorf("更新设备: 序列化响应失败: %v", err)
|
||||||
|
controller.SendErrorResponse(ctx, controller.CodeInternalError, "设备更新成功,但响应生成失败")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
controller.SendResponse(ctx, controller.CodeSuccess, "设备更新成功", resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteDevice godoc
|
// DeleteDevice godoc
|
||||||
|
|||||||
@@ -1,15 +1,16 @@
|
|||||||
package plan
|
package plan
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git.huangwc.com/pig/pig-farm-controller/internal/app/controller"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
||||||
"gorm.io/datatypes"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// PlanToResponse 将Plan模型转换为PlanResponse
|
// PlanToResponse 将Plan模型转换为PlanResponse
|
||||||
func PlanToResponse(plan *models.Plan) *PlanResponse {
|
func PlanToResponse(plan *models.Plan) (*PlanResponse, error) {
|
||||||
if plan == nil {
|
if plan == nil {
|
||||||
return nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
response := &PlanResponse{
|
response := &PlanResponse{
|
||||||
@@ -28,7 +29,11 @@ func PlanToResponse(plan *models.Plan) *PlanResponse {
|
|||||||
if plan.ContentType == models.PlanContentTypeSubPlans {
|
if plan.ContentType == models.PlanContentTypeSubPlans {
|
||||||
response.SubPlans = make([]SubPlanResponse, len(plan.SubPlans))
|
response.SubPlans = make([]SubPlanResponse, len(plan.SubPlans))
|
||||||
for i, subPlan := range plan.SubPlans {
|
for i, subPlan := range plan.SubPlans {
|
||||||
response.SubPlans[i] = SubPlanToResponse(&subPlan)
|
subPlanResp, err := SubPlanToResponse(&subPlan)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
response.SubPlans[i] = subPlanResp
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -36,11 +41,15 @@ func PlanToResponse(plan *models.Plan) *PlanResponse {
|
|||||||
if plan.ContentType == models.PlanContentTypeTasks {
|
if plan.ContentType == models.PlanContentTypeTasks {
|
||||||
response.Tasks = make([]TaskResponse, len(plan.Tasks))
|
response.Tasks = make([]TaskResponse, len(plan.Tasks))
|
||||||
for i, task := range plan.Tasks {
|
for i, task := range plan.Tasks {
|
||||||
response.Tasks[i] = TaskToResponse(&task)
|
taskResp, err := TaskToResponse(&task)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
response.Tasks[i] = taskResp
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return response
|
return response, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// PlanFromCreateRequest 将CreatePlanRequest转换为Plan模型,并进行业务规则验证
|
// PlanFromCreateRequest 将CreatePlanRequest转换为Plan模型,并进行业务规则验证
|
||||||
@@ -73,8 +82,11 @@ func PlanFromCreateRequest(req *CreatePlanRequest) (*models.Plan, error) {
|
|||||||
if req.ContentType == models.PlanContentTypeTasks && req.Tasks != nil {
|
if req.ContentType == models.PlanContentTypeTasks && req.Tasks != nil {
|
||||||
plan.Tasks = make([]models.Task, len(req.Tasks))
|
plan.Tasks = make([]models.Task, len(req.Tasks))
|
||||||
for i, taskReq := range req.Tasks {
|
for i, taskReq := range req.Tasks {
|
||||||
// 使用来自请求的ExecutionOrder
|
task, err := TaskFromRequest(&taskReq)
|
||||||
plan.Tasks[i] = TaskFromRequest(&taskReq)
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
plan.Tasks[i] = task
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -120,8 +132,11 @@ func PlanFromUpdateRequest(req *UpdatePlanRequest) (*models.Plan, error) {
|
|||||||
if req.ContentType == models.PlanContentTypeTasks && req.Tasks != nil {
|
if req.ContentType == models.PlanContentTypeTasks && req.Tasks != nil {
|
||||||
plan.Tasks = make([]models.Task, len(req.Tasks))
|
plan.Tasks = make([]models.Task, len(req.Tasks))
|
||||||
for i, taskReq := range req.Tasks {
|
for i, taskReq := range req.Tasks {
|
||||||
// 使用来自请求的ExecutionOrder
|
task, err := TaskFromRequest(&taskReq)
|
||||||
plan.Tasks[i] = TaskFromRequest(&taskReq)
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
plan.Tasks[i] = task
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -138,9 +153,9 @@ func PlanFromUpdateRequest(req *UpdatePlanRequest) (*models.Plan, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// SubPlanToResponse 将SubPlan模型转换为SubPlanResponse
|
// SubPlanToResponse 将SubPlan模型转换为SubPlanResponse
|
||||||
func SubPlanToResponse(subPlan *models.SubPlan) SubPlanResponse {
|
func SubPlanToResponse(subPlan *models.SubPlan) (SubPlanResponse, error) {
|
||||||
if subPlan == nil {
|
if subPlan == nil {
|
||||||
return SubPlanResponse{}
|
return SubPlanResponse{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
response := SubPlanResponse{
|
response := SubPlanResponse{
|
||||||
@@ -152,16 +167,27 @@ func SubPlanToResponse(subPlan *models.SubPlan) SubPlanResponse {
|
|||||||
|
|
||||||
// 如果有完整的子计划数据,也进行转换
|
// 如果有完整的子计划数据,也进行转换
|
||||||
if subPlan.ChildPlan != nil {
|
if subPlan.ChildPlan != nil {
|
||||||
response.ChildPlan = PlanToResponse(subPlan.ChildPlan)
|
childPlanResp, err := PlanToResponse(subPlan.ChildPlan)
|
||||||
|
if err != nil {
|
||||||
|
return SubPlanResponse{}, err
|
||||||
|
}
|
||||||
|
response.ChildPlan = childPlanResp
|
||||||
}
|
}
|
||||||
|
|
||||||
return response
|
return response, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// TaskToResponse 将Task模型转换为TaskResponse
|
// TaskToResponse 将Task模型转换为TaskResponse
|
||||||
func TaskToResponse(task *models.Task) TaskResponse {
|
func TaskToResponse(task *models.Task) (TaskResponse, error) {
|
||||||
if task == nil {
|
if task == nil {
|
||||||
return TaskResponse{}
|
return TaskResponse{}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var params map[string]interface{}
|
||||||
|
if len(task.Parameters) > 0 && string(task.Parameters) != "null" {
|
||||||
|
if err := json.Unmarshal(task.Parameters, ¶ms); err != nil {
|
||||||
|
return TaskResponse{}, fmt.Errorf("parsing task parameters failed (ID: %d): %w", task.ID, err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return TaskResponse{
|
return TaskResponse{
|
||||||
@@ -171,14 +197,19 @@ func TaskToResponse(task *models.Task) TaskResponse {
|
|||||||
Description: task.Description,
|
Description: task.Description,
|
||||||
ExecutionOrder: task.ExecutionOrder,
|
ExecutionOrder: task.ExecutionOrder,
|
||||||
Type: task.Type,
|
Type: task.Type,
|
||||||
Parameters: controller.Properties(task.Parameters),
|
Parameters: params,
|
||||||
}
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// TaskFromRequest 将TaskRequest转换为Task模型
|
// TaskFromRequest 将TaskRequest转换为Task模型
|
||||||
func TaskFromRequest(req *TaskRequest) models.Task {
|
func TaskFromRequest(req *TaskRequest) (models.Task, error) {
|
||||||
if req == nil {
|
if req == nil {
|
||||||
return models.Task{}
|
return models.Task{}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
paramsJSON, err := json.Marshal(req.Parameters)
|
||||||
|
if err != nil {
|
||||||
|
return models.Task{}, fmt.Errorf("serializing task parameters failed: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return models.Task{
|
return models.Task{
|
||||||
@@ -186,6 +217,6 @@ func TaskFromRequest(req *TaskRequest) models.Task {
|
|||||||
Description: req.Description,
|
Description: req.Description,
|
||||||
ExecutionOrder: req.ExecutionOrder,
|
ExecutionOrder: req.ExecutionOrder,
|
||||||
Type: req.Type,
|
Type: req.Type,
|
||||||
Parameters: datatypes.JSON(req.Parameters),
|
Parameters: paramsJSON,
|
||||||
}
|
}, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -71,22 +71,22 @@ type SubPlanResponse struct {
|
|||||||
|
|
||||||
// TaskRequest 定义任务请求结构体
|
// TaskRequest 定义任务请求结构体
|
||||||
type TaskRequest struct {
|
type TaskRequest struct {
|
||||||
Name string `json:"name" example:"打开风扇"`
|
Name string `json:"name" example:"打开风扇"`
|
||||||
Description string `json:"description" example:"打开1号风扇"`
|
Description string `json:"description" example:"打开1号风扇"`
|
||||||
ExecutionOrder int `json:"execution_order" example:"1"`
|
ExecutionOrder int `json:"execution_order" example:"1"`
|
||||||
Type models.TaskType `json:"type" example:"waiting"`
|
Type models.TaskType `json:"type" example:"waiting"`
|
||||||
Parameters controller.Properties `json:"parameters,omitempty"`
|
Parameters map[string]interface{} `json:"parameters,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// TaskResponse 定义任务响应结构体
|
// TaskResponse 定义任务响应结构体
|
||||||
type TaskResponse struct {
|
type TaskResponse struct {
|
||||||
ID int `json:"id" example:"1"`
|
ID int `json:"id" example:"1"`
|
||||||
PlanID uint `json:"plan_id" example:"1"`
|
PlanID uint `json:"plan_id" example:"1"`
|
||||||
Name string `json:"name" example:"打开风扇"`
|
Name string `json:"name" example:"打开风扇"`
|
||||||
Description string `json:"description" example:"打开1号风扇"`
|
Description string `json:"description" example:"打开1号风扇"`
|
||||||
ExecutionOrder int `json:"execution_order" example:"1"`
|
ExecutionOrder int `json:"execution_order" example:"1"`
|
||||||
Type models.TaskType `json:"type" example:"waiting"`
|
Type models.TaskType `json:"type" example:"waiting"`
|
||||||
Parameters controller.Properties `json:"parameters,omitempty"`
|
Parameters map[string]interface{} `json:"parameters,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- Controller 定义 ---
|
// --- Controller 定义 ---
|
||||||
@@ -145,7 +145,12 @@ func (c *Controller) CreatePlan(ctx *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 使用已有的转换函数将创建后的模型转换为响应对象
|
// 使用已有的转换函数将创建后的模型转换为响应对象
|
||||||
resp := PlanToResponse(planToCreate)
|
resp, err := PlanToResponse(planToCreate)
|
||||||
|
if err != nil {
|
||||||
|
c.logger.Errorf("创建计划: 序列化响应失败: %v", err)
|
||||||
|
controller.SendErrorResponse(ctx, controller.CodeInternalError, "计划创建成功,但响应生成失败")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// 使用统一的成功响应函数
|
// 使用统一的成功响应函数
|
||||||
controller.SendResponse(ctx, controller.CodeCreated, "计划创建成功", resp)
|
controller.SendResponse(ctx, controller.CodeCreated, "计划创建成功", resp)
|
||||||
@@ -183,7 +188,12 @@ func (c *Controller) GetPlan(ctx *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 3. 将模型转换为响应 DTO
|
// 3. 将模型转换为响应 DTO
|
||||||
resp := PlanToResponse(plan)
|
resp, err := PlanToResponse(plan)
|
||||||
|
if err != nil {
|
||||||
|
c.logger.Errorf("获取计划详情: 序列化响应失败: %v", err)
|
||||||
|
controller.SendErrorResponse(ctx, controller.CodeInternalError, "获取计划详情失败: 内部数据格式错误")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// 4. 发送成功响应
|
// 4. 发送成功响应
|
||||||
controller.SendResponse(ctx, controller.CodeSuccess, "获取计划详情成功", resp)
|
controller.SendResponse(ctx, controller.CodeSuccess, "获取计划详情成功", resp)
|
||||||
@@ -208,7 +218,13 @@ func (c *Controller) ListPlans(ctx *gin.Context) {
|
|||||||
// 2. 将模型转换为响应 DTO
|
// 2. 将模型转换为响应 DTO
|
||||||
planResponses := make([]PlanResponse, 0, len(plans))
|
planResponses := make([]PlanResponse, 0, len(plans))
|
||||||
for _, p := range plans {
|
for _, p := range plans {
|
||||||
planResponses = append(planResponses, *PlanToResponse(&p))
|
resp, err := PlanToResponse(&p)
|
||||||
|
if err != nil {
|
||||||
|
c.logger.Errorf("获取计划列表: 序列化响应失败: %v", err)
|
||||||
|
controller.SendErrorResponse(ctx, controller.CodeInternalError, "获取计划列表失败: 内部数据格式错误")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
planResponses = append(planResponses, *resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. 构造并发送成功响应
|
// 3. 构造并发送成功响应
|
||||||
@@ -286,7 +302,12 @@ func (c *Controller) UpdatePlan(ctx *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 7. 将模型转换为响应 DTO
|
// 7. 将模型转换为响应 DTO
|
||||||
resp := PlanToResponse(updatedPlan)
|
resp, err := PlanToResponse(updatedPlan)
|
||||||
|
if err != nil {
|
||||||
|
c.logger.Errorf("更新计划: 序列化响应失败: %v", err)
|
||||||
|
controller.SendErrorResponse(ctx, controller.CodeInternalError, "计划更新成功,但响应生成失败")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// 8. 发送成功响应
|
// 8. 发送成功响应
|
||||||
controller.SendResponse(ctx, controller.CodeSuccess, "计划更新成功", resp)
|
controller.SendResponse(ctx, controller.CodeSuccess, "计划更新成功", resp)
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package controller
|
package controller
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
@@ -46,6 +45,3 @@ func SendResponse(ctx *gin.Context, code int, message string, data interface{})
|
|||||||
func SendErrorResponse(ctx *gin.Context, code int, message string) {
|
func SendErrorResponse(ctx *gin.Context, code int, message string) {
|
||||||
SendResponse(ctx, code, message, nil)
|
SendResponse(ctx, code, message, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Properties 是一个自定义类型,用于在 Swagger 中正确表示 JSON 对象
|
|
||||||
type Properties json.RawMessage
|
|
||||||
|
|||||||
@@ -70,7 +70,7 @@ type Device struct {
|
|||||||
gorm.Model
|
gorm.Model
|
||||||
|
|
||||||
// Name 是设备的业务名称,应清晰可读,例如 "1号猪舍温度传感器" 或 "做料车间主控"
|
// Name 是设备的业务名称,应清晰可读,例如 "1号猪舍温度传感器" 或 "做料车间主控"
|
||||||
Name string `gorm:"unique;not null" json:"name"`
|
Name string `gorm:"not null" json:"name"`
|
||||||
|
|
||||||
// Type 是设备的高级类别,用于区分区域主控和普通设备。建立索引以优化按类型查询。
|
// Type 是设备的高级类别,用于区分区域主控和普通设备。建立索引以优化按类型查询。
|
||||||
Type DeviceType `gorm:"not null;index" json:"type"`
|
Type DeviceType `gorm:"not null;index" json:"type"`
|
||||||
|
|||||||
Reference in New Issue
Block a user