stat.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "math"
  6. "net/url"
  7. "strconv"
  8. "go-common/app/job/main/relation/model"
  9. "go-common/library/log"
  10. )
  11. // DelStatCache is
  12. func (d *Dao) DelStatCache(mid int64) (err error) {
  13. params := url.Values{}
  14. params.Set("mid", strconv.FormatInt(mid, 10))
  15. var res struct {
  16. Code int `json:"code"`
  17. }
  18. if err = d.client.Post(context.TODO(), d.clearStatPath, "", params, &res); err != nil {
  19. log.Error("d.client.Post error(%v)", err)
  20. return
  21. }
  22. if res.Code != 0 {
  23. log.Error("url(%s) res code(%d) or res.result(%v)", d.clearStatPath+"?"+params.Encode(), res.Code)
  24. }
  25. return
  26. }
  27. // FollowerAchieve is
  28. func (d *Dao) FollowerAchieve(c context.Context, mid, follower int64) {
  29. // 不为 0 结尾的就不检查了
  30. if follower%10 != 0 {
  31. return
  32. }
  33. flag := model.AchieveFromFollower(follower)
  34. if flag <= 0 {
  35. log.Warn("No achieve flag achieved with mid: %d, follower: %d", mid, follower)
  36. return
  37. }
  38. effected, err := d.UserSetAchieveFlag(c, mid, uint64(flag))
  39. if err != nil {
  40. log.Error("Failed to set user achieve flag: mid: %d, flag: %d: %+v", mid, flag, err)
  41. return
  42. }
  43. if effected <= 0 {
  44. log.Info("Already achieved with mid: %d, flag: %d", mid, flag)
  45. return
  46. }
  47. msg := func() string {
  48. switch flag {
  49. case model.FollowerAchieve1k:
  50. return `恭喜您,您的粉丝已经达到1000粉!`
  51. case model.FollowerAchieve5k:
  52. return `恭喜您,您的粉丝已经达到5000粉!`
  53. case model.FollowerAchieve10k:
  54. return `恭喜您,您的粉丝已经达到1万粉!您将有机会获得UP主粉丝成就奖“一万粉丝成就奖励”, #{戳我领取吧!}{"https://www.bilibili.com/blackboard/activity-zxIQ8otdK.html#/"}`
  55. case model.FollowerAchieve100k:
  56. return `恭喜您,您的粉丝已经达到10万粉!您将有机会获得UP主粉丝成就奖“十万粉丝成就奖励”, #{戳我领取吧!}{"https://www.bilibili.com/blackboard/activity-zxIQ8otdK.html#/"}`
  57. case model.FollowerAchieve1000k:
  58. return `恭喜您,您的粉丝已经达到100万粉!您将有机会获得UP主粉丝成就奖“百万粉丝成就奖励”, #{戳我领取吧!}{"https://www.bilibili.com/blackboard/activity-zxIQ8otdK.html#/"}`
  59. }
  60. if flag >= model.FollowerAchieve100k {
  61. return fmt.Sprintf(`恭喜您,您的粉丝已达%d万粉!`, int64((math.Log2(float64(flag))-2)*100000/10000))
  62. }
  63. return ""
  64. }()
  65. if msg != "" {
  66. log.Info("Follower achieve send message to mid: %d: %s", mid, msg)
  67. d.SendMsg(c, mid, "粉丝增长通知", msg)
  68. }
  69. d.ensureAllFollowerAchieve(c, mid, follower)
  70. }
  71. func (d *Dao) ensureAllFollowerAchieve(c context.Context, mid int64, follower int64) {
  72. flags := model.AllAchieveFromFollower(follower)
  73. v := model.AchieveFlag(0)
  74. for _, f := range flags {
  75. v |= f
  76. }
  77. effected, err := d.UserSetAchieveFlag(c, mid, uint64(v))
  78. if err != nil {
  79. log.Error("Failed to ensure user achieve flag: mid: %d, flags: %+v, follower: %d: %+v", mid, flags, follower, err)
  80. return
  81. }
  82. if effected >= 0 {
  83. log.Warn("Achieve missed on mid: %d, follower: %d, flags: %+v", mid, follower, flags)
  84. return
  85. }
  86. }
  87. // SendMsg send message.
  88. func (d *Dao) SendMsg(c context.Context, mid int64, title string, context string) (err error) {
  89. params := url.Values{}
  90. params.Set("mc", "2_5_1")
  91. params.Set("title", title)
  92. params.Set("data_type", "4")
  93. params.Set("context", context)
  94. params.Set("mid_list", fmt.Sprintf("%d", mid))
  95. var res struct {
  96. Code int `json:"code"`
  97. }
  98. if err = d.client.Post(c, d.followersNotify, "", params, &res); err != nil || res.Code != 0 {
  99. log.Error("sendMsgURL(%s) code(%d) error(%v)", d.followersNotify+"?"+params.Encode(), res.Code, err)
  100. return
  101. }
  102. log.Info("d.sendMsgURL url(%s) res(%d)", d.followersNotify+"?"+params.Encode(), res.Code)
  103. return
  104. }