483 lines
		
	
	
		
			23 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			483 lines
		
	
	
		
			23 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
package device
 | 
						||
 | 
						||
import (
 | 
						||
	"errors"
 | 
						||
 | 
						||
	"git.huangwc.com/pig/pig-farm-controller/internal/app/controller"
 | 
						||
	"git.huangwc.com/pig/pig-farm-controller/internal/app/dto"
 | 
						||
	"git.huangwc.com/pig/pig-farm-controller/internal/app/service"
 | 
						||
	"git.huangwc.com/pig/pig-farm-controller/internal/infra/logs"
 | 
						||
	"github.com/labstack/echo/v4"
 | 
						||
	"gorm.io/gorm"
 | 
						||
)
 | 
						||
 | 
						||
// Controller 设备控制器,封装了所有与设备和区域主控相关的业务逻辑
 | 
						||
type Controller struct {
 | 
						||
	deviceService service.DeviceService
 | 
						||
	logger        *logs.Logger
 | 
						||
}
 | 
						||
 | 
						||
// NewController 创建一个新的设备控制器实例
 | 
						||
func NewController(
 | 
						||
	deviceService service.DeviceService,
 | 
						||
	logger *logs.Logger,
 | 
						||
) *Controller {
 | 
						||
	return &Controller{
 | 
						||
		deviceService: deviceService,
 | 
						||
		logger:        logger,
 | 
						||
	}
 | 
						||
}
 | 
						||
 | 
						||
// --- Controller Methods: Devices ---
 | 
						||
 | 
						||
// CreateDevice godoc
 | 
						||
// @Summary      创建新设备
 | 
						||
// @Description  根据提供的信息创建一个新设备
 | 
						||
// @Tags         设备管理
 | 
						||
// @Security     BearerAuth
 | 
						||
// @Accept       json
 | 
						||
// @Produce      json
 | 
						||
// @Param        device body dto.CreateDeviceRequest true "设备信息"
 | 
						||
// @Success      200 {object} controller.Response{data=dto.DeviceResponse}
 | 
						||
// @Router       /api/v1/devices [post]
 | 
						||
func (c *Controller) CreateDevice(ctx echo.Context) error {
 | 
						||
	const actionType = "创建设备"
 | 
						||
	var req dto.CreateDeviceRequest
 | 
						||
	if err := ctx.Bind(&req); err != nil {
 | 
						||
		c.logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
 | 
						||
		return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
 | 
						||
	}
 | 
						||
 | 
						||
	resp, err := c.deviceService.CreateDevice(&req)
 | 
						||
	if err != nil {
 | 
						||
		c.logger.Errorf("%s: 服务层创建失败: %v", actionType, err)
 | 
						||
		return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "创建设备失败: "+err.Error(), actionType, "服务层创建失败", req)
 | 
						||
	}
 | 
						||
 | 
						||
	c.logger.Infof("%s: 设备创建成功, ID: %d", actionType, resp.ID)
 | 
						||
	return controller.SendSuccessWithAudit(ctx, controller.CodeCreated, "设备创建成功", resp, actionType, "设备创建成功", resp)
 | 
						||
}
 | 
						||
 | 
						||
// GetDevice godoc
 | 
						||
// @Summary      获取设备信息
 | 
						||
// @Description  根据设备ID获取单个设备的详细信息
 | 
						||
// @Tags         设备管理
 | 
						||
// @Security     BearerAuth
 | 
						||
// @Produce      json
 | 
						||
// @Param        id path string true "设备ID"
 | 
						||
// @Success      200 {object} controller.Response{data=dto.DeviceResponse}
 | 
						||
// @Router       /api/v1/devices/{id} [get]
 | 
						||
func (c *Controller) GetDevice(ctx echo.Context) error {
 | 
						||
	const actionType = "获取设备"
 | 
						||
	deviceID := ctx.Param("id")
 | 
						||
 | 
						||
	resp, err := c.deviceService.GetDevice(deviceID)
 | 
						||
	if err != nil {
 | 
						||
		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)
 | 
						||
	}
 | 
						||
 | 
						||
	c.logger.Infof("%s: 获取设备信息成功, ID: %d", actionType, resp.ID)
 | 
						||
	return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取设备信息成功", resp, actionType, "获取设备信息成功", resp)
 | 
						||
}
 | 
						||
 | 
						||
