实现controller

This commit is contained in:
2025-10-25 15:04:47 +08:00
parent f62cc1c4a9
commit 43c1839345
9 changed files with 827 additions and 20 deletions

View File

@@ -975,6 +975,149 @@ const docTemplate = `{
}
}
},
"/api/v1/monitor/notifications": {
"get": {
"security": [
{
"BearerAuth": []
}
],
"description": "根据提供的过滤条件,分页获取通知列表",
"produces": [
"application/json"
],
"tags": [
"数据监控"
],
"summary": "批量查询通知",
"parameters": [
{
"type": "string",
"name": "end_time",
"in": "query"
},
{
"enum": [
7,
-1,
0,
1,
2,
3,
4,
5,
-1,
5,
6
],
"type": "integer",
"format": "int32",
"x-enum-varnames": [
"_numLevels",
"DebugLevel",
"InfoLevel",
"WarnLevel",
"ErrorLevel",
"DPanicLevel",
"PanicLevel",
"FatalLevel",
"_minLevel",
"_maxLevel",
"InvalidLevel"
],
"name": "level",
"in": "query"
},
{
"enum": [
"邮件",
"企业微信",
"飞书",
"日志"
],
"type": "string",
"x-enum-varnames": [
"NotifierTypeSMTP",
"NotifierTypeWeChat",
"NotifierTypeLark",
"NotifierTypeLog"
],
"name": "notifier_type",
"in": "query"
},
{
"type": "string",
"name": "order_by",
"in": "query"
},
{
"type": "integer",
"name": "page",
"in": "query"
},
{
"type": "integer",
"name": "pageSize",
"in": "query"
},
{
"type": "string",
"name": "start_time",
"in": "query"
},
{
"enum": [
"发送成功",
"发送失败",
"已跳过"
],
"type": "string",
"x-enum-comments": {
"NotificationStatusFailed": "通知发送失败",
"NotificationStatusSkipped": "通知因某些原因被跳过(例如:用户未配置联系方式)",
"NotificationStatusSuccess": "通知已成功发送"
},
"x-enum-descriptions": [
"通知已成功发送",
"通知发送失败",
"通知因某些原因被跳过(例如:用户未配置联系方式)"
],
"x-enum-varnames": [
"NotificationStatusSuccess",
"NotificationStatusFailed",
"NotificationStatusSkipped"
],
"name": "status",
"in": "query"
},
{
"type": "integer",
"name": "user_id",
"in": "query"
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"allOf": [
{
"$ref": "#/definitions/controller.Response"
},
{
"type": "object",
"properties": {
"data": {
"$ref": "#/definitions/dto.ListNotificationResponse"
}
}
}
]
}
}
}
}
},
"/api/v1/monitor/pending-collections": {
"get": {
"security": [
@@ -4489,6 +4632,20 @@ const docTemplate = `{
}
}
},
"dto.ListNotificationResponse": {
"type": "object",
"properties": {
"list": {
"type": "array",
"items": {
"$ref": "#/definitions/dto.NotificationDTO"
}
},
"pagination": {
"$ref": "#/definitions/dto.PaginationDTO"
}
}
},
"dto.ListPendingCollectionResponse": {
"type": "object",
"properties": {
@@ -4797,6 +4954,47 @@ const docTemplate = `{
}
}
},
"dto.NotificationDTO": {
"type": "object",
"properties": {
"alarm_timestamp": {
"type": "string"
},
"created_at": {
"type": "string"
},
"error_message": {
"type": "string"
},
"id": {
"type": "integer"
},
"level": {
"$ref": "#/definitions/zapcore.Level"
},
"message": {
"type": "string"
},
"notifier_type": {
"$ref": "#/definitions/notify.NotifierType"
},
"status": {
"$ref": "#/definitions/models.NotificationStatus"
},
"title": {
"type": "string"
},
"to_address": {
"type": "string"
},
"updated_at": {
"type": "string"
},
"user_id": {
"type": "integer"
}
}
},
"dto.PaginationDTO": {
"type": "object",
"properties": {
@@ -6275,6 +6473,29 @@ const docTemplate = `{
"ReasonTypeHealthCare"
]
},
"models.NotificationStatus": {
"type": "string",
"enum": [
"发送成功",
"发送失败",
"已跳过"
],
"x-enum-comments": {
"NotificationStatusFailed": "通知发送失败",
"NotificationStatusSkipped": "通知因某些原因被跳过(例如:用户未配置联系方式)",
"NotificationStatusSuccess": "通知已成功发送"
},
"x-enum-descriptions": [
"通知已成功发送",
"通知发送失败",
"通知因某些原因被跳过(例如:用户未配置联系方式)"
],
"x-enum-varnames": [
"NotificationStatusSuccess",
"NotificationStatusFailed",
"NotificationStatusSkipped"
]
},
"models.PenStatus": {
"type": "string",
"enum": [
@@ -6608,10 +6829,10 @@ const docTemplate = `{
"notify.NotifierType": {
"type": "string",
"enum": [
"smtp",
"wechat",
"lark",
"log"
"邮件",
"企业微信",
"飞书",
"日志"
],
"x-enum-varnames": [
"NotifierTypeSMTP",
@@ -6619,6 +6840,36 @@ const docTemplate = `{
"NotifierTypeLark",
"NotifierTypeLog"
]
},
"zapcore.Level": {
"type": "integer",
"format": "int32",
"enum": [
7,
-1,
0,
1,
2,
3,
4,
5,
-1,
5,
6
],
"x-enum-varnames": [
"_numLevels",
"DebugLevel",
"InfoLevel",
"WarnLevel",
"ErrorLevel",
"DPanicLevel",
"PanicLevel",
"FatalLevel",
"_minLevel",
"_maxLevel",
"InvalidLevel"
]
}
},
"securityDefinitions": {

View File

@@ -967,6 +967,149 @@
}
}
},
"/api/v1/monitor/notifications": {
"get": {
"security": [
{
"BearerAuth": []
}
],
"description": "根据提供的过滤条件,分页获取通知列表",
"produces": [
"application/json"
],
"tags": [
"数据监控"
],
"summary": "批量查询通知",
"parameters": [
{
"type": "string",
"name": "end_time",
"in": "query"
},
{
"enum": [
7,
-1,
0,
1,
2,
3,
4,
5,
-1,
5,
6
],
"type": "integer",
"format": "int32",
"x-enum-varnames": [
"_numLevels",
"DebugLevel",
"InfoLevel",
"WarnLevel",
"ErrorLevel",
"DPanicLevel",
"PanicLevel",
"FatalLevel",
"_minLevel",
"_maxLevel",
"InvalidLevel"
],
"name": "level",
"in": "query"
},
{
"enum": [
"邮件",
"企业微信",
"飞书",
"日志"
],
"type": "string",
"x-enum-varnames": [
"NotifierTypeSMTP",
"NotifierTypeWeChat",
"NotifierTypeLark",
"NotifierTypeLog"
],
"name": "notifier_type",
"in": "query"
},
{
"type": "string",
"name": "order_by",
"in": "query"
},
{
"type": "integer",
"name": "page",
"in": "query"
},
{
"type": "integer",
"name": "pageSize",
"in": "query"
},
{
"type": "string",
"name": "start_time",
"in": "query"
},
{
"enum": [
"发送成功",
"发送失败",
"已跳过"
],
"type": "string",
"x-enum-comments": {
"NotificationStatusFailed": "通知发送失败",
"NotificationStatusSkipped": "通知因某些原因被跳过(例如:用户未配置联系方式)",
"NotificationStatusSuccess": "通知已成功发送"
},
"x-enum-descriptions": [
"通知已成功发送",
"通知发送失败",
"通知因某些原因被跳过(例如:用户未配置联系方式)"
],
"x-enum-varnames": [
"NotificationStatusSuccess",
"NotificationStatusFailed",
"NotificationStatusSkipped"
],
"name": "status",
"in": "query"
},
{
"type": "integer",
"name": "user_id",
"in": "query"
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"allOf": [
{
"$ref": "#/definitions/controller.Response"
},
{
"type": "object",
"properties": {
"data": {
"$ref": "#/definitions/dto.ListNotificationResponse"
}
}
}
]
}
}
}
}
},
"/api/v1/monitor/pending-collections": {
"get": {
"security": [
@@ -4481,6 +4624,20 @@
}
}
},
"dto.ListNotificationResponse": {
"type": "object",
"properties": {
"list": {
"type": "array",
"items": {
"$ref": "#/definitions/dto.NotificationDTO"
}
},
"pagination": {
"$ref": "#/definitions/dto.PaginationDTO"
}
}
},
"dto.ListPendingCollectionResponse": {
"type": "object",
"properties": {
@@ -4789,6 +4946,47 @@
}
}
},
"dto.NotificationDTO": {
"type": "object",
"properties": {
"alarm_timestamp": {
"type": "string"
},
"created_at": {
"type": "string"
},
"error_message": {
"type": "string"
},
"id": {
"type": "integer"
},
"level": {
"$ref": "#/definitions/zapcore.Level"
},
"message": {
"type": "string"
},
"notifier_type": {
"$ref": "#/definitions/notify.NotifierType"
},
"status": {
"$ref": "#/definitions/models.NotificationStatus"
},
"title": {
"type": "string"
},
"to_address": {
"type": "string"
},
"updated_at": {
"type": "string"
},
"user_id": {
"type": "integer"
}
}
},
"dto.PaginationDTO": {
"type": "object",
"properties": {
@@ -6267,6 +6465,29 @@
"ReasonTypeHealthCare"
]
},
"models.NotificationStatus": {
"type": "string",
"enum": [
"发送成功",
"发送失败",
"已跳过"
],
"x-enum-comments": {
"NotificationStatusFailed": "通知发送失败",
"NotificationStatusSkipped": "通知因某些原因被跳过(例如:用户未配置联系方式)",
"NotificationStatusSuccess": "通知已成功发送"
},
"x-enum-descriptions": [
"通知已成功发送",
"通知发送失败",
"通知因某些原因被跳过(例如:用户未配置联系方式)"
],
"x-enum-varnames": [
"NotificationStatusSuccess",
"NotificationStatusFailed",
"NotificationStatusSkipped"
]
},
"models.PenStatus": {
"type": "string",
"enum": [
@@ -6600,10 +6821,10 @@
"notify.NotifierType": {
"type": "string",
"enum": [
"smtp",
"wechat",
"lark",
"log"
"邮件",
"企业微信",
"飞书",
"日志"
],
"x-enum-varnames": [
"NotifierTypeSMTP",
@@ -6611,6 +6832,36 @@
"NotifierTypeLark",
"NotifierTypeLog"
]
},
"zapcore.Level": {
"type": "integer",
"format": "int32",
"enum": [
7,
-1,
0,
1,
2,
3,
4,
5,
-1,
5,
6
],
"x-enum-varnames": [
"_numLevels",
"DebugLevel",
"InfoLevel",
"WarnLevel",
"ErrorLevel",
"DPanicLevel",
"PanicLevel",
"FatalLevel",
"_minLevel",
"_maxLevel",
"InvalidLevel"
]
}
},
"securityDefinitions": {

View File

@@ -349,6 +349,15 @@ definitions:
pagination:
$ref: '#/definitions/dto.PaginationDTO'
type: object
dto.ListNotificationResponse:
properties:
list:
items:
$ref: '#/definitions/dto.NotificationDTO'
type: array
pagination:
$ref: '#/definitions/dto.PaginationDTO'
type: object
dto.ListPendingCollectionResponse:
properties:
list:
@@ -552,6 +561,33 @@ definitions:
- quantity
- toPenID
type: object
dto.NotificationDTO:
properties:
alarm_timestamp:
type: string
created_at:
type: string
error_message:
type: string
id:
type: integer
level:
$ref: '#/definitions/zapcore.Level'
message:
type: string
notifier_type:
$ref: '#/definitions/notify.NotifierType'
status:
$ref: '#/definitions/models.NotificationStatus'
title:
type: string
to_address:
type: string
updated_at:
type: string
user_id:
type: integer
type: object
dto.PaginationDTO:
properties:
page:
@@ -1557,6 +1593,24 @@ definitions:
- ReasonTypePreventive
- ReasonTypeTreatment
- ReasonTypeHealthCare
models.NotificationStatus:
enum:
- 发送成功
- 发送失败
- 已跳过
type: string
x-enum-comments:
NotificationStatusFailed: 通知发送失败
NotificationStatusSkipped: 通知因某些原因被跳过(例如:用户未配置联系方式)
NotificationStatusSuccess: 通知已成功发送
x-enum-descriptions:
- 通知已成功发送
- 通知发送失败
- 通知因某些原因被跳过(例如:用户未配置联系方式)
x-enum-varnames:
- NotificationStatusSuccess
- NotificationStatusFailed
- NotificationStatusSkipped
models.PenStatus:
enum:
- 空闲
@@ -1827,16 +1881,43 @@ definitions:
type: object
notify.NotifierType:
enum:
- smtp
- wechat
- lark
- log
- 邮件
- 企业微信
- 飞书
- 日志
type: string
x-enum-varnames:
- NotifierTypeSMTP
- NotifierTypeWeChat
- NotifierTypeLark
- NotifierTypeLog
zapcore.Level:
enum:
- 7
- -1
- 0
- 1
- 2
- 3
- 4
- 5
- -1
- 5
- 6
format: int32
type: integer
x-enum-varnames:
- _numLevels
- DebugLevel
- InfoLevel
- WarnLevel
- ErrorLevel
- DPanicLevel
- PanicLevel
- FatalLevel
- _minLevel
- _maxLevel
- InvalidLevel
info:
contact:
email: divano@example.com
@@ -2400,6 +2481,105 @@ paths:
summary: 获取用药记录列表
tags:
- 数据监控
/api/v1/monitor/notifications:
get:
description: 根据提供的过滤条件,分页获取通知列表
parameters:
- in: query
name: end_time
type: string
- enum:
- 7
- -1
- 0
- 1
- 2
- 3
- 4
- 5
- -1
- 5
- 6
format: int32
in: query
name: level
type: integer
x-enum-varnames:
- _numLevels
- DebugLevel
- InfoLevel
- WarnLevel
- ErrorLevel
- DPanicLevel
- PanicLevel
- FatalLevel
- _minLevel
- _maxLevel
- InvalidLevel
- enum:
- 邮件
- 企业微信
- 飞书
- 日志
in: query
name: notifier_type
type: string
x-enum-varnames:
- NotifierTypeSMTP
- NotifierTypeWeChat
- NotifierTypeLark
- NotifierTypeLog
- in: query
name: order_by
type: string
- in: query
name: page
type: integer
- in: query
name: pageSize
type: integer
- in: query
name: start_time
type: string
- enum:
- 发送成功
- 发送失败
- 已跳过
in: query
name: status
type: string
x-enum-comments:
NotificationStatusFailed: 通知发送失败
NotificationStatusSkipped: 通知因某些原因被跳过(例如:用户未配置联系方式)
NotificationStatusSuccess: 通知已成功发送
x-enum-descriptions:
- 通知已成功发送
- 通知发送失败
- 通知因某些原因被跳过(例如:用户未配置联系方式)
x-enum-varnames:
- NotificationStatusSuccess
- NotificationStatusFailed
- NotificationStatusSkipped
- in: query
name: user_id
type: integer
produces:
- application/json
responses:
"200":
description: OK
schema:
allOf:
- $ref: '#/definitions/controller.Response'
- properties:
data:
$ref: '#/definitions/dto.ListNotificationResponse'
type: object
security:
- BearerAuth: []
summary: 批量查询通知
tags:
- 数据监控
/api/v1/monitor/pending-collections:
get:
description: 根据提供的过滤条件,分页获取待采集请求

View File

@@ -176,6 +176,7 @@ func (a *API) setupRoutes() {
monitorGroup.GET("/pig-sick-logs", a.monitorController.ListPigSickLogs)
monitorGroup.GET("/pig-purchases", a.monitorController.ListPigPurchases)
monitorGroup.GET("/pig-sales", a.monitorController.ListPigSales)
monitorGroup.GET("/notifications", a.monitorController.ListNotifications)
}
a.logger.Info("数据监控相关接口注册成功 (需要认证和审计)")
}

View File

@@ -839,3 +839,50 @@ func (c *Controller) ListPigSales(ctx *gin.Context) {
c.logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(data), total)
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取猪只售卖记录成功", resp, actionType, "获取猪只售卖记录成功", req)
}
// ListNotifications godoc
// @Summary 批量查询通知
// @Description 根据提供的过滤条件,分页获取通知列表
// @Tags 数据监控
// @Security BearerAuth
// @Produce json
// @Param query query dto.ListNotificationRequest true "查询参数"
// @Success 200 {object} controller.Response{data=dto.ListNotificationResponse}
// @Router /api/v1/monitor/notifications [get]
func (c *Controller) ListNotifications(ctx *gin.Context) {
const actionType = "批量查询通知"
var req dto.ListNotificationRequest
if err := ctx.ShouldBindQuery(&req); err != nil {
c.logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "参数绑定失败", req)
return
}
opts := repository.NotificationListOptions{
UserID: req.UserID,
NotifierType: req.NotifierType,
Level: req.Level,
StartTime: req.StartTime,
EndTime: req.EndTime,
OrderBy: req.OrderBy,
Status: req.Status,
}
data, total, err := c.monitorService.ListNotifications(opts, req.Page, req.PageSize)
if err != nil {
if errors.Is(err, repository.ErrInvalidPagination) {
c.logger.Warnf("%s: 无效的分页参数: %v", actionType, err)
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的分页参数: "+err.Error(), actionType, "无效分页参数", req)
return
}
c.logger.Errorf("%s: 服务层查询失败: %v", actionType, err)
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "批量查询通知失败: "+err.Error(), actionType, "服务层查询失败", req)
return
}
resp := dto.NewListNotificationResponse(data, total, req.Page, req.PageSize)
c.logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(data), total)
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "批量查询通知成功", resp, actionType, "批量查询通知成功", req)
}

