重构设备控制器
This commit is contained in:
@@ -14,25 +14,23 @@ import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// Controller 设备控制器,封装了所有与设备和区域主控相关的业务逻辑
|
||||
type Controller struct {
|
||||
// DeviceController 设备控制器
|
||||
type DeviceController struct {
|
||||
ctx context.Context
|
||||
deviceService service.DeviceService
|
||||
}
|
||||
|
||||
// NewController 创建一个新的设备控制器实例
|
||||
func NewController(
|
||||
// NewDeviceController 创建一个新的设备控制器实例
|
||||
func NewDeviceController(
|
||||
ctx context.Context,
|
||||
deviceService service.DeviceService,
|
||||
) *Controller {
|
||||
return &Controller{
|
||||
) *DeviceController {
|
||||
return &DeviceController{
|
||||
ctx: ctx,
|
||||
deviceService: deviceService,
|
||||
}
|
||||
}
|
||||
|
||||
// --- Controller Methods: Devices ---
|
||||
|
||||
// CreateDevice godoc
|
||||
// @Summary 创建新设备
|
||||
// @Description 根据提供的信息创建一个新设备
|
||||
@@ -43,7 +41,7 @@ func NewController(
|
||||
// @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 {
|
||||
func (c *DeviceController) CreateDevice(ctx echo.Context) error {
|
||||
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "CreateDevice")
|
||||
|
||||
const actionType = "创建设备"
|
||||
@@ -72,7 +70,7 @@ func (c *Controller) CreateDevice(ctx echo.Context) error {
|
||||
// @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 {
|
||||
func (c *DeviceController) GetDevice(ctx echo.Context) error {
|
||||
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "GetDevice")
|
||||
|
||||
const actionType = "获取设备"
|
||||
@@ -106,7 +104,7 @@ func (c *Controller) GetDevice(ctx echo.Context) error {
|
||||
// @Produce json
|
||||
// @Success 200 {object} controller.Response{data=[]dto.DeviceResponse}
|
||||
// @Router /api/v1/devices [get]
|
||||
func (c *Controller) ListDevices(ctx echo.Context) error {
|
||||
func (c *DeviceController) ListDevices(ctx echo.Context) error {
|
||||
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "ListDevices")
|
||||
|
||||
const actionType = "获取设备列表"
|
||||
@@ -131,7 +129,7 @@ func (c *Controller) ListDevices(ctx echo.Context) error {
|
||||
// @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 {
|
||||
func (c *DeviceController) UpdateDevice(ctx echo.Context) error {
|
||||
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "UpdateDevice")
|
||||
|
||||
const actionType = "更新设备"
|
||||
@@ -172,7 +170,7 @@ func (c *Controller) UpdateDevice(ctx echo.Context) error {
|
||||
// @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 {
|
||||
func (c *DeviceController) DeleteDevice(ctx echo.Context) error {
|
||||
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "DeleteDevice")
|
||||
|
||||
const actionType = "删除设备"
|
||||
@@ -214,7 +212,7 @@ func (c *Controller) DeleteDevice(ctx echo.Context) error {
|
||||
// @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 {
|
||||
func (c *DeviceController) ManualControl(ctx echo.Context) error {
|
||||
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "ManualControlDevice")
|
||||
|
||||
const actionType = "手动控制设备"
|
||||
@@ -243,344 +241,3 @@ func (c *Controller) ManualControl(ctx echo.Context) error {
|
||||
|
||||
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 {
|
||||
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "CreateAreaController")
|
||||
|
||||
const actionType = "创建区域主控"
|
||||
var req dto.CreateAreaControllerRequest
|
||||
if err := ctx.Bind(&req); err != nil {
|
||||
logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
|
||||
}
|
||||
|
||||
resp, err := c.deviceService.CreateAreaController(reqCtx, &req)
|
||||
if err != nil {
|
||||
logger.Errorf("%s: 服务层创建失败: %v", actionType, err)
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "创建区域主控失败: "+err.Error(), actionType, "服务层创建失败", req)
|
||||
}
|
||||
|
||||
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 {
|
||||
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "GetAreaController")
|
||||
|
||||
const actionType = "获取区域主控"
|
||||
acID := ctx.Param("id")
|
||||
|
||||
id, err := strconv.ParseUint(acID, 10, 64)
|
||||
if err != nil {
|
||||
logger.Errorf("%s: 无效的ID: %s", actionType, acID)
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID: "+acID, actionType, "无效的ID", acID)
|
||||
}
|
||||
|
||||
resp, err := c.deviceService.GetAreaController(reqCtx, uint32(id))
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
logger.Warnf("%s: 区域主控不存在, ID: %s", actionType, acID)
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "区域主控未找到", actionType, "区域主控不存在", acID)
|
||||
}
|
||||
logger.Errorf("%s: 服务层获取失败: %v, ID: %s", actionType, err, acID)
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取区域主控信息失败: "+err.Error(), actionType, "服务层获取失败", acID)
|
||||
}
|
||||
|
||||
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 {
|
||||
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "ListAreaControllers")
|
||||
|
||||
const actionType = "获取区域主控列表"
|
||||
resp, err := c.deviceService.ListAreaControllers(reqCtx)
|
||||
if err != nil {
|
||||
logger.Errorf("%s: 服务层获取列表失败: %v", actionType, err)
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取区域主控列表失败: "+err.Error(), actionType, "服务层获取列表失败", nil)
|
||||
}
|
||||
|
||||
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 {
|
||||
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "UpdateAreaController")
|
||||
|
||||
const actionType = "更新区域主控"
|
||||
acID := ctx.Param("id")
|
||||
|
||||
var req dto.UpdateAreaControllerRequest
|
||||
if err := ctx.Bind(&req); err != nil {
|
||||
logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
|
||||
}
|
||||
id, err := strconv.ParseUint(acID, 10, 64)
|
||||
if err != nil {
|
||||
logger.Errorf("%s: 无效的ID: %s", actionType, acID)
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID: "+acID, actionType, "无效的ID", acID)
|
||||
}
|
||||
|
||||
resp, err := c.deviceService.UpdateAreaController(reqCtx, uint32(id), &req)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
logger.Warnf("%s: 区域主控不存在, ID: %s", actionType, acID)
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "区域主控未找到", actionType, "区域主控不存在", acID)
|
||||
}
|
||||
logger.Errorf("%s: 服务层更新失败: %v, ID: %s", actionType, err, acID)
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新区域主控失败: "+err.Error(), actionType, "服务层更新失败", acID)
|
||||
}
|
||||
|
||||
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 {
|
||||
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "DeleteAreaController")
|
||||
|
||||
const actionType = "删除区域主控"
|
||||
acID := ctx.Param("id")
|
||||
|
||||
id, err := strconv.ParseUint(acID, 10, 64)
|
||||
if err != nil {
|
||||
logger.Errorf("%s: 无效的ID: %s", actionType, acID)
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID: "+acID, actionType, "无效的ID", acID)
|
||||
}
|
||||
|
||||
if err := c.deviceService.DeleteAreaController(reqCtx, uint32(id)); err != nil {
|
||||
switch {
|
||||
case errors.Is(err, gorm.ErrRecordNotFound):
|
||||
logger.Warnf("%s: 区域主控不存在, ID: %s", actionType, acID)
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "区域主控未找到", actionType, "区域主控不存在", acID)
|
||||
case errors.Is(err, service.ErrAreaControllerInUse):
|
||||
logger.Warnf("%s: 尝试删除正在被使用的主控, ID: %s", actionType, acID)
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeConflict, err.Error(), actionType, "主控正在被使用", acID)
|
||||
default:
|
||||
logger.Errorf("%s: 服务层删除失败: %v, ID: %s", actionType, err, acID)
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "删除区域主控失败: "+err.Error(), actionType, "服务层删除失败", acID)
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "CreateDeviceTemplate")
|
||||
|
||||
const actionType = "创建设备模板"
|
||||
var req dto.CreateDeviceTemplateRequest
|
||||
if err := ctx.Bind(&req); err != nil {
|
||||
logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
|
||||
}
|
||||
|
||||
resp, err := c.deviceService.CreateDeviceTemplate(reqCtx, &req)
|
||||
if err != nil {
|
||||
logger.Errorf("%s: 服务层创建失败: %v", actionType, err)
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "创建设备模板失败: "+err.Error(), actionType, "服务层创建失败", req)
|
||||
}
|
||||
|
||||
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 {
|
||||
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "GetDeviceTemplate")
|
||||
|
||||
const actionType = "获取设备模板"
|
||||
dtID := ctx.Param("id")
|
||||
|
||||
id, err := strconv.ParseUint(dtID, 10, 64)
|
||||
if err != nil {
|
||||
logger.Errorf("%s: 无效的ID: %s", actionType, dtID)
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID: "+dtID, actionType, "无效的ID", dtID)
|
||||
}
|
||||
|
||||
resp, err := c.deviceService.GetDeviceTemplate(reqCtx, uint32(id))
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
logger.Warnf("%s: 设备模板不存在, ID: %s", actionType, dtID)
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "设备模板未找到", actionType, "设备模板不存在", dtID)
|
||||
}
|
||||
logger.Errorf("%s: 服务层获取失败: %v, ID: %s", actionType, err, dtID)
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取设备模板信息失败: "+err.Error(), actionType, "服务层获取失败", dtID)
|
||||
}
|
||||
|
||||
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 {
|
||||
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "ListDeviceTemplates")
|
||||
|
||||
const actionType = "获取设备模板列表"
|
||||
resp, err := c.deviceService.ListDeviceTemplates(reqCtx)
|
||||
if err != nil {
|
||||
logger.Errorf("%s: 服务层获取列表失败: %v", actionType, err)
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取设备模板列表失败: "+err.Error(), actionType, "服务层获取列表失败", nil)
|
||||
}
|
||||
|
||||
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 {
|
||||
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "UpdateDeviceTemplate")
|
||||
|
||||
const actionType = "更新设备模板"
|
||||
dtID := ctx.Param("id")
|
||||
|
||||
var req dto.UpdateDeviceTemplateRequest
|
||||
if err := ctx.Bind(&req); err != nil {
|
||||
logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
|
||||
}
|
||||
|
||||
id, err := strconv.ParseUint(dtID, 10, 64)
|
||||
if err != nil {
|
||||
logger.Errorf("%s: 无效的ID: %s", actionType, dtID)
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID: "+dtID, actionType, "无效的ID", dtID)
|
||||
}
|
||||
|
||||
resp, err := c.deviceService.UpdateDeviceTemplate(reqCtx, uint32(id), &req)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
logger.Warnf("%s: 设备模板不存在, ID: %s", actionType, dtID)
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "设备模板未找到", actionType, "设备模板不存在", dtID)
|
||||
}
|
||||
logger.Errorf("%s: 服务层更新失败: %v, ID: %s", actionType, err, dtID)
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新设备模板失败: "+err.Error(), actionType, "服务层更新失败", dtID)
|
||||
}
|
||||
|
||||
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 {
|
||||
reqCtx, logger := logs.Trace(ctx.Request().Context(), c.ctx, "DeleteDeviceTemplate")
|
||||
|
||||
const actionType = "删除设备模板"
|
||||
dtID := ctx.Param("id")
|
||||
|
||||
id, err := strconv.ParseUint(dtID, 10, 64)
|
||||
if err != nil {
|
||||
logger.Errorf("%s: 无效的ID: %s", actionType, dtID)
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的ID: "+dtID, actionType, "无效的ID", dtID)
|
||||
}
|
||||
|
||||
if err := c.deviceService.DeleteDeviceTemplate(reqCtx, uint32(id)); err != nil {
|
||||
switch {
|
||||
case errors.Is(err, gorm.ErrRecordNotFound):
|
||||
logger.Warnf("%s: 设备模板不存在, ID: %s", actionType, dtID)
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "设备模板未找到", actionType, "设备模板不存在", dtID)
|
||||
case errors.Is(err, service.ErrDeviceTemplateInUse):
|
||||
logger.Warnf("%s: 尝试删除正在被使用的模板, ID: %s", actionType, dtID)
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeConflict, err.Error(), actionType, "模板正在被使用", dtID)
|
||||
default:
|
||||
logger.Errorf("%s: 服务层删除失败: %v, ID: %s", actionType, err, dtID)
|
||||
return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "删除设备模板失败: "+err.Error(), actionType, "服务层删除失败", dtID)
|
||||
}
|
||||
}
|
||||
|
||||
logger.Infof("%s: 设备模板删除成功, ID: %s", actionType, dtID)
|
||||
return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "设备模板删除成功", nil, actionType, "设备模板删除成功", dtID)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user