setting_service.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package settings
  2. import (
  3. "github.com/astaxie/beego/logs"
  4. "github.com/astaxie/beego/orm"
  5. "nginx-ui/server/models"
  6. )
  7. type SettingService struct {
  8. }
  9. // Save 保存或者修改
  10. func (c *SettingService) Save(body *models.Setting) (*models.Setting, error) {
  11. o := orm.NewOrm()
  12. _, err := models.InsertOrUpdate[models.Setting](o, body)
  13. return body, err
  14. }
  15. // GetList 获取全部组织
  16. func (c *SettingService) GetList(vo ListVo) ([]models.Setting, error) {
  17. o := orm.NewOrm()
  18. qs := o.QueryTable(&models.Setting{})
  19. if vo.Prefix != "" {
  20. qs = qs.FilterRaw("ConfigKey", "LIKE '"+vo.Prefix+"%'")
  21. }
  22. if vo.Enable > -1 {
  23. qs = qs.Filter("Enable", vo.Enable)
  24. }
  25. qs = qs.OrderBy("-Id")
  26. var list []models.Setting
  27. _, err := qs.All(&list)
  28. if err != nil {
  29. return nil, err
  30. }
  31. return list, nil
  32. }
  33. // GetList 获取全部组织
  34. func (c *SettingService) GetDetail(key string) (*models.Setting, error) {
  35. o := orm.NewOrm()
  36. settings := models.Setting{ConfigKey: key}
  37. err := o.Read(&settings, "ConfigKey")
  38. if err != nil {
  39. return nil, err
  40. }
  41. return &settings, nil
  42. }
  43. // Delete 获取全部组织
  44. func (c *SettingService) Delete(id int) error {
  45. o := orm.NewOrm()
  46. _, err := o.Delete(&models.Setting{Id: id})
  47. return err
  48. }
  49. var SettingSrv = new(SettingService)
  50. // GetSettings 根据前缀获取设置
  51. func GetSettings(prefix string) []models.Setting {
  52. list, err := SettingSrv.GetList(ListVo{Prefix: prefix, Enable: 1})
  53. if err != nil {
  54. logs.Error("get setting list error:", err)
  55. return make([]models.Setting, 0)
  56. }
  57. return list
  58. }
  59. func GetSetting(key string) (*models.Setting, error) {
  60. return SettingSrv.GetDetail(key)
  61. }