special_wx_msg.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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. "strings"
  12. "time"
  13. )
  14. //预报名改为行程确定的活动时,通知预报名阶段感兴趣的客户
  15. // func init() {
  16. // DetermineTripCancel(55)
  17. // }
  18. func DetermineTripBeInterested(activityId int) (err error) {
  19. var msg string
  20. defer func() {
  21. if err != nil {
  22. fmt.Println(msg)
  23. fmt.Println(err)
  24. go alarm_msg.SendAlarmMsg("发送模版消息失败,Err:"+err.Error()+";msg:"+msg, 3)
  25. }
  26. }()
  27. activityInfo, e := cygx.GetAddActivityInfoSpecialById(activityId)
  28. if e != nil {
  29. err = e
  30. return
  31. }
  32. listSignup, e := cygx.GetCygxActivitySpecialSignupList(activityId)
  33. if e != nil {
  34. err = e
  35. fmt.Println(e)
  36. return
  37. }
  38. if len(listSignup) == 0 {
  39. return
  40. }
  41. var mobileArr []string
  42. for _, v := range listSignup {
  43. if v.Mobile != "" {
  44. mobileArr = append(mobileArr, v.Mobile)
  45. }
  46. }
  47. mobileLen := len(mobileArr)
  48. if mobileLen == 0 {
  49. return
  50. }
  51. //var condition string
  52. //var pars []interface{}
  53. //if mobileLen > 0 {
  54. // condition += ` AND u.mobile IN (` + utils.GetOrmInReplace(mobileLen) + `)`
  55. // pars = append(pars, mobileArr)
  56. //}
  57. openIdList, e := cygx.GetUserRecordListByMobileArr(mobileArr)
  58. if e != nil {
  59. fmt.Println(e)
  60. return
  61. }
  62. if len(openIdList) == 0 {
  63. return
  64. }
  65. openIdArr := make([]string, len(openIdList))
  66. for i, v := range openIdList {
  67. openIdArr[i] = v.OpenId
  68. }
  69. var first, keyword1, keyword2, keyword3, keyword4, remark, redirectUrl string
  70. keyword1 = "专项调研【" + activityInfo.ResearchTheme + "】"
  71. keyword2 = "已确定行程,请点击查看并报名以锁定席位"
  72. redirectUrl = utils.WX_MSG_PATH_ACTIVITY_SPECIAL_DETAIL + strconv.Itoa(activityId)
  73. sendInfo := new(services.SendWxTemplate)
  74. sendInfo.First = first
  75. sendInfo.Keyword1 = keyword1
  76. sendInfo.Keyword2 = keyword2
  77. sendInfo.Keyword3 = keyword3
  78. sendInfo.Keyword4 = keyword4
  79. sendInfo.Remark = remark
  80. sendInfo.TemplateId = utils.WxMsgTemplateIdActivityChangeApplyXzs
  81. sendInfo.RedirectUrl = redirectUrl
  82. sendInfo.RedirectTarget = 3
  83. sendInfo.Resource = strconv.Itoa(activityId)
  84. sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_ACTIVITY_UPDATE
  85. sendInfo.OpenIdArr = openIdArr
  86. err = services.SendTemplateMsg(sendInfo)
  87. if err != nil {
  88. err = errors.New("SendTemplateMsg, Err: " + err.Error())
  89. return
  90. }
  91. return
  92. }
  93. // 取消发布
  94. func DetermineTripCancel(activityId int) (err error) {
  95. var msg string
  96. defer func() {
  97. if err != nil {
  98. fmt.Println(msg)
  99. fmt.Println(err)
  100. go alarm_msg.SendAlarmMsg("发送模版消息失败,Err:"+err.Error()+";msg:"+msg, 3)
  101. }
  102. }()
  103. activityInfo, e := cygx.GetAddActivityInfoSpecialById(activityId)
  104. if e != nil {
  105. err = e
  106. return
  107. }
  108. listSignup, e := cygx.GetCygxActivitySpecialTripListByActivityId(activityId)
  109. if e != nil {
  110. err = e
  111. return
  112. }
  113. var mobileArr []string
  114. for _, v := range listSignup {
  115. if v.Mobile != "" {
  116. mobileArr = append(mobileArr, v.Mobile)
  117. }
  118. }
  119. mobileLen := len(mobileArr)
  120. if mobileLen == 0 {
  121. return
  122. }
  123. openIdList, e := cygx.GetUserRecordListByMobileArr(mobileArr)
  124. if e != nil {
  125. fmt.Println(e)
  126. return
  127. }
  128. if len(openIdList) == 0 {
  129. return
  130. }
  131. openIdArr := make([]string, len(openIdList))
  132. for i, v := range openIdList {
  133. openIdArr[i] = v.OpenId
  134. }
  135. var first, keyword1, keyword2, keyword3, keyword4, remark, redirectUrl string
  136. keyword1 = "专项调研【" + activityInfo.ResearchTheme + "】"
  137. keyword2 = "因专家安排变动,本次调研活动延期举办,具体时间另行通知"
  138. redirectUrl = utils.WX_MSG_PATH_ACTIVITY_SPECIAL_DETAIL + strconv.Itoa(activityId)
  139. sendInfo := new(services.SendWxTemplate)
  140. sendInfo.First = first
  141. sendInfo.Keyword1 = keyword1
  142. sendInfo.Keyword2 = keyword2
  143. sendInfo.Keyword3 = keyword3
  144. sendInfo.Keyword4 = keyword4
  145. sendInfo.Remark = remark
  146. sendInfo.TemplateId = utils.WxMsgTemplateIdActivityChangeApplyXzs
  147. sendInfo.RedirectUrl = redirectUrl
  148. sendInfo.RedirectTarget = 3
  149. sendInfo.Resource = strconv.Itoa(activityId)
  150. sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_ACTIVITY_UPDATE
  151. sendInfo.OpenIdArr = openIdArr
  152. err = services.SendTemplateMsg(sendInfo)
  153. if err != nil {
  154. err = errors.New("SendTemplateMsg, Err: " + err.Error())
  155. return
  156. }
  157. return
  158. }
  159. // 内容变更发布
  160. func DetermineTripChange(activityId int, changeType, changeMsg string) (err error) {
  161. var msg string
  162. defer func() {
  163. if err != nil {
  164. fmt.Println(msg)
  165. fmt.Println(err)
  166. go alarm_msg.SendAlarmMsg("发送模版消息失败,Err:"+err.Error()+";msg:"+msg, 3)
  167. }
  168. }()
  169. activityInfo, e := cygx.GetAddActivityInfoSpecialById(activityId)
  170. if e != nil {
  171. err = e
  172. return
  173. }
  174. listSignup, e := cygx.GetCygxActivitySpecialTripListByActivityId(activityId)
  175. if e != nil {
  176. err = e
  177. fmt.Println(e)
  178. return
  179. }
  180. var mobileArr []string
  181. for _, v := range listSignup {
  182. if v.Mobile != "" {
  183. mobileArr = append(mobileArr, v.Mobile)
  184. }
  185. }
  186. mobileLen := len(mobileArr)
  187. if mobileLen == 0 {
  188. return
  189. }
  190. openIdList, e := cygx.GetUserRecordListByMobileArr(mobileArr)
  191. if e != nil {
  192. fmt.Println(e)
  193. return
  194. }
  195. if len(openIdList) == 0 {
  196. return
  197. }
  198. openIdArr := make([]string, len(openIdList))
  199. for i, v := range openIdList {
  200. openIdArr[i] = v.OpenId
  201. }
  202. var first, keyword1, keyword2, keyword3, keyword4, remark, redirectUrl string
  203. if changeType == "City" {
  204. keyword1 = "【" + activityInfo.ResearchTheme + "】专项调研发生了城市变更"
  205. } else if changeType == "dateList" {
  206. keyword1 = "【" + activityInfo.ResearchTheme + "】专项调研发生了时间变更"
  207. } else {
  208. keyword1 = "【" + activityInfo.ResearchTheme + "】专项调研发生了调研形式变更"
  209. }
  210. keyword2 = changeMsg
  211. redirectUrl = utils.WX_MSG_PATH_ACTIVITY_SPECIAL_DETAIL + strconv.Itoa(activityId)
  212. sendInfo := new(services.SendWxTemplate)
  213. sendInfo.First = first
  214. sendInfo.Keyword1 = keyword1
  215. sendInfo.Keyword2 = keyword2
  216. sendInfo.Keyword3 = keyword3
  217. sendInfo.Keyword4 = keyword4
  218. sendInfo.Remark = remark
  219. sendInfo.TemplateId = utils.WxMsgTemplateIdActivityChangeApplyXzs
  220. sendInfo.RedirectUrl = redirectUrl
  221. sendInfo.RedirectTarget = 3
  222. sendInfo.Resource = strconv.Itoa(activityId)
  223. sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_ACTIVITY_UPDATE
  224. sendInfo.OpenIdArr = openIdArr
  225. err = services.SendTemplateMsg(sendInfo)
  226. if err != nil {
  227. err = errors.New("SendTemplateMsg, Err: " + err.Error())
  228. return
  229. }
  230. return
  231. }
  232. //func init() {
  233. // SendWxMsgWithCygxActivitySpecialSubscribeNoInterested(7)
  234. //}
  235. // SendWxMsgWithCygxActivitySpecialSubscribeNoInterested 确定行程的活动通知,推送给订阅了调研活动通知且该场活动预报名阶段未点击感兴趣的客户和销售
  236. func SendWxMsgWithCygxActivitySpecialSubscribeNoInterested(activityId int) (err error) {
  237. var msg string
  238. defer func() {
  239. if err != nil {
  240. fmt.Println(err)
  241. go alarm_msg.SendAlarmMsg("发送模版消息失败,Err:"+err.Error()+";msg:"+msg, 3)
  242. utils.FileLogCygx.Info(fmt.Sprintf("发送模版消息失败,Err:%s,%s", err.Error(), msg))
  243. }
  244. }()
  245. activityInfo, err := cygx.GetAddActivityInfoSpecialById(activityId)
  246. if err != nil {
  247. msg = "GetReportInfo Err:" + err.Error()
  248. return
  249. }
  250. openIdList, err := cygx.GeFollowSpecialOpenIdList()
  251. if err != nil {
  252. msg = "get openIdList err:" + err.Error()
  253. return
  254. }
  255. listSignup, e := cygx.GetCygxActivitySpecialSignupList(activityId)
  256. if e != nil {
  257. err = e
  258. fmt.Println(e)
  259. return
  260. }
  261. var companyIds []int
  262. mapSignup := make(map[int]int)
  263. var arrOpenId []string
  264. for _, v := range listSignup {
  265. mapSignup[v.UserId] = v.UserId
  266. }
  267. for _, v := range openIdList {
  268. if _, ok := mapSignup[v.UserId]; !ok {
  269. arrOpenId = append(arrOpenId, v.OpenId)
  270. companyIds = append(companyIds, v.CompanyId)
  271. }
  272. }
  273. var condition string
  274. var pars []interface{}
  275. companyIdsLen := len(companyIds)
  276. condition = ` AND p.company_id IN (` + utils.GetOrmInReplace(companyIdsLen) + `) GROUP BY p.company_id `
  277. pars = append(pars, companyIds)
  278. listMobile, e := cygx.GetAdminMobileByCompany(condition, pars)
  279. if e != nil && e.Error() != utils.ErrNoRow() {
  280. err = errors.New("GetAdminOpendidByCompany, Err: " + e.Error())
  281. return
  282. }
  283. if len(listMobile) == 0 {
  284. return
  285. }
  286. var mobiles []string
  287. for _, vOpenid := range listMobile {
  288. mobiles = append(mobiles, vOpenid.Mobile)
  289. }
  290. listSeller, e := cygx.GetUserRecordListByMobileArr(mobiles)
  291. if e != nil && e.Error() != utils.ErrNoRow() {
  292. err = errors.New("GetUserRecordListByMobileArr, Err: " + e.Error())
  293. return
  294. }
  295. if len(listSeller) == 0 {
  296. return
  297. }
  298. for _, vOpenid := range listSeller {
  299. arrOpenId = append(arrOpenId, vOpenid.OpenId)
  300. }
  301. if len(arrOpenId) == 0 {
  302. return
  303. }
  304. mapOpenid := make(map[string]string)
  305. for _, v := range arrOpenId {
  306. mapOpenid[v] = v
  307. }
  308. openIdArr := make([]string, len(mapOpenid))
  309. var i int
  310. for _, v := range mapOpenid {
  311. openIdArr[i] = v
  312. i++
  313. }
  314. var first string
  315. var keyword1 string
  316. var keyword2 string
  317. //keyword1 = activityInfo.ResearchTheme
  318. keyword1 = "【" + activityInfo.ResearchTheme + "】专项调研已确定行程"
  319. //keyword2 = "活动形式:"
  320. if activityInfo.SpecialType == 1 {
  321. keyword2 += "线上 开始时间:" + activityInfo.ActivityTimeTextByDay
  322. } else {
  323. keyword2 += "线下 " + "(" + activityInfo.City + ") 开始时间:" + activityInfo.ActivityTimeTextByDay
  324. }
  325. remark := "点击查看活动详情"
  326. redirectUrl := utils.WX_MSG_PATH_ACTIVITY_SPECIAL_DETAIL + strconv.Itoa(activityId)
  327. sendInfo := new(services.SendWxTemplate)
  328. sendInfo.First = first
  329. sendInfo.Keyword1 = keyword1
  330. sendInfo.Keyword2 = keyword2
  331. sendInfo.Remark = remark
  332. sendInfo.TemplateId = utils.WxMsgTemplateIdActivityChangeApplyXzs
  333. sendInfo.RedirectUrl = redirectUrl
  334. sendInfo.RedirectTarget = 3
  335. sendInfo.Resource = strconv.Itoa(activityId)
  336. sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_ACTIVITY_UPDATE
  337. sendInfo.OpenIdArr = openIdArr
  338. err = services.SendTemplateMsg(sendInfo)
  339. return
  340. }
  341. //func init() {
  342. // SendWxMsgWithCygxActivitySpecialArticle(1000570)
  343. //}
  344. // 发布后,对7天内,相同产业名称的专项调研活动的报名和空降客户,推送模板消息
  345. func SendWxMsgWithCygxActivitySpecialArticle(articleId int) (err error) {
  346. var msg string
  347. defer func() {
  348. if err != nil {
  349. fmt.Println(err)
  350. go alarm_msg.SendAlarmMsg("发布后,对7天内,相同产业名称的专项调研活动的报名和空降客户,发送模版消息失败,Err:"+err.Error()+";msg:"+msg, 3)
  351. utils.FileLogCygx.Info(fmt.Sprintf("发送模版消息失败,Err:%s,%s", err.Error(), msg))
  352. }
  353. }()
  354. articleInfo, e := cygx.GetArticleIdInfoByArticleId(articleId)
  355. if e != nil {
  356. msg = "获取文章信息失败!articleId:" + strconv.Itoa(articleId)
  357. err = errors.New("GetArticleIdInfoByArticleId, Err: " + e.Error())
  358. return
  359. }
  360. industrialList, e := cygx.GetIndustrialArticleGroupManagementList(articleInfo.ArticleId)
  361. if e != nil && err.Error() != utils.ErrNoRow() {
  362. err = errors.New("获取活动关联的产业列表信息失败,GetIndustrialArticleGroupManagementList Err: " + e.Error())
  363. return
  364. }
  365. var industrialName string
  366. if len(industrialList) == 0 {
  367. for _, v := range industrialList {
  368. industrialName += v.IndustryName + ","
  369. }
  370. }
  371. industrialName = strings.TrimRight(industrialName, ",")
  372. //10分钟之内的二次编辑不重复推送
  373. cacheKey := fmt.Sprint("Source:ADMIN", "_ArticleId:", articleInfo.ArticleId)
  374. isExist := utils.Rc.IsExist(cacheKey)
  375. if isExist {
  376. return err
  377. }
  378. var condition string
  379. var pars []interface{}
  380. //condition = ` AND m.industrial_management_id IN (` + utils.GetOrmInReplace(len(industryIds)) + `)`
  381. //pars = append(pars, industryIds)
  382. //condition += ` AND a.activity_time > ? `
  383. //pars = append(pars, time.Now().AddDate(0, 0, -7))
  384. //activityList, err := cygx.GetActivitySpecialByIndustryIds(condition, pars)
  385. //if err != nil {
  386. // return
  387. //}
  388. //if len(activityList) == 0 {
  389. // return
  390. //}
  391. //for _, v := range activityList {
  392. //activityInfo, e := cygx.GetAddActivityInfoSpecialById(v.ActivityId)
  393. //if e != nil {
  394. // err = errors.New("GetAddActivityInfoSpecialById, Err: " + e.Error())
  395. // return
  396. //}
  397. condition = ` AND category_id = ? AND sub_category_name = '专项调研' `
  398. pars = append(pars, articleInfo.CategoryId)
  399. charInfo, e := cygx.GetReportMappingDetail(condition, pars)
  400. if e != nil {
  401. err = errors.New("GetReportMappingDetail, Err: " + e.Error())
  402. return
  403. }
  404. //获取有权限的相关公司
  405. companyList, e := company.GetCompanyReportPermissionUpgradeByChartPermissionId(charInfo.ChartPermissionId)
  406. if e != nil {
  407. err = errors.New("GetCompanyReportPermissionUpgradeByChartPermissionId, Err: " + e.Error())
  408. return
  409. }
  410. if len(companyList) == 0 {
  411. return
  412. }
  413. var companyIdArr []int
  414. for _, v := range companyList {
  415. companyIdArr = append(companyIdArr, v.CompanyId)
  416. }
  417. pars = make([]interface{}, 0)
  418. condition = ` AND u.company_id IN (` + utils.GetOrmInReplace(len(companyIdArr)) + `)`
  419. pars = append(pars, companyIdArr)
  420. listMobile, e := cygx.GetAdminMobileByCompany(condition, pars)
  421. if e != nil {
  422. err = errors.New("GetWxOpenIdByMobileList, Err: " + e.Error())
  423. return
  424. }
  425. var mobiles []string
  426. for _, vOpenid := range listMobile {
  427. mobiles = append(mobiles, vOpenid.Mobile)
  428. }
  429. openIdList, e := cygx.GetUserRecordListByMobileArr(mobiles)
  430. if e != nil {
  431. err = errors.New("GetWxOpenIdByMobileList, Err: " + e.Error())
  432. return
  433. }
  434. if len(openIdList) == 0 {
  435. return
  436. }
  437. openIdArr := make([]string, len(openIdList))
  438. for i, vO := range openIdList {
  439. openIdArr[i] = vO.OpenId
  440. }
  441. var keyword1 string
  442. first := ""
  443. if industrialName != "" {
  444. keyword1 = "【" + industrialName + "】有报告发布/更新"
  445. } else {
  446. keyword1 = articleInfo.Title + "已发布"
  447. }
  448. keyword2 := articleInfo.Title
  449. keyword3 := articleInfo.PublishDate.Format(utils.FormatDate)
  450. keyword4 := "--"
  451. remark := "点击查看报告详情"
  452. redirectUrl := utils.WX_MSG_PATH_ARTICLE_DETAIL + strconv.Itoa(articleInfo.ArticleId)
  453. sendInfo := new(services.SendWxTemplate)
  454. sendInfo.First = first
  455. sendInfo.Keyword1 = keyword1
  456. sendInfo.Keyword2 = keyword2
  457. sendInfo.Keyword3 = keyword3
  458. sendInfo.Keyword4 = keyword4
  459. sendInfo.Remark = remark
  460. sendInfo.TemplateId = utils.TemplateIdByProductXzs
  461. sendInfo.RedirectUrl = redirectUrl
  462. sendInfo.RedirectTarget = 3
  463. sendInfo.Resource = strconv.Itoa(articleInfo.ArticleId)
  464. sendInfo.SendType = utils.TEMPLATE_MSG_CYGX_ARTICLE_ADD
  465. sendInfo.OpenIdArr = openIdArr
  466. //fmt.Println(sendInfo)
  467. e = services.SendTemplateMsg(sendInfo)
  468. if e != nil {
  469. err = errors.New("SendTemplateMsg, Err: " + e.Error())
  470. return
  471. }
  472. setNX := utils.Rc.SetNX(cacheKey, articleInfo.ArticleId, time.Minute*10)
  473. if !setNX {
  474. go alarm_msg.SendAlarmMsg("发送模版消息失败,10分钟之内的活动二次编辑不重复推送,设置Redis Key 过期时间失败:key"+cacheKey, 3)
  475. return err
  476. }
  477. return
  478. }