keyword.go 4.4 KB

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