activity_wx_msg.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. package cygx
  2. import (
  3. "errors"
  4. "fmt"
  5. "hongze/hz_crm_api/models"
  6. "hongze/hz_crm_api/models/company"
  7. "hongze/hz_crm_api/models/cygx"
  8. "hongze/hz_crm_api/services"
  9. "hongze/hz_crm_api/services/alarm_msg"
  10. "hongze/hz_crm_api/utils"
  11. "strconv"
  12. "time"
  13. )
  14. // 修改活动人数限制之后,对于因为人数已满而失败的用户,推送模板消息
  15. func SendWxMsgWithCygxActivityUpdateLimitPeople(activityId int) (err error) {
  16. var msg string
  17. defer func() {
  18. if err != nil {
  19. fmt.Println(err)
  20. go alarm_msg.SendAlarmMsg("修改活动人数限制之后,对于因为人数已满而失败的用户,推送模板消息,发送模版消息失败,Err:"+err.Error()+";msg:"+msg, 3)
  21. utils.FileLogCygx.Info(fmt.Sprintf("发送模版消息失败,Err:%s,%s", err.Error(), msg))
  22. }
  23. }()
  24. activityInfo, e := cygx.GetAddActivityInfoById(activityId)
  25. if e != nil {
  26. err = errors.New("活动不存在, Err: " + e.Error())
  27. return
  28. }
  29. if activityInfo == nil {
  30. return
  31. }
  32. //已经推送过的不做二次推送
  33. //listSendOpendId, err := models.GeUserTemplateRecordListBysendType(utils.TEMPLATE_MSG_CYGX_ACTIVITY_LIMIT_PEOPLE, strconv.Itoa(activityId))
  34. //已经推送过的不做二次推送
  35. listSendOpendId, err := cygx.GeCygxActivityUserTemplateRecordListByActivityId(activityId, utils.TEMPLATE_MSG_CYGX_ACTIVITY_LIMIT_PEOPLE)
  36. if err != nil && err.Error() != utils.ErrNoRow() {
  37. return err
  38. }
  39. if err != nil && err.Error() != utils.ErrNoRow() {
  40. return err
  41. }
  42. mapSendOpenid := make(map[string]int)
  43. for _, v := range listSendOpendId {
  44. mapSendOpenid[v.OpenId] = v.UserId
  45. }
  46. var condition string
  47. var pars []interface{}
  48. condition = ` AND do_fail_type = 1 AND activity_id = ? `
  49. pars = append(pars, activityId)
  50. listSignup, e := cygx.GetActivitySignupList(condition, pars)
  51. if e != nil {
  52. err = errors.New("GetResourceDataList, Err: " + e.Error())
  53. return
  54. }
  55. var mobileArr []string
  56. for _, v := range listSignup {
  57. if v.Mobile != "" {
  58. mobileArr = append(mobileArr, v.Mobile)
  59. }
  60. }
  61. mobileLen := len(mobileArr)
  62. if mobileLen == 0 {
  63. return
  64. }
  65. //condition = ""
  66. //pars = make([]interface{}, 0)
  67. //condition += ` AND u.mobile IN (` + utils.GetOrmInReplace(mobileLen) + `)`
  68. //pars = append(pars, mobileArr)
  69. openIdList, e := cygx.GetUserRecordListByMobileArr(mobileArr)
  70. if e != nil {
  71. err = errors.New("GetWxOpenIdBList, Err: " + e.Error())
  72. return
  73. }
  74. if len(openIdList) == 0 {
  75. return
  76. }
  77. //openIdArr := make([]string, len(openIdList))
  78. //for i, v := range openIdList {
  79. // if mapSendOpenid[v.OpenId] > 0 {
  80. // continue
  81. // }
  82. // openIdArr[i] = v.OpenId
  83. //}
  84. var openiditems []*cygx.CygxActivityUserTemplateRecord
  85. var openIdArr []string
  86. for _, v := range openIdList {
  87. openIdArr = append(openIdArr, v.OpenId)
  88. openiditem := new(cygx.CygxActivityUserTemplateRecord)
  89. openiditem.ActivityId = activityId
  90. openiditem.UserId = v.UserId
  91. openiditem.OpenId = v.OpenId
  92. openiditem.SendType = utils.TEMPLATE_MSG_CYGX_ACTIVITY_LIMIT_PEOPLE
  93. openiditem.CreateTime = time.Now()
  94. openiditems = append(openiditems, openiditem)
  95. }
  96. first := "您好,本场活动已增加活动名额至" + activityInfo.LimitPeopleNum + "人,欢迎报名"
  97. keyword1 := activityInfo.ActivityName
  98. keyword2 := "已增加活动名额至" + activityInfo.LimitPeopleNum + "人,欢迎报名"
  99. keyword3 := ""
  100. keyword4 := ""
  101. remark := "点击查看活动详情"
  102. redirectUrl := utils.WX_MSG_PATH_ACTIVITY_DETAIL + strconv.Itoa(activityId)
  103. sendInfo := new(services.SendWxTemplate)
  104. sendInfo.First = first
  105. sendInfo.Keyword1 = keyword1
  106. sendInfo.Keyword2 = keyword2
  107. sendInfo.Keyword3 = keyword3
  108. sendInfo.Keyword4 = keyword4
  109. sendInfo.Remark = remark
  110. sendInfo.TemplateId = utils.WxMsgTemplateIdActivityChangeApplyXzs
  111. sendInfo.RedirectUrl = redirectUrl
  112. sendInfo.RedirectTarget = 3
  113. sendInfo.Resource = strconv.Itoa(activityId)
  114. sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_ACTIVITY_LIMIT_PEOPLE
  115. sendInfo.OpenIdArr = openIdArr
  116. e = services.SendTemplateMsg(sendInfo)
  117. if e != nil {
  118. err = errors.New("SendTemplateMsg, Err: " + e.Error())
  119. return
  120. }
  121. //活动发布推送的Openid记录
  122. if len(openiditems) > 0 {
  123. e = cygx.AddCygxActivityUserTemplateRecordMulti(openiditems)
  124. if e != nil {
  125. err = errors.New("AddCygxActivityUserTemplateRecordMulti, Err: " + e.Error())
  126. return
  127. }
  128. }
  129. return
  130. }
  131. // 销售或后台管理员给用户添加报名之后,进行模版消息推送
  132. func SendWxMsgWithCygxActivitySignUpBySell(items []*cygx.CygxSignupUser) (err error) {
  133. var msg string
  134. defer func() {
  135. if err != nil {
  136. fmt.Println(err)
  137. go alarm_msg.SendAlarmMsg("修改活动人数限制之后,对于因为人数已满而失败的用户,推送模板消息,发送模版消息失败,Err:"+err.Error()+";msg:"+msg, 3)
  138. utils.FileLogCygx.Info(fmt.Sprintf("发送模版消息失败,Err:%s,%s", err.Error(), msg))
  139. }
  140. }()
  141. if len(items) == 0 {
  142. return
  143. }
  144. //var condition string
  145. //var pars []interface{}
  146. for _, v := range items {
  147. activityId := v.ActivityId
  148. uid := v.UserId
  149. activityInfo, e := cygx.GetAddActivityInfoById(activityId)
  150. if e != nil {
  151. err = errors.New("活动不存在, Err: " + e.Error())
  152. return
  153. }
  154. if activityInfo == nil {
  155. return
  156. }
  157. wxUser, e := models.GetWxUserByUserId(uid)
  158. if e != nil {
  159. err = errors.New("查询用户信息失败, Err: " + e.Error())
  160. return
  161. }
  162. var mobileArr []string
  163. mobileArr = append(mobileArr, wxUser.Mobile)
  164. mobileLen := len(mobileArr)
  165. if mobileLen == 0 {
  166. return
  167. }
  168. //condition = ""
  169. //pars = make([]interface{}, 0)
  170. //condition += ` AND u.mobile IN (` + utils.GetOrmInReplace(mobileLen) + `)`
  171. //pars = append(pars, mobileArr)
  172. openIdList, e := cygx.GetUserRecordListByMobileArr(mobileArr)
  173. if e != nil {
  174. err = errors.New("GetWxOpenIdBList, Err: " + e.Error())
  175. return
  176. }
  177. if len(openIdList) == 0 {
  178. return
  179. }
  180. openIdArr := make([]string, len(openIdList))
  181. for i, v := range openIdList {
  182. fmt.Println(v.OpenId)
  183. openIdArr[i] = v.OpenId
  184. }
  185. first := "对口销售已为您报名本场活动"
  186. keyword1 := "销售已为您报名:" + activityInfo.ActivityName
  187. keyword2 := activityInfo.ActivityTimeText
  188. keyword3 := ""
  189. keyword4 := ""
  190. remark := "点击查看活动详情"
  191. redirectUrl := utils.WX_MSG_PATH_ACTIVITY_DETAIL + strconv.Itoa(activityId)
  192. sendInfo := new(services.SendWxTemplate)
  193. sendInfo.First = first
  194. sendInfo.Keyword1 = keyword1
  195. sendInfo.Keyword2 = keyword2
  196. sendInfo.Keyword3 = keyword3
  197. sendInfo.Keyword4 = keyword4
  198. sendInfo.Remark = remark
  199. sendInfo.TemplateId = utils.WxMsgTemplateIdActivityApplyXzs
  200. sendInfo.RedirectUrl = redirectUrl
  201. sendInfo.RedirectTarget = 3
  202. sendInfo.Resource = strconv.Itoa(activityId)
  203. sendInfo.SendType = utils.TEMPLATE_MSG_ACTIVITY_APPOINTMENT
  204. sendInfo.OpenIdArr = openIdArr
  205. e = services.SendTemplateMsg(sendInfo)
  206. if e != nil {
  207. err = errors.New("SendTemplateMsg, Err: " + e.Error())
  208. return
  209. }
  210. }
  211. return
  212. }
  213. // 已结束的活动上传音频或视频后给已关注这个活动关联的产业的正式、试用 客户发送模板信息
  214. func SendWxMsgWithUpdateActivityVideoAndVoice(activityId int, fileName string) (err error) {
  215. defer func() {
  216. if err != nil {
  217. fmt.Println(err)
  218. go alarm_msg.SendAlarmMsg(fmt.Sprint("已结束的活动上传音频或视频后给已关注这个活动关联的产业的正式、试用 客户发送模板信息失败,Err:"+err.Error()+";activityId:", activityId), 3)
  219. //utils.FileLogCygx.Info(fmt.Sprintf("发送模版消息失败,Err:%s,%s", err.Error(), msg))
  220. }
  221. }()
  222. activityInfo, e := cygx.GetAddActivityInfoById(activityId)
  223. if e != nil {
  224. err = errors.New("活动不存在, Err: " + e.Error())
  225. return
  226. }
  227. if activityInfo == nil {
  228. return
  229. }
  230. industrialList, e := cygx.GetIndustrialActivityGroupListByactivityIds(strconv.Itoa(activityId))
  231. if e != nil && e.Error() != utils.ErrNoRow() {
  232. err = errors.New("GetIndustrialActivityGroupListByactivityIds: " + e.Error())
  233. return
  234. }
  235. //if len(industrialList) == 0 {
  236. // return
  237. //}
  238. var industrialManagementIds []int
  239. for _, v := range industrialList {
  240. industrialManagementIds = append(industrialManagementIds, v.IndustrialManagementId)
  241. }
  242. lenindustrialManagementIds := len(industrialManagementIds)
  243. if lenindustrialManagementIds == 0 {
  244. return
  245. }
  246. var condition string
  247. var pars []interface{}
  248. //获取试用正式权益客户
  249. condition = " AND product_id = 2 AND `status` IN('正式','试用') "
  250. listCompany, e := company.GetCompanyProductList(condition, pars)
  251. if e != nil && e.Error() != utils.ErrNoRow() {
  252. err = errors.New("GetCompanyProductList, Err: " + e.Error())
  253. return
  254. }
  255. var companyIds []int
  256. for _, v := range listCompany {
  257. companyIds = append(companyIds, v.CompanyId)
  258. }
  259. lencompanyIds := len(companyIds)
  260. if lencompanyIds == 0 {
  261. return
  262. }
  263. //根据产业关注ID,与有权限的公司ID获取对应的用户列表
  264. condition = " AND industrial_management_id IN (" + utils.GetOrmInReplace(lenindustrialManagementIds) + ") AND company_id IN (" + utils.GetOrmInReplace(lencompanyIds) + ") "
  265. pars = append(pars, industrialManagementIds, companyIds)
  266. userFllowList, e := cygx.GetCygxIndustryFllowList(condition, pars, 0, 0)
  267. if e != nil && e.Error() != utils.ErrNoRow() {
  268. err = errors.New("GetCygxIndustryFllowList, Err: " + e.Error())
  269. return
  270. }
  271. //获取这些用户的手机号
  272. var mobileArr []string
  273. for _, v := range userFllowList {
  274. mobileArr = append(mobileArr, v.Mobile)
  275. }
  276. mobileLen := len(mobileArr)
  277. if mobileLen == 0 {
  278. return
  279. }
  280. openIdList, e := cygx.GetUserRecordListByMobileArr(mobileArr)
  281. if e != nil {
  282. err = errors.New("GetWxOpenIdBList, Err: " + e.Error())
  283. return
  284. }
  285. if len(openIdList) == 0 {
  286. return
  287. }
  288. openIdArr := make([]string, len(openIdList))
  289. for _, v := range openIdList {
  290. openIdArr = append(openIdArr, v.OpenId)
  291. }
  292. keyword1 := fileName
  293. keyword2 := "路演回放已发布,欢迎查看"
  294. keyword3 := ""
  295. keyword4 := ""
  296. redirectUrl := utils.WX_MSG_PATH_ACTIVITY_DETAIL + strconv.Itoa(activityId)
  297. sendInfo := new(services.SendWxTemplate)
  298. sendInfo.Keyword1 = keyword1
  299. sendInfo.Keyword2 = keyword2
  300. sendInfo.Keyword3 = keyword3
  301. sendInfo.Keyword4 = keyword4
  302. sendInfo.TemplateId = utils.WxMsgTemplateIdActivityChangeApplyXzs
  303. sendInfo.RedirectUrl = redirectUrl
  304. sendInfo.RedirectTarget = 3
  305. sendInfo.Resource = strconv.Itoa(activityId)
  306. sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_ACTIVITY_LIMIT_PEOPLE
  307. sendInfo.OpenIdArr = openIdArr
  308. e = services.SendTemplateMsg(sendInfo)
  309. if e != nil {
  310. err = errors.New("SendTemplateMsg, Err: " + e.Error())
  311. return
  312. }
  313. return
  314. }