2021-09-28 11:47:19 +08:00
|
|
|
package controller
|
|
|
|
|
|
|
|
import (
|
2022-01-05 11:29:27 +08:00
|
|
|
"SciencesServer/app/api/admin/model"
|
2022-01-10 17:43:43 +08:00
|
|
|
config2 "SciencesServer/app/basic/config"
|
2022-01-06 22:02:09 +08:00
|
|
|
"SciencesServer/app/basic/controller"
|
2021-09-28 11:47:19 +08:00
|
|
|
model2 "SciencesServer/app/common/model"
|
|
|
|
"SciencesServer/config"
|
|
|
|
"SciencesServer/serve/orm"
|
|
|
|
"errors"
|
2022-01-06 17:11:57 +08:00
|
|
|
"fmt"
|
2021-09-28 11:47:19 +08:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct{}
|
|
|
|
|
|
|
|
type ConfigHandle func() *Config
|
|
|
|
|
2022-01-15 09:00:47 +08:00
|
|
|
type ConfigInfo struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
*model2.SysConfig
|
|
|
|
}
|
|
|
|
|
2022-01-06 22:02:09 +08:00
|
|
|
func (c *Config) Config(kind, page, pageSize int) (*controller.ReturnPages, error) {
|
2021-09-28 11:47:19 +08:00
|
|
|
mSysConfig := model.NewSysConfig()
|
|
|
|
|
|
|
|
where := []*model2.ModelWhereOrder{
|
2022-01-17 09:57:57 +08:00
|
|
|
&model2.ModelWhereOrder{Order: model2.NewOrder("sort", model2.OrderModeToAsc)},
|
2021-09-28 11:47:19 +08:00
|
|
|
&model2.ModelWhereOrder{Order: model2.NewOrder("kind", model2.OrderModeToAsc)},
|
|
|
|
}
|
|
|
|
if kind > 0 {
|
|
|
|
where = append(where, &model2.ModelWhereOrder{Where: model2.NewWhere("kind", kind)})
|
|
|
|
}
|
|
|
|
out := make([]*model2.SysConfig, 0)
|
|
|
|
|
|
|
|
var count int64
|
|
|
|
|
|
|
|
if err := model2.Pages(mSysConfig.SysConfig, &out, page, pageSize, &count, where...); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-01-15 09:00:47 +08:00
|
|
|
list := make([]*ConfigInfo, 0)
|
|
|
|
|
|
|
|
for _, v := range out {
|
|
|
|
list = append(list, &ConfigInfo{
|
|
|
|
ID: v.GetEncodeID(),
|
|
|
|
SysConfig: v,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return &controller.ReturnPages{Data: list, Count: count}, nil
|
2021-09-28 11:47:19 +08:00
|
|
|
}
|
|
|
|
|
2022-01-17 09:57:57 +08:00
|
|
|
func (c *Config) Add(kind int, name, key, value string) error {
|
2022-01-06 17:11:57 +08:00
|
|
|
mSysConfig := model.NewSysConfig()
|
|
|
|
|
|
|
|
var count int64
|
|
|
|
|
2022-01-15 09:00:47 +08:00
|
|
|
err := model2.Count(mSysConfig.SysConfig, &count, model2.NewWhere("`key`", key))
|
2022-01-06 17:11:57 +08:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if count > 0 {
|
|
|
|
return errors.New("key已存在")
|
|
|
|
}
|
|
|
|
mSysConfig.Kind = model2.SysConfigKind(kind)
|
|
|
|
mSysConfig.Name = name
|
|
|
|
mSysConfig.Key = key
|
|
|
|
mSysConfig.Value = fmt.Sprintf("%v", value)
|
|
|
|
|
|
|
|
if err = model2.Create(mSysConfig.SysConfig); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
config.SystemConfig[key] = value
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-01-17 09:57:57 +08:00
|
|
|
func (c *Config) Form(id uint64, name, value string) error {
|
2022-01-15 09:00:47 +08:00
|
|
|
mSysConfig := model.NewSysConfig()
|
|
|
|
mSysConfig.ID = id
|
|
|
|
|
|
|
|
isExist, err := model2.FirstField(mSysConfig.SysConfig, []string{"id", "`key`"})
|
|
|
|
|
|
|
|
if err != nil {
|
2021-09-28 11:47:19 +08:00
|
|
|
return nil
|
2022-01-15 09:00:47 +08:00
|
|
|
} else if !isExist {
|
|
|
|
return errors.New("操作错误,数据不存在")
|
2021-09-28 11:47:19 +08:00
|
|
|
}
|
|
|
|
return orm.GetDB().Transaction(func(tx *gorm.DB) error {
|
|
|
|
now := time.Now()
|
|
|
|
|
2022-01-15 09:00:47 +08:00
|
|
|
if err := model2.Updates(mSysConfig.SysConfig, map[string]interface{}{
|
|
|
|
"`name`": name, "`value`": value, "updated_at": now,
|
|
|
|
}, tx); err != nil {
|
|
|
|
return err
|
2021-09-28 11:47:19 +08:00
|
|
|
}
|
2022-01-15 09:00:47 +08:00
|
|
|
config.SystemConfig[mSysConfig.Key] = value
|
2021-09-28 11:47:19 +08:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-01-10 17:43:43 +08:00
|
|
|
// Area 区域信息
|
|
|
|
func (c *Config) Area(key string) map[string]string {
|
|
|
|
if key == "" {
|
|
|
|
key = config.DefaultChinaAreaCode
|
|
|
|
}
|
|
|
|
return config2.MemoryForAreaInfo[key]
|
|
|
|
}
|
|
|
|
|
2021-09-28 11:47:19 +08:00
|
|
|
func NewConfig() ConfigHandle {
|
|
|
|
return func() *Config {
|
|
|
|
return &Config{}
|
|
|
|
}
|
|
|
|
}
|