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

477 lines
22 KiB
Go
Raw Normal View History

2025-09-12 17:18:14 +08:00
package device
import (
"errors"
"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-10-30 17:15:14 +08:00
"github.com/labstack/echo/v4"
2025-09-12 17:18:14 +08:00
"gorm.io/gorm"
)
2025-09-30 15:25:07 +08:00
// Controller 设备控制器,封装了所有与设备和区域主控相关的业务逻辑
2025-09-12 17:18:14 +08:00
type Controller struct {
2025-10-31 15:38:10 +08:00
deviceService service.DeviceService
logger *logs.Logger
2025-09-12 17:18:14 +08:00
}
// NewController 创建一个新的设备控制器实例
2025-09-30 15:25:07 +08:00
func NewController(
2025-10-31 15:38:10 +08:00
deviceService service.DeviceService,
2025-09-30 15:25:07 +08:00
logger *logs.Logger,
) *Controller {
2025-09-12 17:18:14 +08:00
return &Controller{
2025-10-31 15:38:10 +08:00
deviceService: deviceService,
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 设备管理
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-10-30 17:15:14 +08:00
func (c *Controller) CreateDevice(ctx echo.Context) error {
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-09-28 01:33:19 +08:00
c.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-10-31 15:38:10 +08:00
resp, err := c.deviceService.CreateDevice(&req)
2025-09-20 17:11:04 +08:00
if err != nil {
2025-10-31 15:38:10 +08:00
c.logger.Errorf("%s: 服务层创建失败: %v", actionType, err)
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "创建设备失败: "+err.Error(), actionType, "服务层创建失败", req)
2025-09-20 17:11:04 +08:00
}
2025-10-31 15:38:10 +08:00
c.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-10-30 17:15:14 +08:00
func (c *Controller) GetDevice(ctx echo.Context) error {
2025-09-28 01:28:54 +08:00
const actionType = "获取设备"
2025-09-12 17:18:14 +08:00
deviceID := ctx.Param("id")
2025-10-31 15:38:10 +08:00
resp, err := c.deviceService.GetDevice(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-10-30 17:15:14 +08:00
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "设备未找到", actionType, "设备不存在", deviceID)
2025-09-12 17:18:14 +08:00
}
2025-10-31 15:38:10 +08:00
c.logger.Errorf("%s: 服务层获取失败: %v, ID: %s", actionType, err, deviceID)
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取设备信息失败: "+err.Error(), actionType, "服务层获取失败", deviceID)
2025-09-12 17:18:14 +08:00
}
2025-10-31 15:38:10 +08:00
c.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-10-30 17:15:14 +08:00
func (c *Controller) ListDevices(ctx echo.Context) error {
2025-09-28 01:28:54 +08:00
const actionType = "获取设备列表"
2025-10-31 15:38:10 +08:00
resp, err := c.deviceService.ListDevices()
2025-09-12 17:18:14 +08:00
if err != nil {
2025-10-31 15:38:10 +08:00
c.logger.Errorf("%s: 服务层获取列表失败: %v", actionType, err)
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取设备列表失败: "+err.Error(), actionType, "服务层获取列表失败", nil)
2025-09-12 17:18:14 +08:00
}
2025-10-31 15:38:10 +08:00
c.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-10-30 17:15:14 +08:00
func (c *Controller) UpdateDevice(ctx echo.Context) error {
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-09-28 01:33:19 +08:00
c.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-10-31 15:38:10 +08:00
resp, err := c.deviceService.UpdateDevice(deviceID, &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) {
c.logger.Warnf("%s: 设备不存在, ID: %s", actionType, deviceID)
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "设备未找到", actionType, "设备不存在", deviceID)
}
c.logger.Errorf("%s: 服务层更新失败: %v, ID: %s", actionType, err, deviceID)
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新设备失败: "+err.Error(), actionType, "服务层更新失败", deviceID)
2025-09-20 17:11:04 +08:00
}
2025-10-31 15:38:10 +08:00
c.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-10-30 17:15:14 +08:00
func (c *Controller) DeleteDevice(ctx echo.Context) error {
2025-09-28 01:28:54 +08:00
const actionType = "删除设备"
2025-09-12 17:18:14 +08:00
deviceID := ctx.Param("id")
2025-10-31 15:38:10 +08:00
if err := c.deviceService.DeleteDevice(deviceID); err != nil {
2025-09-28 01:33:19 +08:00
if errors.Is(err, gorm.ErrRecordNotFound) {
c.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-28 01:33:19 +08:00
}
2025-10-31 15:38:10 +08:00
c.logger.Errorf("%s: 服务层删除失败: %v, ID: %s", actionType, err, deviceID)
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "删除设备失败: "+err.Error(), actionType, "服务层删除失败", deviceID)
2025-09-12 17:18:14 +08:00
}
2025-10-31 15:38:10 +08:00
c.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-10-30 17:15:14 +08:00
func (c *Controller) ManualControl(ctx echo.Context) error {
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-10-13 10:39:51 +08:00
c.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-10-31 15:38:10 +08:00
if err := c.deviceService.ManualControl(deviceID, &req); err != nil {
2025-10-13 10:39:51 +08:00
if errors.Is(err, gorm.ErrRecordNotFound) {
c.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-10-31 15:38:10 +08:00
c.logger.Errorf("%s: 服务层手动控制失败: %v, ID: %s", actionType, err, deviceID)
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
}
2025-09-30 15:25:07 +08:00
// --- Controller Methods: Area Controllers ---
// CreateAreaController godoc
// @Summary 创建新区域主控
// @Description 根据提供的信息创建一个新区域主控
// @Tags 区域主控管理
2025-10-13 14:15:38 +08:00
// @Security BearerAuth
2025-09-30 15:25:07 +08:00
// @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]
2025-10-30 17:15:14 +08:00
func (c *Controller) CreateAreaController(ctx echo.Context) error {
2025-09-30 15:25:07 +08:00
const actionType = "创建区域主控"
2025-10-03 23:02:43 +08:00
var req dto.CreateAreaControllerRequest
2025-10-30 17:15:14 +08:00
if err := ctx.Bind(&req); err != nil {
2025-09-30 15:25:07 +08:00
c.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-30 15:25:07 +08:00
}
2025-10-31 15:38:10 +08:00
resp, err := c.deviceService.CreateAreaController(&req)
2025-09-30 15:25:07 +08:00
if err != nil {
2025-10-31 15:38:10 +08:00
c.logger.Errorf("%s: 服务层创建失败: %v", actionType, err)
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "创建区域主控失败: "+err.Error(), actionType, "服务层创建失败", req)
2025-09-30 15:25:07 +08:00
}
2025-10-31 15:38:10 +08:00
c.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-30 15:25:07 +08:00
}
// GetAreaController godoc
// @Summary 获取区域主控信息
// @Description 根据ID获取单个区域主控的详细信息
// @Tags 区域主控管理
2025-10-13 14:15:38 +08:00
// @Security BearerAuth
2025-09-30 15:25:07 +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.AreaControllerResponse}
2025-09-30 15:25:07 +08:00
// @Router /api/v1/area-controllers/{id} [get]
2025-10-30 17:15:14 +08:00
func (c *Controller) GetAreaController(ctx echo.Context) error {
2025-09-30 15:25:07 +08:00
const actionType = "获取区域主控"
acID := ctx.Param("id")
2025-10-31 15:38:10 +08:00
resp, err := c.deviceService.GetAreaController(acID)
2025-09-30 15:25:07 +08:00
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
c.logger.Warnf("%s: 区域主控不存在, ID: %s", actionType, acID)
2025-10-30 17:15:14 +08:00
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "区域主控未找到", actionType, "区域主控不存在", acID)
2025-09-30 15:25:07 +08:00
}
2025-10-31 15:38:10 +08:00
c.logger.Errorf("%s: 服务层获取失败: %v, ID: %s", actionType, err, acID)
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取区域主控信息失败: "+err.Error(), actionType, "服务层获取失败", acID)
2025-09-30 15:25:07 +08:00
}
2025-10-31 15:38:10 +08:00
c.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-30 15:25:07 +08:00
}
// ListAreaControllers godoc
// @Summary 获取所有区域主控列表
// @Description 获取系统中所有区域主控的列表
// @Tags 区域主控管理
2025-10-13 14:15:38 +08:00
// @Security BearerAuth
2025-09-30 15:25:07 +08:00
// @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]
2025-10-30 17:15:14 +08:00
func (c *Controller) ListAreaControllers(ctx echo.Context) error {
2025-09-30 15:25:07 +08:00
const actionType = "获取区域主控列表"
2025-10-31 15:38:10 +08:00
resp, err := c.deviceService.ListAreaControllers()
2025-09-30 15:25:07 +08:00
if err != nil {
2025-10-31 15:38:10 +08:00
c.logger.Errorf("%s: 服务层获取列表失败: %v", actionType, err)
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取区域主控列表失败: "+err.Error(), actionType, "服务层获取列表失败", nil)
2025-09-30 15:25:07 +08:00
}
2025-10-31 15:38:10 +08:00
c.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-30 15:25:07 +08:00
}
// UpdateAreaController godoc
// @Summary 更新区域主控信息
// @Description 根据ID更新一个已存在的区域主控信息
// @Tags 区域主控管理
2025-10-13 14:15:38 +08:00
// @Security BearerAuth
2025-09-30 15:25:07 +08:00
// @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]
2025-10-30 17:15:14 +08:00
func (c *Controller) UpdateAreaController(ctx echo.Context) error {
2025-09-30 15:25:07 +08:00
const actionType = "更新区域主控"
acID := ctx.Param("id")
2025-10-03 23:02:43 +08:00
var req dto.UpdateAreaControllerRequest
2025-10-30 17:15:14 +08:00
if err := ctx.Bind(&req); err != nil {
2025-09-30 15:25:07 +08:00
c.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-30 15:25:07 +08:00
}
2025-10-31 15:38:10 +08:00
resp, err := c.deviceService.UpdateAreaController(acID, &req)
2025-09-30 15:25:07 +08:00
if err != nil {
2025-10-31 15:38:10 +08:00
if errors.Is(err, gorm.ErrRecordNotFound) {
c.logger.Warnf("%s: 区域主控不存在, ID: %s", actionType, acID)
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "区域主控未找到", actionType, "区域主控不存在", acID)
}
c.logger.Errorf("%s: 服务层更新失败: %v, ID: %s", actionType, err, acID)
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新区域主控失败: "+err.Error(), actionType, "服务层更新失败", acID)
2025-09-30 15:25:07 +08:00
}
2025-10-31 15:38:10 +08:00
c.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-30 15:25:07 +08:00
}
// DeleteAreaController godoc
// @Summary 删除区域主控
// @Description 根据ID删除一个区域主控软删除
// @Tags 区域主控管理
2025-10-13 14:15:38 +08:00
// @Security BearerAuth
2025-09-30 15:25:07 +08:00
// @Produce json
// @Param id path string true "区域主控ID"
// @Success 200 {object} controller.Response
// @Router /api/v1/area-controllers/{id} [delete]
2025-10-30 17:15:14 +08:00
func (c *Controller) DeleteAreaController(ctx echo.Context) error {
2025-09-30 15:25:07 +08:00
const actionType = "删除区域主控"
acID := ctx.Param("id")
2025-10-31 15:38:10 +08:00
if err := c.deviceService.DeleteAreaController(acID); err != nil {
2025-09-30 15:25:07 +08:00
if errors.Is(err, gorm.ErrRecordNotFound) {
c.logger.Warnf("%s: 区域主控不存在, ID: %s", actionType, acID)
2025-10-30 17:15:14 +08:00
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "区域主控未找到", actionType, "区域主控不存在", acID)
2025-09-30 15:25:07 +08:00
}
2025-10-31 15:38:10 +08:00
c.logger.Errorf("%s: 服务层删除失败: %v, ID: %s", actionType, err, acID)
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "删除区域主控失败: "+err.Error(), actionType, "服务层删除失败", acID)
2025-09-30 15:25:07 +08:00
}
2025-10-31 15:38:10 +08:00
c.logger.Infof("%s: 区域主控删除成功, ID: %s", actionType, acID)
2025-10-30 17:15:14 +08:00
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "区域主控删除成功", nil, actionType, "区域主控删除成功", acID)
2025-09-30 15:25:07 +08:00
}
2025-09-30 22:07:55 +08:00
// --- Controller Methods: Device Templates ---
// CreateDeviceTemplate godoc
// @Summary 创建新设备模板
// @Description 根据提供的信息创建一个新设备模板
// @Tags 设备模板管理
2025-10-13 14:15:38 +08:00
// @Security BearerAuth
2025-09-30 22:07:55 +08:00
// @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]
2025-10-30 17:15:14 +08:00
func (c *Controller) CreateDeviceTemplate(ctx echo.Context) error {
2025-09-30 22:07:55 +08:00
const actionType = "创建设备模板"
2025-10-03 23:02:43 +08:00
var req dto.CreateDeviceTemplateRequest
2025-10-30 17:15:14 +08:00
if err := ctx.Bind(&req); err != nil {
2025-09-30 22:07:55 +08:00
c.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-30 22:07:55 +08:00
}
2025-10-31 15:38:10 +08:00
resp, err := c.deviceService.CreateDeviceTemplate(&req)
2025-09-30 22:07:55 +08:00
if err != nil {
2025-10-31 15:38:10 +08:00
c.logger.Errorf("%s: 服务层创建失败: %v", actionType, err)
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "创建设备模板失败: "+err.Error(), actionType, "服务层创建失败", req)
2025-09-30 22:07:55 +08:00
}
2025-10-31 15:38:10 +08:00
c.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-30 22:07:55 +08:00
}
// GetDeviceTemplate godoc
// @Summary 获取设备模板信息
// @Description 根据设备模板ID获取单个设备模板的详细信息
// @Tags 设备模板管理
2025-10-13 14:15:38 +08:00
// @Security BearerAuth
2025-09-30 22:07:55 +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.DeviceTemplateResponse}
2025-09-30 22:07:55 +08:00
// @Router /api/v1/device-templates/{id} [get]
2025-10-30 17:15:14 +08:00
func (c *Controller) GetDeviceTemplate(ctx echo.Context) error {
2025-09-30 22:07:55 +08:00
const actionType = "获取设备模板"
dtID := ctx.Param("id")
2025-10-31 15:38:10 +08:00
resp, err := c.deviceService.GetDeviceTemplate(dtID)
2025-09-30 22:07:55 +08:00
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
c.logger.Warnf("%s: 设备模板不存在, ID: %s", actionType, dtID)
2025-10-30 17:15:14 +08:00
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "设备模板未找到", actionType, "设备模板不存在", dtID)
2025-09-30 22:07:55 +08:00
}
2025-10-31 15:38:10 +08:00
c.logger.Errorf("%s: 服务层获取失败: %v, ID: %s", actionType, err, dtID)
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取设备模板信息失败: "+err.Error(), actionType, "服务层获取失败", dtID)
2025-09-30 22:07:55 +08:00
}
2025-10-31 15:38:10 +08:00
c.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-30 22:07:55 +08:00
}
// ListDeviceTemplates godoc
// @Summary 获取设备模板列表
// @Description 获取系统中所有设备模板的列表
// @Tags 设备模板管理
2025-10-13 14:15:38 +08:00
// @Security BearerAuth
2025-09-30 22:07:55 +08:00
// @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]
2025-10-30 17:15:14 +08:00
func (c *Controller) ListDeviceTemplates(ctx echo.Context) error {
2025-09-30 22:07:55 +08:00
const actionType = "获取设备模板列表"
2025-10-31 15:38:10 +08:00
resp, err := c.deviceService.ListDeviceTemplates()
2025-09-30 22:07:55 +08:00
if err != nil {
2025-10-31 15:38:10 +08:00
c.logger.Errorf("%s: 服务层获取列表失败: %v", actionType, err)
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取设备模板列表失败: "+err.Error(), actionType, "服务层获取列表失败", nil)
2025-09-30 22:07:55 +08:00
}
2025-10-31 15:38:10 +08:00
c.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-30 22:07:55 +08:00
}
// UpdateDeviceTemplate godoc
// @Summary 更新设备模板信息
// @Description 根据设备模板ID更新一个已存在的设备模板信息
// @Tags 设备模板管理
2025-10-13 14:15:38 +08:00
// @Security BearerAuth
2025-09-30 22:07:55 +08:00
// @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]
2025-10-30 17:15:14 +08:00
func (c *Controller) UpdateDeviceTemplate(ctx echo.Context) error {
2025-09-30 22:07:55 +08:00
const actionType = "更新设备模板"
dtID := ctx.Param("id")
2025-10-03 23:02:43 +08:00
var req dto.UpdateDeviceTemplateRequest
2025-10-30 17:15:14 +08:00
if err := ctx.Bind(&req); err != nil {
2025-09-30 22:07:55 +08:00
c.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-30 22:07:55 +08:00
}
2025-10-31 15:38:10 +08:00
resp, err := c.deviceService.UpdateDeviceTemplate(dtID, &req)
2025-09-30 22:07:55 +08:00
if err != nil {
2025-10-31 15:38:10 +08:00
if errors.Is(err, gorm.ErrRecordNotFound) {
c.logger.Warnf("%s: 设备模板不存在, ID: %s", actionType, dtID)
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "设备模板未找到", actionType, "设备模板不存在", dtID)
}
c.logger.Errorf("%s: 服务层更新失败: %v, ID: %s", actionType, err, dtID)
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新设备模板失败: "+err.Error(), actionType, "服务层更新失败", dtID)
2025-09-30 22:07:55 +08:00
}
2025-10-31 15:38:10 +08:00
c.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-30 22:07:55 +08:00
}
// DeleteDeviceTemplate godoc
// @Summary 删除设备模板
// @Description 根据设备模板ID删除一个设备模板软删除
// @Tags 设备模板管理
2025-10-13 14:15:38 +08:00
// @Security BearerAuth
2025-09-30 22:07:55 +08:00
// @Produce json
// @Param id path string true "设备模板ID"
// @Success 200 {object} controller.Response
// @Router /api/v1/device-templates/{id} [delete]
2025-10-30 17:15:14 +08:00
func (c *Controller) DeleteDeviceTemplate(ctx echo.Context) error {
2025-09-30 22:07:55 +08:00
const actionType = "删除设备模板"
dtID := ctx.Param("id")
2025-10-31 15:38:10 +08:00
if err := c.deviceService.DeleteDeviceTemplate(dtID); err != nil {
2025-09-30 22:07:55 +08:00
if errors.Is(err, gorm.ErrRecordNotFound) {
c.logger.Warnf("%s: 设备模板不存在, ID: %s", actionType, dtID)
2025-10-30 17:15:14 +08:00
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "设备模板未找到", actionType, "设备模板不存在", dtID)
2025-09-30 22:07:55 +08:00
}
2025-10-31 15:38:10 +08:00
c.logger.Errorf("%s: 服务层删除失败: %v, ID: %s", actionType, err, dtID)
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "删除设备模板失败: "+err.Error(), actionType, "服务层删除失败", dtID)
2025-09-30 22:07:55 +08:00
}
2025-10-31 15:38:10 +08:00
c.logger.Infof("%s: 设备模板删除成功, ID: %s", actionType, dtID)
2025-10-30 17:15:14 +08:00
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "设备模板删除成功", nil, actionType, "设备模板删除成功", dtID)
2025-09-30 22:07:55 +08:00
}