1. 修复错误

This commit is contained in:
2025-09-08 18:54:41 +08:00
parent 7112a16ca8
commit 5ce0848929
7 changed files with 1152 additions and 31 deletions

View File

@@ -3,6 +3,9 @@
package device
import (
"encoding/json"
"strconv"
"git.huangwc.com/pig/pig-farm-controller/internal/api/middleware"
"git.huangwc.com/pig/pig-farm-controller/internal/controller"
"git.huangwc.com/pig/pig-farm-controller/internal/logs"
@@ -19,10 +22,49 @@ type ListResponse struct {
// DeviceRequest 设备创建/更新请求结构体
type DeviceRequest struct {
Name string `json:"name" binding:"required"`
Type model.DeviceType `json:"type" binding:"required"`
ParentID *uint `json:"parent_id"`
Status string `json:"status" binding:"required"`
Name string `json:"name" binding:"required"` // 设备名称,必填
Type model.DeviceType `json:"type" binding:"required"` // 设备类型,必填
ParentID *uint `json:"parent_id,omitempty"` // 父设备ID可选
}
// BindAndValidate 绑定并验证请求数据
func (req *DeviceRequest) BindAndValidate(data []byte) error {
// 创建一个map来解析原始JSON
raw := make(map[string]interface{})
if err := json.Unmarshal(data, &raw); err != nil {
return err
}
// 解析已知字段
if name, ok := raw["name"].(string); ok {
req.Name = name
}
if typ, ok := raw["type"].(string); ok {
req.Type = model.DeviceType(typ)
}
// 特殊处理parent_id字段
if parentIDVal, exists := raw["parent_id"]; exists && parentIDVal != nil {
switch v := parentIDVal.(type) {
case float64:
// JSON数字默认是float64类型
if v >= 0 {
parentID := uint(v)
req.ParentID = &parentID
}
case string:
// 如果是字符串尝试转换为uint
if v != "" && v != "null" {
if parentID, err := strconv.ParseUint(v, 10, 32); err == nil {
parentIDUint := uint(parentID)
req.ParentID = &parentIDUint
}
}
}
}
return nil
}
// Controller 设备控制控制器
@@ -58,16 +100,26 @@ func (c *Controller) List(ctx *gin.Context) {
// Create 创建设备
func (c *Controller) Create(ctx *gin.Context) {
var req DeviceRequest
if err := ctx.ShouldBindJSON(&req); err != nil {
// 直接使用绑定和验证方法处理JSON数据
rawData, err := ctx.GetRawData()
if err != nil {
controller.SendErrorResponse(ctx, controller.InvalidParameterCode, "无法读取请求数据: "+err.Error())
return
}
if err := req.BindAndValidate(rawData); err != nil {
controller.SendErrorResponse(ctx, controller.InvalidParameterCode, "请求参数错误: "+err.Error())
return
}
// TODO: 设备状态应该由系统自动获取,而不是由用户指定
// 这里设置默认状态为active后续需要实现自动状态检测
device := &model.Device{
Name: req.Name,
Type: req.Type,
ParentID: req.ParentID,
Status: req.Status,
Status: "active", // 默认设置为active状态
}
if err := c.deviceRepo.Create(device); err != nil {
@@ -86,7 +138,30 @@ func (c *Controller) Update(ctx *gin.Context) {
DeviceRequest
}
if err := ctx.ShouldBindJSON(&req); err != nil {
// 直接使用绑定和验证方法处理JSON数据
rawData, err := ctx.GetRawData()
if err != nil {
controller.SendErrorResponse(ctx, controller.InvalidParameterCode, "无法读取请求数据: "+err.Error())
return
}
// 先解析ID
var raw map[string]interface{}
if json.Unmarshal(rawData, &raw) == nil {
if idVal, ok := raw["id"]; ok {
switch id := idVal.(type) {
case float64:
req.ID = uint(id)
case string:
if idUint, err := strconv.ParseUint(id, 10, 32); err == nil {
req.ID = uint(idUint)
}
}
}
}
// 再解析DeviceRequest部分
if err := req.DeviceRequest.BindAndValidate(rawData); err != nil {
controller.SendErrorResponse(ctx, controller.InvalidParameterCode, "请求参数错误: "+err.Error())
return
}
@@ -101,7 +176,9 @@ func (c *Controller) Update(ctx *gin.Context) {
device.Name = req.Name
device.Type = req.Type
device.ParentID = req.ParentID
device.Status = req.Status
// TODO: 设备状态应该由系统自动获取,而不是由用户指定
// 这里保持设备原有状态,后续需要实现自动状态检测
// device.Status = req.Status
if err := c.deviceRepo.Update(device); err != nil {
c.logger.Error("更新设备失败: " + err.Error())