keyword.go 4.3 KB

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