mysql_setting.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package dao
  2. import (
  3. "context"
  4. "database/sql"
  5. "encoding/json"
  6. "go-common/library/log"
  7. )
  8. const (
  9. _settingSQL = "SELECT value FROM push_user_settings WHERE mid=? AND dtime=0"
  10. _setSettingSQL = "INSERT INTO push_user_settings (mid,value) VALUES (?,?) ON DUPLICATE KEY UPDATE value=?"
  11. )
  12. // SetSetting saves user notify settings.
  13. func (d *Dao) SetSetting(c context.Context, mid int64, st map[int]int) (err error) {
  14. bs, err := json.Marshal(st)
  15. if err != nil {
  16. log.Error("SetSetting(%d) json.Marshal(%v) error(%v)", mid, st, err)
  17. return
  18. }
  19. if _, err = d.setSettingStmt.Exec(c, mid, string(bs), string(bs)); err != nil {
  20. PromError("mysql:保存用户通知开关")
  21. log.Error("d.SetSetting(%d,%s) error(%v)", mid, bs, err)
  22. }
  23. return
  24. }
  25. // Setting gets user push setting.
  26. func (d *Dao) Setting(c context.Context, mid int64) (st map[int]int, err error) {
  27. var v string
  28. if err = d.settingStmt.QueryRow(c, mid).Scan(&v); err != nil {
  29. if err == sql.ErrNoRows {
  30. st = nil
  31. err = nil
  32. return
  33. }
  34. log.Error("d.settingStmt.Query() error(%v)", err)
  35. PromError("mysql:获取用户通知开关配置")
  36. return
  37. }
  38. if v == "" {
  39. return
  40. }
  41. if err = json.Unmarshal([]byte(v), &st); err != nil {
  42. log.Error("d.Setting(%d) json.Unmarshal(%s) error(%v)", mid, v, err)
  43. }
  44. return
  45. }