2025-09-12 17:18:14 +08:00
|
|
|
|
package device
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2025-09-20 17:11:04 +08:00
|
|
|
|
"encoding/json"
|
2025-09-12 17:18:14 +08:00
|
|
|
|
"errors"
|
|
|
|
|
|
"strconv"
|
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
|
|
"git.huangwc.com/pig/pig-farm-controller/internal/app/controller"
|
2025-10-03 23:02:43 +08:00
|
|
|
|
"git.huangwc.com/pig/pig-farm-controller/internal/app/dto"
|
2025-09-12 17:18:14 +08:00
|
|
|
|
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
|
|
|
|
|
|
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
|
|
|
|
|
"git.huangwc.com/pig/pig-farm-controller/internal/infra/repository"
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
|
"gorm.io/gorm"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2025-09-30 15:25:07 +08:00
|
|
|
|
// Controller 设备控制器,封装了所有与设备和区域主控相关的业务逻辑
|
2025-09-12 17:18:14 +08:00
|
|
|
|
type Controller struct {
|
2025-09-30 15:25:07 +08:00
|
|
|
|
deviceRepo repository.DeviceRepository
|
|
|
|
|
|
areaControllerRepo repository.AreaControllerRepository
|
2025-09-30 22:07:55 +08:00
|
|
|
|
deviceTemplateRepo repository.DeviceTemplateRepository // 设备模板仓库
|
2025-09-30 15:25:07 +08:00
|
|
|
|
logger *logs.Logger
|
2025-09-12 17:18:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// NewController 创建一个新的设备控制器实例
|
2025-09-30 15:25:07 +08:00
|
|
|
|
func NewController(
|
|
|
|
|
|
deviceRepo repository.DeviceRepository,
|
|
|
|
|
|
areaControllerRepo repository.AreaControllerRepository,
|
2025-09-30 22:07:55 +08:00
|
|
|
|
deviceTemplateRepo repository.DeviceTemplateRepository, // 注入设备模板仓库
|
2025-09-30 15:25:07 +08:00
|
|
|
|
logger *logs.Logger,
|
|
|
|
|
|
) *Controller {
|
2025-09-12 17:18:14 +08:00
|
|
|
|
return &Controller{
|
2025-09-30 15:25:07 +08:00
|
|
|
|
deviceRepo: deviceRepo,
|
|
|
|
|
|
areaControllerRepo: areaControllerRepo,
|
2025-10-03 23:02:43 +08:00
|
|
|
|
deviceTemplateRepo: deviceTemplateRepo,
|
2025-09-30 15:25:07 +08:00
|
|
|
|
logger: logger,
|
2025-09-12 17:18:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-30 15:25:07 +08:00
|
|
|
|
// --- Controller Methods: Devices ---
|
2025-09-12 17:18:14 +08:00
|
|
|
|
|
|
|
|
|
|
// CreateDevice godoc
|
|
|
|
|
|
// @Summary 创建新设备
|
2025-09-12 17:43:42 +08:00
|
|
|
|
// @Description 根据提供的信息创建一个新设备
|
2025-09-12 17:18:14 +08:00
|
|
|
|
// @Tags 设备管理
|
|
|
|
|
|
// @Accept json
|
|
|
|
|
|
// @Produce json
|
2025-10-03 23:02:43 +08:00
|
|
|
|
// @Param device body dto.CreateDeviceRequest true "设备信息"
|
|
|
|
|
|
// @Success 200 {object} controller.Response{data=dto.DeviceResponse}
|
2025-09-19 15:55:56 +08:00
|
|
|
|
// @Router /api/v1/devices [post]
|
2025-09-12 17:18:14 +08:00
|
|
|
|
func (c *Controller) CreateDevice(ctx *gin.Context) {
|
2025-09-28 01:28:54 +08:00
|
|
|
|
const actionType = "创建设备"
|
2025-10-03 23:02:43 +08:00
|
|
|
|
var req dto.CreateDeviceRequest
|
2025-09-12 17:18:14 +08:00
|
|
|
|
if err := ctx.ShouldBindJSON(&req); err != nil {
|
2025-09-28 01:33:19 +08:00
|
|
|
|
c.logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
2025-09-28 01:28:54 +08:00
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
|
2025-09-12 17:18:14 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-20 17:11:04 +08:00
|
|
|
|
propertiesJSON, err := json.Marshal(req.Properties)
|
|
|
|
|
|
if err != nil {
|
2025-09-28 01:33:19 +08:00
|
|
|
|
c.logger.Errorf("%s: 序列化属性失败: %v", actionType, err)
|
2025-09-28 01:28:54 +08:00
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "属性字段格式错误", actionType, "属性序列化失败", req.Properties)
|
2025-09-20 17:11:04 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-12 17:18:14 +08:00
|
|
|
|
device := &models.Device{
|
2025-09-30 00:18:21 +08:00
|
|
|
|
Name: req.Name,
|
|
|
|
|
|
DeviceTemplateID: req.DeviceTemplateID,
|
|
|
|
|
|
AreaControllerID: req.AreaControllerID,
|
|
|
|
|
|
Location: req.Location,
|
|
|
|
|
|
Properties: propertiesJSON,
|
2025-09-12 17:18:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-30 00:18:21 +08:00
|
|
|
|
if err := device.SelfCheck(); err != nil {
|
|
|
|
|
|
c.logger.Errorf("%s: 设备属性自检失败: %v", actionType, err)
|
|
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "设备属性不符合要求: "+err.Error(), actionType, "设备属性自检失败", device)
|
2025-09-27 01:03:58 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-30 15:25:07 +08:00
|
|
|
|
if err := c.deviceRepo.Create(device); err != nil {
|
2025-09-28 01:33:19 +08:00
|
|
|
|
c.logger.Errorf("%s: 数据库操作失败: %v", actionType, err)
|
2025-09-28 01:28:54 +08:00
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "创建设备失败: "+err.Error(), actionType, "数据库创建失败", device)
|
2025-09-12 17:18:14 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-30 15:25:07 +08:00
|
|
|
|
createdDevice, err := c.deviceRepo.FindByID(device.ID)
|
2025-09-30 00:18:21 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
c.logger.Errorf("%s: 重新加载创建的设备失败: %v", actionType, err)
|
|
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "设备创建成功,但重新加载设备失败", actionType, "重新加载设备失败", device)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-03 23:02:43 +08:00
|
|
|
|
resp, err := dto.NewDeviceResponse(createdDevice)
|
2025-09-20 17:11:04 +08:00
|
|
|
|
if err != nil {
|
2025-10-03 23:02:43 +08:00
|
|
|
|
c.logger.Errorf("%s: 序列化响应失败: %v, Device: %+v", actionType, err, createdDevice)
|
2025-09-30 00:18:21 +08:00
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "设备创建成功,但响应生成失败", actionType, "响应序列化失败", createdDevice)
|
2025-09-20 17:11:04 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-28 01:33:19 +08:00
|
|
|
|
c.logger.Infof("%s: 设备创建成功, ID: %d", actionType, device.ID)
|
2025-09-28 01:28:54 +08:00
|
|
|
|
controller.SendSuccessWithAudit(ctx, controller.CodeCreated, "设备创建成功", resp, actionType, "设备创建成功", resp)
|
2025-09-12 17:18:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// GetDevice godoc
|
|
|
|
|
|
// @Summary 获取设备信息
|
|
|
|
|
|
// @Description 根据设备ID获取单个设备的详细信息
|
|
|
|
|
|
// @Tags 设备管理
|
|
|
|
|
|
// @Produce json
|
|
|
|
|
|
// @Param id path string true "设备ID"
|
2025-10-03 23:02:43 +08:00
|
|
|
|
// @Success 200 {object} controller.Response{data=dto.DeviceResponse}
|
2025-09-19 15:55:56 +08:00
|
|
|
|
// @Router /api/v1/devices/{id} [get]
|
2025-09-12 17:18:14 +08:00
|
|
|
|
func (c *Controller) GetDevice(ctx *gin.Context) {
|
2025-09-28 01:28:54 +08:00
|
|
|
|
const actionType = "获取设备"
|
2025-09-12 17:18:14 +08:00
|
|
|
|
deviceID := ctx.Param("id")
|
|
|
|
|
|
|
2025-09-28 01:33:19 +08:00
|
|
|
|
if deviceID == "" {
|
|
|
|
|
|
c.logger.Errorf("%s: 设备ID为空", actionType)
|
|
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "设备ID不能为空", actionType, "设备ID为空", nil)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-30 15:25:07 +08:00
|
|
|
|
device, err := c.deviceRepo.FindByIDString(deviceID)
|
2025-09-12 17:18:14 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
2025-09-28 01:33:19 +08:00
|
|
|
|
c.logger.Warnf("%s: 设备不存在, ID: %s", actionType, deviceID)
|
2025-09-28 01:28:54 +08:00
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "设备未找到", actionType, "设备不存在", deviceID)
|
2025-09-12 17:18:14 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
if strings.Contains(err.Error(), "无效的设备ID格式") {
|
2025-09-28 01:33:19 +08:00
|
|
|
|
c.logger.Errorf("%s: 设备ID格式错误: %v, ID: %s", actionType, err, deviceID)
|
2025-09-28 01:28:54 +08:00
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, err.Error(), actionType, "设备ID格式错误", deviceID)
|
2025-09-12 17:18:14 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
2025-09-28 01:33:19 +08:00
|
|
|
|
c.logger.Errorf("%s: 数据库查询失败: %v, ID: %s", actionType, err, deviceID)
|
2025-09-28 01:28:54 +08:00
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取设备信息失败: "+err.Error(), actionType, "数据库查询失败", deviceID)
|
2025-09-12 17:18:14 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-03 23:02:43 +08:00
|
|
|
|
resp, err := dto.NewDeviceResponse(device)
|
2025-09-20 17:11:04 +08:00
|
|
|
|
if err != nil {
|
2025-09-28 01:33:19 +08:00
|
|
|
|
c.logger.Errorf("%s: 序列化响应失败: %v, Device: %+v", actionType, err, device)
|
2025-09-28 01:28:54 +08:00
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取设备信息失败: 内部数据格式错误", actionType, "响应序列化失败", device)
|
2025-09-20 17:11:04 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-28 01:33:19 +08:00
|
|
|
|
c.logger.Infof("%s: 获取设备信息成功, ID: %d", actionType, device.ID)
|
2025-09-28 01:28:54 +08:00
|
|
|
|
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取设备信息成功", resp, actionType, "获取设备信息成功", resp)
|
2025-09-12 17:18:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ListDevices godoc
|
|
|
|
|
|
// @Summary 获取设备列表
|
|
|
|
|
|
// @Description 获取系统中所有设备的列表
|
|
|
|
|
|
// @Tags 设备管理
|
|
|
|
|
|
// @Produce json
|
2025-10-03 23:02:43 +08:00
|
|
|
|
// @Success 200 {object} controller.Response{data=[]dto.DeviceResponse}
|
2025-09-19 15:55:56 +08:00
|
|
|
|
// @Router /api/v1/devices [get]
|
2025-09-12 17:18:14 +08:00
|
|
|
|
func (c *Controller) ListDevices(ctx *gin.Context) {
|
2025-09-28 01:28:54 +08:00
|
|
|
|
const actionType = "获取设备列表"
|
2025-09-30 15:25:07 +08:00
|
|
|
|
devices, err := c.deviceRepo.ListAll()
|
2025-09-12 17:18:14 +08:00
|
|
|
|
if err != nil {
|
2025-09-28 01:33:19 +08:00
|
|
|
|
c.logger.Errorf("%s: 数据库查询失败: %v", actionType, err)
|
2025-09-28 01:28:54 +08:00
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取设备列表失败: "+err.Error(), actionType, "数据库查询失败", nil)
|
2025-09-12 17:18:14 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-03 23:02:43 +08:00
|
|
|
|
resp, err := dto.NewListDeviceResponse(devices)
|
2025-09-20 17:11:04 +08:00
|
|
|
|
if err != nil {
|
2025-09-28 01:33:19 +08:00
|
|
|
|
c.logger.Errorf("%s: 序列化响应失败: %v, Devices: %+v", actionType, err, devices)
|
2025-09-28 01:28:54 +08:00
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取设备列表失败: 内部数据格式错误", actionType, "响应序列化失败", devices)
|
2025-09-20 17:11:04 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-28 01:33:19 +08:00
|
|
|
|
c.logger.Infof("%s: 获取设备列表成功, 数量: %d", actionType, len(devices))
|
2025-09-28 01:28:54 +08:00
|
|
|
|
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取设备列表成功", resp, actionType, "获取设备列表成功", resp)
|
2025-09-12 17:18:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// UpdateDevice godoc
|
|
|
|
|
|
// @Summary 更新设备信息
|
|
|
|
|
|
// @Description 根据设备ID更新一个已存在的设备信息
|
|
|
|
|
|
// @Tags 设备管理
|
|
|
|
|
|
// @Accept json
|
|
|
|
|
|
// @Produce json
|
|
|
|
|
|
// @Param id path string true "设备ID"
|
2025-10-03 23:02:43 +08:00
|
|
|
|
// @Param device body dto.UpdateDeviceRequest true "要更新的设备信息"
|
|
|
|
|
|
// @Success 200 {object} controller.Response{data=dto.DeviceResponse}
|
2025-09-19 15:55:56 +08:00
|
|
|
|
// @Router /api/v1/devices/{id} [put]
|
2025-09-12 17:18:14 +08:00
|
|
|
|
func (c *Controller) UpdateDevice(ctx *gin.Context) {
|
2025-09-28 01:28:54 +08:00
|
|
|
|
const actionType = "更新设备"
|
2025-09-12 17:18:14 +08:00
|
|
|
|
deviceID := ctx.Param("id")
|
|
|
|
|
|
|
2025-09-30 15:25:07 +08:00
|
|
|
|
existingDevice, err := c.deviceRepo.FindByIDString(deviceID)
|
2025-09-12 17:18:14 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
2025-09-28 01:33:19 +08:00
|
|
|
|
c.logger.Warnf("%s: 设备不存在, ID: %s", actionType, deviceID)
|
2025-09-28 01:28:54 +08:00
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "设备未找到", actionType, "设备不存在", deviceID)
|
2025-09-12 17:18:14 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
if strings.Contains(err.Error(), "无效的设备ID格式") {
|
2025-09-28 01:33:19 +08:00
|
|
|
|
c.logger.Errorf("%s: 设备ID格式错误: %v, ID: %s", actionType, err, deviceID)
|
2025-09-28 01:28:54 +08:00
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, err.Error(), actionType, "设备ID格式错误", deviceID)
|
2025-09-12 17:18:14 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
2025-09-28 01:33:19 +08:00
|
|
|
|
c.logger.Errorf("%s: 数据库查询失败: %v, ID: %s", actionType, err, deviceID)
|
2025-09-28 01:28:54 +08:00
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新设备失败: "+err.Error(), actionType, "数据库查询失败", deviceID)
|
2025-09-12 17:18:14 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-03 23:02:43 +08:00
|
|
|
|
var req dto.UpdateDeviceRequest
|
2025-09-12 17:18:14 +08:00
|
|
|
|
if err := ctx.ShouldBindJSON(&req); err != nil {
|
2025-09-28 01:33:19 +08:00
|
|
|
|
c.logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
2025-09-28 01:28:54 +08:00
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
|
2025-09-12 17:18:14 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-20 17:11:04 +08:00
|
|
|
|
propertiesJSON, err := json.Marshal(req.Properties)
|
|
|
|
|
|
if err != nil {
|
2025-09-28 01:33:19 +08:00
|
|
|
|
c.logger.Errorf("%s: 序列化属性失败: %v", actionType, err)
|
2025-09-28 01:28:54 +08:00
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "属性字段格式错误", actionType, "属性序列化失败", req.Properties)
|
2025-09-20 17:11:04 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-12 17:18:14 +08:00
|
|
|
|
existingDevice.Name = req.Name
|
2025-09-30 00:18:21 +08:00
|
|
|
|
existingDevice.DeviceTemplateID = req.DeviceTemplateID
|
|
|
|
|
|
existingDevice.AreaControllerID = req.AreaControllerID
|
2025-09-12 17:18:14 +08:00
|
|
|
|
existingDevice.Location = req.Location
|
2025-09-20 17:11:04 +08:00
|
|
|
|
existingDevice.Properties = propertiesJSON
|
2025-09-12 17:18:14 +08:00
|
|
|
|
|
2025-09-30 00:18:21 +08:00
|
|
|
|
if err := existingDevice.SelfCheck(); err != nil {
|
|
|
|
|
|
c.logger.Errorf("%s: 设备属性自检失败: %v", actionType, err)
|
|
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "设备属性不符合要求: "+err.Error(), actionType, "设备属性自检失败", existingDevice)
|
2025-09-27 01:03:58 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-30 15:25:07 +08:00
|
|
|
|
if err := c.deviceRepo.Update(existingDevice); err != nil {
|
2025-09-28 01:33:19 +08:00
|
|
|
|
c.logger.Errorf("%s: 数据库更新失败: %v, Device: %+v", actionType, err, existingDevice)
|
2025-09-28 01:28:54 +08:00
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新设备失败: "+err.Error(), actionType, "数据库更新失败", existingDevice)
|
2025-09-12 17:18:14 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-30 15:25:07 +08:00
|
|
|
|
updatedDevice, err := c.deviceRepo.FindByID(existingDevice.ID)
|
2025-09-20 17:11:04 +08:00
|
|
|
|
if err != nil {
|
2025-09-30 00:18:21 +08:00
|
|
|
|
c.logger.Errorf("%s: 重新加载更新的设备失败: %v", actionType, err)
|
|
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "设备更新成功,但重新加载设备失败", actionType, "重新加载设备失败", existingDevice)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-03 23:02:43 +08:00
|
|
|
|
resp, err := dto.NewDeviceResponse(updatedDevice)
|
2025-09-30 00:18:21 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
c.logger.Errorf("%s: 序列化响应失败: %v, Device: %+v", actionType, err, updatedDevice)
|
|
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "设备更新成功,但响应生成失败", actionType, "响应序列化失败", updatedDevice)
|
2025-09-20 17:11:04 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-28 01:33:19 +08:00
|
|
|
|
c.logger.Infof("%s: 设备更新成功, ID: %d", actionType, existingDevice.ID)
|
2025-09-28 01:28:54 +08:00
|
|
|
|
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "设备更新成功", resp, actionType, "设备更新成功", resp)
|
2025-09-12 17:18:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// DeleteDevice godoc
|
|
|
|
|
|
// @Summary 删除设备
|
|
|
|
|
|
// @Description 根据设备ID删除一个设备(软删除)
|
|
|
|
|
|
// @Tags 设备管理
|
|
|
|
|
|
// @Produce json
|
|
|
|
|
|
// @Param id path string true "设备ID"
|
2025-09-19 23:51:13 +08:00
|
|
|
|
// @Success 200 {object} controller.Response
|
2025-09-19 15:55:56 +08:00
|
|
|
|
// @Router /api/v1/devices/{id} [delete]
|
2025-09-12 17:18:14 +08:00
|
|
|
|
func (c *Controller) DeleteDevice(ctx *gin.Context) {
|
2025-09-28 01:28:54 +08:00
|
|
|
|
const actionType = "删除设备"
|
2025-09-12 17:18:14 +08:00
|
|
|
|
deviceID := ctx.Param("id")
|
|
|
|
|
|
|
|
|
|
|
|
idUint, err := strconv.ParseUint(deviceID, 10, 64)
|
|
|
|
|
|
if err != nil {
|
2025-09-28 01:33:19 +08:00
|
|
|
|
c.logger.Errorf("%s: 设备ID格式错误: %v, ID: %s", actionType, err, deviceID)
|
2025-09-28 01:28:54 +08:00
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的设备ID格式", actionType, "设备ID格式错误", deviceID)
|
2025-09-12 17:18:14 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-30 15:25:07 +08:00
|
|
|
|
_, err = c.deviceRepo.FindByIDString(deviceID)
|
2025-09-28 01:33:19 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
|
|
c.logger.Warnf("%s: 设备不存在, ID: %s", actionType, deviceID)
|
2025-09-30 00:18:21 +08:00
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "设备未找到", actionType, "设备不存在", deviceID)
|
2025-09-28 01:33:19 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
c.logger.Errorf("%s: 查找设备失败: %v, ID: %s", actionType, err, deviceID)
|
|
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "删除设备失败: 查找设备时发生内部错误", actionType, "数据库查询失败", deviceID)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-30 15:25:07 +08:00
|
|
|
|
if err := c.deviceRepo.Delete(uint(idUint)); err != nil {
|
2025-09-28 01:33:19 +08:00
|
|
|
|
c.logger.Errorf("%s: 数据库删除失败: %v, ID: %d", actionType, err, idUint)
|
2025-09-28 01:28:54 +08:00
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "删除设备失败: "+err.Error(), actionType, "数据库删除失败", deviceID)
|
2025-09-12 17:18:14 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-28 01:33:19 +08:00
|
|
|
|
c.logger.Infof("%s: 设备删除成功, ID: %d", actionType, idUint)
|
2025-09-28 01:28:54 +08:00
|
|
|
|
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "设备删除成功", nil, actionType, "设备删除成功", deviceID)
|
2025-09-12 17:18:14 +08:00
|
|
|
|
}
|
2025-09-30 15:25:07 +08:00
|
|
|
|
|
|
|
|
|
|
// --- Controller Methods: Area Controllers ---
|
|
|
|
|
|
|
|
|
|
|
|
// CreateAreaController godoc
|
|
|
|
|
|
// @Summary 创建新区域主控
|
|
|
|
|
|
// @Description 根据提供的信息创建一个新区域主控
|
|
|
|
|
|
// @Tags 区域主控管理
|
|
|
|
|
|
// @Accept json
|
|
|
|
|
|
// @Produce json
|
2025-10-03 23:02:43 +08:00
|
|
|
|
// @Param areaController body dto.CreateAreaControllerRequest true "区域主控信息"
|
|
|
|
|
|
// @Success 200 {object} controller.Response{data=dto.AreaControllerResponse}
|
2025-09-30 15:25:07 +08:00
|
|
|
|
// @Router /api/v1/area-controllers [post]
|
|
|
|
|
|
func (c *Controller) CreateAreaController(ctx *gin.Context) {
|
|
|
|
|
|
const actionType = "创建区域主控"
|
2025-10-03 23:02:43 +08:00
|
|
|
|
var req dto.CreateAreaControllerRequest
|
2025-09-30 15:25:07 +08:00
|
|
|
|
if err := ctx.ShouldBindJSON(&req); err != nil {
|
|
|
|
|
|
c.logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
|
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
propertiesJSON, err := json.Marshal(req.Properties)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
c.logger.Errorf("%s: 序列化属性失败: %v", actionType, err)
|
|
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "属性字段格式错误", actionType, "属性序列化失败", req.Properties)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ac := &models.AreaController{
|
|
|
|
|
|
Name: req.Name,
|
|
|
|
|
|
NetworkID: req.NetworkID,
|
|
|
|
|
|
Location: req.Location,
|
|
|
|
|
|
Properties: propertiesJSON,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if err := ac.SelfCheck(); err != nil {
|
|
|
|
|
|
c.logger.Errorf("%s: 区域主控自检失败: %v", actionType, err)
|
|
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "区域主控参数不符合要求: "+err.Error(), actionType, "区域主控自检失败", ac)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if err := c.areaControllerRepo.Create(ac); err != nil {
|
|
|
|
|
|
c.logger.Errorf("%s: 数据库操作失败: %v", actionType, err)
|
|
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "创建区域主控失败: "+err.Error(), actionType, "数据库创建失败", ac)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-03 23:02:43 +08:00
|
|
|
|
resp, err := dto.NewAreaControllerResponse(ac)
|
2025-09-30 15:25:07 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
c.logger.Errorf("%s: 序列化响应失败: %v", actionType, err)
|
|
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "区域主控创建成功,但响应生成失败", actionType, "响应序列化失败", ac)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
c.logger.Infof("%s: 区域主控创建成功, ID: %d", actionType, ac.ID)
|
|
|
|
|
|
controller.SendSuccessWithAudit(ctx, controller.CodeCreated, "区域主控创建成功", resp, actionType, "区域主控创建成功", resp)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// GetAreaController godoc
|
|
|
|
|
|
// @Summary 获取区域主控信息
|
|
|
|
|
|
// @Description 根据ID获取单个区域主控的详细信息
|
|
|
|
|
|
// @Tags 区域主控管理
|
|
|
|
|
|
// @Produce json
|
|
|
|
|
|
// @Param id path string true "区域主控ID"
|
2025-10-03 23:02:43 +08:00
|
|
|
|
// @Success 200 {object} controller.Response{data=dto.AreaControllerResponse}
|
2025-09-30 15:25:07 +08:00
|
|
|
|
// @Router /api/v1/area-controllers/{id} [get]
|
|
|
|
|
|
func (c *Controller) GetAreaController(ctx *gin.Context) {
|
|
|
|
|
|
const actionType = "获取区域主控"
|
|
|
|
|
|
acID := ctx.Param("id")
|
|
|
|
|
|
|
|
|
|
|
|
idUint, err := strconv.ParseUint(acID, 10, 64)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
c.logger.Errorf("%s: 区域主控ID格式错误: %v, ID: %s", actionType, err, acID)
|
|
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的区域主控ID格式", actionType, "ID格式错误", acID)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ac, err := c.areaControllerRepo.FindByID(uint(idUint))
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
|
|
c.logger.Warnf("%s: 区域主控不存在, ID: %s", actionType, acID)
|
|
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "区域主控未找到", actionType, "区域主控不存在", acID)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
c.logger.Errorf("%s: 数据库查询失败: %v, ID: %s", actionType, err, acID)
|
|
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取区域主控信息失败: "+err.Error(), actionType, "数据库查询失败", acID)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-03 23:02:43 +08:00
|
|
|
|
resp, err := dto.NewAreaControllerResponse(ac)
|
2025-09-30 15:25:07 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
c.logger.Errorf("%s: 序列化响应失败: %v, AreaController: %+v", actionType, err, ac)
|
|
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取区域主控信息失败: 内部数据格式错误", actionType, "响应序列化失败", ac)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
c.logger.Infof("%s: 获取区域主控信息成功, ID: %d", actionType, ac.ID)
|
|
|
|
|
|
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取区域主控信息成功", resp, actionType, "获取区域主控信息成功", resp)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ListAreaControllers godoc
|
|
|
|
|
|
// @Summary 获取所有区域主控列表
|
|
|
|
|
|
// @Description 获取系统中所有区域主控的列表
|
|
|
|
|
|
// @Tags 区域主控管理
|
|
|
|
|
|
// @Produce json
|
2025-10-03 23:02:43 +08:00
|
|
|
|
// @Success 200 {object} controller.Response{data=[]dto.AreaControllerResponse}
|
2025-09-30 15:25:07 +08:00
|
|
|
|
// @Router /api/v1/area-controllers [get]
|
|
|
|
|
|
func (c *Controller) ListAreaControllers(ctx *gin.Context) {
|
|
|
|
|
|
const actionType = "获取区域主控列表"
|
|
|
|
|
|
acs, err := c.areaControllerRepo.ListAll()
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
c.logger.Errorf("%s: 数据库查询失败: %v", actionType, err)
|
|
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取区域主控列表失败: "+err.Error(), actionType, "数据库查询失败", nil)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-03 23:02:43 +08:00
|
|
|
|
resp, err := dto.NewListAreaControllerResponse(acs)
|
2025-09-30 15:25:07 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
c.logger.Errorf("%s: 序列化响应失败: %v, AreaControllers: %+v", actionType, err, acs)
|
|
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取区域主控列表失败: 内部数据格式错误", actionType, "响应序列化失败", acs)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
c.logger.Infof("%s: 获取区域主控列表成功, 数量: %d", actionType, len(acs))
|
|
|
|
|
|
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取区域主控列表成功", resp, actionType, "获取区域主控列表成功", resp)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// UpdateAreaController godoc
|
|
|
|
|
|
// @Summary 更新区域主控信息
|
|
|
|
|
|
// @Description 根据ID更新一个已存在的区域主控信息
|
|
|
|
|
|
// @Tags 区域主控管理
|
|
|
|
|
|
// @Accept json
|
|
|
|
|
|
// @Produce json
|
|
|
|
|
|
// @Param id path string true "区域主控ID"
|
2025-10-03 23:02:43 +08:00
|
|
|
|
// @Param areaController body dto.UpdateAreaControllerRequest true "要更新的区域主控信息"
|
|
|
|
|
|
// @Success 200 {object} controller.Response{data=dto.AreaControllerResponse}
|
2025-09-30 15:25:07 +08:00
|
|
|
|
// @Router /api/v1/area-controllers/{id} [put]
|
|
|
|
|
|
func (c *Controller) UpdateAreaController(ctx *gin.Context) {
|
|
|
|
|
|
const actionType = "更新区域主控"
|
|
|
|
|
|
acID := ctx.Param("id")
|
|
|
|
|
|
|
|
|
|
|
|
idUint, err := strconv.ParseUint(acID, 10, 64)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
c.logger.Errorf("%s: 区域主控ID格式错误: %v, ID: %s", actionType, err, acID)
|
|
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的区域主控ID格式", actionType, "ID格式错误", acID)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
existingAC, err := c.areaControllerRepo.FindByID(uint(idUint))
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
|
|
c.logger.Warnf("%s: 区域主控不存在, ID: %s", actionType, acID)
|
|
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "区域主控未找到", actionType, "区域主控不存在", acID)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
c.logger.Errorf("%s: 数据库查询失败: %v, ID: %s", actionType, err, acID)
|
|
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新区域主控失败: "+err.Error(), actionType, "数据库查询失败", acID)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-03 23:02:43 +08:00
|
|
|
|
var req dto.UpdateAreaControllerRequest
|
2025-09-30 15:25:07 +08:00
|
|
|
|
if err := ctx.ShouldBindJSON(&req); err != nil {
|
|
|
|
|
|
c.logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
|
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
propertiesJSON, err := json.Marshal(req.Properties)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
c.logger.Errorf("%s: 序列化属性失败: %v", actionType, err)
|
|
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "属性字段格式错误", actionType, "属性序列化失败", req.Properties)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
existingAC.Name = req.Name
|
|
|
|
|
|
existingAC.NetworkID = req.NetworkID
|
|
|
|
|
|
existingAC.Location = req.Location
|
|
|
|
|
|
existingAC.Properties = propertiesJSON
|
|
|
|
|
|
|
|
|
|
|
|
if err := existingAC.SelfCheck(); err != nil {
|
|
|
|
|
|
c.logger.Errorf("%s: 区域主控自检失败: %v", actionType, err)
|
|
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "区域主控参数不符合要求: "+err.Error(), actionType, "区域主控自检失败", existingAC)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if err := c.areaControllerRepo.Update(existingAC); err != nil {
|
|
|
|
|
|
c.logger.Errorf("%s: 数据库更新失败: %v, AreaController: %+v", actionType, err, existingAC)
|
|
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新区域主控失败: "+err.Error(), actionType, "数据库更新失败", existingAC)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-03 23:02:43 +08:00
|
|
|
|
resp, err := dto.NewAreaControllerResponse(existingAC)
|
2025-09-30 15:25:07 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
c.logger.Errorf("%s: 序列化响应失败: %v, AreaController: %+v", actionType, err, existingAC)
|
|
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "区域主控更新成功,但响应生成失败", actionType, "响应序列化失败", existingAC)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
c.logger.Infof("%s: 区域主控更新成功, ID: %d", actionType, existingAC.ID)
|
|
|
|
|
|
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "区域主控更新成功", resp, actionType, "区域主控更新成功", resp)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// DeleteAreaController godoc
|
|
|
|
|
|
// @Summary 删除区域主控
|
|
|
|
|
|
// @Description 根据ID删除一个区域主控(软删除)
|
|
|
|
|
|
// @Tags 区域主控管理
|
|
|
|
|
|
// @Produce json
|
|
|
|
|
|
// @Param id path string true "区域主控ID"
|
|
|
|
|
|
// @Success 200 {object} controller.Response
|
|
|
|
|
|
// @Router /api/v1/area-controllers/{id} [delete]
|
|
|
|
|
|
func (c *Controller) DeleteAreaController(ctx *gin.Context) {
|
|
|
|
|
|
const actionType = "删除区域主控"
|
|
|
|
|
|
acID := ctx.Param("id")
|
|
|
|
|
|
|
|
|
|
|
|
idUint, err := strconv.ParseUint(acID, 10, 64)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
c.logger.Errorf("%s: 区域主控ID格式错误: %v, ID: %s", actionType, err, acID)
|
|
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的区域主控ID格式", actionType, "ID格式错误", acID)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_, err = c.areaControllerRepo.FindByID(uint(idUint))
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
|
|
c.logger.Warnf("%s: 区域主控不存在, ID: %s", actionType, acID)
|
|
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "区域主控未找到", actionType, "区域主控不存在", acID)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
c.logger.Errorf("%s: 查找区域主控失败: %v, ID: %s", actionType, err, acID)
|
|
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "删除区域主控失败: 查找时发生内部错误", actionType, "数据库查询失败", acID)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if err := c.areaControllerRepo.Delete(uint(idUint)); err != nil {
|
|
|
|
|
|
c.logger.Errorf("%s: 数据库删除失败: %v, ID: %d", actionType, err, idUint)
|
|
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "删除区域主控失败: "+err.Error(), actionType, "数据库删除失败", acID)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
c.logger.Infof("%s: 区域主控删除成功, ID: %d", actionType, idUint)
|
|
|
|
|
|
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "区域主控删除成功", nil, actionType, "区域主控删除成功", acID)
|
|
|
|
|
|
}
|
2025-09-30 22:07:55 +08:00
|
|
|
|
|
|
|
|
|
|
// --- Controller Methods: Device Templates ---
|
|
|
|
|
|
|
|
|
|
|
|
// CreateDeviceTemplate godoc
|
|
|
|
|
|
// @Summary 创建新设备模板
|
|
|
|
|
|
// @Description 根据提供的信息创建一个新设备模板
|
|
|
|
|
|
// @Tags 设备模板管理
|
|
|
|
|
|
// @Accept json
|
|
|
|
|
|
// @Produce json
|
2025-10-03 23:02:43 +08:00
|
|
|
|
// @Param deviceTemplate body dto.CreateDeviceTemplateRequest true "设备模板信息"
|
|
|
|
|
|
// @Success 200 {object} controller.Response{data=dto.DeviceTemplateResponse}
|
2025-09-30 22:07:55 +08:00
|
|
|
|
// @Router /api/v1/device-templates [post]
|
|
|
|
|
|
func (c *Controller) CreateDeviceTemplate(ctx *gin.Context) {
|
|
|
|
|
|
const actionType = "创建设备模板"
|
2025-10-03 23:02:43 +08:00
|
|
|
|
var req dto.CreateDeviceTemplateRequest
|
2025-09-30 22:07:55 +08:00
|
|
|
|
if err := ctx.ShouldBindJSON(&req); err != nil {
|
|
|
|
|
|
c.logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
|
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
commandsJSON, err := json.Marshal(req.Commands)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
c.logger.Errorf("%s: 序列化命令失败: %v", actionType, err)
|
|
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "命令字段格式错误", actionType, "命令序列化失败", req.Commands)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
valuesJSON, err := json.Marshal(req.Values)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
c.logger.Errorf("%s: 序列化值描述符失败: %v", actionType, err)
|
|
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "值描述符字段格式错误", actionType, "值描述符序列化失败", req.Values)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
deviceTemplate := &models.DeviceTemplate{
|
|
|
|
|
|
Name: req.Name,
|
|
|
|
|
|
Manufacturer: req.Manufacturer,
|
|
|
|
|
|
Description: req.Description,
|
|
|
|
|
|
Category: req.Category,
|
|
|
|
|
|
Commands: commandsJSON,
|
|
|
|
|
|
Values: valuesJSON,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if err := deviceTemplate.SelfCheck(); err != nil {
|
|
|
|
|
|
c.logger.Errorf("%s: 设备模板自检失败: %v", actionType, err)
|
|
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "设备模板参数不符合要求: "+err.Error(), actionType, "设备模板自检失败", deviceTemplate)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if err := c.deviceTemplateRepo.Create(deviceTemplate); err != nil {
|
|
|
|
|
|
c.logger.Errorf("%s: 数据库操作失败: %v", actionType, err)
|
|
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "创建设备模板失败: "+err.Error(), actionType, "数据库创建失败", deviceTemplate)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-03 23:02:43 +08:00
|
|
|
|
resp, err := dto.NewDeviceTemplateResponse(deviceTemplate)
|
2025-09-30 22:07:55 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
c.logger.Errorf("%s: 序列化响应失败: %v", actionType, err)
|
|
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "设备模板创建成功,但响应生成失败", actionType, "响应序列化失败", deviceTemplate)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
c.logger.Infof("%s: 设备模板创建成功, ID: %d", actionType, deviceTemplate.ID)
|
|
|
|
|
|
controller.SendSuccessWithAudit(ctx, controller.CodeCreated, "设备模板创建成功", resp, actionType, "设备模板创建成功", resp)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// GetDeviceTemplate godoc
|
|
|
|
|
|
// @Summary 获取设备模板信息
|
|
|
|
|
|
// @Description 根据设备模板ID获取单个设备模板的详细信息
|
|
|
|
|
|
// @Tags 设备模板管理
|
|
|
|
|
|
// @Produce json
|
|
|
|
|
|
// @Param id path string true "设备模板ID"
|
2025-10-03 23:02:43 +08:00
|
|
|
|
// @Success 200 {object} controller.Response{data=dto.DeviceTemplateResponse}
|
2025-09-30 22:07:55 +08:00
|
|
|
|
// @Router /api/v1/device-templates/{id} [get]
|
|
|
|
|
|
func (c *Controller) GetDeviceTemplate(ctx *gin.Context) {
|
|
|
|
|
|
const actionType = "获取设备模板"
|
|
|
|
|
|
dtID := ctx.Param("id")
|
|
|
|
|
|
|
|
|
|
|
|
idUint, err := strconv.ParseUint(dtID, 10, 64)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
c.logger.Errorf("%s: 设备模板ID格式错误: %v, ID: %s", actionType, err, dtID)
|
|
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的设备模板ID格式", actionType, "ID格式错误", dtID)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
deviceTemplate, err := c.deviceTemplateRepo.FindByID(uint(idUint))
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
|
|
c.logger.Warnf("%s: 设备模板不存在, ID: %s", actionType, dtID)
|
|
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "设备模板未找到", actionType, "设备模板不存在", dtID)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
c.logger.Errorf("%s: 数据库查询失败: %v, ID: %s", actionType, err, dtID)
|
|
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取设备模板信息失败: "+err.Error(), actionType, "数据库查询失败", dtID)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-03 23:02:43 +08:00
|
|
|
|
resp, err := dto.NewDeviceTemplateResponse(deviceTemplate)
|
2025-09-30 22:07:55 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
c.logger.Errorf("%s: 序列化响应失败: %v, DeviceTemplate: %+v", actionType, err, deviceTemplate)
|
|
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取设备模板信息失败: 内部数据格式错误", actionType, "响应序列化失败", deviceTemplate)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
c.logger.Infof("%s: 获取设备模板信息成功, ID: %d", actionType, deviceTemplate.ID)
|
|
|
|
|
|
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取设备模板信息成功", resp, actionType, "获取设备模板信息成功", resp)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ListDeviceTemplates godoc
|
|
|
|
|
|
// @Summary 获取设备模板列表
|
|
|
|
|
|
// @Description 获取系统中所有设备模板的列表
|
|
|
|
|
|
// @Tags 设备模板管理
|
|
|
|
|
|
// @Produce json
|
2025-10-03 23:02:43 +08:00
|
|
|
|
// @Success 200 {object} controller.Response{data=[]dto.DeviceTemplateResponse}
|
2025-09-30 22:07:55 +08:00
|
|
|
|
// @Router /api/v1/device-templates [get]
|
|
|
|
|
|
func (c *Controller) ListDeviceTemplates(ctx *gin.Context) {
|
|
|
|
|
|
const actionType = "获取设备模板列表"
|
|
|
|
|
|
deviceTemplates, err := c.deviceTemplateRepo.ListAll()
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
c.logger.Errorf("%s: 数据库查询失败: %v", actionType, err)
|
|
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取设备模板列表失败: "+err.Error(), actionType, "数据库查询失败", nil)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-03 23:02:43 +08:00
|
|
|
|
resp, err := dto.NewListDeviceTemplateResponse(deviceTemplates)
|
2025-09-30 22:07:55 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
c.logger.Errorf("%s: 序列化响应失败: %v, DeviceTemplates: %+v", actionType, err, deviceTemplates)
|
|
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取设备模板列表失败: 内部数据格式错误", actionType, "响应序列化失败", deviceTemplates)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
c.logger.Infof("%s: 获取设备模板列表成功, 数量: %d", actionType, len(deviceTemplates))
|
|
|
|
|
|
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取设备模板列表成功", resp, actionType, "获取设备模板列表成功", resp)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// UpdateDeviceTemplate godoc
|
|
|
|
|
|
// @Summary 更新设备模板信息
|
|
|
|
|
|
// @Description 根据设备模板ID更新一个已存在的设备模板信息
|
|
|
|
|
|
// @Tags 设备模板管理
|
|
|
|
|
|
// @Accept json
|
|
|
|
|
|
// @Produce json
|
|
|
|
|
|
// @Param id path string true "设备模板ID"
|
2025-10-03 23:02:43 +08:00
|
|
|
|
// @Param deviceTemplate body dto.UpdateDeviceTemplateRequest true "要更新的设备模板信息"
|
|
|
|
|
|
// @Success 200 {object} controller.Response{data=dto.DeviceTemplateResponse}
|
2025-09-30 22:07:55 +08:00
|
|
|
|
// @Router /api/v1/device-templates/{id} [put]
|
|
|
|
|
|
func (c *Controller) UpdateDeviceTemplate(ctx *gin.Context) {
|
|
|
|
|
|
const actionType = "更新设备模板"
|
|
|
|
|
|
dtID := ctx.Param("id")
|
|
|
|
|
|
|
|
|
|
|
|
idUint, err := strconv.ParseUint(dtID, 10, 64)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
c.logger.Errorf("%s: 设备模板ID格式错误: %v, ID: %s", actionType, err, dtID)
|
|
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的设备模板ID格式", actionType, "ID格式错误", dtID)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
existingDeviceTemplate, err := c.deviceTemplateRepo.FindByID(uint(idUint))
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
|
|
c.logger.Warnf("%s: 设备模板不存在, ID: %s", actionType, dtID)
|
|
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "设备模板未找到", actionType, "设备模板不存在", dtID)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
c.logger.Errorf("%s: 数据库查询失败: %v, ID: %s", actionType, err, dtID)
|
|
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新设备模板失败: "+err.Error(), actionType, "数据库查询失败", dtID)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-03 23:02:43 +08:00
|
|
|
|
var req dto.UpdateDeviceTemplateRequest
|
2025-09-30 22:07:55 +08:00
|
|
|
|
if err := ctx.ShouldBindJSON(&req); err != nil {
|
|
|
|
|
|
c.logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
|
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
commandsJSON, err := json.Marshal(req.Commands)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
c.logger.Errorf("%s: 序列化命令失败: %v", actionType, err)
|
|
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "命令字段格式错误", actionType, "命令序列化失败", req.Commands)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
valuesJSON, err := json.Marshal(req.Values)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
c.logger.Errorf("%s: 序列化值描述符失败: %v", actionType, err)
|
|
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "值描述符字段格式错误", actionType, "值描述符序列化失败", req.Values)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
existingDeviceTemplate.Name = req.Name
|
|
|
|
|
|
existingDeviceTemplate.Manufacturer = req.Manufacturer
|
|
|
|
|
|
existingDeviceTemplate.Description = req.Description
|
|
|
|
|
|
existingDeviceTemplate.Category = req.Category
|
|
|
|
|
|
existingDeviceTemplate.Commands = commandsJSON
|
|
|
|
|
|
existingDeviceTemplate.Values = valuesJSON
|
|
|
|
|
|
|
|
|
|
|
|
if err := existingDeviceTemplate.SelfCheck(); err != nil {
|
|
|
|
|
|
c.logger.Errorf("%s: 设备模板自检失败: %v", actionType, err)
|
|
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "设备模板参数不符合要求: "+err.Error(), actionType, "设备模板自检失败", existingDeviceTemplate)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if err := c.deviceTemplateRepo.Update(existingDeviceTemplate); err != nil {
|
|
|
|
|
|
c.logger.Errorf("%s: 数据库更新失败: %v, DeviceTemplate: %+v", actionType, err, existingDeviceTemplate)
|
|
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新设备模板失败: "+err.Error(), actionType, "数据库更新失败", existingDeviceTemplate)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-10-03 23:02:43 +08:00
|
|
|
|
resp, err := dto.NewDeviceTemplateResponse(existingDeviceTemplate)
|
2025-09-30 22:07:55 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
c.logger.Errorf("%s: 序列化响应失败: %v, DeviceTemplate: %+v", actionType, err, existingDeviceTemplate)
|
|
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "设备模板更新成功,但响应生成失败", actionType, "响应序列化失败", existingDeviceTemplate)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
c.logger.Infof("%s: 设备模板更新成功, ID: %d", actionType, existingDeviceTemplate.ID)
|
|
|
|
|
|
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "设备模板更新成功", resp, actionType, "设备模板更新成功", resp)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// DeleteDeviceTemplate godoc
|
|
|
|
|
|
// @Summary 删除设备模板
|
|
|
|
|
|
// @Description 根据设备模板ID删除一个设备模板(软删除)
|
|
|
|
|
|
// @Tags 设备模板管理
|
|
|
|
|
|
// @Produce json
|
|
|
|
|
|
// @Param id path string true "设备模板ID"
|
|
|
|
|
|
// @Success 200 {object} controller.Response
|
|
|
|
|
|
// @Router /api/v1/device-templates/{id} [delete]
|
|
|
|
|
|
func (c *Controller) DeleteDeviceTemplate(ctx *gin.Context) {
|
|
|
|
|
|
const actionType = "删除设备模板"
|
|
|
|
|
|
dtID := ctx.Param("id")
|
|
|
|
|
|
|
|
|
|
|
|
idUint, err := strconv.ParseUint(dtID, 10, 64)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
c.logger.Errorf("%s: 设备模板ID格式错误: %v, ID: %s", actionType, err, dtID)
|
|
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的设备模板ID格式", actionType, "ID格式错误", dtID)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 在尝试删除之前,先检查设备模板是否存在
|
|
|
|
|
|
_, err = c.deviceTemplateRepo.FindByID(uint(idUint))
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
|
|
c.logger.Warnf("%s: 设备模板不存在, ID: %s", actionType, dtID)
|
|
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "设备模板未找到", actionType, "设备模板不存在", dtID)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
c.logger.Errorf("%s: 查找设备模板失败: %v, ID: %s", actionType, err, dtID)
|
|
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "删除设备模板失败: 查找时发生内部错误", actionType, "数据库查询失败", dtID)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 调用仓库层的删除方法,该方法会检查模板是否被使用
|
|
|
|
|
|
if err := c.deviceTemplateRepo.Delete(uint(idUint)); err != nil {
|
|
|
|
|
|
c.logger.Errorf("%s: 数据库删除失败: %v, ID: %d", actionType, err, idUint)
|
|
|
|
|
|
// 如果错误信息包含“设备模板正在被设备使用,无法删除”,则返回特定的错误码
|
|
|
|
|
|
if strings.Contains(err.Error(), "设备模板正在被设备使用,无法删除") {
|
|
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, err.Error(), actionType, "设备模板正在使用", dtID)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// 其他数据库错误
|
|
|
|
|
|
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "删除设备模板失败: "+err.Error(), actionType, "数据库删除失败", dtID)
|
|
|
|
|
|
}
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
c.logger.Infof("%s: 设备模板删除成功, ID: %d", actionType, idUint)
|
|
|
|
|
|
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "设备模板删除成功", nil, actionType, "设备模板删除成功", dtID)
|
|
|
|
|
|
}
|