mysql.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. package dao
  2. import (
  3. "context"
  4. "database/sql"
  5. "encoding/json"
  6. "strconv"
  7. "time"
  8. pushmdl "go-common/app/service/main/push/model"
  9. xsql "go-common/library/database/sql"
  10. "go-common/library/log"
  11. )
  12. const (
  13. // task
  14. _addTaskSQL = "INSERT INTO push_tasks (job,type,app_id,business_id,platform,title,summary,link_type,link_value,build,sound,vibration,pass_through,mid_file,progress,push_time,expire_time,status,`group`,image_url,extra) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
  15. _taskSQL = "SELECT id,job,type,app_id,business_id,platform,title,summary,link_type,link_value,build,sound,vibration,pass_through,mid_file,progress,push_time,expire_time,status,`group`,image_url,extra FROM push_tasks WHERE id=?"
  16. // app && business
  17. _appsSQL = "SELECT id,name,push_limit_user FROM push_apps WHERE dtime=0"
  18. _businessesSQL = "SELECT id,app_id,name,description,token,sound,vibration,receive_switch,push_switch,silent_time,push_limit_user,whitelist FROM push_business WHERE dtime=0"
  19. // setting
  20. _settingsByRangeSQL = "SELECT mid,value FROM push_user_settings WHERE id>? AND id<=? AND dtime=0"
  21. _maxSettingIDSQL = "SELECT MAX(id) FROM push_user_settings"
  22. )
  23. // Apps get all app info
  24. func (d *Dao) Apps(ctx context.Context) (res map[int64]*pushmdl.APP, err error) {
  25. rows, err := d.appsStmt.Query(ctx)
  26. if err != nil {
  27. log.Error("d.appsStmt.Query() error(%v)", err)
  28. PromError("mysql:查询应用")
  29. return
  30. }
  31. defer rows.Close()
  32. res = make(map[int64]*pushmdl.APP)
  33. for rows.Next() {
  34. app := new(pushmdl.APP)
  35. if err = rows.Scan(&app.ID, &app.Name, &app.PushLimitUser); err != nil {
  36. log.Error("d.Apps() Scan() error(%v)", err)
  37. PromError("mysql:查询应用Scan")
  38. return
  39. }
  40. res[app.ID] = app
  41. }
  42. err = rows.Err()
  43. return
  44. }
  45. // Businesses gets all business info.
  46. func (d *Dao) Businesses(ctx context.Context) (res map[int64]*pushmdl.Business, err error) {
  47. rows, err := d.businessesStmt.Query(ctx)
  48. if err != nil {
  49. log.Error("d.businessesStmt.Query() error(%v)", err)
  50. PromError("mysql:查询业务方")
  51. return
  52. }
  53. defer rows.Close()
  54. res = make(map[int64]*pushmdl.Business)
  55. for rows.Next() {
  56. var (
  57. silentTime string
  58. b = &pushmdl.Business{}
  59. )
  60. if err = rows.Scan(&b.ID, &b.APPID, &b.Name, &b.Desc, &b.Token,
  61. &b.Sound, &b.Vibration, &b.ReceiveSwitch, &b.PushSwitch, &silentTime, &b.PushLimitUser, &b.Whitelist); err != nil {
  62. PromError("mysql:查询业务方Scan")
  63. log.Error("d.Business() Scan() error(%v)", err)
  64. return
  65. }
  66. b.SilentTime = pushmdl.ParseSilentTime(silentTime)
  67. res[b.ID] = b
  68. }
  69. err = rows.Err()
  70. return
  71. }
  72. // AddTask adds task.
  73. func (d *Dao) AddTask(ctx context.Context, t *pushmdl.Task) (id int64, err error) {
  74. var (
  75. res sql.Result
  76. platform = pushmdl.JoinInts(t.Platform)
  77. build, _ = json.Marshal(t.Build)
  78. progress, _ = json.Marshal(t.Progress)
  79. extra, _ = json.Marshal(t.Extra)
  80. )
  81. if res, err = d.addTaskStmt.Exec(ctx, t.Job, t.Type, t.APPID, t.BusinessID, platform, t.Title, t.Summary, t.LinkType, t.LinkValue,
  82. build, t.Sound, t.Vibration, t.PassThrough, t.MidFile, progress, t.PushTime, t.ExpireTime, t.Status, t.Group, t.ImageURL, extra); err != nil {
  83. log.Error("d.AddTask(%+v) error(%v)", t, err)
  84. PromError("mysql:添加推送任务")
  85. return
  86. }
  87. id, err = res.LastInsertId()
  88. return
  89. }
  90. // Task loads task by id.
  91. func (d *Dao) Task(ctx context.Context, id int64) (t *pushmdl.Task, err error) {
  92. var (
  93. platform string
  94. build string
  95. progress string
  96. extra string
  97. now = time.Now()
  98. )
  99. t = &pushmdl.Task{Progress: &pushmdl.Progress{}, Extra: &pushmdl.TaskExtra{}}
  100. if err = d.taskStmt.QueryRow(ctx, id).Scan(&id, &t.Job, &t.Type, &t.APPID, &t.BusinessID, &platform, &t.Title, &t.Summary, &t.LinkType, &t.LinkValue, &build,
  101. &t.Sound, &t.Vibration, &t.PassThrough, &t.MidFile, &progress, &t.PushTime, &t.ExpireTime, &t.Status, &t.Group, &t.ImageURL, &extra); err != nil {
  102. if err == sql.ErrNoRows {
  103. t = nil
  104. err = nil
  105. return
  106. }
  107. log.Error("d.taskStmt.QueryRow(%d) error(%v)", id, now, err)
  108. PromError("mysql:按ID查询任务")
  109. return
  110. }
  111. t.ID = strconv.FormatInt(id, 10)
  112. t.Platform = pushmdl.SplitInts(platform)
  113. t.Build = pushmdl.ParseBuild(build)
  114. if progress != "" {
  115. if err = json.Unmarshal([]byte(progress), t.Progress); err != nil {
  116. log.Error("json.Unmarshal(%s) error(%v)", progress, err)
  117. PromError("mysql:unmarshal progress")
  118. }
  119. }
  120. if extra != "" {
  121. if err = json.Unmarshal([]byte(extra), t.Extra); err != nil {
  122. log.Error("json.Unmarshal(%s) extra error(%v)", extra, err)
  123. PromError("mysql:unmarshal extra")
  124. }
  125. }
  126. return
  127. }
  128. // MaxSettingID gets max setting id in DB.
  129. func (d *Dao) MaxSettingID(ctx context.Context) (id int64, err error) {
  130. if err = d.maxSettingIDStmt.QueryRow(ctx).Scan(&id); err != nil {
  131. if err == sql.ErrNoRows {
  132. err = nil
  133. return
  134. }
  135. log.Error("d.maxSettingIDStmt.QueryRow.Scan error(%v)", err)
  136. PromError("db:max setting id")
  137. }
  138. return
  139. }
  140. // SettingsByRange gets user setting by range.
  141. func (d *Dao) SettingsByRange(ctx context.Context, start, end int64) (res map[int64]map[int]int, err error) {
  142. var rows *xsql.Rows
  143. if rows, err = d.settingsByRangeStmt.Query(ctx, start, end); err != nil {
  144. log.Error("d.settingsStmt.Query(%d,%d) error(%v)", start, end, err)
  145. PromError("mysql:Settings")
  146. return
  147. }
  148. defer rows.Close()
  149. res = make(map[int64]map[int]int)
  150. for rows.Next() {
  151. var (
  152. mid int64
  153. v string
  154. st = make(map[int]int)
  155. )
  156. if err = rows.Scan(&mid, &v); err != nil {
  157. PromError("mysql:Settings scan")
  158. log.Error("d.Settings() Scan() error(%v)", err)
  159. return
  160. }
  161. if e := json.Unmarshal([]byte(v), &st); e != nil {
  162. log.Error("d.Settings() json unmarshal(%s) error(%v)", v, st)
  163. continue
  164. }
  165. res[mid] = st
  166. }
  167. err = rows.Err()
  168. return
  169. }