push.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. package dao
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "go-common/app/service/bbq/notice-service/api/v1"
  10. push "go-common/app/service/bbq/push/api/grpc/v1"
  11. "go-common/library/cache/redis"
  12. "go-common/library/log"
  13. )
  14. const (
  15. _queryUserPushDev = "select `reg_id`, `sdk`, `platform` from `user_push_device` where `mid` = ? and `state` = 0 order by `last_login_time` desc limit 1;"
  16. _queryUserName = "select `uname` from `user_base` where mid in %s;"
  17. )
  18. // PushNotice .
  19. func (d *Dao) PushNotice(c context.Context, req *push.NotificationRequest) (result []*push.PushResult, err error) {
  20. resp, err := d.pushClient.AsyncNotification(c, req)
  21. if err != nil {
  22. log.Errorv(c, log.KV("log", "push notification error"), log.KV("error", err))
  23. return
  24. }
  25. result = resp.Result
  26. for _, item := range result {
  27. if item.Error != nil {
  28. log.Errorv(c, log.KV("log", "push device notifiaction error"), log.KV("error", item.Error))
  29. }
  30. }
  31. return
  32. }
  33. // PushMessage .
  34. func (d *Dao) PushMessage(c context.Context, req *push.MessageRequest) (result []*push.PushResult, err error) {
  35. resp, err := d.pushClient.AsyncMessage(c, req)
  36. if err != nil {
  37. log.Errorv(c, log.KV("log", "push message error"), log.KV("error", err))
  38. return
  39. }
  40. result = resp.Result
  41. for _, item := range result {
  42. if item.Error != nil {
  43. log.Errorv(c, log.KV("log", "push device message error"), log.KV("error", item.Error))
  44. }
  45. }
  46. return nil, err
  47. }
  48. // FetchPushDev .
  49. func (d *Dao) FetchPushDev(c context.Context, mid int64) (result *push.Device, err error) {
  50. result = &push.Device{}
  51. err = d.db.QueryRow(c, _queryUserPushDev, mid).Scan(&result.RegisterID, &result.SDK, &result.Platform)
  52. if err == sql.ErrNoRows {
  53. err = nil
  54. log.Warnw(c, "log", "no row in push", "sql", _queryUserPushDev, "mid", mid)
  55. }
  56. return
  57. }
  58. // IncrDailyPushCount .
  59. func (d *Dao) IncrDailyPushCount(c context.Context, mid int64) (count int, err error) {
  60. dt := time.Now().Format("20060102")
  61. key := fmt.Sprintf("bbq:push:u:%d:%s:count", mid, dt)
  62. conn := d.redis.Get(c)
  63. defer conn.Close()
  64. count, err = redis.Int(conn.Do("INCR", key, 1))
  65. if err != nil {
  66. return
  67. }
  68. _, err = conn.Do("EXPIRE", 86400)
  69. return
  70. }
  71. // IncrHourPushAction .
  72. func (d *Dao) IncrHourPushAction(c context.Context, mid int64, noticeType int32, t int) (count int, err error) {
  73. dt := time.Now().Format("2006010215")
  74. key := fmt.Sprintf("bbq:push:u:%d:%s:action:%d", mid, dt, noticeType)
  75. conn := d.redis.Get(c)
  76. defer conn.Close()
  77. count, err = redis.Int(conn.Do("INCR", key))
  78. if err != nil {
  79. return
  80. }
  81. _, err = conn.Do("EXPIRE", t)
  82. return
  83. }
  84. // ClearHourPushAction .
  85. func (d *Dao) ClearHourPushAction(c context.Context, mid int64, noticeType int32) error {
  86. dt := time.Now().Format("2006010215")
  87. key := fmt.Sprintf("bbq:push:u:%d:%s:action:%d", mid, dt, noticeType)
  88. conn := d.redis.Get(c)
  89. defer conn.Close()
  90. _, err := conn.Do("DEL", key)
  91. return err
  92. }
  93. // SetPushActionMid .
  94. func (d *Dao) SetPushActionMid(c context.Context, mid int64, actionMid int64, noticeType int32) error {
  95. dt := time.Now().Format("2006010215")
  96. key := fmt.Sprintf("bbq:v1:push:u:%d:%s:action:%d", mid, dt, noticeType)
  97. conn := d.redis.Get(c)
  98. defer conn.Close()
  99. values, _ := redis.Values(conn.Do("HGETALL", key))
  100. if len(values) >= 4 {
  101. return nil
  102. }
  103. _, err := conn.Do("HMSET", key, actionMid, actionMid)
  104. return err
  105. }
  106. // GetPushActionMid .
  107. func (d *Dao) GetPushActionMid(c context.Context, mid int64, noticeType int32) ([]int64, error) {
  108. dt := time.Now().Format("2006010215")
  109. key := fmt.Sprintf("bbq:v1:push:u:%d:%s:action:%d", mid, dt, noticeType)
  110. conn := d.redis.Get(c)
  111. defer conn.Close()
  112. m, err := redis.Int64Map(conn.Do("HGETALL", key))
  113. if err != nil {
  114. return nil, err
  115. }
  116. result := make([]int64, 0)
  117. for _, v := range m {
  118. result = append(result, v)
  119. }
  120. return result, nil
  121. }
  122. // ClearPushActionMid .
  123. func (d *Dao) ClearPushActionMid(c context.Context, mid int64, noticeType int32) error {
  124. conn := d.redis.Get(c)
  125. defer conn.Close()
  126. dt := time.Now().Format("2006010215")
  127. key := fmt.Sprintf("bbq:v1:push:u:%d:%s:action:%d", mid, dt, noticeType)
  128. _, err := conn.Do("DEL", key)
  129. return err
  130. }
  131. // GetUserName .
  132. func (d *Dao) GetUserName(c context.Context, midList []int64, count int) (names []string, err error) {
  133. mids := []string{}
  134. list := midList
  135. if len(midList) > count {
  136. list = midList[:count]
  137. }
  138. for _, v := range list {
  139. mids = append(mids, strconv.Itoa(int(v)))
  140. }
  141. where := "(" + strings.Join(mids, ",") + ")"
  142. row, err := d.db.Query(c, fmt.Sprintf(_queryUserName, where))
  143. if err != nil {
  144. return
  145. }
  146. for row.Next() {
  147. n := ""
  148. err = row.Scan(&n)
  149. if err != nil {
  150. return
  151. }
  152. names = append(names, n)
  153. }
  154. return
  155. }
  156. // FetchUserPushDev .
  157. func (d *Dao) FetchUserPushDev(c context.Context, mid int64, buvid string) (result *v1.UserPushDev, err error) {
  158. querySQL := "select `id` from `user_push_device` where `mid` = ? and `buvid` = ?"
  159. row := d.db.QueryRow(c, querySQL, mid, buvid)
  160. result = &v1.UserPushDev{}
  161. row.Scan(&result.Id)
  162. return
  163. }
  164. // InsertUserPushDev .
  165. func (d *Dao) InsertUserPushDev(c context.Context, req *v1.UserPushDev) (int64, error) {
  166. _insertUserPushDev := "insert into `user_push_device` (`mid`, `reg_id`, `buvid`, `sdk`, `platform`) values (?, ?, ?, ?, ?);"
  167. result, err := d.db.Exec(c, _insertUserPushDev, req.Mid, req.RegisterId, req.Buvid, req.Sdk, req.Platform)
  168. if err != nil {
  169. return 0, err
  170. }
  171. return result.LastInsertId()
  172. }
  173. // UpdateUserPushDev .
  174. func (d *Dao) UpdateUserPushDev(c context.Context, req *v1.UserPushDev) (int64, error) {
  175. _updateUserPushDev := "update `user_push_device` set `reg_id`=?,`sdk`=?,`platform`=?,`state`=?,`last_login_time`=now() where `mid`=? and `buvid`=?"
  176. result, err := d.db.Exec(c, _updateUserPushDev, req.RegisterId, req.Sdk, req.Platform, req.State, req.Mid, req.Buvid)
  177. if err != nil {
  178. return 0, err
  179. }
  180. return result.RowsAffected()
  181. }
  182. // DeleteUserPushDev .
  183. func (d *Dao) DeleteUserPushDev(c context.Context, req *v1.UserPushDev) (int64, error) {
  184. _updateUserPushDev := "update `user_push_device` set `state`=1 where `mid`=? and `buvid`=?"
  185. result, err := d.db.Exec(c, _updateUserPushDev, req.Mid, req.Buvid)
  186. if err != nil {
  187. return 0, err
  188. }
  189. return result.RowsAffected()
  190. }