57 lines
1.5 KiB
Go
57 lines
1.5 KiB
Go
|
|
package model
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"fmt"
|
||
|
|
|
||
|
|
"github.com/zeromicro/go-zero/core/stores/cache"
|
||
|
|
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||
|
|
)
|
||
|
|
|
||
|
|
var _ SysConfigModel = (*customSysConfigModel)(nil)
|
||
|
|
|
||
|
|
type (
|
||
|
|
SysConfigModel interface {
|
||
|
|
sysConfigModel
|
||
|
|
FindByGroup(ctx context.Context, group string) ([]*SysConfig, error)
|
||
|
|
FindAll(ctx context.Context) ([]*SysConfig, error)
|
||
|
|
UpdateByKey(ctx context.Context, key, value string) error
|
||
|
|
}
|
||
|
|
|
||
|
|
customSysConfigModel struct {
|
||
|
|
*defaultSysConfigModel
|
||
|
|
}
|
||
|
|
)
|
||
|
|
|
||
|
|
func NewSysConfigModel(conn sqlx.SqlConn, c cache.CacheConf, opts ...cache.Option) SysConfigModel {
|
||
|
|
return &customSysConfigModel{
|
||
|
|
defaultSysConfigModel: newSysConfigModel(conn, c, opts...),
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func (m *customSysConfigModel) FindByGroup(ctx context.Context, group string) ([]*SysConfig, error) {
|
||
|
|
var list []*SysConfig
|
||
|
|
query := fmt.Sprintf("SELECT %s FROM %s WHERE `config_group` = ?", sysConfigRows, m.table)
|
||
|
|
err := m.QueryRowsNoCacheCtx(ctx, &list, query, group)
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
return list, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (m *customSysConfigModel) FindAll(ctx context.Context) ([]*SysConfig, error) {
|
||
|
|
var list []*SysConfig
|
||
|
|
query := fmt.Sprintf("SELECT %s FROM %s", sysConfigRows, m.table)
|
||
|
|
err := m.QueryRowsNoCacheCtx(ctx, &list, query)
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
return list, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (m *customSysConfigModel) UpdateByKey(ctx context.Context, key, value string) error {
|
||
|
|
query := fmt.Sprintf("UPDATE %s SET `config_value` = ? WHERE `config_key` = ?", m.table)
|
||
|
|
_, err := m.ExecNoCacheCtx(ctx, query, value, key)
|
||
|
|
return err
|
||
|
|
}
|