notice.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package dao
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "go-common/app/interface/main/space/model"
  7. "go-common/library/log"
  8. )
  9. const (
  10. _noticeKeyFmt = "spc_nt_%d"
  11. _noticeSQL = `SELECT notice,is_forbid FROM member_up_notice%d WHERE mid = ?`
  12. _noticeSetSQL = `INSERT INTO member_up_notice%d (mid,notice) VALUES (?,?) ON DUPLICATE KEY UPDATE notice = ?`
  13. )
  14. func noticeHit(mid int64) int64 {
  15. return mid % 10
  16. }
  17. func noticeKey(mid int64) string {
  18. return fmt.Sprintf(_noticeKeyFmt, mid)
  19. }
  20. // RawNotice get notice from db.
  21. func (d *Dao) RawNotice(c context.Context, mid int64) (res *model.Notice, err error) {
  22. row := d.db.QueryRow(c, fmt.Sprintf(_noticeSQL, noticeHit(mid)), mid)
  23. res = new(model.Notice)
  24. if err = row.Scan(&res.Notice, &res.IsForbid); err != nil {
  25. if err == sql.ErrNoRows {
  26. err = nil
  27. } else {
  28. log.Error("Notice row.Scan() error(%v)", err)
  29. }
  30. }
  31. return
  32. }
  33. // SetNotice change notice.
  34. func (d *Dao) SetNotice(c context.Context, mid int64, notice string) (err error) {
  35. if _, err = d.db.Exec(c, fmt.Sprintf(_noticeSetSQL, noticeHit(mid)), mid, notice, notice); err != nil {
  36. log.Error("SetNotice error d.db.Exec(%d,%s) error(%v)", mid, notice, err)
  37. }
  38. return
  39. }