mysql_report.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. package dao
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "time"
  7. "go-common/app/service/main/push/model"
  8. xsql "go-common/library/database/sql"
  9. "go-common/library/log"
  10. "go-common/library/xstr"
  11. )
  12. const (
  13. _batch = 1000
  14. _reportSQL = `SELECT id,app_id,platform_id,mid,buvid,device_token,build,time_zone,notify_switch,device_brand,device_model,os_version,extra,dtime FROM push_reports WHERE token_hash=?`
  15. _reportByIDSQL = `SELECT id,app_id,platform_id,mid,buvid,device_token,build,time_zone,notify_switch,device_brand,device_model,os_version,extra FROM push_reports WHERE id=?`
  16. _reportsSQL = `SELECT id,app_id,platform_id,mid,buvid,device_token,build,time_zone,notify_switch,device_brand,device_model,os_version,extra FROM push_reports WHERE token_hash IN(%s) AND dtime=0`
  17. _lastReportIDSQL = `SELECT id FROM push_reports ORDER BY id DESC LIMIT 1`
  18. _addReportSQL = `INSERT INTO push_reports (app_id,platform_id,mid,buvid,device_token,token_hash,build,time_zone,notify_switch,device_brand,device_model,os_version,extra) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)`
  19. _updateReportSQL = `UPDATE push_reports SET app_id=?,platform_id=?,mid=?,buvid=?,build=?,time_zone=?,notify_switch=?,device_brand=?,device_model=?,os_version=?,extra=?,mtime=?,dtime=0 WHERE id=?`
  20. _delReportSQL = `UPDATE push_reports SET dtime=? WHERE token_hash=?`
  21. _reportsByMidSQL = `SELECT id,app_id,platform_id,mid,buvid,device_token,build,time_zone,notify_switch,device_brand,device_model,os_version,extra FROM push_reports WHERE mid=? AND dtime=0`
  22. _reportsByMidsSQL = `SELECT id,app_id,platform_id,mid,buvid,device_token,build,time_zone,notify_switch,device_brand,device_model,os_version,extra FROM push_reports WHERE mid IN (%s) AND dtime=0`
  23. _reportsByIDSQL = `SELECT id,app_id,platform_id,mid,buvid,device_token,build,time_zone,notify_switch,device_brand,device_model,os_version,extra FROM push_reports WHERE id IN (%s) AND dtime=0`
  24. )
  25. // Report gets report by device_token.
  26. func (d *Dao) Report(c context.Context, dt string) (r *model.Report, err error) {
  27. r = &model.Report{}
  28. th := model.HashToken(dt)
  29. if err = d.reportStmt.QueryRow(c, th).Scan(&r.ID, &r.APPID, &r.PlatformID, &r.Mid, &r.Buvid,
  30. &r.DeviceToken, &r.Build, &r.TimeZone, &r.NotifySwitch, &r.DeviceBrand, &r.DeviceModel, &r.OSVersion, &r.Extra, &r.Dtime); err != nil {
  31. if err == sql.ErrNoRows {
  32. err = nil
  33. r = nil
  34. return
  35. }
  36. log.Error("d.reportStmt.QueryRow(%s) error(%v)", dt, err)
  37. PromError("mysql:获取上报")
  38. }
  39. return
  40. }
  41. // ReportByID gets report by id.
  42. func (d *Dao) ReportByID(c context.Context, id int64) (r *model.Report, err error) {
  43. r = &model.Report{}
  44. if err = d.reportByIDStmt.QueryRow(c, id).Scan(&r.ID, &r.APPID, &r.PlatformID, &r.Mid, &r.Buvid,
  45. &r.DeviceToken, &r.Build, &r.TimeZone, &r.NotifySwitch, &r.DeviceBrand, &r.DeviceModel, &r.OSVersion, &r.Extra); err != nil {
  46. if err == sql.ErrNoRows {
  47. err = nil
  48. r = nil
  49. return
  50. }
  51. log.Error("d.reportByIDStmt.QueryRow(%d) error(%v)", id, err)
  52. PromError("mysql:获取上报")
  53. }
  54. return
  55. }
  56. // Reports gets reports by device_token.
  57. func (d *Dao) Reports(c context.Context, dts []string) (rs []*model.Report, err error) {
  58. var ths []int64
  59. for _, dt := range dts {
  60. ths = append(ths, model.HashToken(dt))
  61. }
  62. s := fmt.Sprintf(_reportsSQL, xstr.JoinInts(ths))
  63. var rows *xsql.Rows
  64. if rows, err = d.db.Query(c, s); err != nil {
  65. log.Error("d.reports Query() error(%v)", err)
  66. PromError("mysql:通过tokens获取上报Query")
  67. return
  68. }
  69. defer rows.Close()
  70. for rows.Next() {
  71. r := &model.Report{}
  72. if err = rows.Scan(&r.ID, &r.APPID, &r.PlatformID, &r.Mid, &r.Buvid, &r.DeviceToken,
  73. &r.Build, &r.TimeZone, &r.NotifySwitch, &r.DeviceBrand, &r.DeviceModel, &r.OSVersion, &r.Extra); err != nil {
  74. log.Error("d.Reports Scan() error(%v)", err)
  75. PromError("mysql:通过tokens获取上报Scan")
  76. return
  77. }
  78. rs = append(rs, r)
  79. }
  80. return
  81. }
  82. // AddReport adds report.
  83. func (d *Dao) AddReport(ctx context.Context, r *model.Report) (id int64, err error) {
  84. th := model.HashToken(r.DeviceToken)
  85. res, err := d.addReportStmt.Exec(ctx, r.APPID, r.PlatformID, r.Mid, r.Buvid, r.DeviceToken, th, r.Build, r.TimeZone, r.NotifySwitch, r.DeviceBrand, r.DeviceModel, r.OSVersion, r.Extra)
  86. if err != nil {
  87. log.Error("d.AddReport(%+v) error(%v)", r, err)
  88. PromError("mysql:AddReport")
  89. return
  90. }
  91. PromInfo("mysql:AddReport")
  92. return res.LastInsertId()
  93. }
  94. // UpdateReport update report.
  95. func (d *Dao) UpdateReport(ctx context.Context, r *model.Report) (err error) {
  96. if _, err = d.updateReportStmt.Exec(ctx, r.APPID, r.PlatformID, r.Mid, r.Buvid, r.Build, r.TimeZone, r.NotifySwitch, r.DeviceBrand, r.DeviceModel, r.OSVersion, r.Extra, time.Now(), r.ID); err != nil {
  97. log.Error("UpdateReport(%+v) error(%v)", r, err)
  98. PromError("mysql:UpdateReport")
  99. return
  100. }
  101. PromInfo("mysql:UpdateReport")
  102. return
  103. }
  104. // DelReport delete report.
  105. func (d *Dao) DelReport(c context.Context, dt string) (rows int64, err error) {
  106. th := model.HashToken(dt)
  107. now := time.Now().Unix()
  108. var res sql.Result
  109. if res, err = d.delReportStmt.Exec(c, now, th); err != nil {
  110. log.Error("d.delReportStmt.Exec(%s,%d) error(%v)", dt, th, err)
  111. PromError("mysql:删除上报")
  112. return
  113. }
  114. return res.RowsAffected()
  115. }
  116. // ReportsByMid gets reports by mid.
  117. func (d *Dao) ReportsByMid(c context.Context, mid int64) (res []*model.Report, err error) {
  118. var rows *xsql.Rows
  119. if rows, err = d.reportsByMidStmt.Query(c, mid); err != nil {
  120. log.Error("d.reportsByMid Query() error(%v)", err)
  121. PromError("mysql:通过mid获取上报Query")
  122. return
  123. }
  124. defer rows.Close()
  125. for rows.Next() {
  126. r := &model.Report{}
  127. if err = rows.Scan(&r.ID, &r.APPID, &r.PlatformID, &r.Mid, &r.Buvid, &r.DeviceToken,
  128. &r.Build, &r.TimeZone, &r.NotifySwitch, &r.DeviceBrand, &r.DeviceModel, &r.OSVersion, &r.Extra); err != nil {
  129. log.Error("d.ReportsByMid Scan() error(%v)", err)
  130. PromError("mysql:通过mid获取上报Scan")
  131. return
  132. }
  133. res = append(res, r)
  134. }
  135. PromInfo("mysql:通过mid获取上报")
  136. return
  137. }
  138. // ReportsByMids gets reports by mids.
  139. func (d *Dao) ReportsByMids(c context.Context, mids []int64) (reports map[int64][]*model.Report, err error) {
  140. reports = make(map[int64][]*model.Report)
  141. for {
  142. midsLen := len(mids)
  143. if midsLen == 0 {
  144. return
  145. }
  146. var part []int64
  147. if midsLen >= _batch {
  148. part = mids[:_batch]
  149. } else {
  150. part = mids[:]
  151. }
  152. mids = mids[len(part):]
  153. s := fmt.Sprintf(_reportsByMidsSQL, xstr.JoinInts(part))
  154. var rows *xsql.Rows
  155. if rows, err = d.db.Query(c, s); err != nil {
  156. log.Error("d.reportsByMids Query() error(%v)", err)
  157. PromError("mysql:通过mids获取上报Query")
  158. return
  159. }
  160. defer rows.Close()
  161. for rows.Next() {
  162. r := &model.Report{}
  163. if err = rows.Scan(&r.ID, &r.APPID, &r.PlatformID, &r.Mid, &r.Buvid, &r.DeviceToken,
  164. &r.Build, &r.TimeZone, &r.NotifySwitch, &r.DeviceBrand, &r.DeviceModel, &r.OSVersion, &r.Extra); err != nil {
  165. log.Error("d.ReportsByMids Scan() error(%v)", err)
  166. PromError("mysql:通过mids获取上报Scan")
  167. return
  168. }
  169. reports[r.Mid] = append(reports[r.Mid], r)
  170. }
  171. PromInfo("mysql:通过mids获取上报")
  172. }
  173. }
  174. // ReportsByID gets reports by mids.
  175. func (d *Dao) ReportsByID(c context.Context, ids []int64) (reports []*model.Report, err error) {
  176. s := fmt.Sprintf(_reportsByIDSQL, xstr.JoinInts(ids))
  177. var rows *xsql.Rows
  178. if rows, err = d.db.Query(c, s); err != nil {
  179. log.Error("d.reportsByID Query() error(%v)", err)
  180. PromError("mysql:通过id获取上报Query")
  181. return
  182. }
  183. defer rows.Close()
  184. for rows.Next() {
  185. r := &model.Report{}
  186. if err = rows.Scan(&r.ID, &r.APPID, &r.PlatformID, &r.Mid, &r.Buvid, &r.DeviceToken,
  187. &r.Build, &r.TimeZone, &r.NotifySwitch, &r.DeviceBrand, &r.DeviceModel, &r.OSVersion, &r.Extra); err != nil {
  188. log.Error("d.ReportsByID Scan() error(%v)", err)
  189. PromError("mysql:通过id获取上报Scan")
  190. return
  191. }
  192. reports = append(reports, r)
  193. }
  194. PromInfo("mysql:通过id获取上报")
  195. return
  196. }