1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- package settings
- import (
- "github.com/astaxie/beego/logs"
- "github.com/astaxie/beego/orm"
- "nginx-ui/server/models"
- )
- type SettingService struct {
- }
- // Save 保存或者修改
- func (c *SettingService) Save(body *models.Setting) (*models.Setting, error) {
- o := orm.NewOrm()
- _, err := models.InsertOrUpdate[models.Setting](o, body)
- return body, err
- }
- // GetList 获取全部组织
- func (c *SettingService) GetList(vo ListVo) ([]models.Setting, error) {
- o := orm.NewOrm()
- qs := o.QueryTable(&models.Setting{})
- if vo.Prefix != "" {
- qs = qs.FilterRaw("ConfigKey", "LIKE '"+vo.Prefix+"%'")
- }
- if vo.Enable > -1 {
- qs = qs.Filter("Enable", vo.Enable)
- }
- qs = qs.OrderBy("-Id")
- var list []models.Setting
- _, err := qs.All(&list)
- if err != nil {
- return nil, err
- }
- return list, nil
- }
- // GetList 获取全部组织
- func (c *SettingService) GetDetail(key string) (*models.Setting, error) {
- o := orm.NewOrm()
- settings := models.Setting{ConfigKey: key}
- err := o.Read(&settings, "ConfigKey")
- if err != nil {
- return nil, err
- }
- return &settings, nil
- }
- // Delete 获取全部组织
- func (c *SettingService) Delete(id int) error {
- o := orm.NewOrm()
- _, err := o.Delete(&models.Setting{Id: id})
- return err
- }
- var SettingSrv = new(SettingService)
- // GetSettings 根据前缀获取设置
- func GetSettings(prefix string) []models.Setting {
- list, err := SettingSrv.GetList(ListVo{Prefix: prefix, Enable: 1})
- if err != nil {
- logs.Error("get setting list error:", err)
- return make([]models.Setting, 0)
- }
- return list
- }
- func GetSetting(key string) (*models.Setting, error) {
- return SettingSrv.GetDetail(key)
- }
|