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) }