// ListDevices godoc
 | 
						||
// @Summary      获取设备列表
 | 
						||
// @Description  获取系统中所有设备的列表
 | 
						||
// @Tags         设备管理
 | 
						||
// @Security     BearerAuth
 | 
						||
// @Produce      json
 | 
						||
// @Success      200 {object} controller.Response{data=[]dto.DeviceResponse}
 | 
						||
// @Router       /api/v1/devices [get]
 | 
						||
func (c *Controller) ListDevices(ctx echo.Context) error {
 | 
						||
	const actionType = "获取设备列表"
 | 
						||
	resp, err := c.deviceService.ListDevices()
 | 
						||
	if err != nil {
 | 
						||
		c.logger.Errorf("%s: 服务层获取列表失败: %v", actionType, err)
 | 
						||
		return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取设备列表失败: "+err.Error(), actionType, "服务层获取列表失败", nil)
 | 
						||
	}
 | 
						||
 | 
						||
	c.logger.Infof("%s: 获取设备列表成功, 数量: %d", actionType, len(resp))
 | 
						||
	return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取设备列表成功", resp, actionType, "获取设备列表成功", resp)
 | 
						||
}
 | 
						||
 | 
						||
// UpdateDevice godoc
 | 
						||
// @Summary      更新设备信息
 | 
						||
// @Description  根据设备ID更新一个已存在的设备信息
 | 
						||
// @Tags         设备管理
 | 
						||
// @Security     BearerAuth
 | 
						||
// @Accept       json
 | 
						||
// @Produce      json
 | 
						||
// @Param        id path string true "设备ID"
 | 
						||
// @Param        device body dto.UpdateDeviceRequest true "要更新的设备信息"
 | 
						||
// @Success      200 {object} controller.Response{data=dto.DeviceResponse}
 | 
						||
// @Router       /api/v1/devices/{id} [put]
 | 
						||
func (c *Controller) UpdateDevice(ctx echo.Context) error {
 | 
						||
	const actionType = "更新设备"
 | 
						||
	deviceID := ctx.Param("id")
 | 
						||
 | 
						||
	var req dto.UpdateDeviceRequest
 | 
						||
	if err := ctx.Bind(&req); err != nil {
 | 
						||
		c.logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
 | 
						||
		return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
 | 
						||
	}
 | 
						||
 | 
						||
	resp, err := c.deviceService.UpdateDevice(deviceID, &req)
 | 
						||
	if err != nil {
 | 
						||
		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)
 | 
						||
	}
 | 
						||
 | 
						||
	c.logger.Infof("%s: 设备更新成功, ID: %d", actionType, resp.ID)
 | 
						||
	return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "设备更新成功", resp, actionType, "设备更新成功", resp)
 | 
						||
}
 | 
						||
 | 
						||
// DeleteDevice godoc
 | 
						||
// @Summary      删除设备
 | 
						||
// @Description  根据设备ID删除一个设备(软删除)
 | 
						||
// @Tags         设备管理
 | 
						||
// @Security     BearerAuth
 | 
						||
// @Produce      json
 | 
						||
// @Param        id path string true "设备ID"
 | 
						||
// @Success      200 {object} controller.Response
 | 
						||
// @Router       /api/v1/devices/{id} [delete]
 | 
						||
