micro_roadshow_video.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package cygx
  2. import (
  3. "errors"
  4. "fmt"
  5. "hongze/hz_crm_api/models/company"
  6. "hongze/hz_crm_api/models/cygx"
  7. "hongze/hz_crm_api/services"
  8. "hongze/hz_crm_api/services/alarm_msg"
  9. "hongze/hz_crm_api/utils"
  10. "strconv"
  11. )
  12. //func init() {
  13. // SendWxMsgWithCygxMicroRoadshowVideo(52)
  14. //}
  15. // SendWxMsgWithCygxMicroRoadshowVideo 后台发布新的产业视频时,对关注了该产业的用户,发送模板消息。
  16. func SendWxMsgWithCygxMicroRoadshowVideo(videoId int) (err error) {
  17. defer func() {
  18. if err != nil {
  19. fmt.Println("err:", err)
  20. go alarm_msg.SendAlarmMsg("查研观向处理活动是否进行模板消息推送,发送模版消息失败,Err:"+err.Error(), 3)
  21. utils.FileLog.Info(fmt.Sprintf("发送模版消息失败,Err:%s,%s", err.Error()))
  22. }
  23. }()
  24. detail, e := cygx.GetMicroRoadshowVideoByVideoId(videoId)
  25. if e != nil {
  26. err = errors.New("获取活动详情失败" + e.Error())
  27. return
  28. }
  29. if detail == nil {
  30. err = errors.New("获取活动详情失败")
  31. return
  32. }
  33. if detail.IsSendWxMsg == 1 {
  34. return
  35. }
  36. permissionStr, e := company.GetPermissionIdById(strconv.Itoa(detail.ChartPermissionId))
  37. if e != nil {
  38. err = errors.New("获取主客观权限失败" + e.Error())
  39. return
  40. }
  41. // 获取所有有权的用户的 openid
  42. openidPowerList, e := cygx.GetCygxUserRecordPower(permissionStr)
  43. if e != nil {
  44. err = errors.New("获取所有有权的用户的 openid失败" + e.Error())
  45. return
  46. }
  47. //获取关注对应产业的用户信息
  48. industryFllowList, e := cygx.GetCygxUserindustryFllowOpenid(detail.IndustryId)
  49. if e != nil {
  50. err = errors.New("获取关注对应产业的用户信息失败 " + e.Error())
  51. return
  52. }
  53. mapOpenidFllow := make(map[int]string)
  54. for _, v := range industryFllowList {
  55. mapOpenidFllow[v.UserId] = v.OpenId
  56. }
  57. //获取拒绝接收推送的的用户的 openid
  58. mapOpenidRefuset := make(map[int]string)
  59. openidRefusetList, e := cygx.GetCygxUserRefusetOpenid()
  60. if e != nil && e.Error() != utils.ErrNoRow() {
  61. err = errors.New("获取拒绝接收推送的的用户的 openid 失败" + e.Error())
  62. return
  63. }
  64. for _, v := range openidRefusetList {
  65. mapOpenidRefuset[v.UserId] = v.OpenId
  66. }
  67. //获取提交过推送规则的用户的 openid
  68. mapUserIdChooseSend := make(map[int]int)
  69. chooseSendtList, err := cygx.GetCygxXzsChooseSend("")
  70. if err != nil && err.Error() != utils.ErrNoRow() {
  71. return err
  72. }
  73. for _, v := range chooseSendtList {
  74. mapUserIdChooseSend[v.UserId] = v.UserId
  75. }
  76. openIdArr := make([]string, len(openidPowerList))
  77. for i, v := range openidPowerList {
  78. if _, ok := mapOpenidRefuset[v.UserId]; ok {
  79. continue //如果用户选择了拒绝推送消息,那么就不做模板消息推送
  80. }
  81. if _, ok := mapUserIdChooseSend[v.UserId]; ok {
  82. if _, ok := mapOpenidFllow[v.UserId]; !ok {
  83. continue //如果用户提交过关注信息,而且这个产业他没有关注,那么不做推送
  84. }
  85. }
  86. openIdArr[i] = v.OpenId
  87. }
  88. first := "【" + detail.IndustryName + "】产业,发布了5min逻辑解析视频,欢迎查看"
  89. keyword1 := "5min【" + detail.IndustryName + "】逻辑解析"
  90. keyword2 := "已发布"
  91. remark := "点击进入产业资源包详情页"
  92. redirectUrl := utils.WX_MSG_PATH_INDUSTRY_DETAIL + strconv.Itoa(detail.IndustryId) + "&IsSendWx=1"
  93. sendInfo := new(services.SendWxTemplate)
  94. sendInfo.First = first
  95. sendInfo.Keyword1 = keyword1
  96. sendInfo.Keyword2 = keyword2
  97. sendInfo.Remark = remark
  98. sendInfo.TemplateId = utils.WxMsgTemplateIdActivityChangeApplyXzs
  99. sendInfo.RedirectUrl = redirectUrl
  100. sendInfo.RedirectTarget = 3
  101. sendInfo.Resource = strconv.Itoa(detail.VideoId)
  102. sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_ROADSHOW_VIDEO
  103. sendInfo.OpenIdArr = openIdArr
  104. e = services.SendTemplateMsg(sendInfo)
  105. if e != nil {
  106. err = errors.New("推送模板消息失败" + e.Error())
  107. return
  108. }
  109. e = cygx.UpdateIsSendWxMsg(videoId, 1)
  110. if e != nil {
  111. err = errors.New("修改推送状态失败" + e.Error())
  112. return
  113. }
  114. return
  115. }