2022-11-03 21:47:36 +08:00
|
|
|
package store
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2023-06-29 22:55:03 +08:00
|
|
|
type SystemSetting struct {
|
2023-06-26 23:06:53 +08:00
|
|
|
Name string
|
|
|
|
Value string
|
|
|
|
Description string
|
|
|
|
}
|
|
|
|
|
2023-06-29 22:55:03 +08:00
|
|
|
type FindSystemSetting struct {
|
2023-06-26 23:06:53 +08:00
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
2023-07-02 18:56:25 +08:00
|
|
|
func (s *Store) UpsertSystemSetting(ctx context.Context, upsert *SystemSetting) (*SystemSetting, error) {
|
|
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
query := `
|
|
|
|
INSERT INTO system_setting (
|
|
|
|
name, value, description
|
|
|
|
)
|
|
|
|
VALUES (?, ?, ?)
|
|
|
|
ON CONFLICT(name) DO UPDATE
|
|
|
|
SET
|
|
|
|
value = EXCLUDED.value,
|
|
|
|
description = EXCLUDED.description
|
|
|
|
`
|
|
|
|
if _, err := tx.ExecContext(ctx, query, upsert.Name, upsert.Value, upsert.Description); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
systemSetting := upsert
|
|
|
|
return systemSetting, nil
|
|
|
|
}
|
|
|
|
|
2023-06-29 22:55:03 +08:00
|
|
|
func (s *Store) ListSystemSettings(ctx context.Context, find *FindSystemSetting) ([]*SystemSetting, error) {
|
2023-06-26 23:06:53 +08:00
|
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
|
|
|
if err != nil {
|
2023-07-02 18:56:25 +08:00
|
|
|
return nil, err
|
2023-06-26 23:06:53 +08:00
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
list, err := listSystemSettings(ctx, tx, find)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, systemSettingMessage := range list {
|
|
|
|
s.systemSettingCache.Store(systemSettingMessage.Name, systemSettingMessage)
|
|
|
|
}
|
|
|
|
return list, nil
|
|
|
|
}
|
|
|
|
|
2023-06-29 22:55:03 +08:00
|
|
|
func (s *Store) GetSystemSetting(ctx context.Context, find *FindSystemSetting) (*SystemSetting, error) {
|
2023-06-26 23:06:53 +08:00
|
|
|
if find.Name != "" {
|
|
|
|
if cache, ok := s.systemSettingCache.Load(find.Name); ok {
|
2023-06-29 22:55:03 +08:00
|
|
|
return cache.(*SystemSetting), nil
|
2023-06-26 23:06:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tx, err := s.db.BeginTx(ctx, nil)
|
|
|
|
if err != nil {
|
2023-07-02 18:56:25 +08:00
|
|
|
return nil, err
|
2023-06-26 23:06:53 +08:00
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
|
|
|
|
list, err := listSystemSettings(ctx, tx, find)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(list) == 0 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
systemSettingMessage := list[0]
|
|
|
|
s.systemSettingCache.Store(systemSettingMessage.Name, systemSettingMessage)
|
|
|
|
return systemSettingMessage, nil
|
|
|
|
}
|
|
|
|
|
2023-07-02 18:56:25 +08:00
|
|
|
func (s *Store) GetSystemSettingValueWithDefault(ctx *context.Context, settingName string, defaultValue string) string {
|
|
|
|
if setting, err := s.GetSystemSetting(*ctx, &FindSystemSetting{
|
|
|
|
Name: settingName,
|
|
|
|
}); err == nil && setting != nil {
|
|
|
|
return setting.Value
|
|
|
|
}
|
|
|
|
return defaultValue
|
|
|
|
}
|
|
|
|
|
2023-06-29 22:55:03 +08:00
|
|
|
func listSystemSettings(ctx context.Context, tx *sql.Tx, find *FindSystemSetting) ([]*SystemSetting, error) {
|
2023-06-26 23:06:53 +08:00
|
|
|
where, args := []string{"1 = 1"}, []any{}
|
|
|
|
if find.Name != "" {
|
|
|
|
where, args = append(where, "name = ?"), append(args, find.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
query := `
|
|
|
|
SELECT
|
|
|
|
name,
|
|
|
|
value,
|
|
|
|
description
|
|
|
|
FROM system_setting
|
|
|
|
WHERE ` + strings.Join(where, " AND ")
|
|
|
|
|
|
|
|
rows, err := tx.QueryContext(ctx, query, args...)
|
|
|
|
if err != nil {
|
2023-07-02 18:56:25 +08:00
|
|
|
return nil, err
|
2023-06-26 23:06:53 +08:00
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
2023-06-29 22:55:03 +08:00
|
|
|
list := []*SystemSetting{}
|
2023-06-26 23:06:53 +08:00
|
|
|
for rows.Next() {
|
2023-06-29 22:55:03 +08:00
|
|
|
systemSettingMessage := &SystemSetting{}
|
2023-06-26 23:06:53 +08:00
|
|
|
if err := rows.Scan(
|
|
|
|
&systemSettingMessage.Name,
|
|
|
|
&systemSettingMessage.Value,
|
|
|
|
&systemSettingMessage.Description,
|
|
|
|
); err != nil {
|
2023-07-02 18:56:25 +08:00
|
|
|
return nil, err
|
2023-06-26 23:06:53 +08:00
|
|
|
}
|
|
|
|
list = append(list, systemSettingMessage)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return list, nil
|
|
|
|
}
|