2017-09-26 12:50:09 +00:00
|
|
|
package biz
|
|
|
|
|
|
|
|
import (
|
2021-12-23 10:00:14 +00:00
|
|
|
"bytes"
|
2021-12-06 12:24:22 +00:00
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2021-12-16 08:11:16 +00:00
|
|
|
"time"
|
2017-09-26 12:50:09 +00:00
|
|
|
|
2021-12-06 12:24:22 +00:00
|
|
|
"github.com/cuigh/auxo/data"
|
2017-09-26 12:50:09 +00:00
|
|
|
"github.com/cuigh/auxo/net/web"
|
|
|
|
"github.com/cuigh/swirl/dao"
|
|
|
|
)
|
|
|
|
|
2021-12-06 12:24:22 +00:00
|
|
|
type SettingBiz interface {
|
2022-01-06 08:54:14 +00:00
|
|
|
Find(ctx context.Context, id string) (options interface{}, err error)
|
|
|
|
Load(ctx context.Context) (options data.Map, err error)
|
|
|
|
Save(ctx context.Context, id string, options interface{}, user web.User) (err error)
|
2021-12-06 12:24:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewSetting(d dao.Interface, eb EventBiz) SettingBiz {
|
|
|
|
return &settingBiz{d: d, eb: eb}
|
|
|
|
}
|
2017-09-26 12:50:09 +00:00
|
|
|
|
|
|
|
type settingBiz struct {
|
2021-12-06 12:24:22 +00:00
|
|
|
d dao.Interface
|
|
|
|
eb EventBiz
|
2017-09-26 12:50:09 +00:00
|
|
|
}
|
|
|
|
|
2022-01-06 08:54:14 +00:00
|
|
|
func (b *settingBiz) Find(ctx context.Context, id string) (options interface{}, err error) {
|
2021-12-23 11:28:31 +00:00
|
|
|
var setting *dao.Setting
|
2022-01-06 08:54:14 +00:00
|
|
|
setting, err = b.d.SettingGet(ctx, id)
|
2021-12-23 10:00:14 +00:00
|
|
|
if err == nil && setting != nil {
|
|
|
|
return b.unmarshal(setting.Options)
|
2021-12-06 12:24:22 +00:00
|
|
|
}
|
2017-09-26 12:50:09 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-06 12:24:22 +00:00
|
|
|
// Load returns settings of swirl. If not found, default settings will be returned.
|
2022-01-06 08:54:14 +00:00
|
|
|
func (b *settingBiz) Load(ctx context.Context) (options data.Map, err error) {
|
2021-12-23 11:28:31 +00:00
|
|
|
var settings []*dao.Setting
|
2022-01-06 08:54:14 +00:00
|
|
|
settings, err = b.d.SettingGetAll(ctx)
|
2021-12-06 12:24:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
options = data.Map{}
|
|
|
|
for _, s := range settings {
|
2021-12-23 10:00:14 +00:00
|
|
|
var v interface{}
|
|
|
|
if v, err = b.unmarshal(s.Options); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
options[s.ID] = v
|
2021-12-06 12:24:22 +00:00
|
|
|
}
|
2017-09-26 12:50:09 +00:00
|
|
|
return
|
|
|
|
}
|
2021-12-06 12:24:22 +00:00
|
|
|
|
2022-01-06 08:54:14 +00:00
|
|
|
func (b *settingBiz) Save(ctx context.Context, id string, options interface{}, user web.User) (err error) {
|
2021-12-23 11:28:31 +00:00
|
|
|
setting := &dao.Setting{
|
2021-12-16 08:11:16 +00:00
|
|
|
ID: id,
|
|
|
|
UpdatedAt: time.Now(),
|
2021-12-22 09:43:26 +00:00
|
|
|
}
|
|
|
|
if user != nil {
|
2021-12-23 11:28:31 +00:00
|
|
|
setting.UpdatedBy = dao.Operator{ID: user.ID(), Name: user.Name()}
|
2021-12-16 08:11:16 +00:00
|
|
|
}
|
2021-12-23 10:00:14 +00:00
|
|
|
|
|
|
|
setting.Options, err = b.marshal(options)
|
|
|
|
if err == nil {
|
2022-01-06 08:54:14 +00:00
|
|
|
err = b.d.SettingUpdate(ctx, setting)
|
2021-12-23 10:00:14 +00:00
|
|
|
}
|
2021-12-06 12:24:22 +00:00
|
|
|
if err == nil && user != nil {
|
|
|
|
b.eb.CreateSetting(EventActionUpdate, user)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-23 10:00:14 +00:00
|
|
|
func (b *settingBiz) marshal(v interface{}) (s string, err error) {
|
|
|
|
var buf []byte
|
|
|
|
if buf, err = json.Marshal(v); err == nil {
|
|
|
|
s = string(buf)
|
2021-12-06 12:24:22 +00:00
|
|
|
}
|
2021-12-23 10:00:14 +00:00
|
|
|
return
|
2021-12-06 12:24:22 +00:00
|
|
|
}
|
|
|
|
|
2021-12-23 10:00:14 +00:00
|
|
|
func (b *settingBiz) unmarshal(s string) (v interface{}, err error) {
|
|
|
|
d := json.NewDecoder(bytes.NewBuffer([]byte(s)))
|
|
|
|
d.UseNumber()
|
|
|
|
err = d.Decode(&v)
|
|
|
|
return
|
2021-12-06 12:24:22 +00:00
|
|
|
}
|