setting_service.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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.FilterRaw("config_key LIKE '?%'", vo.Prefix)
  21. }
  22. if vo.Enable > -1 {
  23. qs.Filter("Enable", vo.Enable)
  24. }
  25. 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. // Delete 获取全部组织
  34. func (c *SettingService) Delete(id int) error {
  35. o := orm.NewOrm()
  36. _, err := o.Delete(&models.Setting{Id: id})
  37. return err
  38. }
  39. var SettingSrv = new(SettingService)
  40. // GetSettings 根据前缀获取设置
  41. func GetSettings(prefix string) []models.Setting {
  42. list, err := SettingSrv.GetList(ListVo{Prefix: prefix, Enable: 1})
  43. if err != nil {
  44. logs.Error("get setting list error:", err)
  45. return make([]models.Setting, 0)
  46. }
  47. return list
  48. }