func (c *Controller) DeleteDevice(ctx echo.Context) error {
 | 
						||
	const actionType = "删除设备"
 | 
						||
	deviceID := ctx.Param("id")
 | 
						||
 | 
						||
	if err := c.deviceService.DeleteDevice(deviceID); err != nil {
 | 
						||
		switch {
 | 
						||
		case errors.Is(err, gorm.ErrRecordNotFound):
 | 
						||
			c.logger.Warnf("%s: 设备不存在, ID: %s", actionType, deviceID)
 | 
						||
			return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "设备未找到", actionType, "设备不存在", deviceID)
 | 
						||
		case errors.Is(err, service.ErrDeviceInUse):
 | 
						||
			c.logger.Warnf("%s: 尝试删除正在被使用的设备, ID: %s", actionType, deviceID)
 | 
						||
			// 返回 409 Conflict 状态码,表示请求与服务器当前状态冲突
 | 
						||
			return controller.SendErrorWithAudit(ctx, controller.CodeConflict, err.Error(), actionType, "设备正在被使用", deviceID)
 | 
						||
		default:
 | 
						||
			c.logger.Errorf("%s: 服务层删除失败: %v, ID: %s", actionType, err, deviceID)
 | 
						||
			return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "删除设备失败: "+err.Error(), actionType, "服务层删除失败", deviceID)
 | 
						||
		}
 | 
						||
	}
 | 
						||
 | 
						||
	c.logger.Infof("%s: 设备删除成功, ID: %s", actionType, deviceID)
 | 
						||
	return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "设备删除成功", nil, actionType, "设备删除成功", deviceID)
 | 
						||
}
 | 
						||
 | 
						||
// ManualControl godoc
 | 
						||
// @Summary      手动控制设备
 | 
						||
// @Description  根据设备ID和指定的动作(开启或关闭)来手动控制设备
 | 
						||
// @Tags         设备管理
 | 
						||
// @Security     BearerAuth
 | 
						||
// @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]
 | 
						||
func (c *Controller) ManualControl(ctx echo.Context) error {
 | 
						||
	const actionType = "手动控制设备"
 | 
						||
	deviceID := ctx.Param("id")
 | 
						||
 | 
						||
	var req dto.ManualControlDeviceRequest
 | 
						||
	if err := ctx.Bind(&req); err != nil {
 | 
						||
		c.logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
 | 
						||
		return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
 | 
						||
	}
 | 
						||
 | 
						||
	if err := c.deviceService.ManualControl(deviceID, &req); err != nil {
 | 
						||
		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)
 | 
						||
	}
 | 
						||
 | 
						||
	return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "指令已发送", nil, actionType, "指令发送成功", nil)
 | 
						||
}
 | 
						||
 | 
						||
// --- Controller Methods: Area Controllers ---
 | 
						||
 | 
						||
// CreateAreaController godoc
 | 
						||
// @Summary      创建新区域主控
 | 
						||
// @Description  根据提供的信息创建一个新区域主控
 | 
						||
// @Tags         区域主控管理
 | 
						||
// @Security     BearerAuth
 | 
						||
// @Accept       json
 | 
						||
// @Produce      json
 | 
						||
// @Param        areaController body dto.CreateAreaControllerRequest true "区域主控信息"
 | 
						||
// @Success      200 {object} controller.Response{data=dto.AreaControllerResponse}
 | 
						||
// @Router       /api/v1/area-controllers [post]
 | 
						||
func (c *Controller) CreateAreaController(ctx echo.Context) error {
 | 
						||
	const actionType = "创建区域主控"
 | 
						||
	var req dto.CreateAreaControllerRequest
 | 
						||
	if err := ctx.Bind(&req); err != nil {
 | 
						||
		c.logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
 | 
						||
		return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
 | 
						||
	}
 | 
						||
 | 
						||
	resp, err := c.deviceService.CreateAreaController(&req)
 | 
						||
	if err != nil {
 | 
						||
		c.logger.Errorf("%s: 服务层创建失败: %v", actionType, err)
 | 
						||
		return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "创建区域主控失败: "+err.Error(), actionType, "服务层创建失败", req)
 | 
						||
	}
 | 
						||
 | 
						||
	c.logger.Infof("%s: 区域主控创建成功, ID: %d", actionType, resp.ID)
 | 
						||
	return controller.SendSuccessWithAudit(ctx, controller.CodeCreated, "区域主控创建成功", resp, actionType, "区域主控创建成功", resp)
 | 
						||
}
 | 
						||
 | 
						||
