wx_user_rai_label.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package services
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "hongze/hongze_cygx/models"
  7. "hongze/hongze_cygx/utils"
  8. "time"
  9. )
  10. // 添加用户搜索标签到Redis
  11. func KeyWordsWxUserRaiLabelRedisAdd(sourceId, uid int, label string) (err error) {
  12. defer func() {
  13. if err != nil {
  14. fmt.Println(err)
  15. msg := fmt.Sprint("sourceId:", sourceId, "userId:", uid)
  16. go utils.SendAlarmMsg("用户关注产业更新相关标签,写入Redis队列消息失败:"+err.Error()+msg, 2)
  17. }
  18. }()
  19. log := &models.WxUserRaiLabelRedis{UserId: uid, SourceId: sourceId, SourceType: 1, Label: label, CreateTime: time.Now(), RegisterPlatform: utils.REGISTER_PLATFORM}
  20. if utils.Re == nil {
  21. err := utils.Rc.LPush(utils.WX_USER_RAI_LABEL_KEY, log)
  22. if err != nil {
  23. fmt.Println("WxUserRaiLabelRedis LPush Err:" + err.Error())
  24. }
  25. }
  26. return
  27. }
  28. func UpdateWxUserRaiLabelRedis() (err error) {
  29. for {
  30. //SourceType int `description:"1:文章阅读、 2产业关注、3:活动到会、4系列关注、5专项调研活动到会。"`
  31. utils.Rc.Brpop(utils.WX_USER_RAI_LABEL_KEY, func(b []byte) {
  32. var log models.WxUserRaiLabelRedis
  33. if err := json.Unmarshal(b, &log); err != nil {
  34. fmt.Println("json unmarshal wrong!")
  35. go utils.SendAlarmMsg("用户更新相关标签处理Redis队列消息失败:"+err.Error()+string(b), 2)
  36. }
  37. switch log.SourceType {
  38. case 1:
  39. go KeyWordsWxUserRaiLabelRedisAddReduce(log)
  40. fmt.Println("文章阅读")
  41. break
  42. //case 2:
  43. // go IndustryFllowUserLabelLogReduce(log)
  44. // fmt.Println("2产业关注")
  45. // break
  46. //case 3:
  47. // go ActivityUserLabelLogReduce(log)
  48. // fmt.Println("活动到会")
  49. // break
  50. //case 4:
  51. // go CategoryFllowUserLabelLogReduce(log)
  52. // fmt.Println("4系列关注")
  53. // break
  54. //case 5:
  55. // go ActivitySpecialUserLabelLogReduce(log)
  56. // fmt.Println("5专项调研活动到会")
  57. // break
  58. default:
  59. fmt.Println(string(b))
  60. go utils.SendAlarmMsg("用户更新相关标签处理Redis队列消息失败:"+string(b), 2)
  61. }
  62. })
  63. }
  64. }
  65. // 1:搜索关键词
  66. func KeyWordsWxUserRaiLabelRedisAddReduce(log models.WxUserRaiLabelRedis) (err error) {
  67. defer func() {
  68. if err != nil {
  69. fmt.Println(err)
  70. go utils.SendAlarmMsg("用户文章阅读更新相关标签,处理Redis队列消息失败:"+err.Error()+fmt.Sprint("articleId", log.SourceId, "userId", log.UserId), 2)
  71. }
  72. }()
  73. userId := log.UserId
  74. label := log.Label
  75. if userId == 0 {
  76. return
  77. }
  78. wxUser, e := models.GetWxUserItemByUserId(userId)
  79. if e != nil {
  80. err = errors.New("GetWxUserItemByUserId" + e.Error())
  81. return
  82. }
  83. item := new(models.WxUserRaiLabel)
  84. item.UserId = wxUser.UserId
  85. item.RealName = wxUser.RealName
  86. item.Mobile = wxUser.Mobile
  87. item.Email = wxUser.Email
  88. item.CompanyId = wxUser.CompanyId
  89. item.CompanyName = wxUser.CompanyName
  90. item.Label = label
  91. item.SourceType = log.SourceType
  92. item.CreateTime = log.CreateTime
  93. item.ModifyTime = time.Now()
  94. item.RegisterPlatform = log.RegisterPlatform
  95. err = models.AddWxUserRaiLabel(item)
  96. if e != nil {
  97. err = errors.New("AddWxUserRaiLabel" + e.Error())
  98. return
  99. }
  100. return
  101. }