mysql.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package dao
  2. import (
  3. "container/list"
  4. "context"
  5. "go-common/app/service/main/msm/model"
  6. "go-common/library/log"
  7. xtime "go-common/library/time"
  8. )
  9. const (
  10. _codesSQL = "SELECT code, message, mtime FROM codes"
  11. _diffCodesSQL = "SELECT code, message, mtime FROM codes WHERE mtime > ? ORDER BY mtime LIMIT 100"
  12. _codesLangsSQL = "select a.code,a.message,a.mtime,IFNULL(b.locale,''),IFNULL(b.msg,''),IFNULL(b.mtime,'') as bmtime from codes as a left join code_msg as b on a.id=b.code_id"
  13. )
  14. // Codes get all codes.
  15. func (d *Dao) Codes(c context.Context) (codes map[int]string, lcode *model.Code, err error) {
  16. var (
  17. code int
  18. msg string
  19. tmp int64
  20. mtime xtime.Time
  21. )
  22. rows, err := d.db.Query(c, _codesSQL)
  23. if err != nil {
  24. log.Error("d.db.Query(%v) error(%v)", _codesSQL, err)
  25. return
  26. }
  27. defer rows.Close()
  28. lcode = &model.Code{}
  29. codes = make(map[int]string)
  30. for rows.Next() {
  31. if err = rows.Scan(&code, &msg, &mtime); err != nil {
  32. log.Error("rows.Scan error(%v)", err)
  33. return
  34. }
  35. codes[code] = msg
  36. if int64(mtime) > tmp {
  37. lcode.Code = code
  38. lcode.Ver = int64(mtime)
  39. lcode.Msg = msg
  40. tmp = int64(mtime)
  41. }
  42. }
  43. return
  44. }
  45. // Diff get change codes.
  46. func (d *Dao) Diff(c context.Context, ver int64) (vers *list.List, err error) {
  47. var (
  48. code int
  49. msg string
  50. mtime xtime.Time
  51. )
  52. rows, err := d.db.Query(c, _diffCodesSQL, xtime.Time(ver))
  53. if err != nil {
  54. log.Error("d.db.Query(%v) error(%v)", _diffCodesSQL, err)
  55. return
  56. }
  57. defer rows.Close()
  58. vers = list.New()
  59. for rows.Next() {
  60. if err = rows.Scan(&code, &msg, &mtime); err != nil {
  61. log.Error("rows.Scan error(%v)", err)
  62. return
  63. }
  64. vers.PushBack(&model.Code{Ver: int64(mtime), Code: code, Msg: msg})
  65. }
  66. return
  67. }
  68. // CodesLang get all codes.
  69. func (d *Dao) CodesLang(c context.Context) (codes map[int]map[string]string, lcode *model.CodeLangs, err error) {
  70. var (
  71. code int
  72. tmp int64
  73. mtime xtime.Time
  74. message string
  75. bl string
  76. bmsg string
  77. bmtime xtime.Time
  78. )
  79. rows, err := d.db.Query(c, _codesLangsSQL)
  80. if err != nil {
  81. log.Error("d.db.Query(%v) error(%v)", _codesLangsSQL, err)
  82. return
  83. }
  84. defer rows.Close()
  85. lcode = &model.CodeLangs{}
  86. codes = make(map[int]map[string]string)
  87. for rows.Next() {
  88. t := make(map[string]string)
  89. bl = ""
  90. if err = rows.Scan(&code, &message, &mtime, &bl, &bmsg, &bmtime); err != nil {
  91. log.Error("rows.Scan error(%v)", err)
  92. return
  93. }
  94. t["default"] = message
  95. if len(bl) > 0 {
  96. t[bl] = bmsg
  97. }
  98. codes[code] = t
  99. if int64(mtime) > tmp {
  100. lcode.Code = code
  101. lcode.Ver = int64(mtime)
  102. lcode.Msg = t
  103. tmp = int64(mtime)
  104. }
  105. }
  106. err = rows.Err()
  107. return
  108. }