View File

@@ -0,0 +1,35 @@
package dto
import (
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
)
// NewListNotificationResponse 从模型数据创建通知列表响应 DTO
func NewListNotificationResponse(data []models.Notification, total int64, page, pageSize int) *ListNotificationResponse {
dtos := make([]NotificationDTO, len(data))
for i, item := range data {
dtos[i] = NotificationDTO{
ID: item.ID,
CreatedAt: item.CreatedAt,
UpdatedAt: item.UpdatedAt,
NotifierType: item.NotifierType,
UserID: item.UserID,
Title: item.Title,
Message: item.Message,
Level: item.Level,
AlarmTimestamp: item.AlarmTimestamp,
ToAddress: item.ToAddress,
Status: item.Status,
ErrorMessage: item.ErrorMessage,
}
}
return &ListNotificationResponse{
List: dtos,
Pagination: PaginationDTO{
Total: total,
Page: page,
PageSize: pageSize,
},
}
}

View File

@@ -1,9 +1,50 @@
package dto
import "git.huangwc.com/pig/pig-farm-controller/internal/infra/notify"
import (
"time"
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
"git.huangwc.com/pig/pig-farm-controller/internal/infra/notify"
"go.uber.org/zap/zapcore"
)
// SendTestNotificationRequest 定义了发送测试通知请求的 JSON 结构
type SendTestNotificationRequest struct {
// Type 指定要测试的通知渠道
Type notify.NotifierType `json:"type" binding:"required"`
}
// ListNotificationRequest 定义了获取通知列表的请求参数
type ListNotificationRequest struct {
Page int `form:"page,default=1"`
PageSize int `form:"pageSize,default=10"`
UserID *uint `form:"user_id"`
NotifierType *notify.NotifierType `form:"notifier_type"`
Status *models.NotificationStatus `form:"status"`
Level *zapcore.Level `form:"level"`
StartTime *time.Time `form:"start_time"`
EndTime *time.Time `form:"end_time"`
OrderBy string `form:"order_by"`
}
// NotificationDTO 是用于API响应的通知结构
type NotificationDTO struct {
ID uint `json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
NotifierType notify.NotifierType `json:"notifier_type"`
UserID uint `json:"user_id"`
Title string `json:"title"`
Message string `json:"message"`
Level zapcore.Level `json:"level"`
AlarmTimestamp time.Time `json:"alarm_timestamp"`
ToAddress string `json:"to_address"`
Status models.NotificationStatus `json:"status"`
ErrorMessage string `json:"error_message"`
}
// ListNotificationResponse 是获取通知列表的响应结构
type ListNotificationResponse struct {
List []NotificationDTO `json:"list"`
Pagination PaginationDTO `json:"pagination"`
}

View File

@@ -118,6 +118,7 @@ func NewApplication(configPath string) (*Application, error) {
pigTransferLogRepo,
pigSickPigLogRepo,
pigTradeRepo,
notificationRepo,
)
// 初始化审计服务

View File

@@ -11,13 +11,13 @@ import (
// NotificationListOptions 定义了查询通知列表时的可选参数
type NotificationListOptions struct {
UserID *uint // 按用户ID过滤
NotifierType *notify.NotifierType // 按通知器类型过滤
Status *string // 按通知状态过滤 (例如:"success", "failed", "pending")
Level *zapcore.Level // 按通知等级过滤 (例如:"info", "warning", "error")
StartTime *time.Time // 通知内容生成时间范围 - 开始时间 (对应 AlarmTimestamp)
EndTime *time.Time // 通知内容生成时间范围 - 结束时间 (对应 AlarmTimestamp)
OrderBy string // 排序字段,例如 "alarm_timestamp DESC"
UserID *uint // 按用户ID过滤
NotifierType *notify.NotifierType // 按通知器类型过滤
Status *models.NotificationStatus // 按通知状态过滤 (例如:"success", "failed")
Level *zapcore.Level // 按通知等级过滤 (例如:"info", "warning", "error")
StartTime *time.Time // 通知内容生成时间范围 - 开始时间 (对应 AlarmTimestamp)
EndTime *time.Time // 通知内容生成时间范围 - 结束时间 (对应 AlarmTimestamp)
OrderBy string // 排序字段,例如 "alarm_timestamp DESC"
}
// NotificationRepository 定义了与通知记录相关的数据库操作接口。