删除设备时检查

This commit is contained in:
2025-11-03 16:46:23 +08:00
parent 8669dcd9b0
commit f569876225
5 changed files with 86 additions and 7 deletions

View File

@@ -154,12 +154,18 @@ func (c *Controller) DeleteDevice(ctx echo.Context) error {
deviceID := ctx.Param("id")
if err := c.deviceService.DeleteDevice(deviceID); err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
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.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)