setting.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package service
  2. import (
  3. "context"
  4. "strconv"
  5. "go-common/app/admin/main/spy/model"
  6. "go-common/library/ecode"
  7. "go-common/library/log"
  8. )
  9. // SettingList get all setting
  10. func (s *Service) SettingList(c context.Context) (list []*model.Setting, err error) {
  11. if list, err = s.spyDao.SettingList(c); err != nil {
  12. log.Error("s.spyDao.SettingList() error(%v)", err)
  13. return
  14. }
  15. return
  16. }
  17. // UpdateSetting update setting
  18. func (s *Service) UpdateSetting(c context.Context, name string, property string, val string) (err error) {
  19. if err = s.checkSettingVal(property, val); err != nil {
  20. return
  21. }
  22. var effected int64
  23. if effected, err = s.spyDao.UpdateSetting(c, property, val); err != nil {
  24. log.Error("s.spyDao.UpdateSetting(%s,%d) error(%v)", property, val, err)
  25. return
  26. }
  27. if effected > 0 {
  28. updatedSetting := &model.Setting{Property: property, Val: val}
  29. if err := s.AddLog(c, name, model.UpdateSetting, updatedSetting); err != nil {
  30. log.Error("s.AddLog(%s,%d,%+v) error(%v)", name, model.UpdateSetting, updatedSetting, err)
  31. }
  32. }
  33. return
  34. }
  35. func (s *Service) checkSettingVal(prop string, val string) (err error) {
  36. switch prop {
  37. case model.AutoBlock:
  38. var block int64
  39. if block, err = strconv.ParseInt(val, 10, 64); err != nil {
  40. err = ecode.SpySettingValTypeError
  41. return
  42. }
  43. if block != 1 && block != 0 {
  44. err = ecode.SpySettingValueOutOfRange
  45. return
  46. }
  47. case model.LimitBlockCount:
  48. var count int64
  49. if count, err = strconv.ParseInt(val, 10, 64); err != nil {
  50. err = ecode.SpySettingValTypeError
  51. return
  52. }
  53. if count < 0 {
  54. err = ecode.SpySettingValueOutOfRange
  55. return
  56. }
  57. case model.LessBlockScore:
  58. var score int64
  59. if score, err = strconv.ParseInt(val, 10, 64); err != nil {
  60. err = ecode.SpySettingValTypeError
  61. return
  62. }
  63. if score < 0 || score > 30 {
  64. err = ecode.SpySettingValueOutOfRange
  65. return
  66. }
  67. default:
  68. err = ecode.SpySettingUnknown
  69. }
  70. return err
  71. }