Files
smart-go/internal/system/repository/param_repository.go
T
2026-04-23 18:58:13 +08:00

140 lines
4.1 KiB
Go

package repository
import (
"context"
"errors"
"giter.top/smart/internal/system/entity"
"gorm.io/gorm"
)
// ErrNotFound 记录未找到
var ErrNotFound = errors.New("param not found")
// ParamRepository 系统参数数据访问层
type ParamRepository interface {
// Create 创建系统参数
Create(ctx context.Context, param *entity.SystemParam) error
// Update 更新系统参数
Update(ctx context.Context, param *entity.SystemParam) error
// Delete 删除系统参数
Delete(ctx context.Context, id string) error
// DeleteBatch 批量删除
DeleteBatch(ctx context.Context, ids []string) error
// GetByID 根据 ID 获取
GetByID(ctx context.Context, id string) (*entity.SystemParam, error)
// GetByKey 根据键获取
GetByKey(ctx context.Context, key string) (*entity.SystemParam, error)
// List 获取列表(支持分页和筛选)
List(ctx context.Context, group string, paramKey string, page, pageSize int) ([]entity.SystemParam, int64, error)
// GetAll 获取所有参数(用于缓存)
GetAll(ctx context.Context) (map[string]entity.SystemParam, error)
// ExistsByKey 检查键是否存在(排除指定 ID)
ExistsByKey(ctx context.Context, key string, excludeID string) (bool, error)
}
type paramRepository struct {
db *gorm.DB
}
// NewParamRepository 创建参数仓库实例
func NewParamRepository(db *gorm.DB) ParamRepository {
return &paramRepository{db: db}
}
func (r *paramRepository) Create(ctx context.Context, param *entity.SystemParam) error {
return r.db.WithContext(ctx).Create(param).Error
}
func (r *paramRepository) Update(ctx context.Context, param *entity.SystemParam) error {
return r.db.WithContext(ctx).Save(param).Error
}
func (r *paramRepository) Delete(ctx context.Context, id string) error {
return r.db.WithContext(ctx).Where("id = ?", id).Delete(&entity.SystemParam{}).Error
}
func (r *paramRepository) DeleteBatch(ctx context.Context, ids []string) error {
return r.db.WithContext(ctx).Where("id IN ?", ids).Delete(&entity.SystemParam{}).Error
}
func (r *paramRepository) GetByID(ctx context.Context, id string) (*entity.SystemParam, error) {
var param entity.SystemParam
err := r.db.WithContext(ctx).Where("id = ?", id).First(&param).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, ErrNotFound
}
return nil, err
}
return &param, nil
}
func (r *paramRepository) GetByKey(ctx context.Context, key string) (*entity.SystemParam, error) {
var param entity.SystemParam
err := r.db.WithContext(ctx).Where("param_key = ?", key).First(&param).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, ErrNotFound
}
return nil, err
}
return &param, nil
}
func (r *paramRepository) List(ctx context.Context, group string, paramKey string, page, pageSize int) ([]entity.SystemParam, int64, error) {
var params []entity.SystemParam
var total int64
query := r.db.WithContext(ctx).Model(&entity.SystemParam{})
// 应用筛选条件
if group != "" {
query = query.Where("param_group = ?", group)
}
if paramKey != "" {
query = query.Where("param_key LIKE ?", "%"+paramKey+"%")
}
// 获取总数
if err := query.Count(&total).Error; err != nil {
return nil, 0, err
}
// 分页查询
offset := (page - 1) * pageSize
if offset < 0 {
offset = 0
}
if pageSize <= 0 {
pageSize = 10
}
err := query.Order("id DESC").Offset(offset).Limit(pageSize).Find(&params).Error
if err != nil {
return nil, 0, err
}
return params, total, nil
}
func (r *paramRepository) GetAll(ctx context.Context) (map[string]entity.SystemParam, error) {
var params []entity.SystemParam
err := r.db.WithContext(ctx).Find(&params).Error
if err != nil {
return nil, err
}
result := make(map[string]entity.SystemParam, len(params))
for _, param := range params {
result[param.ParamKey] = param
}
return result, nil
}
func (r *paramRepository) ExistsByKey(ctx context.Context, key string, excludeID string) (bool, error) {
query := r.db.WithContext(ctx).Where("param_key = ?", key)
if excludeID != "" {
query = query.Where("id != ?", excludeID)
}
var count int64
err := query.Model(&entity.SystemParam{}).Count(&count).Error
return count > 0, err
}