- Remove BOM from all Go source files - Standardize import ordering and whitespace across handlers and services - Replace PostgreSQL full-text search with ILIKE pattern matching in post and user repositories - Enhance JPush client with TLS transport, rate limit monitoring, and simplified API - Refactor PushService to include userID in device management methods - Add MaxDevicesPerUser limit and extract helper functions for push payload construction
174 lines
7.2 KiB
Go
174 lines
7.2 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"with_you/internal/dto"
|
|
"with_you/internal/model"
|
|
"with_you/internal/repository"
|
|
)
|
|
|
|
// OperationLogService 操作日志服务接口
|
|
type OperationLogService interface {
|
|
RecordOperation(log *model.OperationLog)
|
|
GetOperationLogs(ctx context.Context, filters dto.LogFilter, page, pageSize int) ([]*model.OperationLog, int64, error)
|
|
GetOperationLogsByUser(ctx context.Context, userID string, page, pageSize int) ([]*model.OperationLog, int64, error)
|
|
GetOperationLogsByTimeRange(ctx context.Context, startTime, endTime string, page, pageSize int) ([]*model.OperationLog, int64, error)
|
|
GetStatistics(ctx context.Context, startTime, endTime string) (*repository.OperationLogStatistics, error)
|
|
}
|
|
|
|
// operationLogServiceImpl 操作日志服务实现
|
|
type operationLogServiceImpl struct {
|
|
asyncManager *AsyncLogManager
|
|
repo repository.OperationLogRepository
|
|
}
|
|
|
|
// NewOperationLogService 创建操作日志服务
|
|
func NewOperationLogService(
|
|
asyncManager *AsyncLogManager,
|
|
repo repository.OperationLogRepository,
|
|
) OperationLogService {
|
|
return &operationLogServiceImpl{
|
|
asyncManager: asyncManager,
|
|
repo: repo,
|
|
}
|
|
}
|
|
|
|
// RecordOperation 记录操作日志
|
|
func (s *operationLogServiceImpl) RecordOperation(log *model.OperationLog) {
|
|
s.asyncManager.RecordOperation(log)
|
|
}
|
|
|
|
// GetOperationLogs 获取操作日志列表
|
|
func (s *operationLogServiceImpl) GetOperationLogs(ctx context.Context, filters dto.LogFilter, page, pageSize int) ([]*model.OperationLog, int64, error) {
|
|
return s.repo.GetOperationLogs(ctx, filters, page, pageSize)
|
|
}
|
|
|
|
// GetOperationLogsByUser 获取指定用户的操作日志
|
|
func (s *operationLogServiceImpl) GetOperationLogsByUser(ctx context.Context, userID string, page, pageSize int) ([]*model.OperationLog, int64, error) {
|
|
return s.repo.GetOperationLogsByUser(ctx, userID, page, pageSize)
|
|
}
|
|
|
|
// GetOperationLogsByTimeRange 按时间范围获取操作日志
|
|
func (s *operationLogServiceImpl) GetOperationLogsByTimeRange(ctx context.Context, startTime, endTime string, page, pageSize int) ([]*model.OperationLog, int64, error) {
|
|
return s.repo.GetOperationLogsByTimeRange(ctx, startTime, endTime, page, pageSize)
|
|
}
|
|
|
|
// GetStatistics 获取操作日志统计
|
|
func (s *operationLogServiceImpl) GetStatistics(ctx context.Context, startTime, endTime string) (*repository.OperationLogStatistics, error) {
|
|
return s.repo.GetStatistics(ctx, startTime, endTime)
|
|
}
|
|
|
|
// LoginLogService 登录日志服务接口
|
|
type LoginLogService interface {
|
|
RecordLogin(log *model.LoginLog)
|
|
GetLoginLogs(ctx context.Context, filters dto.LoginFilter, page, pageSize int) ([]*model.LoginLog, int64, error)
|
|
GetLoginLogsByUser(ctx context.Context, userID string, page, pageSize int) ([]*model.LoginLog, int64, error)
|
|
GetFailedLoginsByIP(ctx context.Context, ip string, timeWindow string) (int64, error)
|
|
GetRecentSuccessLogins(ctx context.Context, userID string, limit int) ([]*model.LoginLog, error)
|
|
GetStatistics(ctx context.Context, startTime, endTime string) (*repository.LoginLogStatistics, error)
|
|
}
|
|
|
|
// loginLogServiceImpl 登录日志服务实现
|
|
type loginLogServiceImpl struct {
|
|
asyncManager *AsyncLogManager
|
|
repo repository.LoginLogRepository
|
|
}
|
|
|
|
// NewLoginLogService 创建登录日志服务
|
|
func NewLoginLogService(
|
|
asyncManager *AsyncLogManager,
|
|
repo repository.LoginLogRepository,
|
|
) LoginLogService {
|
|
return &loginLogServiceImpl{
|
|
asyncManager: asyncManager,
|
|
repo: repo,
|
|
}
|
|
}
|
|
|
|
// RecordLogin 记录登录日志
|
|
func (s *loginLogServiceImpl) RecordLogin(log *model.LoginLog) {
|
|
s.asyncManager.RecordLogin(log)
|
|
}
|
|
|
|
// GetLoginLogs 获取登录日志列表
|
|
func (s *loginLogServiceImpl) GetLoginLogs(ctx context.Context, filters dto.LoginFilter, page, pageSize int) ([]*model.LoginLog, int64, error) {
|
|
return s.repo.GetLoginLogs(ctx, filters, page, pageSize)
|
|
}
|
|
|
|
// GetLoginLogsByUser 获取指定用户的登录日志
|
|
func (s *loginLogServiceImpl) GetLoginLogsByUser(ctx context.Context, userID string, page, pageSize int) ([]*model.LoginLog, int64, error) {
|
|
return s.repo.GetLoginLogsByUser(ctx, userID, page, pageSize)
|
|
}
|
|
|
|
// GetFailedLoginsByIP 获取指定IP的失败登录记录
|
|
func (s *loginLogServiceImpl) GetFailedLoginsByIP(ctx context.Context, ip string, timeWindow string) (int64, error) {
|
|
duration, err := time.ParseDuration(timeWindow)
|
|
if err != nil {
|
|
duration = time.Hour // 默认1小时
|
|
}
|
|
return s.repo.GetFailedLoginsByIP(ctx, ip, duration)
|
|
}
|
|
|
|
// GetRecentSuccessLogins 获取最近成功的登录记录
|
|
func (s *loginLogServiceImpl) GetRecentSuccessLogins(ctx context.Context, userID string, limit int) ([]*model.LoginLog, error) {
|
|
return s.repo.GetRecentSuccessLogins(ctx, userID, limit)
|
|
}
|
|
|
|
// GetStatistics 获取登录日志统计
|
|
func (s *loginLogServiceImpl) GetStatistics(ctx context.Context, startTime, endTime string) (*repository.LoginLogStatistics, error) {
|
|
return s.repo.GetStatistics(ctx, startTime, endTime)
|
|
}
|
|
|
|
// DataChangeLogService 数据变更日志服务接口
|
|
type DataChangeLogService interface {
|
|
RecordDataChange(log *model.DataChangeLog)
|
|
GetDataChangeLogs(ctx context.Context, filters dto.DataChangeFilter, page, pageSize int) ([]*model.DataChangeLog, int64, error)
|
|
GetDataChangeLogsByUser(ctx context.Context, userID string, page, pageSize int) ([]*model.DataChangeLog, int64, error)
|
|
GetDataChangeLogsByOperator(ctx context.Context, operatorID string, page, pageSize int) ([]*model.DataChangeLog, int64, error)
|
|
GetStatistics(ctx context.Context, startTime, endTime string) (*repository.DataChangeLogStatistics, error)
|
|
}
|
|
|
|
// dataChangeLogServiceImpl 数据变更日志服务实现
|
|
type dataChangeLogServiceImpl struct {
|
|
asyncManager *AsyncLogManager
|
|
repo repository.DataChangeLogRepository
|
|
}
|
|
|
|
// NewDataChangeLogService 创建数据变更日志服务
|
|
func NewDataChangeLogService(
|
|
asyncManager *AsyncLogManager,
|
|
repo repository.DataChangeLogRepository,
|
|
) DataChangeLogService {
|
|
return &dataChangeLogServiceImpl{
|
|
asyncManager: asyncManager,
|
|
repo: repo,
|
|
}
|
|
}
|
|
|
|
// RecordDataChange 记录数据变更日志
|
|
func (s *dataChangeLogServiceImpl) RecordDataChange(log *model.DataChangeLog) {
|
|
s.asyncManager.RecordDataChange(log)
|
|
}
|
|
|
|
// GetDataChangeLogs 获取数据变更日志列表
|
|
func (s *dataChangeLogServiceImpl) GetDataChangeLogs(ctx context.Context, filters dto.DataChangeFilter, page, pageSize int) ([]*model.DataChangeLog, int64, error) {
|
|
return s.repo.GetDataChangeLogs(ctx, filters, page, pageSize)
|
|
}
|
|
|
|
// GetDataChangeLogsByUser 获取指定用户的数据变更日志
|
|
func (s *dataChangeLogServiceImpl) GetDataChangeLogsByUser(ctx context.Context, userID string, page, pageSize int) ([]*model.DataChangeLog, int64, error) {
|
|
return s.repo.GetDataChangeLogsByUser(ctx, userID, page, pageSize)
|
|
}
|
|
|
|
// GetDataChangeLogsByOperator 获取指定操作人的日志
|
|
func (s *dataChangeLogServiceImpl) GetDataChangeLogsByOperator(ctx context.Context, operatorID string, page, pageSize int) ([]*model.DataChangeLog, int64, error) {
|
|
return s.repo.GetDataChangeLogsByOperator(ctx, operatorID, page, pageSize)
|
|
}
|
|
|
|
// GetStatistics 获取数据变更日志统计
|
|
func (s *dataChangeLogServiceImpl) GetStatistics(ctx context.Context, startTime, endTime string) (*repository.DataChangeLogStatistics, error) {
|
|
return s.repo.GetStatistics(ctx, startTime, endTime)
|
|
}
|