mirror of
https://github.com/skyle1995/NetworkAuth.git
synced 2026-05-25 02:24:05 +08:00
Add classification annotations
This commit is contained in:
@@ -1,85 +1,101 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"networkDev/models"
|
||||
"networkDev/utils"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// FindSettingByName 根据名称查找设置
|
||||
// name: 设置名称
|
||||
// db: 数据库连接
|
||||
// 返回: 设置信息和错误
|
||||
func FindSettingByName(name string, db *gorm.DB) (*models.Settings, error) {
|
||||
key := fmt.Sprintf("setting:%s", name)
|
||||
return utils.RedisGetOrSet(context.Background(), key, 5*time.Minute, func() (*models.Settings, error) {
|
||||
var setting models.Settings
|
||||
err := db.Where("name = ?", name).First(&setting).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &setting, nil
|
||||
})
|
||||
}
|
||||
|
||||
// UpdateEntityByID 根据ID更新实体
|
||||
// model: 模型类型
|
||||
// id: 实体ID
|
||||
// updates: 更新字段
|
||||
// db: 数据库连接
|
||||
// 返回: 错误
|
||||
func UpdateEntityByID(model interface{}, id uint, updates map[string]interface{}, db *gorm.DB) error {
|
||||
return db.Model(model).Where("id = ?", id).Updates(updates).Error
|
||||
}
|
||||
|
||||
// BatchUpdateEntityStatus 批量更新实体状态
|
||||
// model: 模型类型
|
||||
// ids: 实体ID列表
|
||||
// status: 新状态
|
||||
// db: 数据库连接
|
||||
// 返回: 错误
|
||||
func BatchUpdateEntityStatus(model interface{}, ids []uint, status int, db *gorm.DB) error {
|
||||
if len(ids) == 0 {
|
||||
return nil
|
||||
}
|
||||
return db.Model(model).Where("id IN ?", ids).Update("status", status).Error
|
||||
}
|
||||
|
||||
// CountEntitiesByCondition 根据条件统计实体数量
|
||||
// model: 模型类型
|
||||
// condition: 查询条件
|
||||
// db: 数据库连接
|
||||
// args: 查询参数
|
||||
// 返回: 数量和错误
|
||||
func CountEntitiesByCondition(model interface{}, condition string, db *gorm.DB, args ...interface{}) (int64, error) {
|
||||
var count int64
|
||||
err := db.Model(model).Where(condition, args...).Count(&count).Error
|
||||
return count, err
|
||||
}
|
||||
|
||||
// FindEntitiesByCondition 根据条件查找实体
|
||||
// model: 模型类型
|
||||
// result: 结果容器
|
||||
// condition: 查询条件
|
||||
// db: 数据库连接
|
||||
// args: 查询参数
|
||||
// 返回: 错误
|
||||
func FindEntitiesByCondition(model interface{}, result interface{}, condition string, db *gorm.DB, args ...interface{}) error {
|
||||
return db.Model(model).Where(condition, args...).Find(result).Error
|
||||
}
|
||||
|
||||
// CheckEntityExists 检查实体是否存在
|
||||
// model: 模型类型
|
||||
// condition: 查询条件
|
||||
// db: 数据库连接
|
||||
// args: 查询参数
|
||||
// 返回: 是否存在和错误
|
||||
func CheckEntityExists(model interface{}, condition string, db *gorm.DB, args ...interface{}) (bool, error) {
|
||||
var count int64
|
||||
err := db.Model(model).Where(condition, args...).Count(&count).Error
|
||||
return count > 0, err
|
||||
}
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"networkDev/models"
|
||||
"networkDev/utils"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// ============================================================================
|
||||
// 查询函数
|
||||
// ============================================================================
|
||||
|
||||
// FindSettingByName 根据名称查找设置
|
||||
// name: 设置名称
|
||||
// db: 数据库连接
|
||||
// 返回: 设置信息和错误
|
||||
func FindSettingByName(name string, db *gorm.DB) (*models.Settings, error) {
|
||||
key := fmt.Sprintf("setting:%s", name)
|
||||
return utils.RedisGetOrSet(context.Background(), key, 5*time.Minute, func() (*models.Settings, error) {
|
||||
var setting models.Settings
|
||||
err := db.Where("name = ?", name).First(&setting).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &setting, nil
|
||||
})
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// 更新函数
|
||||
// ============================================================================
|
||||
|
||||
// UpdateEntityByID 根据ID更新实体
|
||||
// model: 模型类型
|
||||
// id: 实体ID
|
||||
// updates: 更新字段
|
||||
// db: 数据库连接
|
||||
// 返回: 错误
|
||||
func UpdateEntityByID(model interface{}, id uint, updates map[string]interface{}, db *gorm.DB) error {
|
||||
return db.Model(model).Where("id = ?", id).Updates(updates).Error
|
||||
}
|
||||
|
||||
// BatchUpdateEntityStatus 批量更新实体状态
|
||||
// model: 模型类型
|
||||
// ids: 实体ID列表
|
||||
// status: 新状态
|
||||
// db: 数据库连接
|
||||
// 返回: 错误
|
||||
func BatchUpdateEntityStatus(model interface{}, ids []uint, status int, db *gorm.DB) error {
|
||||
if len(ids) == 0 {
|
||||
return nil
|
||||
}
|
||||
return db.Model(model).Where("id IN ?", ids).Update("status", status).Error
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// 统计函数
|
||||
// ============================================================================
|
||||
|
||||
// CountEntitiesByCondition 根据条件统计实体数量
|
||||
// model: 模型类型
|
||||
// condition: 查询条件
|
||||
// db: 数据库连接
|
||||
// args: 查询参数
|
||||
// 返回: 数量和错误
|
||||
func CountEntitiesByCondition(model interface{}, condition string, db *gorm.DB, args ...interface{}) (int64, error) {
|
||||
var count int64
|
||||
err := db.Model(model).Where(condition, args...).Count(&count).Error
|
||||
return count, err
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// 通用查询函数
|
||||
// ============================================================================
|
||||
|
||||
// FindEntitiesByCondition 根据条件查找实体
|
||||
// model: 模型类型
|
||||
// result: 结果容器
|
||||
// condition: 查询条件
|
||||
// db: 数据库连接
|
||||
// args: 查询参数
|
||||
// 返回: 错误
|
||||
func FindEntitiesByCondition(model interface{}, result interface{}, condition string, db *gorm.DB, args ...interface{}) error {
|
||||
return db.Model(model).Where(condition, args...).Find(result).Error
|
||||
}
|
||||
|
||||
// CheckEntityExists 检查实体是否存在
|
||||
// model: 模型类型
|
||||
// condition: 查询条件
|
||||
// db: 数据库连接
|
||||
// args: 查询参数
|
||||
// 返回: 是否存在和错误
|
||||
func CheckEntityExists(model interface{}, condition string, db *gorm.DB, args ...interface{}) (bool, error) {
|
||||
var count int64
|
||||
err := db.Model(model).Where(condition, args...).Count(&count).Error
|
||||
return count > 0, err
|
||||
}
|
||||
|
||||
@@ -9,15 +9,27 @@ import (
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// ============================================================================
|
||||
// 结构体定义
|
||||
// ============================================================================
|
||||
|
||||
// SettingsService 设置服务
|
||||
type SettingsService struct {
|
||||
mu sync.RWMutex
|
||||
cache map[string]string
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// 全局变量
|
||||
// ============================================================================
|
||||
|
||||
var settingsService *SettingsService
|
||||
var settingsOnce sync.Once
|
||||
|
||||
// ============================================================================
|
||||
// 公共函数
|
||||
// ============================================================================
|
||||
|
||||
// GetSettingsService 获取设置服务单例
|
||||
func GetSettingsService() *SettingsService {
|
||||
settingsOnce.Do(func() {
|
||||
@@ -30,6 +42,10 @@ func GetSettingsService() *SettingsService {
|
||||
return settingsService
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// 私有函数
|
||||
// ============================================================================
|
||||
|
||||
// loadAllSettings 从数据库加载所有设置到缓存
|
||||
func (s *SettingsService) loadAllSettings() {
|
||||
db, err := database.GetDB()
|
||||
|
||||
Reference in New Issue
Block a user