wx_user_rai_label.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. package services
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "hongze/hongze_cygx/models"
  7. "hongze/hongze_cygx/utils"
  8. "strconv"
  9. "time"
  10. )
  11. // 添加用户搜索标签到Redis
  12. func KeyWordsWxUserRaiLabelRedisAdd(sourceId, uid int, label string) (err error) {
  13. defer func() {
  14. if err != nil {
  15. fmt.Println(err)
  16. msg := fmt.Sprint("sourceId:", sourceId, "userId:", uid)
  17. go utils.SendAlarmMsg("用户关注产业更新相关标签,写入Redis队列消息失败:"+err.Error()+msg, 2)
  18. }
  19. }()
  20. log := &models.WxUserRaiLabelRedis{UserId: uid, SourceId: sourceId, SourceType: 1, Label: label, CreateTime: time.Now(), RegisterPlatform: utils.REGISTER_PLATFORM}
  21. if utils.Re == nil {
  22. err := utils.Rc.LPush(utils.WX_USER_RAI_LABEL_KEY, log)
  23. if err != nil {
  24. fmt.Println("WxUserRaiLabelRedis LPush Err:" + err.Error())
  25. }
  26. }
  27. return
  28. }
  29. // 添加用户阅读文章标签到Redis
  30. func ArticleWxUserRaiLabelRedisAdd(sourceId, uid int, createTime time.Time) (err error) {
  31. defer func() {
  32. if err != nil {
  33. fmt.Println(err)
  34. msg := fmt.Sprint("sourceId:", sourceId, "userId:", uid)
  35. go utils.SendAlarmMsg("用户关注产业更新相关标签,写入Redis队列消息失败:"+err.Error()+msg, 2)
  36. }
  37. }()
  38. log := &models.WxUserRaiLabelRedis{UserId: uid, SourceId: sourceId, SourceType: 1, TableName: utils.CYGX_OBJ_ARTICLE, CreateTime: createTime, RegisterPlatform: utils.REGISTER_PLATFORM}
  39. if utils.Re == nil {
  40. err := utils.Rc.LPush(utils.WX_USER_RAI_LABEL_KEY, log)
  41. if err != nil {
  42. fmt.Println("WxUserRaiLabelRedis LPush Err:" + err.Error())
  43. }
  44. }
  45. return
  46. }
  47. // 添加用户阅读本周研究汇总,与重点公司标签到Redis
  48. func ReportSelectionWxUserRaiLabelRedisAdd(sourceId, uid int, createTime time.Time, tableName, label string) (err error) {
  49. defer func() {
  50. if err != nil {
  51. fmt.Println(err)
  52. msg := fmt.Sprint("sourceId:", sourceId, "userId:", uid)
  53. go utils.SendAlarmMsg("用户关注产业更新相关标签,写入Redis队列消息失败:"+err.Error()+msg, 2)
  54. }
  55. }()
  56. log := &models.WxUserRaiLabelRedis{UserId: uid, SourceId: sourceId, SourceType: 1, TableName: tableName, Label: label, CreateTime: createTime, RegisterPlatform: utils.REGISTER_PLATFORM}
  57. if utils.Re == nil {
  58. err := utils.Rc.LPush(utils.WX_USER_RAI_LABEL_KEY, log)
  59. if err != nil {
  60. fmt.Println("ReportSelectionWxUserRaiLabelRedisAdd LPush Err:" + err.Error())
  61. }
  62. }
  63. return
  64. }
  65. // 添加用户参加活动标签到Redis
  66. func ActivityWxUserRaiLabelRedisAdd(sourceId, uid int, createTime time.Time) (err error) {
  67. defer func() {
  68. if err != nil {
  69. fmt.Println(err)
  70. msg := fmt.Sprint("sourceId:", sourceId, "userId:", uid)
  71. go utils.SendAlarmMsg("添加用户参加活动标签,写入Redis队列消息失败:"+err.Error()+msg, 2)
  72. }
  73. }()
  74. log := &models.WxUserRaiLabelRedis{UserId: uid, SourceId: sourceId, SourceType: 2, CreateTime: createTime, RegisterPlatform: utils.REGISTER_PLATFORM}
  75. if utils.Re == nil {
  76. err := utils.Rc.LPush(utils.WX_USER_RAI_LABEL_KEY, log)
  77. if err != nil {
  78. fmt.Println("ActivityWxUserRaiLabelRedisAdd LPush Err:" + err.Error())
  79. }
  80. }
  81. return
  82. }
  83. func UpdateWxUserRaiLabelRedis() (err error) {
  84. for {
  85. // SourceType int `description:"来源1:搜索关键字标签、2:产业/个股标签(线下活动)、3:产业/个股标签(线下路演)、4:产业/个股标签(线上活动)、5:产业/个股标签(线上路演)、6:销售输入标签、7:产业/个股标签(报告)、8:报告类型标签"`
  86. utils.Rc.Brpop(utils.WX_USER_RAI_LABEL_KEY, func(b []byte) {
  87. var log models.WxUserRaiLabelRedis
  88. if err := json.Unmarshal(b, &log); err != nil {
  89. fmt.Println("json unmarshal wrong!")
  90. go utils.SendAlarmMsg("用户更新相关标签处理Redis队列消息失败:"+err.Error()+string(b), 2)
  91. }
  92. if log.TableName == "" {
  93. switch log.SourceType {
  94. case 1:
  95. go KeyWordsWxUserRaiLabelRedisAddReduce(log)
  96. fmt.Println("搜索关键词")
  97. break
  98. case 2, 4:
  99. go ActivityWxUserRaiLabelRedisAddReduce(log)
  100. fmt.Println("2产业关注")
  101. break
  102. default:
  103. fmt.Println(string(b))
  104. go utils.SendAlarmMsg("用户更新相关标签处理Redis队列消息失败:"+string(b), 2)
  105. }
  106. } else {
  107. switch log.TableName {
  108. case utils.CYGX_OBJ_ARTICLE:
  109. go ArticleWxUserRaiLabelRedisAddReduce(log)
  110. fmt.Println("阅读文章")
  111. case "cygx_research_summary":
  112. go ReportSelectionWxUserRaiLabelRedisAddReduce(log)
  113. fmt.Println("阅读本周研究汇总")
  114. case "cygx_report_selection":
  115. go ReportSelectionWxUserRaiLabelRedisAddReduce(log)
  116. fmt.Println("阅读报告精选(重点公司)")
  117. }
  118. }
  119. })
  120. }
  121. }
  122. // 1:搜索关键词
  123. func KeyWordsWxUserRaiLabelRedisAddReduce(log models.WxUserRaiLabelRedis) (err error) {
  124. defer func() {
  125. if err != nil {
  126. fmt.Println(err)
  127. go utils.SendAlarmMsg("用户文章阅读更新相关标签,处理Redis队列消息失败:"+err.Error()+fmt.Sprint("articleId", log.SourceId, "userId", log.UserId), 2)
  128. }
  129. }()
  130. userId := log.UserId
  131. label := log.Label
  132. if userId == 0 {
  133. return
  134. }
  135. wxUser, e := models.GetWxUserItemByUserId(userId)
  136. if e != nil {
  137. err = errors.New("GetWxUserItemByUserId" + e.Error())
  138. return
  139. }
  140. item := new(models.WxUserRaiLabel)
  141. item.UserId = wxUser.UserId
  142. item.RealName = wxUser.RealName
  143. item.Mobile = wxUser.Mobile
  144. item.Email = wxUser.Email
  145. item.CompanyId = wxUser.CompanyId
  146. item.CompanyName = wxUser.CompanyName
  147. item.Label = label
  148. item.SourceType = log.SourceType
  149. item.CreateTime = log.CreateTime
  150. item.ModifyTime = time.Now()
  151. item.RegisterPlatform = log.RegisterPlatform
  152. err = models.AddWxUserRaiLabel(item)
  153. if e != nil {
  154. err = errors.New("AddWxUserRaiLabel" + e.Error())
  155. return
  156. }
  157. return
  158. }
  159. // 2:产业/个股标签(线下活动)、 4:产业/个股标签(线上活动)
  160. // 用户参加活动,相关标签
  161. func ActivityWxUserRaiLabelRedisAddReduce(log models.WxUserRaiLabelRedis) (err error) {
  162. defer func() {
  163. if err != nil {
  164. fmt.Println(err)
  165. go utils.SendAlarmMsg("用户参加活动,相关标签,处理Redis队列消息失败:ActivityWxUserRaiLabelRedisAddReduce"+err.Error()+fmt.Sprint("SourceId", log.SourceId, "userId", log.UserId), 2)
  166. }
  167. }()
  168. userId := log.UserId
  169. sourceId := log.SourceId
  170. wxUser, e := models.GetWxUserItemByUserId(userId)
  171. if e != nil {
  172. err = errors.New("GetWxUserItemByUserId" + e.Error())
  173. return
  174. }
  175. activityDetail, e := models.GetAddActivityDetailByActivityId(sourceId)
  176. if e != nil {
  177. err = errors.New("GetArticleDetailTestById" + e.Error())
  178. return
  179. }
  180. var sourceType int
  181. if activityDetail.ActivityType == 1 {
  182. sourceType = 4
  183. } else {
  184. sourceType = 2
  185. }
  186. //正常的有产业报告
  187. var labelArr []string
  188. //建立首页资源表,与产业的关系
  189. industrialList, e := models.GetIndustrialActivityGroupManagementListByArticleId(sourceId)
  190. if e != nil && e.Error() != utils.ErrNoRow() {
  191. err = errors.New("GetIndustrialArticleGroupManagementListByArticleId, Err: " + e.Error() + "sourceId:" + strconv.Itoa(sourceId))
  192. return
  193. }
  194. var items []*models.WxUserRaiLabel
  195. for _, v := range industrialList {
  196. item := new(models.WxUserRaiLabel)
  197. item.UserId = wxUser.UserId
  198. item.RealName = wxUser.RealName
  199. item.Mobile = wxUser.Mobile
  200. item.Email = wxUser.Email
  201. item.CompanyId = wxUser.CompanyId
  202. item.CompanyName = wxUser.CompanyName
  203. item.Label = v.IndustryName
  204. item.SourceType = sourceType
  205. item.SourceId = sourceId
  206. item.CreateTime = log.CreateTime
  207. item.ModifyTime = time.Now()
  208. item.RegisterPlatform = log.RegisterPlatform
  209. item.TableName = ""
  210. items = append(items, item)
  211. labelArr = append(labelArr, v.IndustryName)
  212. }
  213. //建立首页资源表,与标的 的关系
  214. subjectList, e := models.GetSubjectArticleGroupManagementListByArtcileId(sourceId)
  215. if e != nil && e.Error() != utils.ErrNoRow() {
  216. err = errors.New("GetSubjectArticleGroupManagementListByArtcileId, Err: " + e.Error() + "sourceId:" + strconv.Itoa(sourceId))
  217. return
  218. }
  219. for _, v := range subjectList {
  220. item := new(models.WxUserRaiLabel)
  221. item.UserId = wxUser.UserId
  222. item.RealName = wxUser.RealName
  223. item.Mobile = wxUser.Mobile
  224. item.Email = wxUser.Email
  225. item.CompanyId = wxUser.CompanyId
  226. item.CompanyName = wxUser.CompanyName
  227. item.Label = v.SubjectName
  228. item.SourceType = sourceType
  229. item.SourceId = sourceId
  230. item.CreateTime = log.CreateTime
  231. item.ModifyTime = time.Now()
  232. item.RegisterPlatform = log.RegisterPlatform
  233. item.TableName = ""
  234. items = append(items, item)
  235. labelArr = append(labelArr, v.SubjectName)
  236. }
  237. //如果两个标签都没有,就添加临时标签
  238. if len(labelArr) == 0 {
  239. item := new(models.WxUserRaiLabel)
  240. item.UserId = wxUser.UserId
  241. item.RealName = wxUser.RealName
  242. item.Mobile = wxUser.Mobile
  243. item.Email = wxUser.Email
  244. item.CompanyId = wxUser.CompanyId
  245. item.CompanyName = wxUser.CompanyName
  246. item.Label = activityDetail.Label
  247. item.SourceType = sourceType
  248. item.SourceId = sourceId
  249. item.CreateTime = log.CreateTime
  250. item.ModifyTime = time.Now()
  251. item.RegisterPlatform = log.RegisterPlatform
  252. item.TableName = ""
  253. items = append(items, item)
  254. labelArr = append(labelArr, activityDetail.Label)
  255. }
  256. e = models.AddWxUserRaiLabelMulti(items, labelArr, userId)
  257. if e != nil {
  258. err = errors.New("AddWxUserRaiLabelMulti" + e.Error())
  259. return
  260. }
  261. return
  262. }
  263. // 7:产业/个股标签(报告)、8:报告类型标签
  264. func ArticleWxUserRaiLabelRedisAddReduce(log models.WxUserRaiLabelRedis) (err error) {
  265. defer func() {
  266. if err != nil {
  267. fmt.Println(err)
  268. go utils.SendAlarmMsg("用户文章阅读更新相关标签,处理Redis队列消息失败:"+err.Error()+fmt.Sprint("articleId", log.SourceId, "userId", log.UserId), 2)
  269. }
  270. }()
  271. userId := log.UserId
  272. sourceId := log.SourceId
  273. wxUser, e := models.GetWxUserItemByUserId(userId)
  274. if e != nil {
  275. err = errors.New("GetWxUserItemByUserId" + e.Error())
  276. return
  277. }
  278. articleDetail, e := models.GetArticleDetailTestById(sourceId)
  279. if e != nil {
  280. err = errors.New("GetArticleDetailTestById" + e.Error())
  281. return
  282. }
  283. articlePermission, e := models.GetArticlePermission(articleDetail.CategoryId)
  284. if e != nil {
  285. err = errors.New("GetArticlePermission" + e.Error())
  286. return
  287. }
  288. if articlePermission == nil {
  289. err = errors.New("报告权限不存在" + e.Error())
  290. return
  291. }
  292. articlePermissionName := articlePermission.PermissionName
  293. matchTypeName := articlePermission.MatchTypeName
  294. matchTypeNameArr := []string{"季度策略", "科技前沿(周报)", "本周荟", "智造精粹"}
  295. //1;四大行业汇总类报告:季度策略、科技前沿(周报)、本周荟、智造精粹以报告类型做标签
  296. //2:策略、固收的所有报告,以报告类型做标签
  297. if articlePermissionName == utils.CE_LUE_NAME || articlePermissionName == utils.GU_SHOU_NAME || utils.InArrayByStr(matchTypeNameArr, matchTypeName) { // 策略、固收的所有报告,以报告类型做标签
  298. if matchTypeName == "季度策略" {
  299. matchTypeName = articlePermissionName + matchTypeName //其中季度策略以:xx行业季度策略 呈现
  300. }
  301. item := new(models.WxUserRaiLabel)
  302. item.UserId = wxUser.UserId
  303. item.RealName = wxUser.RealName
  304. item.Mobile = wxUser.Mobile
  305. item.Email = wxUser.Email
  306. item.CompanyId = wxUser.CompanyId
  307. item.CompanyName = wxUser.CompanyName
  308. item.Label = matchTypeName
  309. item.SourceType = 8
  310. item.SourceId = sourceId
  311. item.CreateTime = log.CreateTime
  312. item.ModifyTime = time.Now()
  313. item.RegisterPlatform = log.RegisterPlatform
  314. item.TableName = "cygx_article"
  315. err = models.AddWxUserRaiLabel(item)
  316. if e != nil {
  317. err = errors.New("AddWxUserRaiLabel" + e.Error())
  318. return
  319. }
  320. } else if articlePermissionName == utils.ZHOU_QI_NAME {
  321. //周期的行业深度、调研纪要、产业跟踪这些系列的报告用领域做标签,其余的周期报告,都用报告类型(也就是ficc定义的类型)做标签
  322. //其中周期的周度观点,以 周期周度观点 呈现
  323. matchTypeNameArrZhouqi := []string{"行业深度", "调研纪要", "产业跟踪"}
  324. if utils.InArrayByStr(matchTypeNameArrZhouqi, matchTypeName) {
  325. matchTypeName = articleDetail.FieldName
  326. }
  327. item := new(models.WxUserRaiLabel)
  328. item.UserId = wxUser.UserId
  329. item.RealName = wxUser.RealName
  330. item.Mobile = wxUser.Mobile
  331. item.Email = wxUser.Email
  332. item.CompanyId = wxUser.CompanyId
  333. item.CompanyName = wxUser.CompanyName
  334. item.Label = matchTypeName
  335. item.SourceType = 8
  336. item.SourceId = sourceId
  337. item.CreateTime = log.CreateTime
  338. item.ModifyTime = time.Now()
  339. item.RegisterPlatform = log.RegisterPlatform
  340. item.TableName = "cygx_article"
  341. e = models.AddWxUserRaiLabel(item)
  342. if e != nil {
  343. err = errors.New("AddWxUserRaiLabel" + e.Error())
  344. return
  345. }
  346. } else {
  347. //正常的有产业报告
  348. var labelArr []string
  349. //建立首页资源表,与产业的关系
  350. industrialList, e := models.GetIndustrialArticleGroupManagementListByArticleId(sourceId)
  351. if e != nil && e.Error() != utils.ErrNoRow() {
  352. err = errors.New("GetIndustrialArticleGroupManagementListByArticleId, Err: " + e.Error() + "sourceId:" + strconv.Itoa(sourceId))
  353. return
  354. }
  355. var items []*models.WxUserRaiLabel
  356. for _, v := range industrialList {
  357. item := new(models.WxUserRaiLabel)
  358. item.UserId = wxUser.UserId
  359. item.RealName = wxUser.RealName
  360. item.Mobile = wxUser.Mobile
  361. item.Email = wxUser.Email
  362. item.CompanyId = wxUser.CompanyId
  363. item.CompanyName = wxUser.CompanyName
  364. item.Label = v.IndustryName
  365. item.SourceType = 7
  366. item.SourceId = sourceId
  367. item.CreateTime = log.CreateTime
  368. item.ModifyTime = time.Now()
  369. item.RegisterPlatform = log.RegisterPlatform
  370. item.TableName = "cygx_article"
  371. items = append(items, item)
  372. labelArr = append(labelArr, v.IndustryName)
  373. }
  374. //建立首页资源表,与标的 的关系
  375. subjectList, e := models.GetSubjectArticleGroupManagementListByArtcileId(sourceId)
  376. if e != nil && e.Error() != utils.ErrNoRow() {
  377. err = errors.New("GetSubjectArticleGroupManagementListByArtcileId, Err: " + e.Error() + "sourceId:" + strconv.Itoa(sourceId))
  378. return
  379. }
  380. for _, v := range subjectList {
  381. item := new(models.WxUserRaiLabel)
  382. item.UserId = wxUser.UserId
  383. item.RealName = wxUser.RealName
  384. item.Mobile = wxUser.Mobile
  385. item.Email = wxUser.Email
  386. item.CompanyId = wxUser.CompanyId
  387. item.CompanyName = wxUser.CompanyName
  388. item.Label = v.SubjectName
  389. item.SourceType = 7
  390. item.SourceId = sourceId
  391. item.CreateTime = log.CreateTime
  392. item.ModifyTime = time.Now()
  393. item.RegisterPlatform = log.RegisterPlatform
  394. item.TableName = "cygx_article"
  395. items = append(items, item)
  396. labelArr = append(labelArr, v.SubjectName)
  397. }
  398. e = models.AddWxUserRaiLabelMulti(items, labelArr, userId)
  399. if e != nil {
  400. err = errors.New("AddWxUserRaiLabelMulti" + e.Error())
  401. return
  402. }
  403. }
  404. return
  405. }
  406. // 8:报告类型标签 (本周研究汇总,与重点公司)
  407. func ReportSelectionWxUserRaiLabelRedisAddReduce(log models.WxUserRaiLabelRedis) (err error) {
  408. defer func() {
  409. if err != nil {
  410. fmt.Println(err)
  411. go utils.SendAlarmMsg("报告类型标签 (本周研究汇总,与重点公司),处理Redis队列消息失败:ReportSelectionWxUserRaiLabelRedisAddReduce"+err.Error()+fmt.Sprint("SourceId", log.SourceId, "userId", log.UserId), 2)
  412. }
  413. }()
  414. userId := log.UserId
  415. sourceId := log.SourceId
  416. wxUser, e := models.GetWxUserItemByUserId(userId)
  417. if e != nil {
  418. err = errors.New("GetWxUserItemByUserId" + e.Error())
  419. return
  420. }
  421. item := new(models.WxUserRaiLabel)
  422. item.UserId = wxUser.UserId
  423. item.RealName = wxUser.RealName
  424. item.Mobile = wxUser.Mobile
  425. item.Email = wxUser.Email
  426. item.CompanyId = wxUser.CompanyId
  427. item.CompanyName = wxUser.CompanyName
  428. item.Label = log.Label
  429. item.SourceType = 8
  430. item.SourceId = sourceId
  431. item.CreateTime = log.CreateTime
  432. item.ModifyTime = time.Now()
  433. item.RegisterPlatform = log.RegisterPlatform
  434. item.TableName = log.TableName
  435. err = models.AddWxUserRaiLabel(item)
  436. if e != nil {
  437. err = errors.New("AddWxUserRaiLabel" + e.Error())
  438. return
  439. }
  440. return
  441. }