Files
pig-farm-controller/internal/app/controller/device/device_controller.go

244 lines
11 KiB
Go
Raw Normal View History

2025-09-12 17:18:14 +08:00
package device
import (
2025-11-05 17:24:19 +08:00
"context"
2025-09-12 17:18:14 +08:00
"errors"
2025-11-03 17:27:29 +08:00
"strconv"
2025-09-12 17:18:14 +08:00
"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-10-31 15:38:10 +08:00
"git.huangwc.com/pig/pig-farm-controller/internal/app/service"
2025-09-12 17:18:14 +08:00
"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
2025-11-05 19:57:30 +08:00
2025-10-30 17:15:14 +08:00
"github.com/labstack/echo/v4"
2025-09-12 17:18:14 +08:00
"gorm.io/gorm"
)
2025-12-05 14:44:36 +08:00
// DeviceController 设备控制器
type DeviceController struct {
2025-11-05 17:24:19 +08:00
ctx context.Context
2025-10-31 15:38:10 +08:00
deviceService service.DeviceService
2025-09-12 17:18:14 +08:00
}
2025-12-05 14:44:36 +08:00
// NewDeviceController 创建一个新的设备控制器实例
func NewDeviceController(
2025-11-05 17:24:19 +08:00
ctx context.Context,
2025-10-31 15:38:10 +08:00
deviceService service.DeviceService,
2025-12-05 14:44:36 +08:00
) *DeviceController {
return &DeviceController{
2025-11-05 17:24:19 +08:00
ctx: ctx,
2025-10-31 15:38:10 +08:00
deviceService: deviceService,
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 设备管理
2025-10-13 14:15:38 +08:00
// @Security BearerAuth
2025-09-12 17:18:14 +08:00
// @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-12-05 14:44:36 +08:00
func (c *DeviceController) CreateDevice(ctx echo.Context) error {
2025-11-05 17:24:19 +08:00
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "CreateDevice")
2025-09-28 01:28:54 +08:00
const actionType = "创建设备"
2025-10-03 23:02:43 +08:00
var req dto.CreateDeviceRequest
2025-10-30 17:15:14 +08:00
if err := ctx.Bind(&req); err != nil {
2025-11-05 17:24:19 +08:00
logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
2025-10-30 17:15:14 +08:00
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
2025-09-12 17:18:14 +08:00
}
2025-11-05 17:24:19 +08:00
resp, err := c.deviceService.CreateDevice(reqCtx, &req)
2025-09-20 17:11:04 +08:00
if err != nil {
2025-11-05 17:24:19 +08:00
logger.Errorf("%s: 服务层创建失败: %v", actionType, err)
2025-10-31 15:38:10 +08:00
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "创建设备失败: "+err.Error(), actionType, "服务层创建失败", req)
2025-09-20 17:11:04 +08:00
}
2025-11-05 17:24:19 +08:00
logger.Infof("%s: 设备创建成功, ID: %d", actionType, resp.ID)
2025-10-30 17:15:14 +08:00
return controller.SendSuccessWithAudit(ctx, controller.CodeCreated, "设备创建成功", resp, actionType, "设备创建成功", resp)
2025-09-12 17:18:14 +08:00
}
// GetDevice godoc
// @Summary 获取设备信息
// @Description 根据设备ID获取单个设备的详细信息
// @Tags 设备管理
2025-10-13 14:15:38 +08:00
// @Security BearerAuth
2025-09-12 17:18:14 +08:00
// @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-12-05 14:44:36 +08:00
func (c *DeviceController) GetDevice(ctx echo.Context) error {
2025-11-05 17:24:19 +08:00
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "GetDevice")
2025-09-28 01:28:54 +08:00
const actionType = "获取设备"
2025-09-12 17:18:14 +08:00
deviceID := ctx.Param("id")
2025-11-03 17:27:29 +08:00
id, err := strconv.ParseUint(deviceID, 10, 64)
if err != nil {
2025-11-05 17:24:19 +08:00
logger.Errorf("%s: 无效的ID: %s", actionType, deviceID)
2025-11-03 17:27:29 +08:00
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID: "+deviceID, actionType, "无效的ID", deviceID)
}
2025-11-10 22:23:31 +08:00
resp, err := c.deviceService.GetDevice(reqCtx, uint32(id))
2025-09-12 17:18:14 +08:00
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
2025-11-05 17:24:19 +08:00
logger.Warnf("%s: 设备不存在, ID: %s", actionType, deviceID)
2025-10-30 17:15:14 +08:00
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "设备未找到", actionType, "设备不存在", deviceID)
2025-09-12 17:18:14 +08:00
}
2025-11-05 17:24:19 +08:00
logger.Errorf("%s: 服务层获取失败: %v, ID: %s", actionType, err, deviceID)
2025-10-31 15:38:10 +08:00
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取设备信息失败: "+err.Error(), actionType, "服务层获取失败", deviceID)
2025-09-12 17:18:14 +08:00
}
2025-11-05 17:24:19 +08:00
logger.Infof("%s: 获取设备信息成功, ID: %d", actionType, resp.ID)
2025-10-30 17:15:14 +08:00
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取设备信息成功", resp, actionType, "获取设备信息成功", resp)
2025-09-12 17:18:14 +08:00
}
// ListDevices godoc
// @Summary 获取设备列表
// @Description 获取系统中所有设备的列表
// @Tags 设备管理
2025-10-13 14:15:38 +08:00
// @Security BearerAuth
2025-09-12 17:18:14 +08:00
// @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-12-05 14:44:36 +08:00
func (c *DeviceController) ListDevices(ctx echo.Context) error {
2025-11-05 17:24:19 +08:00
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "ListDevices")
2025-09-28 01:28:54 +08:00
const actionType = "获取设备列表"
2025-11-05 17:24:19 +08:00
resp, err := c.deviceService.ListDevices(reqCtx)
2025-09-12 17:18:14 +08:00
if err != nil {
2025-11-05 17:24:19 +08:00
logger.Errorf("%s: 服务层获取列表失败: %v", actionType, err)
2025-10-31 15:38:10 +08:00
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取设备列表失败: "+err.Error(), actionType, "服务层获取列表失败", nil)
2025-09-12 17:18:14 +08:00
}
2025-11-05 17:24:19 +08:00
logger.Infof("%s: 获取设备列表成功, 数量: %d", actionType, len(resp))
2025-10-30 17:15:14 +08:00
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取设备列表成功", resp, actionType, "获取设备列表成功", resp)
2025-09-12 17:18:14 +08:00
}
// UpdateDevice godoc
// @Summary 更新设备信息
// @Description 根据设备ID更新一个已存在的设备信息
// @Tags 设备管理
2025-10-13 14:15:38 +08:00
// @Security BearerAuth
2025-09-12 17:18:14 +08:00
// @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-12-05 14:44:36 +08:00
func (c *DeviceController) UpdateDevice(ctx echo.Context) error {
2025-11-05 17:24:19 +08:00
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "UpdateDevice")
2025-09-28 01:28:54 +08:00
const actionType = "更新设备"
2025-09-12 17:18:14 +08:00
deviceID := ctx.Param("id")
2025-10-03 23:02:43 +08:00
var req dto.UpdateDeviceRequest
2025-10-30 17:15:14 +08:00
if err := ctx.Bind(&req); err != nil {
2025-11-05 17:24:19 +08:00
logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
2025-10-30 17:15:14 +08:00
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
2025-09-12 17:18:14 +08:00
}
2025-11-03 17:27:29 +08:00
id, err := strconv.ParseUint(deviceID, 10, 64)
if err != nil {
2025-11-05 17:24:19 +08:00
logger.Errorf("%s: 无效的ID: %s", actionType, deviceID)
2025-11-03 17:27:29 +08:00
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID: "+deviceID, actionType, "无效的ID", deviceID)
}
2025-11-10 22:23:31 +08:00
resp, err := c.deviceService.UpdateDevice(reqCtx, uint32(id), &req)
2025-09-30 00:18:21 +08:00
if err != nil {
2025-10-31 15:38:10 +08:00
if errors.Is(err, gorm.ErrRecordNotFound) {
2025-11-05 17:24:19 +08:00
logger.Warnf("%s: 设备不存在, ID: %s", actionType, deviceID)
2025-10-31 15:38:10 +08:00
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "设备未找到", actionType, "设备不存在", deviceID)
}
2025-11-05 17:24:19 +08:00
logger.Errorf("%s: 服务层更新失败: %v, ID: %s", actionType, err, deviceID)
2025-10-31 15:38:10 +08:00
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新设备失败: "+err.Error(), actionType, "服务层更新失败", deviceID)
2025-09-20 17:11:04 +08:00
}
2025-11-05 17:24:19 +08:00
logger.Infof("%s: 设备更新成功, ID: %d", actionType, resp.ID)
2025-10-30 17:15:14 +08:00
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "设备更新成功", resp, actionType, "设备更新成功", resp)
2025-09-12 17:18:14 +08:00
}
// DeleteDevice godoc
// @Summary 删除设备
// @Description 根据设备ID删除一个设备软删除
// @Tags 设备管理
2025-10-13 14:15:38 +08:00
// @Security BearerAuth
2025-09-12 17:18:14 +08:00
// @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-12-05 14:44:36 +08:00
func (c *DeviceController) DeleteDevice(ctx echo.Context) error {
2025-11-05 17:24:19 +08:00
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "DeleteDevice")
2025-09-28 01:28:54 +08:00
const actionType = "删除设备"
2025-09-12 17:18:14 +08:00
deviceID := ctx.Param("id")
2025-11-03 17:27:29 +08:00
id, err := strconv.ParseUint(deviceID, 10, 64)
if err != nil {
2025-11-05 17:24:19 +08:00
logger.Errorf("%s: 无效的ID: %s", actionType, deviceID)
2025-11-03 17:27:29 +08:00
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID: "+deviceID, actionType, "无效的ID", deviceID)
}
2025-11-10 22:23:31 +08:00
if err := c.deviceService.DeleteDevice(reqCtx, uint32(id)); err != nil {
2025-11-03 16:46:23 +08:00
switch {
case errors.Is(err, gorm.ErrRecordNotFound):
2025-11-05 17:24:19 +08:00
logger.Warnf("%s: 设备不存在, ID: %s", actionType, deviceID)
2025-10-30 17:15:14 +08:00
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "设备未找到", actionType, "设备不存在", deviceID)
2025-11-03 16:46:23 +08:00
case errors.Is(err, service.ErrDeviceInUse):
2025-11-05 17:24:19 +08:00
logger.Warnf("%s: 尝试删除正在被使用的设备, ID: %s", actionType, deviceID)
2025-11-03 16:46:23 +08:00
// 返回 409 Conflict 状态码,表示请求与服务器当前状态冲突
return controller.SendErrorWithAudit(ctx, controller.CodeConflict, err.Error(), actionType, "设备正在被使用", deviceID)
default:
2025-11-05 17:24:19 +08:00
logger.Errorf("%s: 服务层删除失败: %v, ID: %s", actionType, err, deviceID)
2025-11-03 16:46:23 +08:00
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "删除设备失败: "+err.Error(), actionType, "服务层删除失败", deviceID)
2025-09-28 01:33:19 +08:00
}
2025-09-12 17:18:14 +08:00
}
2025-11-05 17:24:19 +08:00
logger.Infof("%s: 设备删除成功, ID: %s", actionType, deviceID)
2025-10-30 17:15:14 +08:00
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "设备删除成功", nil, actionType, "设备删除成功", deviceID)
2025-09-12 17:18:14 +08:00
}
2025-09-30 15:25:07 +08:00
2025-10-13 10:39:51 +08:00
// ManualControl godoc
// @Summary 手动控制设备
// @Description 根据设备ID和指定的动作开启或关闭来手动控制设备
// @Tags 设备管理
2025-10-13 14:15:38 +08:00
// @Security BearerAuth
2025-10-13 10:39:51 +08:00
// @Accept json
// @Produce json
// @Param id path string true "设备ID"
// @Param manualControl body dto.ManualControlDeviceRequest true "手动控制指令"
// @Success 200 {object} controller.Response
// @Router /api/v1/devices/manual-control/{id} [post]
2025-12-05 14:44:36 +08:00
func (c *DeviceController) ManualControl(ctx echo.Context) error {
2025-11-05 17:24:19 +08:00
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "ManualControlDevice")
2025-10-13 10:39:51 +08:00
const actionType = "手动控制设备"
deviceID := ctx.Param("id")
var req dto.ManualControlDeviceRequest
2025-10-30 17:15:14 +08:00
if err := ctx.Bind(&req); err != nil {
2025-11-05 17:24:19 +08:00
logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
2025-10-30 17:15:14 +08:00
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
2025-10-13 10:39:51 +08:00
}
2025-11-03 17:27:29 +08:00
id, err := strconv.ParseUint(deviceID, 10, 64)
if err != nil {
2025-11-05 17:24:19 +08:00
logger.Errorf("%s: 无效的ID: %s", actionType, deviceID)
2025-11-03 17:27:29 +08:00
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID: "+deviceID, actionType, "无效的ID", deviceID)
}
2025-11-10 22:23:31 +08:00
if err := c.deviceService.ManualControl(reqCtx, uint32(id), &req); err != nil {
2025-10-13 10:39:51 +08:00
if errors.Is(err, gorm.ErrRecordNotFound) {
2025-11-05 17:24:19 +08:00
logger.Warnf("%s: 设备不存在, ID: %s", actionType, deviceID)
2025-10-30 17:15:14 +08:00
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "设备未找到", actionType, "设备不存在", deviceID)
2025-10-13 10:39:51 +08:00
}
2025-11-05 17:24:19 +08:00
logger.Errorf("%s: 服务层手动控制失败: %v, ID: %s", actionType, err, deviceID)
2025-10-31 15:38:10 +08:00
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "手动控制失败: "+err.Error(), actionType, "服务层手动控制失败", deviceID)
2025-10-13 10:39:51 +08:00
}
2025-10-31 15:38:10 +08:00
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "指令已发送", nil, actionType, "指令发送成功", nil)
2025-10-13 10:39:51 +08:00
}