diff --git a/design/exceeding-threshold-alarm/index.md b/design/exceeding-threshold-alarm/index.md index aa57023..f9e25c8 100644 --- a/design/exceeding-threshold-alarm/index.md +++ b/design/exceeding-threshold-alarm/index.md @@ -140,4 +140,5 @@ 8. 实现列表查询活跃告警和历史告警 9. 系统初始化时健康计划调整(包括增加延时任务) 10. 实现区域阈值告警任务 -11. 实现区域阈值告警和设备阈值告警的增删改查 \ No newline at end of file +11. 实现区域阈值告警和设备阈值告警的增删改查 +12. 实现任务11应的八个web接口 \ No newline at end of file diff --git a/docs/docs.go b/docs/docs.go index 5147fa8..05aa23a 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -145,6 +145,340 @@ const docTemplate = `{ } } }, + "/api/v1/alarm/threshold/area": { + "post": { + "security": [ + { + "BearerAuth": [] + } + ], + "description": "为指定的区域主控创建一个新的阈值告警规则", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "告警管理" + ], + "summary": "创建区域阈值告警", + "parameters": [ + { + "description": "创建区域阈值告警请求体", + "name": "request", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/dto.CreateAreaThresholdAlarmDTO" + } + } + ], + "responses": { + "200": { + "description": "成功创建区域阈值告警", + "schema": { + "$ref": "#/definitions/controller.Response" + } + } + } + } + }, + "/api/v1/alarm/threshold/area/{task_id}": { + "get": { + "security": [ + { + "BearerAuth": [] + } + ], + "description": "根据任务ID获取单个区域阈值告警规则的详细信息", + "produces": [ + "application/json" + ], + "tags": [ + "告警管理" + ], + "summary": "获取区域阈值告警", + "parameters": [ + { + "type": "integer", + "description": "任务ID", + "name": "task_id", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "成功获取区域阈值告警", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/controller.Response" + }, + { + "type": "object", + "properties": { + "data": { + "$ref": "#/definitions/dto.AreaThresholdAlarmDTO" + } + } + } + ] + } + } + } + }, + "put": { + "security": [ + { + "BearerAuth": [] + } + ], + "description": "根据任务ID更新已存在的区域阈值告警规则", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "告警管理" + ], + "summary": "更新区域阈值告警", + "parameters": [ + { + "type": "integer", + "description": "任务ID", + "name": "task_id", + "in": "path", + "required": true + }, + { + "description": "更新区域阈值告警请求体", + "name": "request", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/dto.UpdateAreaThresholdAlarmDTO" + } + } + ], + "responses": { + "200": { + "description": "成功更新区域阈值告警", + "schema": { + "$ref": "#/definitions/controller.Response" + } + } + } + }, + "delete": { + "security": [ + { + "BearerAuth": [] + } + ], + "description": "根据任务ID删除区域阈值告警规则", + "produces": [ + "application/json" + ], + "tags": [ + "告警管理" + ], + "summary": "删除区域阈值告警", + "parameters": [ + { + "type": "integer", + "description": "任务ID", + "name": "task_id", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "成功删除区域阈值告警", + "schema": { + "$ref": "#/definitions/controller.Response" + } + } + } + } + }, + "/api/v1/alarm/threshold/device": { + "post": { + "security": [ + { + "BearerAuth": [] + } + ], + "description": "为单个设备创建一条新的阈值告警规则", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "告警管理" + ], + "summary": "创建设备阈值告警", + "parameters": [ + { + "description": "创建设备阈值告警请求体", + "name": "request", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/dto.CreateDeviceThresholdAlarmDTO" + } + } + ], + "responses": { + "200": { + "description": "成功创建设备阈值告警", + "schema": { + "$ref": "#/definitions/controller.Response" + } + } + } + } + }, + "/api/v1/alarm/threshold/device/{task_id}": { + "get": { + "security": [ + { + "BearerAuth": [] + } + ], + "description": "根据任务ID获取单个设备阈值告警规则的详细信息", + "produces": [ + "application/json" + ], + "tags": [ + "告警管理" + ], + "summary": "获取设备阈值告警", + "parameters": [ + { + "type": "integer", + "description": "任务ID", + "name": "task_id", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "成功获取设备阈值告警", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/controller.Response" + }, + { + "type": "object", + "properties": { + "data": { + "$ref": "#/definitions/dto.DeviceThresholdAlarmDTO" + } + } + } + ] + } + } + } + }, + "put": { + "security": [ + { + "BearerAuth": [] + } + ], + "description": "根据任务ID更新已存在的设备阈值告警规则", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "告警管理" + ], + "summary": "更新设备阈值告警", + "parameters": [ + { + "type": "integer", + "description": "任务ID", + "name": "task_id", + "in": "path", + "required": true + }, + { + "description": "更新设备阈值告警请求体", + "name": "request", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/dto.UpdateDeviceThresholdAlarmDTO" + } + } + ], + "responses": { + "200": { + "description": "成功更新设备阈值告警", + "schema": { + "$ref": "#/definitions/controller.Response" + } + } + } + }, + "delete": { + "security": [ + { + "BearerAuth": [] + } + ], + "description": "根据任务ID删除设备阈值告警规则", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "告警管理" + ], + "summary": "删除设备阈值告警", + "parameters": [ + { + "type": "integer", + "description": "任务ID", + "name": "task_id", + "in": "path", + "required": true + }, + { + "description": "删除设备阈值告警请求体", + "name": "request", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/dto.DeleteDeviceThresholdAlarmDTO" + } + } + ], + "responses": { + "200": { + "description": "成功删除设备阈值告警", + "schema": { + "$ref": "#/definitions/controller.Response" + } + } + } + } + }, "/api/v1/alarm/threshold/historical-alarms": { "get": { "security": [ @@ -4577,6 +4911,29 @@ const docTemplate = `{ } } }, + "dto.AreaThresholdAlarmDTO": { + "type": "object", + "properties": { + "area_controller_id": { + "type": "integer" + }, + "id": { + "type": "integer" + }, + "level": { + "$ref": "#/definitions/models.SeverityLevel" + }, + "operator": { + "$ref": "#/definitions/models.Operator" + }, + "sensor_type": { + "$ref": "#/definitions/models.SensorType" + }, + "thresholds": { + "type": "number" + } + } + }, "dto.AssignEmptyPensToBatchRequest": { "type": "object", "required": [ @@ -4664,6 +5021,49 @@ const docTemplate = `{ } } }, + "dto.CreateAreaThresholdAlarmDTO": { + "type": "object", + "required": [ + "area_controller_id", + "operator", + "sensor_type", + "thresholds" + ], + "properties": { + "area_controller_id": { + "description": "区域主控ID", + "type": "integer" + }, + "level": { + "description": "告警等级,可选", + "allOf": [ + { + "$ref": "#/definitions/models.SeverityLevel" + } + ] + }, + "operator": { + "description": "操作符", + "allOf": [ + { + "$ref": "#/definitions/models.Operator" + } + ] + }, + "sensor_type": { + "description": "传感器类型", + "allOf": [ + { + "$ref": "#/definitions/models.SensorType" + } + ] + }, + "thresholds": { + "description": "阈值", + "type": "number" + } + } + }, "dto.CreateDeviceRequest": { "type": "object", "required": [ @@ -4722,6 +5122,49 @@ const docTemplate = `{ } } }, + "dto.CreateDeviceThresholdAlarmDTO": { + "type": "object", + "required": [ + "device_id", + "operator", + "sensor_type", + "thresholds" + ], + "properties": { + "device_id": { + "description": "设备ID", + "type": "integer" + }, + "level": { + "description": "告警等级,可选,如果未提供则使用默认值", + "allOf": [ + { + "$ref": "#/definitions/models.SeverityLevel" + } + ] + }, + "operator": { + "description": "操作符 (使用string类型,与前端交互更通用)", + "allOf": [ + { + "$ref": "#/definitions/models.Operator" + } + ] + }, + "sensor_type": { + "description": "传感器类型", + "allOf": [ + { + "$ref": "#/definitions/models.SensorType" + } + ] + }, + "thresholds": { + "description": "阈值", + "type": "number" + } + } + }, "dto.CreatePenRequest": { "type": "object", "required": [ @@ -4831,6 +5274,22 @@ const docTemplate = `{ } } }, + "dto.DeleteDeviceThresholdAlarmDTO": { + "type": "object", + "required": [ + "sensor_type" + ], + "properties": { + "sensor_type": { + "description": "传感器类型", + "allOf": [ + { + "$ref": "#/definitions/models.SensorType" + } + ] + } + } + }, "dto.DeviceCommandLogDTO": { "type": "object", "properties": { @@ -4923,6 +5382,29 @@ const docTemplate = `{ } } }, + "dto.DeviceThresholdAlarmDTO": { + "type": "object", + "properties": { + "device_id": { + "type": "integer" + }, + "id": { + "type": "integer" + }, + "level": { + "$ref": "#/definitions/models.SeverityLevel" + }, + "operator": { + "$ref": "#/definitions/models.Operator" + }, + "sensor_type": { + "$ref": "#/definitions/models.SensorType" + }, + "thresholds": { + "type": "number" + } + } + }, "dto.FeedFormulaDTO": { "type": "object", "properties": { @@ -6592,6 +7074,35 @@ const docTemplate = `{ } } }, + "dto.UpdateAreaThresholdAlarmDTO": { + "type": "object", + "required": [ + "operator", + "thresholds" + ], + "properties": { + "level": { + "description": "新的告警等级,可选", + "allOf": [ + { + "$ref": "#/definitions/models.SeverityLevel" + } + ] + }, + "operator": { + "description": "新的操作符", + "allOf": [ + { + "$ref": "#/definitions/models.Operator" + } + ] + }, + "thresholds": { + "description": "新的阈值", + "type": "number" + } + } + }, "dto.UpdateDeviceRequest": { "type": "object", "required": [ @@ -6650,6 +7161,35 @@ const docTemplate = `{ } } }, + "dto.UpdateDeviceThresholdAlarmDTO": { + "type": "object", + "required": [ + "operator", + "thresholds" + ], + "properties": { + "level": { + "description": "新的告警等级,可选", + "allOf": [ + { + "$ref": "#/definitions/models.SeverityLevel" + } + ] + }, + "operator": { + "description": "新的操作符", + "allOf": [ + { + "$ref": "#/definitions/models.Operator" + } + ] + }, + "thresholds": { + "description": "新的阈值", + "type": "number" + } + } + }, "dto.UpdatePenRequest": { "type": "object", "required": [ @@ -7027,6 +7567,25 @@ const docTemplate = `{ "NotifierTypeLog" ] }, + "models.Operator": { + "type": "string", + "enum": [ + "\u003c", + "\u003c=", + "\u003e", + "\u003e=", + "=", + "!=" + ], + "x-enum-varnames": [ + "OperatorLessThan", + "OperatorLessThanOrEqualTo", + "OperatorGreaterThan", + "OperatorGreaterThanOrEqualTo", + "OperatorEqualTo", + "OperatorNotEqualTo" + ] + }, "models.PenStatus": { "type": "string", "enum": [ @@ -7357,11 +7916,15 @@ const docTemplate = `{ "等待", "下料", "全量采集", - "告警通知" + "告警通知", + "设备阈值检查", + "区域阈值检查" ], "x-enum-comments": { "TaskPlanAnalysis": "解析Plan的Task列表并添加到待执行队列的特殊任务", "TaskTypeAlarmNotification": "告警通知任务", + "TaskTypeAreaCollectorThresholdCheck": "区域阈值检查任务", + "TaskTypeDeviceThresholdCheck": "设备阈值检查任务", "TaskTypeFullCollection": "新增的全量采集任务", "TaskTypeReleaseFeedWeight": "下料口释放指定重量任务", "TaskTypeWaiting": "等待任务" @@ -7371,14 +7934,18 @@ const docTemplate = `{ "等待任务", "下料口释放指定重量任务", "新增的全量采集任务", - "告警通知任务" + "告警通知任务", + "设备阈值检查任务", + "区域阈值检查任务" ], "x-enum-varnames": [ "TaskPlanAnalysis", "TaskTypeWaiting", "TaskTypeReleaseFeedWeight", "TaskTypeFullCollection", - "TaskTypeAlarmNotification" + "TaskTypeAlarmNotification", + "TaskTypeDeviceThresholdCheck", + "TaskTypeAreaCollectorThresholdCheck" ] }, "models.ValueDescriptor": { diff --git a/docs/swagger.json b/docs/swagger.json index 8fa4a88..b3cfe1a 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -137,6 +137,340 @@ } } }, + "/api/v1/alarm/threshold/area": { + "post": { + "security": [ + { + "BearerAuth": [] + } + ], + "description": "为指定的区域主控创建一个新的阈值告警规则", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "告警管理" + ], + "summary": "创建区域阈值告警", + "parameters": [ + { + "description": "创建区域阈值告警请求体", + "name": "request", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/dto.CreateAreaThresholdAlarmDTO" + } + } + ], + "responses": { + "200": { + "description": "成功创建区域阈值告警", + "schema": { + "$ref": "#/definitions/controller.Response" + } + } + } + } + }, + "/api/v1/alarm/threshold/area/{task_id}": { + "get": { + "security": [ + { + "BearerAuth": [] + } + ], + "description": "根据任务ID获取单个区域阈值告警规则的详细信息", + "produces": [ + "application/json" + ], + "tags": [ + "告警管理" + ], + "summary": "获取区域阈值告警", + "parameters": [ + { + "type": "integer", + "description": "任务ID", + "name": "task_id", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "成功获取区域阈值告警", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/controller.Response" + }, + { + "type": "object", + "properties": { + "data": { + "$ref": "#/definitions/dto.AreaThresholdAlarmDTO" + } + } + } + ] + } + } + } + }, + "put": { + "security": [ + { + "BearerAuth": [] + } + ], + "description": "根据任务ID更新已存在的区域阈值告警规则", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "告警管理" + ], + "summary": "更新区域阈值告警", + "parameters": [ + { + "type": "integer", + "description": "任务ID", + "name": "task_id", + "in": "path", + "required": true + }, + { + "description": "更新区域阈值告警请求体", + "name": "request", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/dto.UpdateAreaThresholdAlarmDTO" + } + } + ], + "responses": { + "200": { + "description": "成功更新区域阈值告警", + "schema": { + "$ref": "#/definitions/controller.Response" + } + } + } + }, + "delete": { + "security": [ + { + "BearerAuth": [] + } + ], + "description": "根据任务ID删除区域阈值告警规则", + "produces": [ + "application/json" + ], + "tags": [ + "告警管理" + ], + "summary": "删除区域阈值告警", + "parameters": [ + { + "type": "integer", + "description": "任务ID", + "name": "task_id", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "成功删除区域阈值告警", + "schema": { + "$ref": "#/definitions/controller.Response" + } + } + } + } + }, + "/api/v1/alarm/threshold/device": { + "post": { + "security": [ + { + "BearerAuth": [] + } + ], + "description": "为单个设备创建一条新的阈值告警规则", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "告警管理" + ], + "summary": "创建设备阈值告警", + "parameters": [ + { + "description": "创建设备阈值告警请求体", + "name": "request", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/dto.CreateDeviceThresholdAlarmDTO" + } + } + ], + "responses": { + "200": { + "description": "成功创建设备阈值告警", + "schema": { + "$ref": "#/definitions/controller.Response" + } + } + } + } + }, + "/api/v1/alarm/threshold/device/{task_id}": { + "get": { + "security": [ + { + "BearerAuth": [] + } + ], + "description": "根据任务ID获取单个设备阈值告警规则的详细信息", + "produces": [ + "application/json" + ], + "tags": [ + "告警管理" + ], + "summary": "获取设备阈值告警", + "parameters": [ + { + "type": "integer", + "description": "任务ID", + "name": "task_id", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "成功获取设备阈值告警", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/controller.Response" + }, + { + "type": "object", + "properties": { + "data": { + "$ref": "#/definitions/dto.DeviceThresholdAlarmDTO" + } + } + } + ] + } + } + } + }, + "put": { + "security": [ + { + "BearerAuth": [] + } + ], + "description": "根据任务ID更新已存在的设备阈值告警规则", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "告警管理" + ], + "summary": "更新设备阈值告警", + "parameters": [ + { + "type": "integer", + "description": "任务ID", + "name": "task_id", + "in": "path", + "required": true + }, + { + "description": "更新设备阈值告警请求体", + "name": "request", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/dto.UpdateDeviceThresholdAlarmDTO" + } + } + ], + "responses": { + "200": { + "description": "成功更新设备阈值告警", + "schema": { + "$ref": "#/definitions/controller.Response" + } + } + } + }, + "delete": { + "security": [ + { + "BearerAuth": [] + } + ], + "description": "根据任务ID删除设备阈值告警规则", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "告警管理" + ], + "summary": "删除设备阈值告警", + "parameters": [ + { + "type": "integer", + "description": "任务ID", + "name": "task_id", + "in": "path", + "required": true + }, + { + "description": "删除设备阈值告警请求体", + "name": "request", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/dto.DeleteDeviceThresholdAlarmDTO" + } + } + ], + "responses": { + "200": { + "description": "成功删除设备阈值告警", + "schema": { + "$ref": "#/definitions/controller.Response" + } + } + } + } + }, "/api/v1/alarm/threshold/historical-alarms": { "get": { "security": [ @@ -4569,6 +4903,29 @@ } } }, + "dto.AreaThresholdAlarmDTO": { + "type": "object", + "properties": { + "area_controller_id": { + "type": "integer" + }, + "id": { + "type": "integer" + }, + "level": { + "$ref": "#/definitions/models.SeverityLevel" + }, + "operator": { + "$ref": "#/definitions/models.Operator" + }, + "sensor_type": { + "$ref": "#/definitions/models.SensorType" + }, + "thresholds": { + "type": "number" + } + } + }, "dto.AssignEmptyPensToBatchRequest": { "type": "object", "required": [ @@ -4656,6 +5013,49 @@ } } }, + "dto.CreateAreaThresholdAlarmDTO": { + "type": "object", + "required": [ + "area_controller_id", + "operator", + "sensor_type", + "thresholds" + ], + "properties": { + "area_controller_id": { + "description": "区域主控ID", + "type": "integer" + }, + "level": { + "description": "告警等级,可选", + "allOf": [ + { + "$ref": "#/definitions/models.SeverityLevel" + } + ] + }, + "operator": { + "description": "操作符", + "allOf": [ + { + "$ref": "#/definitions/models.Operator" + } + ] + }, + "sensor_type": { + "description": "传感器类型", + "allOf": [ + { + "$ref": "#/definitions/models.SensorType" + } + ] + }, + "thresholds": { + "description": "阈值", + "type": "number" + } + } + }, "dto.CreateDeviceRequest": { "type": "object", "required": [ @@ -4714,6 +5114,49 @@ } } }, + "dto.CreateDeviceThresholdAlarmDTO": { + "type": "object", + "required": [ + "device_id", + "operator", + "sensor_type", + "thresholds" + ], + "properties": { + "device_id": { + "description": "设备ID", + "type": "integer" + }, + "level": { + "description": "告警等级,可选,如果未提供则使用默认值", + "allOf": [ + { + "$ref": "#/definitions/models.SeverityLevel" + } + ] + }, + "operator": { + "description": "操作符 (使用string类型,与前端交互更通用)", + "allOf": [ + { + "$ref": "#/definitions/models.Operator" + } + ] + }, + "sensor_type": { + "description": "传感器类型", + "allOf": [ + { + "$ref": "#/definitions/models.SensorType" + } + ] + }, + "thresholds": { + "description": "阈值", + "type": "number" + } + } + }, "dto.CreatePenRequest": { "type": "object", "required": [ @@ -4823,6 +5266,22 @@ } } }, + "dto.DeleteDeviceThresholdAlarmDTO": { + "type": "object", + "required": [ + "sensor_type" + ], + "properties": { + "sensor_type": { + "description": "传感器类型", + "allOf": [ + { + "$ref": "#/definitions/models.SensorType" + } + ] + } + } + }, "dto.DeviceCommandLogDTO": { "type": "object", "properties": { @@ -4915,6 +5374,29 @@ } } }, + "dto.DeviceThresholdAlarmDTO": { + "type": "object", + "properties": { + "device_id": { + "type": "integer" + }, + "id": { + "type": "integer" + }, + "level": { + "$ref": "#/definitions/models.SeverityLevel" + }, + "operator": { + "$ref": "#/definitions/models.Operator" + }, + "sensor_type": { + "$ref": "#/definitions/models.SensorType" + }, + "thresholds": { + "type": "number" + } + } + }, "dto.FeedFormulaDTO": { "type": "object", "properties": { @@ -6584,6 +7066,35 @@ } } }, + "dto.UpdateAreaThresholdAlarmDTO": { + "type": "object", + "required": [ + "operator", + "thresholds" + ], + "properties": { + "level": { + "description": "新的告警等级,可选", + "allOf": [ + { + "$ref": "#/definitions/models.SeverityLevel" + } + ] + }, + "operator": { + "description": "新的操作符", + "allOf": [ + { + "$ref": "#/definitions/models.Operator" + } + ] + }, + "thresholds": { + "description": "新的阈值", + "type": "number" + } + } + }, "dto.UpdateDeviceRequest": { "type": "object", "required": [ @@ -6642,6 +7153,35 @@ } } }, + "dto.UpdateDeviceThresholdAlarmDTO": { + "type": "object", + "required": [ + "operator", + "thresholds" + ], + "properties": { + "level": { + "description": "新的告警等级,可选", + "allOf": [ + { + "$ref": "#/definitions/models.SeverityLevel" + } + ] + }, + "operator": { + "description": "新的操作符", + "allOf": [ + { + "$ref": "#/definitions/models.Operator" + } + ] + }, + "thresholds": { + "description": "新的阈值", + "type": "number" + } + } + }, "dto.UpdatePenRequest": { "type": "object", "required": [ @@ -7019,6 +7559,25 @@ "NotifierTypeLog" ] }, + "models.Operator": { + "type": "string", + "enum": [ + "\u003c", + "\u003c=", + "\u003e", + "\u003e=", + "=", + "!=" + ], + "x-enum-varnames": [ + "OperatorLessThan", + "OperatorLessThanOrEqualTo", + "OperatorGreaterThan", + "OperatorGreaterThanOrEqualTo", + "OperatorEqualTo", + "OperatorNotEqualTo" + ] + }, "models.PenStatus": { "type": "string", "enum": [ @@ -7349,11 +7908,15 @@ "等待", "下料", "全量采集", - "告警通知" + "告警通知", + "设备阈值检查", + "区域阈值检查" ], "x-enum-comments": { "TaskPlanAnalysis": "解析Plan的Task列表并添加到待执行队列的特殊任务", "TaskTypeAlarmNotification": "告警通知任务", + "TaskTypeAreaCollectorThresholdCheck": "区域阈值检查任务", + "TaskTypeDeviceThresholdCheck": "设备阈值检查任务", "TaskTypeFullCollection": "新增的全量采集任务", "TaskTypeReleaseFeedWeight": "下料口释放指定重量任务", "TaskTypeWaiting": "等待任务" @@ -7363,14 +7926,18 @@ "等待任务", "下料口释放指定重量任务", "新增的全量采集任务", - "告警通知任务" + "告警通知任务", + "设备阈值检查任务", + "区域阈值检查任务" ], "x-enum-varnames": [ "TaskPlanAnalysis", "TaskTypeWaiting", "TaskTypeReleaseFeedWeight", "TaskTypeFullCollection", - "TaskTypeAlarmNotification" + "TaskTypeAlarmNotification", + "TaskTypeDeviceThresholdCheck", + "TaskTypeAreaCollectorThresholdCheck" ] }, "models.ValueDescriptor": { diff --git a/docs/swagger.yaml b/docs/swagger.yaml index ebfb500..c228a4f 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -102,6 +102,21 @@ definitions: updated_at: type: string type: object + dto.AreaThresholdAlarmDTO: + properties: + area_controller_id: + type: integer + id: + type: integer + level: + $ref: '#/definitions/models.SeverityLevel' + operator: + $ref: '#/definitions/models.Operator' + sensor_type: + $ref: '#/definitions/models.SensorType' + thresholds: + type: number + type: object dto.AssignEmptyPensToBatchRequest: properties: pen_ids: @@ -166,6 +181,32 @@ definitions: - name - network_id type: object + dto.CreateAreaThresholdAlarmDTO: + properties: + area_controller_id: + description: 区域主控ID + type: integer + level: + allOf: + - $ref: '#/definitions/models.SeverityLevel' + description: 告警等级,可选 + operator: + allOf: + - $ref: '#/definitions/models.Operator' + description: 操作符 + sensor_type: + allOf: + - $ref: '#/definitions/models.SensorType' + description: 传感器类型 + thresholds: + description: 阈值 + type: number + required: + - area_controller_id + - operator + - sensor_type + - thresholds + type: object dto.CreateDeviceRequest: properties: area_controller_id: @@ -206,6 +247,32 @@ definitions: - commands - name type: object + dto.CreateDeviceThresholdAlarmDTO: + properties: + device_id: + description: 设备ID + type: integer + level: + allOf: + - $ref: '#/definitions/models.SeverityLevel' + description: 告警等级,可选,如果未提供则使用默认值 + operator: + allOf: + - $ref: '#/definitions/models.Operator' + description: 操作符 (使用string类型,与前端交互更通用) + sensor_type: + allOf: + - $ref: '#/definitions/models.SensorType' + description: 传感器类型 + thresholds: + description: 阈值 + type: number + required: + - device_id + - operator + - sensor_type + - thresholds + type: object dto.CreatePenRequest: properties: capacity: @@ -280,6 +347,15 @@ definitions: example: newuser type: string type: object + dto.DeleteDeviceThresholdAlarmDTO: + properties: + sensor_type: + allOf: + - $ref: '#/definitions/models.SensorType' + description: 传感器类型 + required: + - sensor_type + type: object dto.DeviceCommandLogDTO: properties: acknowledged_at: @@ -341,6 +417,21 @@ definitions: $ref: '#/definitions/models.ValueDescriptor' type: array type: object + dto.DeviceThresholdAlarmDTO: + properties: + device_id: + type: integer + id: + type: integer + level: + $ref: '#/definitions/models.SeverityLevel' + operator: + $ref: '#/definitions/models.Operator' + sensor_type: + $ref: '#/definitions/models.SensorType' + thresholds: + type: number + type: object dto.FeedFormulaDTO: properties: id: @@ -1454,6 +1545,23 @@ definitions: - name - network_id type: object + dto.UpdateAreaThresholdAlarmDTO: + properties: + level: + allOf: + - $ref: '#/definitions/models.SeverityLevel' + description: 新的告警等级,可选 + operator: + allOf: + - $ref: '#/definitions/models.Operator' + description: 新的操作符 + thresholds: + description: 新的阈值 + type: number + required: + - operator + - thresholds + type: object dto.UpdateDeviceRequest: properties: area_controller_id: @@ -1494,6 +1602,23 @@ definitions: - commands - name type: object + dto.UpdateDeviceThresholdAlarmDTO: + properties: + level: + allOf: + - $ref: '#/definitions/models.SeverityLevel' + description: 新的告警等级,可选 + operator: + allOf: + - $ref: '#/definitions/models.Operator' + description: 新的操作符 + thresholds: + description: 新的阈值 + type: number + required: + - operator + - thresholds + type: object dto.UpdatePenRequest: properties: capacity: @@ -1767,6 +1892,22 @@ definitions: - NotifierTypeWeChat - NotifierTypeLark - NotifierTypeLog + models.Operator: + enum: + - < + - <= + - '>' + - '>=' + - = + - '!=' + type: string + x-enum-varnames: + - OperatorLessThan + - OperatorLessThanOrEqualTo + - OperatorGreaterThan + - OperatorGreaterThanOrEqualTo + - OperatorEqualTo + - OperatorNotEqualTo models.PenStatus: enum: - 空闲 @@ -2039,10 +2180,14 @@ definitions: - 下料 - 全量采集 - 告警通知 + - 设备阈值检查 + - 区域阈值检查 type: string x-enum-comments: TaskPlanAnalysis: 解析Plan的Task列表并添加到待执行队列的特殊任务 TaskTypeAlarmNotification: 告警通知任务 + TaskTypeAreaCollectorThresholdCheck: 区域阈值检查任务 + TaskTypeDeviceThresholdCheck: 设备阈值检查任务 TaskTypeFullCollection: 新增的全量采集任务 TaskTypeReleaseFeedWeight: 下料口释放指定重量任务 TaskTypeWaiting: 等待任务 @@ -2052,12 +2197,16 @@ definitions: - 下料口释放指定重量任务 - 新增的全量采集任务 - 告警通知任务 + - 设备阈值检查任务 + - 区域阈值检查任务 x-enum-varnames: - TaskPlanAnalysis - TaskTypeWaiting - TaskTypeReleaseFeedWeight - TaskTypeFullCollection - TaskTypeAlarmNotification + - TaskTypeDeviceThresholdCheck + - TaskTypeAreaCollectorThresholdCheck models.ValueDescriptor: properties: multiplier: @@ -2251,6 +2400,210 @@ paths: summary: 批量查询活跃告警 tags: - 告警管理 + /api/v1/alarm/threshold/area: + post: + consumes: + - application/json + description: 为指定的区域主控创建一个新的阈值告警规则 + parameters: + - description: 创建区域阈值告警请求体 + in: body + name: request + required: true + schema: + $ref: '#/definitions/dto.CreateAreaThresholdAlarmDTO' + produces: + - application/json + responses: + "200": + description: 成功创建区域阈值告警 + schema: + $ref: '#/definitions/controller.Response' + security: + - BearerAuth: [] + summary: 创建区域阈值告警 + tags: + - 告警管理 + /api/v1/alarm/threshold/area/{task_id}: + delete: + description: 根据任务ID删除区域阈值告警规则 + parameters: + - description: 任务ID + in: path + name: task_id + required: true + type: integer + produces: + - application/json + responses: + "200": + description: 成功删除区域阈值告警 + schema: + $ref: '#/definitions/controller.Response' + security: + - BearerAuth: [] + summary: 删除区域阈值告警 + tags: + - 告警管理 + get: + description: 根据任务ID获取单个区域阈值告警规则的详细信息 + parameters: + - description: 任务ID + in: path + name: task_id + required: true + type: integer + produces: + - application/json + responses: + "200": + description: 成功获取区域阈值告警 + schema: + allOf: + - $ref: '#/definitions/controller.Response' + - properties: + data: + $ref: '#/definitions/dto.AreaThresholdAlarmDTO' + type: object + security: + - BearerAuth: [] + summary: 获取区域阈值告警 + tags: + - 告警管理 + put: + consumes: + - application/json + description: 根据任务ID更新已存在的区域阈值告警规则 + parameters: + - description: 任务ID + in: path + name: task_id + required: true + type: integer + - description: 更新区域阈值告警请求体 + in: body + name: request + required: true + schema: + $ref: '#/definitions/dto.UpdateAreaThresholdAlarmDTO' + produces: + - application/json + responses: + "200": + description: 成功更新区域阈值告警 + schema: + $ref: '#/definitions/controller.Response' + security: + - BearerAuth: [] + summary: 更新区域阈值告警 + tags: + - 告警管理 + /api/v1/alarm/threshold/device: + post: + consumes: + - application/json + description: 为单个设备创建一条新的阈值告警规则 + parameters: + - description: 创建设备阈值告警请求体 + in: body + name: request + required: true + schema: + $ref: '#/definitions/dto.CreateDeviceThresholdAlarmDTO' + produces: + - application/json + responses: + "200": + description: 成功创建设备阈值告警 + schema: + $ref: '#/definitions/controller.Response' + security: + - BearerAuth: [] + summary: 创建设备阈值告警 + tags: + - 告警管理 + /api/v1/alarm/threshold/device/{task_id}: + delete: + consumes: + - application/json + description: 根据任务ID删除设备阈值告警规则 + parameters: + - description: 任务ID + in: path + name: task_id + required: true + type: integer + - description: 删除设备阈值告警请求体 + in: body + name: request + required: true + schema: + $ref: '#/definitions/dto.DeleteDeviceThresholdAlarmDTO' + produces: + - application/json + responses: + "200": + description: 成功删除设备阈值告警 + schema: + $ref: '#/definitions/controller.Response' + security: + - BearerAuth: [] + summary: 删除设备阈值告警 + tags: + - 告警管理 + get: + description: 根据任务ID获取单个设备阈值告警规则的详细信息 + parameters: + - description: 任务ID + in: path + name: task_id + required: true + type: integer + produces: + - application/json + responses: + "200": + description: 成功获取设备阈值告警 + schema: + allOf: + - $ref: '#/definitions/controller.Response' + - properties: + data: + $ref: '#/definitions/dto.DeviceThresholdAlarmDTO' + type: object + security: + - BearerAuth: [] + summary: 获取设备阈值告警 + tags: + - 告警管理 + put: + consumes: + - application/json + description: 根据任务ID更新已存在的设备阈值告警规则 + parameters: + - description: 任务ID + in: path + name: task_id + required: true + type: integer + - description: 更新设备阈值告警请求体 + in: body + name: request + required: true + schema: + $ref: '#/definitions/dto.UpdateDeviceThresholdAlarmDTO' + produces: + - application/json + responses: + "200": + description: 成功更新设备阈值告警 + schema: + $ref: '#/definitions/controller.Response' + security: + - BearerAuth: [] + summary: 更新设备阈值告警 + tags: + - 告警管理 /api/v1/alarm/threshold/historical-alarms: get: consumes: diff --git a/internal/app/api/router.go b/internal/app/api/router.go index 5929249..6540165 100644 --- a/internal/app/api/router.go +++ b/internal/app/api/router.go @@ -197,6 +197,19 @@ func (a *API) setupRoutes() { thresholdGroup.POST("/:id/cancel-snooze", a.alarmController.CancelSnoozeThresholdAlarm) // 取消忽略阈值告警 thresholdGroup.GET("/active-alarms", a.alarmController.ListActiveAlarms) // 获取活跃告警 thresholdGroup.GET("/historical-alarms", a.alarmController.ListHistoricalAlarms) // 获取历史告警 + + // 设备阈值告警配置 + thresholdGroup.POST("/device", a.alarmController.CreateDeviceThresholdAlarm) + thresholdGroup.GET("/device/:task_id", a.alarmController.GetDeviceThresholdAlarm) + thresholdGroup.PUT("/device/:task_id", a.alarmController.UpdateDeviceThresholdAlarm) + thresholdGroup.DELETE("/device/:task_id", a.alarmController.DeleteDeviceThresholdAlarm) + + // 区域阈值告警配置 + thresholdGroup.POST("/area", a.alarmController.CreateAreaThresholdAlarm) + thresholdGroup.GET("/area/:task_id", a.alarmController.GetAreaThresholdAlarm) + thresholdGroup.PUT("/area/:task_id", a.alarmController.UpdateAreaThresholdAlarm) + thresholdGroup.DELETE("/area/:task_id", a.alarmController.DeleteAreaThresholdAlarm) + } } logger.Debug("告警相关接口注册成功 (需要认证和审计)") diff --git a/internal/app/controller/alarm/threshold_alarm_controller.go b/internal/app/controller/alarm/threshold_alarm_controller.go index 06167f1..fed4768 100644 --- a/internal/app/controller/alarm/threshold_alarm_controller.go +++ b/internal/app/controller/alarm/threshold_alarm_controller.go @@ -178,3 +178,279 @@ func (t *ThresholdAlarmController) ListHistoricalAlarms(ctx echo.Context) error logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(resp.List), resp.Pagination.Total) return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "成功获取历史告警列表", resp, actionType, "成功获取历史告警列表", req) } + +// CreateDeviceThresholdAlarm godoc +// @Summary 创建设备阈值告警 +// @Description 为单个设备创建一条新的阈值告警规则 +// @Tags 告警管理 +// @Security BearerAuth +// @Accept json +// @Produce json +// @Param request body dto.CreateDeviceThresholdAlarmDTO true "创建设备阈值告警请求体" +// @Success 200 {object} controller.Response "成功创建设备阈值告警" +// @Router /api/v1/alarm/threshold/device [post] +func (t *ThresholdAlarmController) CreateDeviceThresholdAlarm(ctx echo.Context) error { + reqCtx, logger := logs.Trace(ctx.Request().Context(), t.ctx, "CreateDeviceThresholdAlarm") + const actionType = "创建设备阈值告警" + + var req dto.CreateDeviceThresholdAlarmDTO + if err := ctx.Bind(&req); err != nil { + logger.Errorf("%s: 参数绑定失败: %v", actionType, err) + return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req) + } + + if err := t.thresholdAlarmService.CreateDeviceThresholdAlarm(reqCtx, &req); err != nil { + logger.Errorf("%s: 服务层创建失败: %v", actionType, err) + return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "创建失败: "+err.Error(), actionType, "服务层创建失败", req) + } + + logger.Infof("%s: 成功, DeviceID: %d, SensorType: %s", actionType, req.DeviceID, req.SensorType) + return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "创建成功", nil, actionType, "创建成功", req) +} + +// GetDeviceThresholdAlarm godoc +// @Summary 获取设备阈值告警 +// @Description 根据任务ID获取单个设备阈值告警规则的详细信息 +// @Tags 告警管理 +// @Security BearerAuth +// @Produce json +// @Param task_id path int true "任务ID" +// @Success 200 {object} controller.Response{data=dto.DeviceThresholdAlarmDTO} "成功获取设备阈值告警" +// @Router /api/v1/alarm/threshold/device/{task_id} [get] +func (t *ThresholdAlarmController) GetDeviceThresholdAlarm(ctx echo.Context) error { + reqCtx, logger := logs.Trace(ctx.Request().Context(), t.ctx, "GetDeviceThresholdAlarm") + const actionType = "获取设备阈值告警" + + taskID, err := strconv.Atoi(ctx.Param("task_id")) + if err != nil { + logger.Errorf("%s: 无效的任务ID: %s", actionType, ctx.Param("task_id")) + return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的任务ID", actionType, "无效的任务ID", ctx.Param("task_id")) + } + + resp, err := t.thresholdAlarmService.GetDeviceThresholdAlarm(reqCtx, taskID) + if err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + logger.Warnf("%s: 任务不存在, ID: %d", actionType, taskID) + return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "任务未找到", actionType, "任务不存在", taskID) + } + logger.Errorf("%s: 服务层获取失败: %v, ID: %d", actionType, err, taskID) + return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取失败: "+err.Error(), actionType, "服务层获取失败", taskID) + } + + logger.Infof("%s: 成功, ID: %d", actionType, taskID) + return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取成功", resp, actionType, "获取成功", taskID) +} + +// UpdateDeviceThresholdAlarm godoc +// @Summary 更新设备阈值告警 +// @Description 根据任务ID更新已存在的设备阈值告警规则 +// @Tags 告警管理 +// @Security BearerAuth +// @Accept json +// @Produce json +// @Param task_id path int true "任务ID" +// @Param request body dto.UpdateDeviceThresholdAlarmDTO true "更新设备阈值告警请求体" +// @Success 200 {object} controller.Response "成功更新设备阈值告警" +// @Router /api/v1/alarm/threshold/device/{task_id} [put] +func (t *ThresholdAlarmController) UpdateDeviceThresholdAlarm(ctx echo.Context) error { + reqCtx, logger := logs.Trace(ctx.Request().Context(), t.ctx, "UpdateDeviceThresholdAlarm") + const actionType = "更新设备阈值告警" + + taskID, err := strconv.Atoi(ctx.Param("task_id")) + if err != nil { + logger.Errorf("%s: 无效的任务ID: %s", actionType, ctx.Param("task_id")) + return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的任务ID", actionType, "无效的任务ID", ctx.Param("task_id")) + } + + var req dto.UpdateDeviceThresholdAlarmDTO + if err := ctx.Bind(&req); err != nil { + logger.Errorf("%s: 参数绑定失败: %v", actionType, err) + return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req) + } + + if err := t.thresholdAlarmService.UpdateDeviceThresholdAlarm(reqCtx, taskID, &req); err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + logger.Warnf("%s: 任务不存在, ID: %d", actionType, taskID) + return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "任务未找到", actionType, "任务不存在", taskID) + } + logger.Errorf("%s: 服务层更新失败: %v, ID: %d", actionType, err, taskID) + return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新失败: "+err.Error(), actionType, "服务层更新失败", taskID) + } + + logger.Infof("%s: 成功, ID: %d", actionType, taskID) + return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "更新成功", nil, actionType, "更新成功", taskID) +} + +// DeleteDeviceThresholdAlarm godoc +// @Summary 删除设备阈值告警 +// @Description 根据任务ID删除设备阈值告警规则 +// @Tags 告警管理 +// @Security BearerAuth +// @Accept json +// @Produce json +// @Param task_id path int true "任务ID" +// @Param request body dto.DeleteDeviceThresholdAlarmDTO true "删除设备阈值告警请求体" +// @Success 200 {object} controller.Response "成功删除设备阈值告警" +// @Router /api/v1/alarm/threshold/device/{task_id} [delete] +func (t *ThresholdAlarmController) DeleteDeviceThresholdAlarm(ctx echo.Context) error { + reqCtx, logger := logs.Trace(ctx.Request().Context(), t.ctx, "DeleteDeviceThresholdAlarm") + const actionType = "删除设备阈值告警" + + taskID, err := strconv.Atoi(ctx.Param("task_id")) + if err != nil { + logger.Errorf("%s: 无效的任务ID: %s", actionType, ctx.Param("task_id")) + return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的任务ID", actionType, "无效的任务ID", ctx.Param("task_id")) + } + + var req dto.DeleteDeviceThresholdAlarmDTO + if err := ctx.Bind(&req); err != nil { + logger.Errorf("%s: 参数绑定失败: %v", actionType, err) + return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req) + } + + if err := t.thresholdAlarmService.DeleteDeviceThresholdAlarm(reqCtx, taskID, &req); err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + logger.Warnf("%s: 任务不存在, ID: %d", actionType, taskID) + return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "任务未找到", actionType, "任务不存在", taskID) + } + logger.Errorf("%s: 服务层删除失败: %v, ID: %d", actionType, err, taskID) + return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "删除失败: "+err.Error(), actionType, "服务层删除失败", taskID) + } + + logger.Infof("%s: 成功, ID: %d", actionType, taskID) + return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "删除成功", nil, actionType, "删除成功", taskID) +} + +// CreateAreaThresholdAlarm godoc +// @Summary 创建区域阈值告警 +// @Description 为指定的区域主控创建一个新的阈值告警规则 +// @Tags 告警管理 +// @Security BearerAuth +// @Accept json +// @Produce json +// @Param request body dto.CreateAreaThresholdAlarmDTO true "创建区域阈值告警请求体" +// @Success 200 {object} controller.Response "成功创建区域阈值告警" +// @Router /api/v1/alarm/threshold/area [post] +func (t *ThresholdAlarmController) CreateAreaThresholdAlarm(ctx echo.Context) error { + reqCtx, logger := logs.Trace(ctx.Request().Context(), t.ctx, "CreateAreaThresholdAlarm") + const actionType = "创建区域阈值告警" + + var req dto.CreateAreaThresholdAlarmDTO + if err := ctx.Bind(&req); err != nil { + logger.Errorf("%s: 参数绑定失败: %v", actionType, err) + return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req) + } + + if err := t.thresholdAlarmService.CreateAreaThresholdAlarm(reqCtx, &req); err != nil { + logger.Errorf("%s: 服务层创建失败: %v", actionType, err) + return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "创建失败: "+err.Error(), actionType, "服务层创建失败", req) + } + + logger.Infof("%s: 成功, AreaControllerID: %d, SensorType: %s", actionType, req.AreaControllerID, req.SensorType) + return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "创建成功", nil, actionType, "创建成功", req) +} + +// GetAreaThresholdAlarm godoc +// @Summary 获取区域阈值告警 +// @Description 根据任务ID获取单个区域阈值告警规则的详细信息 +// @Tags 告警管理 +// @Security BearerAuth +// @Produce json +// @Param task_id path int true "任务ID" +// @Success 200 {object} controller.Response{data=dto.AreaThresholdAlarmDTO} "成功获取区域阈值告警" +// @Router /api/v1/alarm/threshold/area/{task_id} [get] +func (t *ThresholdAlarmController) GetAreaThresholdAlarm(ctx echo.Context) error { + reqCtx, logger := logs.Trace(ctx.Request().Context(), t.ctx, "GetAreaThresholdAlarm") + const actionType = "获取区域阈值告警" + + taskID, err := strconv.Atoi(ctx.Param("task_id")) + if err != nil { + logger.Errorf("%s: 无效的任务ID: %s", actionType, ctx.Param("task_id")) + return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的任务ID", actionType, "无效的任务ID", ctx.Param("task_id")) + } + + resp, err := t.thresholdAlarmService.GetAreaThresholdAlarm(reqCtx, taskID) + if err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + logger.Warnf("%s: 任务不存在, ID: %d", actionType, taskID) + return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "任务未找到", actionType, "任务不存在", taskID) + } + logger.Errorf("%s: 服务层获取失败: %v, ID: %d", actionType, err, taskID) + return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取失败: "+err.Error(), actionType, "服务层获取失败", taskID) + } + + logger.Infof("%s: 成功, ID: %d", actionType, taskID) + return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取成功", resp, actionType, "获取成功", taskID) +} + +// UpdateAreaThresholdAlarm godoc +// @Summary 更新区域阈值告警 +// @Description 根据任务ID更新已存在的区域阈值告警规则 +// @Tags 告警管理 +// @Security BearerAuth +// @Accept json +// @Produce json +// @Param task_id path int true "任务ID" +// @Param request body dto.UpdateAreaThresholdAlarmDTO true "更新区域阈值告警请求体" +// @Success 200 {object} controller.Response "成功更新区域阈值告警" +// @Router /api/v1/alarm/threshold/area/{task_id} [put] +func (t *ThresholdAlarmController) UpdateAreaThresholdAlarm(ctx echo.Context) error { + reqCtx, logger := logs.Trace(ctx.Request().Context(), t.ctx, "UpdateAreaThresholdAlarm") + const actionType = "更新区域阈值告警" + + taskID, err := strconv.Atoi(ctx.Param("task_id")) + if err != nil { + logger.Errorf("%s: 无效的任务ID: %s", actionType, ctx.Param("task_id")) + return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的任务ID", actionType, "无效的任务ID", ctx.Param("task_id")) + } + + var req dto.UpdateAreaThresholdAlarmDTO + if err := ctx.Bind(&req); err != nil { + logger.Errorf("%s: 参数绑定失败: %v", actionType, err) + return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的请求体: "+err.Error(), actionType, "请求体绑定失败", req) + } + + if err := t.thresholdAlarmService.UpdateAreaThresholdAlarm(reqCtx, taskID, &req); err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + logger.Warnf("%s: 任务不存在, ID: %d", actionType, taskID) + return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "任务未找到", actionType, "任务不存在", taskID) + } + logger.Errorf("%s: 服务层更新失败: %v, ID: %d", actionType, err, taskID) + return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "更新失败: "+err.Error(), actionType, "服务层更新失败", taskID) + } + + logger.Infof("%s: 成功, ID: %d", actionType, taskID) + return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "更新成功", nil, actionType, "更新成功", taskID) +} + +// DeleteAreaThresholdAlarm godoc +// @Summary 删除区域阈值告警 +// @Description 根据任务ID删除区域阈值告警规则 +// @Tags 告警管理 +// @Security BearerAuth +// @Produce json +// @Param task_id path int true "任务ID" +// @Success 200 {object} controller.Response "成功删除区域阈值告警" +// @Router /api/v1/alarm/threshold/area/{task_id} [delete] +func (t *ThresholdAlarmController) DeleteAreaThresholdAlarm(ctx echo.Context) error { + reqCtx, logger := logs.Trace(ctx.Request().Context(), t.ctx, "DeleteAreaThresholdAlarm") + const actionType = "删除区域阈值告警" + + taskID, err := strconv.Atoi(ctx.Param("task_id")) + if err != nil { + logger.Errorf("%s: 无效的任务ID: %s", actionType, ctx.Param("task_id")) + return controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的任务ID", actionType, "无效的任务ID", ctx.Param("task_id")) + } + + if err := t.thresholdAlarmService.DeleteAreaThresholdAlarm(reqCtx, taskID); err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + logger.Warnf("%s: 任务不存在, ID: %d", actionType, taskID) + return controller.SendErrorWithAudit(ctx, controller.CodeNotFound, "任务未找到", actionType, "任务不存在", taskID) + } + logger.Errorf("%s: 服务层删除失败: %v, ID: %d", actionType, err, taskID) + return controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "删除失败: "+err.Error(), actionType, "服务层删除失败", taskID) + } + + logger.Infof("%s: 成功, ID: %d", actionType, taskID) + return controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "删除成功", nil, actionType, "删除成功", taskID) +}