wx_user_rai_label.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. // 添加用户阅读文章标签到Redis
  29. func ArticleWxUserRaiLabelRedisAdd(sourceId, uid int, createTime time.Time) (err error) {
  30. defer func() {
  31. if err != nil {
  32. fmt.Println(err)
  33. msg := fmt.Sprint("sourceId:", sourceId, "userId:", uid)
  34. go utils.SendAlarmMsg("用户关注产业更新相关标签,写入Redis队列消息失败:"+err.Error()+msg, 2)
  35. }
  36. }()
  37. log := &models.WxUserRaiLabelRedis{UserId: uid, SourceId: sourceId, SourceType: 1, TableName: utils.CYGX_OBJ_ARTICLE, CreateTime: createTime, RegisterPlatform: utils.REGISTER_PLATFORM}
  38. if utils.Re == nil {
  39. err := utils.Rc.LPush(utils.WX_USER_RAI_LABEL_KEY, log)
  40. if err != nil {
  41. fmt.Println("WxUserRaiLabelRedis LPush Err:" + err.Error())
  42. }
  43. }
  44. return
  45. }
  46. func UpdateWxUserRaiLabelRedis() (err error) {
  47. for {
  48. // SourceType int `description:"来源1:搜索关键字标签、2:产业/个股标签(线下活动)、3:产业/个股标签(线下路演)、4:产业/个股标签(线上活动)、5:产业/个股标签(线上路演)、6:销售输入标签、7:产业/个股标签(报告)、8:报告类型标签"`
  49. utils.Rc.Brpop(utils.WX_USER_RAI_LABEL_KEY, func(b []byte) {
  50. var log models.WxUserRaiLabelRedis
  51. if err := json.Unmarshal(b, &log); err != nil {
  52. fmt.Println("json unmarshal wrong!")
  53. go utils.SendAlarmMsg("用户更新相关标签处理Redis队列消息失败:"+err.Error()+string(b), 2)
  54. }
  55. if log.TableName == "" {
  56. switch log.SourceType {
  57. case 1:
  58. go KeyWordsWxUserRaiLabelRedisAddReduce(log)
  59. fmt.Println("搜索关键词")
  60. break
  61. //case 2:
  62. // go IndustryFllowUserLabelLogReduce(log)
  63. // fmt.Println("2产业关注")
  64. // break
  65. //case 3:
  66. // go ActivityUserLabelLogReduce(log)
  67. // fmt.Println("活动到会")
  68. // break
  69. //case 4:
  70. // go CategoryFllowUserLabelLogReduce(log)
  71. // fmt.Println("4系列关注")
  72. // break
  73. //case 5:
  74. // go ActivitySpecialUserLabelLogReduce(log)
  75. // fmt.Println("5专项调研活动到会")
  76. // break
  77. default:
  78. fmt.Println(string(b))
  79. go utils.SendAlarmMsg("用户更新相关标签处理Redis队列消息失败:"+string(b), 2)
  80. }
  81. } else {
  82. switch log.TableName {
  83. case utils.CYGX_OBJ_ARTICLE:
  84. go ArticleWxUserRaiLabelRedisAddReduce(log)
  85. fmt.Println("阅读文章")
  86. }
  87. }
  88. })
  89. }
  90. }
  91. // 1:搜索关键词
  92. func KeyWordsWxUserRaiLabelRedisAddReduce(log models.WxUserRaiLabelRedis) (err error) {
  93. defer func() {
  94. if err != nil {
  95. fmt.Println(err)
  96. go utils.SendAlarmMsg("用户文章阅读更新相关标签,处理Redis队列消息失败:"+err.Error()+fmt.Sprint("articleId", log.SourceId, "userId", log.UserId), 2)
  97. }
  98. }()
  99. userId := log.UserId
  100. label := log.Label
  101. if userId == 0 {
  102. return
  103. }
  104. wxUser, e := models.GetWxUserItemByUserId(userId)
  105. if e != nil {
  106. err = errors.New("GetWxUserItemByUserId" + e.Error())
  107. return
  108. }
  109. item := new(models.WxUserRaiLabel)
  110. item.UserId = wxUser.UserId
  111. item.RealName = wxUser.RealName
  112. item.Mobile = wxUser.Mobile
  113. item.Email = wxUser.Email
  114. item.CompanyId = wxUser.CompanyId
  115. item.CompanyName = wxUser.CompanyName
  116. item.Label = label
  117. item.SourceType = log.SourceType
  118. item.CreateTime = log.CreateTime
  119. item.ModifyTime = time.Now()
  120. item.RegisterPlatform = log.RegisterPlatform
  121. err = models.AddWxUserRaiLabel(item)
  122. if e != nil {
  123. err = errors.New("AddWxUserRaiLabel" + e.Error())
  124. return
  125. }
  126. return
  127. }
  128. // 7:产业/个股标签(报告)、8:报告类型标签
  129. func ArticleWxUserRaiLabelRedisAddReduce(log models.WxUserRaiLabelRedis) (err error) {
  130. defer func() {
  131. if err != nil {
  132. fmt.Println(err)
  133. go utils.SendAlarmMsg("用户文章阅读更新相关标签,处理Redis队列消息失败:"+err.Error()+fmt.Sprint("articleId", log.SourceId, "userId", log.UserId), 2)
  134. }
  135. }()
  136. userId := log.UserId
  137. sourceId := log.SourceId
  138. wxUser, e := models.GetWxUserItemByUserId(userId)
  139. if e != nil {
  140. err = errors.New("GetWxUserItemByUserId" + e.Error())
  141. return
  142. }
  143. articleDetail, e := models.GetArticleDetailTestById(sourceId)
  144. if e != nil {
  145. err = errors.New("GetArticleDetailTestById" + e.Error())
  146. return
  147. }
  148. articlePermission, e := models.GetArticlePermission(articleDetail.CategoryId)
  149. if e != nil {
  150. err = errors.New("GetArticlePermission" + e.Error())
  151. return
  152. }
  153. if articlePermission == nil {
  154. err = errors.New("报告权限不存在" + e.Error())
  155. return
  156. }
  157. articlePermissionName := articlePermission.PermissionName
  158. matchTypeName := articlePermission.MatchTypeName
  159. if articlePermissionName == utils.CE_LUE_NAME || articlePermissionName == utils.GU_SHOU_NAME { // 策略、固收的所有报告,以报告类型做标签
  160. item := new(models.WxUserRaiLabel)
  161. item.UserId = wxUser.UserId
  162. item.RealName = wxUser.RealName
  163. item.Mobile = wxUser.Mobile
  164. item.Email = wxUser.Email
  165. item.CompanyId = wxUser.CompanyId
  166. item.CompanyName = wxUser.CompanyName
  167. item.Label = matchTypeName
  168. item.SourceType = 8
  169. item.CreateTime = log.CreateTime
  170. item.ModifyTime = time.Now()
  171. item.RegisterPlatform = log.RegisterPlatform
  172. item.TableName = "cygx_article"
  173. err = models.AddWxUserRaiLabel(item)
  174. if e != nil {
  175. err = errors.New("AddWxUserRaiLabel" + e.Error())
  176. return
  177. }
  178. }
  179. return
  180. }