调整审计输出

This commit is contained in:
2025-09-28 01:28:54 +08:00
parent 17344cdb89
commit 72d8b45241
2 changed files with 81 additions and 69 deletions

View File

@@ -120,17 +120,18 @@ func newListDeviceResponse(devices []*models.Device) ([]*DeviceResponse, error)
// @Success 200 {object} controller.Response{data=DeviceResponse}
// @Router /api/v1/devices [post]
func (c *Controller) CreateDevice(ctx *gin.Context) {
const actionType = "创建设备"
var req CreateDeviceRequest
if err := ctx.ShouldBindJSON(&req); err != nil {
c.logger.Errorf("创建设备: 参数绑定失败: %v", err)
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, err.Error(), "创建设备", "请求体绑定失败", req)
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
return
}
propertiesJSON, err := json.Marshal(req.Properties)
if err != nil {
c.logger.Errorf("创建设备: 序列化属性失败: %v", err)
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "属性字段格式错误", "创建设备", "属性序列化失败", req.Properties)
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "属性字段格式错误", actionType, "属性序列化失败", req.Properties)
return
}
@@ -146,24 +147,24 @@ func (c *Controller) CreateDevice(ctx *gin.Context) {
// 在创建设备前进行自检
if !device.SelfCheck() {
c.logger.Errorf("创建设备: 设备属性自检失败: %v", device)
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "设备属性不符合要求", "创建设备", "设备属性自检失败", device)
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "设备属性不符合要求", actionType, "设备属性自检失败", device)
return
}
if err := c.repo.Create(device); err != nil {
c.logger.Errorf("创建设备: 数据库操作失败: %v", err)
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "创建设备失败", "创建设备", "数据库创建失败", device)
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "创建设备失败: "+err.Error(), actionType, "数据库创建失败", device)
return
}
resp, err := newDeviceResponse(device)
if err != nil {
c.logger.Errorf("创建设备: 序列化响应失败: %v", err)
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "设备创建成功,但响应生成失败", "创建设备", "响应序列化失败", device)
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "设备创建成功,但响应生成失败", actionType, "响应序列化失败", device)
return
}
controller.SendSuccessWithAudit(ctx, controller.CodeCreated, "设备创建成功", resp, "创建设备", "设备创建成功", resp)
controller.SendSuccessWithAudit(ctx, controller.CodeCreated, "设备创建成功", resp, actionType, "设备创建成功", resp)
}
// GetDevice godoc
@@ -175,31 +176,32 @@ func (c *Controller) CreateDevice(ctx *gin.Context) {
// @Success 200 {object} controller.Response{data=DeviceResponse}
// @Router /api/v1/devices/{id} [get]
func (c *Controller) GetDevice(ctx *gin.Context) {
const actionType = "获取设备"
deviceID := ctx.Param("id")
device, err := c.repo.FindByIDString(deviceID)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "设备未找到", "获取设备", "设备不存在", deviceID)
controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "设备未找到", actionType, "设备不存在", deviceID)
return
}
if strings.Contains(err.Error(), "无效的设备ID格式") {
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, err.Error(), "获取设备", "设备ID格式错误", deviceID)
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, err.Error(), actionType, "设备ID格式错误", deviceID)
return
}
c.logger.Errorf("获取设备: 数据库操作失败: %v", err)
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取设备信息失败", "获取设备", "数据库查询失败", deviceID)
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取设备信息失败: "+err.Error(), actionType, "数据库查询失败", deviceID)
return
}
resp, err := newDeviceResponse(device)
if err != nil {
c.logger.Errorf("获取设备: 序列化响应失败: %v", err)
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取设备信息失败: 内部数据格式错误", "获取设备", "响应序列化失败", device)
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取设备信息失败: 内部数据格式错误", actionType, "响应序列化失败", device)
return
}
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取设备信息成功", resp, "获取设备", "获取设备信息成功", resp)
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取设备信息成功", resp, actionType, "获取设备信息成功", resp)
}
// ListDevices godoc
@@ -210,21 +212,22 @@ func (c *Controller) GetDevice(ctx *gin.Context) {
// @Success 200 {object} controller.Response{data=[]DeviceResponse}
// @Router /api/v1/devices [get]
func (c *Controller) ListDevices(ctx *gin.Context) {
const actionType = "获取设备列表"
devices, err := c.repo.ListAll()
if err != nil {
c.logger.Errorf("获取设备列表: 数据库操作失败: %v", err)
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取设备列表失败", "获取设备列表", "数据库查询失败", nil)
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取设备列表失败: "+err.Error(), actionType, "数据库查询失败", nil)
return
}
resp, err := newListDeviceResponse(devices)
if err != nil {
c.logger.Errorf("获取设备列表: 序列化响应失败: %v", err)
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取设备列表失败: 内部数据格式错误", "获取设备列表", "响应序列化失败", devices)
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取设备列表失败: 内部数据格式错误", actionType, "响应序列化失败", devices)
return
}
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取设备列表成功", resp, "获取设备列表", "获取设备列表成功", resp)
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取设备列表成功", resp, actionType, "获取设备列表成功", resp)
}
// UpdateDevice godoc
@@ -238,21 +241,22 @@ func (c *Controller) ListDevices(ctx *gin.Context) {
// @Success 200 {object} controller.Response{data=DeviceResponse}
// @Router /api/v1/devices/{id} [put]
func (c *Controller) UpdateDevice(ctx *gin.Context) {
const actionType = "更新设备"
deviceID := ctx.Param("id")
// 1. 检查设备是否存在
existingDevice, err := c.repo.FindByIDString(deviceID)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "设备未找到", "更新设备", "设备不存在", deviceID)
controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "设备未找到", actionType, "设备不存在", deviceID)
return
}
if strings.Contains(err.Error(), "无效的设备ID格式") {
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, err.Error(), "更新设备", "设备ID格式错误", deviceID)
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, err.Error(), actionType, "设备ID格式错误", deviceID)
return
}
c.logger.Errorf("更新设备: 查找设备失败: %v", err)
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新设备失败", "更新设备", "数据库查询失败", deviceID)
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新设备失败: "+err.Error(), actionType, "数据库查询失败", deviceID)
return
}
@@ -260,14 +264,14 @@ func (c *Controller) UpdateDevice(ctx *gin.Context) {
var req UpdateDeviceRequest
if err := ctx.ShouldBindJSON(&req); err != nil {
c.logger.Errorf("更新设备: 参数绑定失败: %v", err)
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, err.Error(), "更新设备", "请求体绑定失败", req)
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req)
return
}
propertiesJSON, err := json.Marshal(req.Properties)
if err != nil {
c.logger.Errorf("更新设备: 序列化属性失败: %v", err)
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "属性字段格式错误", "更新设备", "属性序列化失败", req.Properties)
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "属性字段格式错误", actionType, "属性序列化失败", req.Properties)
return
}
@@ -282,25 +286,25 @@ func (c *Controller) UpdateDevice(ctx *gin.Context) {
// 在更新设备前进行自检
if !existingDevice.SelfCheck() {
c.logger.Errorf("更新设备: 设备属性自检失败: %v", existingDevice)
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "设备属性不符合要求", "更新设备", "设备属性自检失败", existingDevice)
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "设备属性不符合要求", actionType, "设备属性自检失败", existingDevice)
return
}
// 4. 将修改后的 existingDevice 对象保存回数据库
if err := c.repo.Update(existingDevice); err != nil {
c.logger.Errorf("更新设备: 数据库操作失败: %v", err)
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新设备失败", "更新设备", "数据库更新失败", existingDevice)
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新设备失败: "+err.Error(), actionType, "数据库更新失败", existingDevice)
return
}
resp, err := newDeviceResponse(existingDevice)
if err != nil {
c.logger.Errorf("更新设备: 序列化响应失败: %v", err)
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "设备更新成功,但响应生成失败", "更新设备", "响应序列化失败", existingDevice)
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "设备更新成功,但响应生成失败", actionType, "响应序列化失败", existingDevice)
return
}
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "设备更新成功", resp, "更新设备", "设备更新成功", resp)
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "设备更新成功", resp, actionType, "设备更新成功", resp)
}
// DeleteDevice godoc
@@ -312,20 +316,21 @@ func (c *Controller) UpdateDevice(ctx *gin.Context) {
// @Success 200 {object} controller.Response
// @Router /api/v1/devices/{id} [delete]
func (c *Controller) DeleteDevice(ctx *gin.Context) {
const actionType = "删除设备"
deviceID := ctx.Param("id")
// 我们需要先将字符串ID转换为uint因为Delete方法需要uint类型
idUint, err := strconv.ParseUint(deviceID, 10, 64)
if err != nil {
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的设备ID格式", "删除设备", "设备ID格式错误", deviceID)
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的设备ID格式", actionType, "设备ID格式错误", deviceID)
return
}
if err := c.repo.Delete(uint(idUint)); err != nil {
c.logger.Errorf("删除设备: 数据库操作失败: %v", err)
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "删除设备失败", "删除设备", "数据库删除失败", deviceID)
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "删除设备失败: "+err.Error(), actionType, "数据库删除失败", deviceID)
return
}
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "设备删除成功", nil, "删除设备", "设备删除成功", deviceID)
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "设备删除成功", nil, actionType, "设备删除成功", deviceID)
}