keyword.go 4.3 KB

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