// GetAreaController godoc
 | 
						||
// @Summary      获取区域主控信息
 | 
						||
// @Description  根据ID获取单个区域主控的详细信息
 | 
						||
// @Tags         区域主控管理
 | 
						||
// @Security     BearerAuth
 | 
						||
// @Produce      json
 | 
						||
// @Param        id path string true "区域主控ID"
 | 
						||
// @Success      200 {object} controller.Response{data=dto.AreaControllerResponse}
 | 
						||
// @Router       /api/v1/area-controllers/{id} [get]
 | 
						||
func (c *Controller) GetAreaController(ctx echo.Context) error {
 | 
						||
	const actionType = "获取区域主控"
 | 
						||
	acID := ctx.Param("id")
 | 
						||
 | 
						||
	resp, err := c.deviceService.GetAreaController(acID)
 | 
						||
	if err != nil {
 | 
						||
		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)
 | 
						||
	}
 | 
						||
 | 
						||
	c.logger.Infof("%s: 获取区域主控信息成功, ID: %d", actionType, resp.ID)
 | 
						||
	return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取区域主控信息成功", resp, actionType, "获取区域主控信息成功", resp)
 | 
						||
}
 | 
						||
 | 
						||
// ListAreaControllers godoc
 | 
						||
// @Summary      获取所有区域主控列表
 | 
						||
// @Description  获取系统中所有区域主控的列表
 | 
						||
// @Tags         区域主控管理
 | 
						||
// @Security     BearerAuth
 | 
						||
// @Produce      json
 | 
						||
// @Success      200 {object} controller.Response{data=[]dto.AreaControllerResponse}
 | 
						||
// @Router       /api/v1/area-controllers [get]
 | 
						||
func (c *Controller) ListAreaControllers(ctx echo.Context) error {
 | 
						||
	const actionType = "获取区域主控列表"
 | 
						||
	resp, err := c.deviceService.ListAreaControllers()
 | 
						||
	if err != nil {
 | 
						||
		c.logger.Errorf("%s: 服务层获取列表失败: %v", actionType, err)
 | 
						||
		return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取区域主控列表失败: "+err.Error(), actionType, "服务层获取列表失败", nil)
 | 
						||
	}
 | 
						||
 | 
						||
	c.logger.Infof("%s: 获取区域主控列表成功, 数量: %d", actionType, len(resp))
 | 
						||
	return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取区域主控列表成功", resp, actionType, "获取区域主控列表成功", resp)
 | 
						||
}
 | 
						||
 | 
						||
// UpdateAreaController godoc
 | 
						||
// @Summary      更新区域主控信息
 | 
						||
// @Description  根据ID更新一个已存在的区域主控信息
 | 
						||
// @Tags         区域主控管理
 | 
						||
// @Security     BearerAuth
 | 
						||
// @Accept       json
 | 
						||
// @Produce      json
 | 
						||
// @Param        id path string true "区域主控ID"
 | 
						||
// @Param        areaController body dto.UpdateAreaControllerRequest true "要更新的区域主控信息"
 | 
						||
// @Success      200 {object} controller.Response{data=dto.AreaControllerResponse}
 | 
						||
// @Router       /api/v1/area-controllers/{id} [put]
 | 
						||
func (c *Controller) UpdateAreaController(ctx echo.Context) error {
 | 
						||
	const actionType = "更新区域主控"
 | 
						||
	acID := ctx.Param("id")
 | 
						||
 | 
						||
	var req dto.UpdateAreaControllerRequest
 | 
						||
	if err := ctx.Bind(&req); err != nil {
 | 
						||
		c.logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
 | 
						||
		return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
 | 
						||
	}
 | 
						||
 | 
						||
	resp, err := c.deviceService.UpdateAreaController(acID, &req)
 | 
						||
	if err != nil {
 | 
						||
		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)
 | 
						||
	}
 | 
						||
 | 
						||
	c.logger.Infof("%s: 区域主控更新成功, ID: %d", actionType, resp.ID)
 | 
						||
	return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "区域主控更新成功", resp, actionType, "区域主控更新成功", resp)
 | 
						||
}
 | 
						||
 | 
						||
