ListUserActionLogs
This commit is contained in:
@@ -263,3 +263,53 @@ func (c *Controller) ListPendingCollections(ctx *gin.Context) {
|
|||||||
c.logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(data), total)
|
c.logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(data), total)
|
||||||
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取待采集请求成功", resp, actionType, "获取待采集请求成功", req)
|
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取待采集请求成功", resp, actionType, "获取待采集请求成功", req)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ListUserActionLogs godoc
|
||||||
|
// @Summary 获取用户操作日志列表
|
||||||
|
// @Description 根据提供的过滤条件,分页获取用户操作日志
|
||||||
|
// @Tags 数据监控
|
||||||
|
// @Security BearerAuth
|
||||||
|
// @Produce json
|
||||||
|
// @Param query query dto.ListUserActionLogRequest true "查询参数"
|
||||||
|
// @Success 200 {object} controller.Response{data=dto.ListUserActionLogResponse}
|
||||||
|
// @Router /api/v1/monitor/user-action-logs [get]
|
||||||
|
func (c *Controller) ListUserActionLogs(ctx *gin.Context) {
|
||||||
|
const actionType = "获取用户操作日志列表"
|
||||||
|
|
||||||
|
var req dto.ListUserActionLogRequest
|
||||||
|
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.UserActionLogListOptions{
|
||||||
|
UserID: req.UserID,
|
||||||
|
Username: req.Username,
|
||||||
|
ActionType: req.ActionType,
|
||||||
|
OrderBy: req.OrderBy,
|
||||||
|
StartTime: req.StartTime,
|
||||||
|
EndTime: req.EndTime,
|
||||||
|
}
|
||||||
|
if req.Status != nil {
|
||||||
|
status := models.AuditStatus(*req.Status)
|
||||||
|
opts.Status = &status
|
||||||
|
}
|
||||||
|
|
||||||
|
data, total, err := c.monitorService.ListUserActionLogs(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.NewListUserActionLogResponse(data, total, req.Page, req.PageSize)
|
||||||
|
c.logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(data), total)
|
||||||
|
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取用户操作日志成功", resp, actionType, "获取用户操作日志成功", req)
|
||||||
|
}
|
||||||
|
|||||||
@@ -165,13 +165,11 @@ func (c *Controller) ListUserHistory(ctx *gin.Context) {
|
|||||||
|
|
||||||
// 3. 调用审计仓库层获取历史数据
|
// 3. 调用审计仓库层获取历史数据
|
||||||
id := uint(userID)
|
id := uint(userID)
|
||||||
findOptions := repository.FindAuditLogOptions{
|
findOptions := repository.UserActionLogListOptions{
|
||||||
UserID: &id,
|
UserID: &id,
|
||||||
ActionType: actionTypeFilter,
|
ActionType: &actionTypeFilter,
|
||||||
Page: page,
|
|
||||||
PageSize: pageSize,
|
|
||||||
}
|
}
|
||||||
logs, total, err := c.auditRepo.List(findOptions)
|
l, total, err := c.auditRepo.List(findOptions, page, pageSize)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.logger.Errorf("%s: 查询历史记录失败: %v, Options: %+v", actionType, err, findOptions)
|
c.logger.Errorf("%s: 查询历史记录失败: %v, Options: %+v", actionType, err, findOptions)
|
||||||
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "查询历史记录失败", actionType, "查询历史记录失败", findOptions)
|
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "查询历史记录失败", actionType, "查询历史记录失败", findOptions)
|
||||||
@@ -179,8 +177,8 @@ func (c *Controller) ListUserHistory(ctx *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 4. 将数据库模型转换为响应 DTO
|
// 4. 将数据库模型转换为响应 DTO
|
||||||
historyResponses := make([]dto.HistoryResponse, 0, len(logs))
|
historyResponses := make([]dto.HistoryResponse, 0, len(l))
|
||||||
for _, log := range logs {
|
for _, log := range l {
|
||||||
historyResponses = append(historyResponses, dto.HistoryResponse{
|
historyResponses = append(historyResponses, dto.HistoryResponse{
|
||||||
UserID: log.UserID,
|
UserID: log.UserID,
|
||||||
Username: log.Username,
|
Username: log.Username,
|
||||||
|
|||||||
@@ -300,3 +300,70 @@ func NewListPendingCollectionResponse(data []models.PendingCollection, total int
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// --- UserActionLog ---
|
||||||
|
|
||||||
|
// ListUserActionLogRequest 定义了获取用户操作日志列表的请求参数
|
||||||
|
type ListUserActionLogRequest struct {
|
||||||
|
Page int `form:"page,default=1"`
|
||||||
|
PageSize int `form:"pageSize,default=10"`
|
||||||
|
UserID *uint `form:"user_id"`
|
||||||
|
Username *string `form:"username"`
|
||||||
|
ActionType *string `form:"action_type"`
|
||||||
|
Status *string `form:"status"`
|
||||||
|
StartTime *time.Time `form:"start_time" time_format:"rfc3339"`
|
||||||
|
EndTime *time.Time `form:"end_time" time_format:"rfc3339"`
|
||||||
|
OrderBy string `form:"order_by"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// UserActionLogDTO 是用于API响应的用户操作日志结构
|
||||||
|
type UserActionLogDTO struct {
|
||||||
|
ID uint `json:"id"`
|
||||||
|
Time time.Time `json:"time"`
|
||||||
|
UserID uint `json:"user_id"`
|
||||||
|
Username string `json:"username"`
|
||||||
|
SourceIP string `json:"source_ip"`
|
||||||
|
ActionType string `json:"action_type"`
|
||||||
|
TargetResource json.RawMessage `json:"target_resource"`
|
||||||
|
Description string `json:"description"`
|
||||||
|
Status models.AuditStatus `json:"status"`
|
||||||
|
HTTPPath string `json:"http_path"`
|
||||||
|
HTTPMethod string `json:"http_method"`
|
||||||
|
ResultDetails string `json:"result_details"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// ListUserActionLogResponse 是获取用户操作日志列表的响应结构
|
||||||
|
type ListUserActionLogResponse struct {
|
||||||
|
List []UserActionLogDTO `json:"list"`
|
||||||
|
Pagination PaginationDTO `json:"pagination"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewListUserActionLogResponse 从模型数据创建列表响应 DTO
|
||||||
|
func NewListUserActionLogResponse(data []models.UserActionLog, total int64, page, pageSize int) *ListUserActionLogResponse {
|
||||||
|
dtos := make([]UserActionLogDTO, len(data))
|
||||||
|
for i, item := range data {
|
||||||
|
dtos[i] = UserActionLogDTO{
|
||||||
|
ID: item.ID,
|
||||||
|
Time: item.Time,
|
||||||
|
UserID: item.UserID,
|
||||||
|
Username: item.Username,
|
||||||
|
SourceIP: item.SourceIP,
|
||||||
|
ActionType: item.ActionType,
|
||||||
|
TargetResource: json.RawMessage(item.TargetResource),
|
||||||
|
Description: item.Description,
|
||||||
|
Status: item.Status,
|
||||||
|
HTTPPath: item.HTTPPath,
|
||||||
|
HTTPMethod: item.HTTPMethod,
|
||||||
|
ResultDetails: item.ResultDetails,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return &ListUserActionLogResponse{
|
||||||
|
List: dtos,
|
||||||
|
Pagination: PaginationDTO{
|
||||||
|
Total: total,
|
||||||
|
Page: page,
|
||||||
|
PageSize: pageSize,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ type MonitorService struct {
|
|||||||
deviceCommandLogRepo repository.DeviceCommandLogRepository
|
deviceCommandLogRepo repository.DeviceCommandLogRepository
|
||||||
executionLogRepo repository.ExecutionLogRepository
|
executionLogRepo repository.ExecutionLogRepository
|
||||||
pendingCollectionRepo repository.PendingCollectionRepository
|
pendingCollectionRepo repository.PendingCollectionRepository
|
||||||
|
userActionLogRepo repository.UserActionLogRepository
|
||||||
// 在这里可以添加其他超表模型的仓库依赖
|
// 在这里可以添加其他超表模型的仓库依赖
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -20,12 +21,14 @@ func NewMonitorService(
|
|||||||
deviceCommandLogRepo repository.DeviceCommandLogRepository,
|
deviceCommandLogRepo repository.DeviceCommandLogRepository,
|
||||||
executionLogRepo repository.ExecutionLogRepository,
|
executionLogRepo repository.ExecutionLogRepository,
|
||||||
pendingCollectionRepo repository.PendingCollectionRepository,
|
pendingCollectionRepo repository.PendingCollectionRepository,
|
||||||
|
userActionLogRepo repository.UserActionLogRepository,
|
||||||
) *MonitorService {
|
) *MonitorService {
|
||||||
return &MonitorService{
|
return &MonitorService{
|
||||||
sensorDataRepo: sensorDataRepo,
|
sensorDataRepo: sensorDataRepo,
|
||||||
deviceCommandLogRepo: deviceCommandLogRepo,
|
deviceCommandLogRepo: deviceCommandLogRepo,
|
||||||
executionLogRepo: executionLogRepo,
|
executionLogRepo: executionLogRepo,
|
||||||
pendingCollectionRepo: pendingCollectionRepo,
|
pendingCollectionRepo: pendingCollectionRepo,
|
||||||
|
userActionLogRepo: userActionLogRepo,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,3 +56,8 @@ func (s *MonitorService) ListTaskExecutionLogs(opts repository.TaskExecutionLogL
|
|||||||
func (s *MonitorService) ListPendingCollections(opts repository.PendingCollectionListOptions, page, pageSize int) ([]models.PendingCollection, int64, error) {
|
func (s *MonitorService) ListPendingCollections(opts repository.PendingCollectionListOptions, page, pageSize int) ([]models.PendingCollection, int64, error) {
|
||||||
return s.pendingCollectionRepo.List(opts, page, pageSize)
|
return s.pendingCollectionRepo.List(opts, page, pageSize)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ListUserActionLogs 负责处理查询用户操作日志列表的业务逻辑
|
||||||
|
func (s *MonitorService) ListUserActionLogs(opts repository.UserActionLogListOptions, page, pageSize int) ([]models.UserActionLog, int64, error) {
|
||||||
|
return s.userActionLogRepo.List(opts, page, pageSize)
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,22 +1,27 @@
|
|||||||
package repository
|
package repository
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
"git.huangwc.com/pig/pig-farm-controller/internal/infra/models"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
// FindAuditLogOptions 定义了查询审计日志的选项
|
// UserActionLogListOptions 定义了查询用户操作日志时的可选参数
|
||||||
type FindAuditLogOptions struct {
|
type UserActionLogListOptions struct {
|
||||||
UserID *uint // 根据用户ID过滤
|
UserID *uint
|
||||||
ActionType string // 根据操作类型过滤
|
Username *string
|
||||||
Page int // 页码
|
ActionType *string
|
||||||
PageSize int // 每页大小
|
Status *models.AuditStatus
|
||||||
|
StartTime *time.Time // 基于 time 字段
|
||||||
|
EndTime *time.Time // 基于 time 字段
|
||||||
|
OrderBy string // 例如 "time asc"
|
||||||
}
|
}
|
||||||
|
|
||||||
// UserActionLogRepository 定义了与用户操作日志相关的数据库操作接口
|
// UserActionLogRepository 定义了与用户操作日志相关的数据库操作接口
|
||||||
type UserActionLogRepository interface {
|
type UserActionLogRepository interface {
|
||||||
Create(log *models.UserActionLog) error
|
Create(log *models.UserActionLog) error
|
||||||
List(options FindAuditLogOptions) ([]*models.UserActionLog, int64, error)
|
List(opts UserActionLogListOptions, page, pageSize int) ([]models.UserActionLog, int64, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// gormUserActionLogRepository 是 UserActionLogRepository 的 GORM 实现
|
// gormUserActionLogRepository 是 UserActionLogRepository 的 GORM 实现
|
||||||
@@ -35,17 +40,33 @@ func (r *gormUserActionLogRepository) Create(log *models.UserActionLog) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// List 根据选项查询用户操作日志,并返回总数
|
// List 根据选项查询用户操作日志,并返回总数
|
||||||
func (r *gormUserActionLogRepository) List(options FindAuditLogOptions) ([]*models.UserActionLog, int64, error) {
|
func (r *gormUserActionLogRepository) List(opts UserActionLogListOptions, page, pageSize int) ([]models.UserActionLog, int64, error) {
|
||||||
var logs []*models.UserActionLog
|
if page <= 0 || pageSize <= 0 {
|
||||||
|
return nil, 0, ErrInvalidPagination
|
||||||
|
}
|
||||||
|
|
||||||
|
var logs []models.UserActionLog
|
||||||
var total int64
|
var total int64
|
||||||
|
|
||||||
query := r.db.Model(&models.UserActionLog{})
|
query := r.db.Model(&models.UserActionLog{})
|
||||||
|
|
||||||
if options.UserID != nil {
|
if opts.UserID != nil {
|
||||||
query = query.Where("user_id = ?", *options.UserID)
|
query = query.Where("user_id = ?", *opts.UserID)
|
||||||
}
|
}
|
||||||
if options.ActionType != "" {
|
if opts.Username != nil {
|
||||||
query = query.Where("action_type = ?", options.ActionType)
|
query = query.Where("username LIKE ?", "%"+*opts.Username+"%")
|
||||||
|
}
|
||||||
|
if opts.ActionType != nil {
|
||||||
|
query = query.Where("action_type = ?", *opts.ActionType)
|
||||||
|
}
|
||||||
|
if opts.Status != nil {
|
||||||
|
query = query.Where("status = ?", *opts.Status)
|
||||||
|
}
|
||||||
|
if opts.StartTime != nil {
|
||||||
|
query = query.Where("time >= ?", *opts.StartTime)
|
||||||
|
}
|
||||||
|
if opts.EndTime != nil {
|
||||||
|
query = query.Where("time <= ?", *opts.EndTime)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 统计总数
|
// 统计总数
|
||||||
@@ -53,16 +74,14 @@ func (r *gormUserActionLogRepository) List(options FindAuditLogOptions) ([]*mode
|
|||||||
return nil, 0, err
|
return nil, 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// 分页查询
|
orderBy := "time DESC"
|
||||||
if options.Page > 0 && options.PageSize > 0 {
|
if opts.OrderBy != "" {
|
||||||
offset := (options.Page - 1) * options.PageSize
|
orderBy = opts.OrderBy
|
||||||
query = query.Offset(offset).Limit(options.PageSize)
|
|
||||||
}
|
}
|
||||||
|
query = query.Order(orderBy)
|
||||||
|
|
||||||
// 默认按创建时间倒序
|
offset := (page - 1) * pageSize
|
||||||
query = query.Order("created_at DESC")
|
if err := query.Limit(pageSize).Offset(offset).Find(&logs).Error; err != nil {
|
||||||
|
|
||||||
if err := query.Find(&logs).Error; err != nil {
|
|
||||||
return nil, 0, err
|
return nil, 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user