删除设备时检查
This commit is contained in:
		@@ -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)
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user