// DeleteAreaController godoc
 | 
						||
// @Summary      删除区域主控
 | 
						||
// @Description  根据ID删除一个区域主控(软删除)
 | 
						||
// @Tags         区域主控管理
 | 
						||
// @Security     BearerAuth
 | 
						||
// @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 echo.Context) error {
 | 
						||
	const actionType = "删除区域主控"
 | 
						||
	acID := ctx.Param("id")
 | 
						||
 | 
						||
	if err := c.deviceService.DeleteAreaController(acID); err != nil {
 | 
						||
		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)
 | 
						||
	}
 | 
						||
 | 
						||
	c.logger.Infof("%s: 区域主控删除成功, ID: %s", actionType, acID)
 | 
						||
	return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "区域主控删除成功", nil, actionType, "区域主控删除成功", acID)
 | 
						||
}
 | 
						||
 | 
						||
// --- Controller Methods: Device Templates ---
 | 
						||
 | 
						||
// CreateDeviceTemplate godoc
 | 
						||
// @Summary      创建新设备模板
 | 
						||
// @Description  根据提供的信息创建一个新设备模板
 | 
						||
// @Tags         设备模板管理
 | 
						||
// @Security     BearerAuth
 | 
						||
// @Accept       json
 | 
						||
// @Produce      json
 | 
						||
// @Param        deviceTemplate body dto.CreateDeviceTemplateRequest true "设备模板信息"
 | 
						||
// @Success      200 {object} controller.Response{data=dto.DeviceTemplateResponse}
 | 
						||
// @Router       /api/v1/device-templates [post]
 | 
						||
func (c *Controller) CreateDeviceTemplate(ctx echo.Context) error {
 | 
						||
	const actionType = "创建设备模板"
 | 
						||
	var req dto.CreateDeviceTemplateRequest
 | 
						||
	if err := ctx.Bind(&req); err != nil {
 | 
						||
		c.logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
 | 
						||
		return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
 | 
						||
	}
 | 
						||
 | 
						||
	resp, err := c.deviceService.CreateDeviceTemplate(&req)
 | 
						||
	if err != nil {
 | 
						||
		c.logger.Errorf("%s: 服务层创建失败: %v", actionType, err)
 | 
						||
		return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "创建设备模板失败: "+err.Error(), actionType, "服务层创建失败", req)
 | 
						||
	}
 | 
						||
 | 
						||
	c.logger.Infof("%s: 设备模板创建成功, ID: %d", actionType, resp.ID)
 | 
						||
	return controller.SendSuccessWithAudit(ctx, controller.CodeCreated, "设备模板创建成功", resp, actionType, "设备模板创建成功", resp)
 | 
						||
}
 | 
						||
 | 
						||
// GetDeviceTemplate godoc
 | 
						||
// @Summary      获取设备模板信息
 | 
						||
// @Description  根据设备模板ID获取单个设备模板的详细信息
 | 
						||
// @Tags         设备模板管理
 | 
						||
// @Security     BearerAuth
 | 
						||
// @Produce      json
 | 
						||
// @Param        id path string true "设备模板ID"
 | 
						||
// @Success      200 {object} controller.Response{data=dto.DeviceTemplateResponse}
 | 
						||
// @Router       /api/v1/device-templates/{id} [get]
 | 
						||
func (c *Controller) GetDeviceTemplate(ctx echo.Context) error {
 | 
						||
	const actionType = "获取设备模板"
 | 
						||
	dtID := ctx.Param("id")
 | 
						||
 | 
						||
	resp, err := c.deviceService.GetDeviceTemplate(dtID)
 | 
						||
	if err != nil {
 | 
						||
		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)
 | 
						||
	}
 | 
						||
 | 
						||
	c.logger.Infof("%s: 获取设备模板信息成功, ID: %d", actionType, resp.ID)
 | 
						||
	return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取设备模板信息成功", resp, actionType, "获取设备模板信息成功", resp)
 | 
						||
}
 | 
						||
 | 
						||
