keyword.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package services
  2. import (
  3. "errors"
  4. "fmt"
  5. "hongze/hongze_web_mfyx/models"
  6. "hongze/hongze_web_mfyx/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)
  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 SearchKeywordUserRmindCategoryMsg(user, keyWord)
  37. go KeyWordsWxUserRaiLabelRedisAdd(0, user.UserId, keyWord)
  38. //一分钟之内的相同搜索词不重复记录
  39. //setNX := utils.Rc.SetNX(cacheKey, keyWord, time.Second*3)
  40. //if !setNX {
  41. // go utils.SendAlarmMsg("记录用户搜索关键词失败,设置Redis Key 过期时间失败:key"+cacheKey, 3)
  42. //}
  43. return
  44. }
  45. // AddUserSearchLog 记录用户搜索关键词的日志
  46. func AddUserSearchLog(user *models.WxUserItem, keyWord string, source int) (err error) {
  47. defer func() {
  48. if err != nil {
  49. fmt.Println(err)
  50. go utils.SendAlarmMsg(" 记录用户搜索关键词的日志"+err.Error(), 2)
  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. item.RegisterPlatform = 2
  64. _, err = models.AddSearchKeyWordLog(item)
  65. return
  66. }
  67. // 用户搜索操作操作行为,模板消息推送
  68. func SearchKeywordUserRmind(user *models.WxUserItem, keyWord string) (err error) {
  69. defer func() {
  70. if err != nil {
  71. fmt.Println(err)
  72. go utils.SendAlarmMsg(fmt.Sprint("用户搜索操作操作行为,模板消息推送失败"+err.Error(), "UserId:", user.UserId, "keyWord:", keyWord), 2)
  73. }
  74. }()
  75. countUser, e := models.GetUserRemind(user.UserId)
  76. if e != nil {
  77. err = errors.New("GetUserRemind, Err: " + e.Error())
  78. return
  79. }
  80. if countUser == 0 {
  81. return
  82. }
  83. var first string
  84. var keyword1 string
  85. var keyword2 string
  86. var keyword3 string
  87. var keyword4 string
  88. var remark string
  89. //获取销售手机号
  90. sellerItemQy, e := models.GetSellerByCompanyIdCheckFicc(user.CompanyId, 2)
  91. if e != nil && e.Error() != utils.ErrNoRow() {
  92. err = errors.New("GetSellerByCompanyIdCheckFicc, Err: " + e.Error())
  93. return
  94. }
  95. if sellerItemQy != nil {
  96. sllerAndShareMobileArr, e := GetCompanySellerAndShareMobileByRai(user.CompanyId) //获取所属销售以及对应销售的手机号
  97. if e != nil {
  98. err = errors.New("GetCompanySellerAndShareMobileByRai, Err: " + e.Error())
  99. return
  100. }
  101. sllerAndShareMobiles := strings.Join(sllerAndShareMobileArr, ",")
  102. openIdList, e := models.GetWxOpenIdByMobileList(sllerAndShareMobiles)
  103. if e != nil {
  104. err = errors.New("GetSellerByAdminId, Err: " + e.Error())
  105. return
  106. }
  107. if len(openIdList) == 0 {
  108. return
  109. }
  110. first = fmt.Sprint(user.RealName, "--", user.CompanyName, ",有新的互动:搜索")
  111. keyword1 = first
  112. keyword2 = "搜索关键词:" + keyWord
  113. openIdArr := make([]string, 0)
  114. for _, v := range openIdList {
  115. openIdArr = append(openIdArr, v.OpenId)
  116. }
  117. redirectUrl := ""
  118. sendInfo := new(SendWxTemplate)
  119. sendInfo.First = first
  120. sendInfo.Keyword1 = keyword1
  121. sendInfo.Keyword2 = keyword2
  122. sendInfo.Keyword3 = keyword3
  123. sendInfo.Keyword4 = keyword4
  124. sendInfo.Remark = remark
  125. sendInfo.TemplateId = utils.WxMsgTemplateIdArticleUserRemind
  126. sendInfo.RedirectUrl = redirectUrl
  127. sendInfo.RedirectTarget = 3
  128. sendInfo.Resource = "0"
  129. sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_ARTICLE_ADD
  130. sendInfo.OpenIdArr = openIdArr
  131. err = PublicSendTemplateMsg(sendInfo)
  132. if err != nil {
  133. return
  134. }
  135. }
  136. return
  137. }