keyword.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package services
  2. import (
  3. "errors"
  4. "fmt"
  5. "hongze/hongze_cygx/models"
  6. "hongze/hongze_cygx/utils"
  7. "strings"
  8. "time"
  9. )
  10. // AddSearchKeyWord 记录用户搜索关键词
  11. func AddSearchKeyWord(user *models.WxUserItem, keyWord string, source int) (err error) {
  12. //cacheKey := fmt.Sprint("Search_uid:", user.UserId, "_KeyWord:", keyWord, "_Source:", source)
  13. //isExist := utils.Rc.IsExist(cacheKey)
  14. //if isExist {
  15. // return err
  16. //}
  17. defer func() {
  18. if err != nil {
  19. go utils.SendAlarmMsg(" 记录用户搜索关键词失败"+err.Error(), 2)
  20. go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "记录用户搜索关键词失败 ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  21. }
  22. }()
  23. keyWordItem := new(models.CygxSearchKeyWord)
  24. keyWordItem.UserId = user.UserId
  25. keyWordItem.KeyWord = keyWord
  26. keyWordItem.CreateTime = time.Now()
  27. keyWordItem.Mobile = user.Mobile
  28. keyWordItem.Email = user.Email
  29. keyWordItem.CompanyId = user.CompanyId
  30. keyWordItem.CompanyName = user.CompanyName
  31. keyWordItem.RealName = user.RealName
  32. keyWordItem.RegisterPlatform = utils.REGISTER_PLATFORM
  33. _, err = models.AddSearchKeyWord(keyWordItem)
  34. go AddUserSearchLog(user, keyWord, source)
  35. go SearchKeywordUserRmind(user, keyWord)
  36. go KeyWordsWxUserRaiLabelRedisAdd(0, user.UserId, keyWord)
  37. ////一分钟之内的相同搜索词不重复记录
  38. //setNX := utils.Rc.SetNX(cacheKey, keyWord, time.Minute*1)
  39. //if !setNX {
  40. // go utils.SendAlarmMsg("记录用户搜索关键词失败,设置Redis Key 过期时间失败:key"+cacheKey, 3)
  41. //}
  42. return
  43. }
  44. // AddUserSearchLog 记录用户搜索关键词的日志
  45. func AddUserSearchLog(user *models.WxUserItem, keyWord string, source int) (err error) {
  46. defer func() {
  47. if err != nil {
  48. fmt.Println(err)
  49. go utils.SendAlarmMsg(" 记录用户搜索关键词的日志"+err.Error(), 2)
  50. go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "修改活动状态至进行中失败 ErrMsg:"+err.Error(), utils.EmailSendToUsers)
  51. }
  52. }()
  53. item := new(models.CygxSearchKeyWordLog)
  54. item.UserId = user.UserId
  55. item.KeyWord = keyWord
  56. item.CreateTime = time.Now()
  57. item.Mobile = user.Mobile
  58. item.Email = user.Email
  59. item.CompanyId = user.CompanyId
  60. item.CompanyName = user.CompanyName
  61. item.RealName = user.RealName
  62. item.Source = source
  63. _, err = models.AddSearchKeyWordLog(item)
  64. return
  65. }
  66. // 用户搜索操作操作行为,模板消息推送
  67. func SearchKeywordUserRmind(user *models.WxUserItem, keyWord string) (err error) {
  68. defer func() {
  69. if err != nil {
  70. fmt.Println(err)
  71. go utils.SendAlarmMsg(fmt.Sprint("用户搜索操作操作行为,模板消息推送失败"+err.Error(), "UserId:", user.UserId, "keyWord:", keyWord), 2)
  72. }
  73. }()
  74. countUser, e := models.GetUserRemind(user.UserId)
  75. if e != nil {
  76. err = errors.New("GetUserRemind, Err: " + e.Error())
  77. return
  78. }
  79. if countUser == 0 {
  80. return
  81. }
  82. var first string
  83. var keyword1 string
  84. var keyword2 string
  85. var keyword3 string
  86. var keyword4 string
  87. var remark string
  88. //获取销售手机号
  89. sellerItemQy, e := models.GetSellerByCompanyIdCheckFicc(user.CompanyId, 2)
  90. if e != nil && e.Error() != utils.ErrNoRow() {
  91. err = errors.New("GetSellerByCompanyIdCheckFicc, Err: " + e.Error())
  92. return
  93. }
  94. if sellerItemQy != nil {
  95. sllerAndShareMobileArr, e := GetCompanySellerAndShareMobileByRai(user.CompanyId) //获取所属销售以及对应销售的手机号
  96. if e != nil {
  97. err = errors.New("GetCompanySellerAndShareMobileByRai, Err: " + e.Error())
  98. return
  99. }
  100. sllerAndShareMobiles := strings.Join(sllerAndShareMobileArr, ",")
  101. openIdList, e := models.GetWxOpenIdByMobileList(sllerAndShareMobiles)
  102. if e != nil {
  103. err = errors.New("GetSellerByAdminId, Err: " + e.Error())
  104. return
  105. }
  106. if len(openIdList) == 0 {
  107. return
  108. }
  109. first = fmt.Sprint("互动:搜索,", user.RealName, "--", user.CompanyName)
  110. keyword1 = "搜索关键词:" + keyWord
  111. keyword2 = first
  112. openIdArr := make([]string, 0)
  113. for _, v := range openIdList {
  114. openIdArr = append(openIdArr, v.OpenId)
  115. }
  116. redirectUrl := ""
  117. sendInfo := new(SendWxTemplate)
  118. sendInfo.First = first
  119. sendInfo.Keyword1 = keyword1
  120. sendInfo.Keyword2 = keyword2
  121. sendInfo.Keyword3 = keyword3
  122. sendInfo.Keyword4 = keyword4
  123. sendInfo.Remark = remark
  124. sendInfo.TemplateId = utils.WxMsgTemplateIdArticleUserRemind
  125. sendInfo.RedirectUrl = redirectUrl
  126. sendInfo.RedirectTarget = 3
  127. sendInfo.Resource = "0"
  128. sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_ARTICLE_ADD
  129. sendInfo.OpenIdArr = openIdArr
  130. err = PublicSendTemplateMsg(sendInfo)
  131. if err != nil {
  132. return
  133. }
  134. }
  135. return
  136. }