// ListDeviceTemplates godoc
 | 
						||
// @Summary      获取设备模板列表
 | 
						||
// @Description  获取系统中所有设备模板的列表
 | 
						||
// @Tags         设备模板管理
 | 
						||
// @Security     BearerAuth
 | 
						||
// @Produce      json
 | 
						||
// @Success      200 {object} controller.Response{data=[]dto.DeviceTemplateResponse}
 | 
						||
// @Router       /api/v1/device-templates [get]
 | 
						||
func (c *Controller) ListDeviceTemplates(ctx echo.Context) error {
 | 
						||
	const actionType = "获取设备模板列表"
 | 
						||
	resp, err := c.deviceService.ListDeviceTemplates()
 | 
						||
	if err != nil {
 | 
						||
		c.logger.Errorf("%s: 服务层获取列表失败: %v", actionType, err)
 | 
						||
		return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取设备模板列表失败: "+err.Error(), actionType, "服务层获取列表失败", nil)
 | 
						||
	}
 | 
						||
 | 
						||
	c.logger.Infof("%s: 获取设备模板列表成功, 数量: %d", actionType, len(resp))
 | 
						||
	return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取设备模板列表成功", resp, actionType, "获取设备模板列表成功", resp)
 | 
						||
}
 | 
						||
 | 
						||
// UpdateDeviceTemplate godoc
 | 
						||
// @Summary      更新设备模板信息
 | 
						||
// @Description  根据设备模板ID更新一个已存在的设备模板信息
 | 
						||
// @Tags         设备模板管理
 | 
						||
// @Security     BearerAuth
 | 
						||
// @Accept       json
 | 
						||
// @Produce      json
 | 
						||
// @Param        id path string true "设备模板ID"
 | 
						||
// @Param        deviceTemplate body dto.UpdateDeviceTemplateRequest true "要更新的设备模板信息"
 | 
						||
// @Success      200 {object} controller.Response{data=dto.DeviceTemplateResponse}
 | 
						||
// @Router       /api/v1/device-templates/{id} [put]
 | 
						||
func (c *Controller) UpdateDeviceTemplate(ctx echo.Context) error {
 | 
						||
	const actionType = "更新设备模板"
 | 
						||
	dtID := ctx.Param("id")
 | 
						||
 | 
						||
	var req dto.UpdateDeviceTemplateRequest
 | 
						||
	if err := ctx.Bind(&req); err != nil {
 | 
						||
		c.logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
 | 
						||
		return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
 | 
						||
	}
 | 
						||
 | 
						||
	resp, err := c.deviceService.UpdateDeviceTemplate(dtID, &req)
 | 
						||
	if err != nil {
 | 
						||
		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)
 | 
						||
	}
 | 
						||
 | 
						||
	c.logger.Infof("%s: 设备模板更新成功, ID: %d", actionType, resp.ID)
 | 
						||
	return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "设备模板更新成功", resp, actionType, "设备模板更新成功", resp)
 | 
						||
}
 | 
						||
 | 
						||
// DeleteDeviceTemplate godoc
 | 
						||
// @Summary      删除设备模板
 | 
						||
// @Description  根据设备模板ID删除一个设备模板(软删除)
 | 
						||
// @Tags         设备模板管理
 | 
						||
// @Security     BearerAuth
 | 
						||
// @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 echo.Context) error {
 | 
						||
	const actionType = "删除设备模板"
 | 
						||
	dtID := ctx.Param("id")
 | 
						||
 | 
						||
	if err := c.deviceService.DeleteDeviceTemplate(dtID); err != nil {
 | 
						||
		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)
 | 
						||
	}
 | 
						||
 | 
						||
	c.logger.Infof("%s: 设备模板删除成功, ID: %s", actionType, dtID)
 | 
						||
	return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "设备模板删除成功", nil, actionType, "设备模板删除成功", dtID)
 | 
						||
}
 |