user_label.go 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  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. "strings"
  10. "time"
  11. )
  12. func init() {
  13. //ArticleHistoryUserLabelLogAdd(8350, 53095)
  14. //UserLabelLogReduce()
  15. //var mobileArr []string
  16. //mobileArr = append(mobileArr, "15557270714")
  17. //ActivityUserLabelLogAdd(2239, mobileArr)
  18. //UserLabelLogReduce()
  19. }
  20. // 添加用户阅读标签到Redis
  21. func ArticleHistoryUserLabelLogAdd(articleId, uid int) (err error) {
  22. defer func() {
  23. if err != nil {
  24. fmt.Println(err)
  25. msg := fmt.Sprint("articleId:", articleId, "userId:", uid)
  26. go utils.SendAlarmMsg("用户关注产业更新相关标签,写入Redis队列消息失败:"+err.Error()+msg, 2)
  27. }
  28. }()
  29. // SourceType 1:文章阅读、 2产业关注、3:活动到会、4系列关注、5专项调研活动到会。
  30. log := &models.CygxUserLabelLogRedis{UserId: uid, SourceId: articleId, SourceType: 1, CreateTime: time.Now()}
  31. if utils.Re == nil {
  32. err := utils.Rc.LPush(utils.CYGX_USER_KEY_LABEL, log)
  33. if err != nil {
  34. fmt.Println("CygxUserLabelLogRedis LPush Err:" + err.Error())
  35. }
  36. }
  37. return
  38. }
  39. // 添加用户2产业关注标签到Redis
  40. func IndustryFllowUserLabelLogAdd(industrialManagementId, count, uid int) (err error) {
  41. var isFllow int
  42. if count == 0 {
  43. isFllow = 1
  44. } else {
  45. isFllow = 0
  46. }
  47. defer func() {
  48. if err != nil {
  49. fmt.Println(err)
  50. msg := fmt.Sprint("industrialManagementId:", industrialManagementId, "isFllow:", isFllow, "userId:", uid)
  51. go utils.SendAlarmMsg("用户关注产业更新相关标签,写入Redis队列消息失败:"+err.Error()+msg, 2)
  52. }
  53. }()
  54. log := &models.CygxUserLabelLogRedis{UserId: uid, SourceId: industrialManagementId, SourceType: 2, IsFllow: isFllow, CreateTime: time.Now()}
  55. if utils.Re == nil {
  56. err := utils.Rc.LPush(utils.CYGX_USER_KEY_LABEL, log)
  57. if err != nil {
  58. fmt.Println("RecordNewLogs LPush Err:" + err.Error())
  59. }
  60. }
  61. return
  62. }
  63. // 添加用户活动到会标签到Redis
  64. func ActivityUserLabelLogAdd(activityId int, mobileArr []string) (err error) {
  65. defer func() {
  66. if err != nil {
  67. fmt.Println(err)
  68. msg := fmt.Sprint("activityId:", activityId, "mobile:", mobileArr)
  69. go utils.SendAlarmMsg("添加用户活动到会标签到Redis,写入Redis队列消息失败:"+err.Error()+msg, 2)
  70. }
  71. }()
  72. var condition string
  73. var pars []interface{}
  74. condition = ` AND activity_id = ? `
  75. pars = append(pars, activityId)
  76. total, e := models.GetCygxIndustrialActivityGroupManagementCount(condition+" AND source = 1 ", pars)
  77. if e != nil {
  78. err = errors.New("GetCygxIndustrialActivityGroupManagementCount" + e.Error())
  79. return
  80. }
  81. if total == 0 {
  82. //没有关联产业的活动不做标签处理
  83. return
  84. }
  85. industrialList, e := models.GetCygxIndustrialActivityGroupManagementList(condition+" AND source = 1 ", pars)
  86. if e != nil {
  87. err = errors.New("GetCygxIndustrialActivityGroupManagementList, Err: " + e.Error())
  88. return
  89. }
  90. //如果有行产业归类就按照产业报告处理
  91. var topCond string
  92. var topPars []interface{}
  93. var industrialManagementIds []int
  94. for _, v := range industrialList {
  95. industrialManagementIds = append(industrialManagementIds, v.IndustrialManagementId)
  96. }
  97. idsLen := len(industrialManagementIds)
  98. if idsLen > 0 {
  99. topCond = ` AND industrial_management_id IN (` + utils.GetOrmInReplace(idsLen) + `)`
  100. topPars = append(topPars, industrialManagementIds)
  101. } else {
  102. return
  103. }
  104. industrNamelist, e := models.GetTopOneMonthArtReadNumIndustryAll(topCond, topPars)
  105. if e != nil {
  106. err = errors.New("GetTopOneMonthArtReadNumIndustryAll, Err: " + e.Error())
  107. return
  108. }
  109. userList, e := models.GetWxUserByOutboundMobiles(mobileArr)
  110. if e != nil {
  111. err = errors.New("GetWxUserByOutboundMobiles" + e.Error())
  112. return
  113. }
  114. listActivityHistory, e := models.GetCygxUserLabelActivity(condition, pars)
  115. if e != nil {
  116. err = errors.New("GetCygxUserLabelActivity" + e.Error())
  117. return
  118. }
  119. activityHistoryMap := make(map[int]bool)
  120. for _, v := range listActivityHistory {
  121. activityHistoryMap[v.UserId] = true
  122. }
  123. var items []*models.CygxUserLabelActivity
  124. for _, user := range userList {
  125. //已经提交到会的活动写入标签的不做二次添加处理
  126. if activityHistoryMap[user.UserId] {
  127. continue
  128. }
  129. // SourceType 1:文章阅读、 2产业关注、3:活动到会、4系列关注、5专项调研活动到会。
  130. log := &models.CygxUserLabelLogRedis{UserId: user.UserId, SourceId: activityId, SourceType: 3, CreateTime: time.Now()}
  131. if utils.Re == nil {
  132. err := utils.Rc.LPush(utils.CYGX_USER_KEY_LABEL, log)
  133. if err != nil {
  134. fmt.Println("CygxUserLabelLogRedis LPush Err:" + err.Error())
  135. }
  136. }
  137. for _, industr := range industrNamelist {
  138. item := new(models.CygxUserLabelActivity)
  139. item.UserId = user.UserId
  140. item.CompanyId = user.CompanyId
  141. item.RealName = user.RealName
  142. item.Mobile = user.Mobile
  143. item.Email = user.Email
  144. item.ActivityId = activityId
  145. item.IndustrialManagementId = industr.IndustrialManagementId
  146. item.Label = industr.IndustryName
  147. item.CreateTime = time.Now()
  148. item.ModifyTime = time.Now()
  149. items = append(items, item)
  150. }
  151. }
  152. if len(items) > 0 {
  153. _, err = models.AddCygxUserLabelActivityList(items)
  154. }
  155. return
  156. }
  157. func UserLabelLogReduce() (err error) {
  158. for {
  159. //SourceType int `description:"1:文章阅读、 2产业关注、3:活动到会、4系列关注、5专项调研活动到会。"`
  160. utils.Rc.Brpop(utils.CYGX_USER_KEY_LABEL, func(b []byte) {
  161. var log models.CygxUserLabelLogRedis
  162. if err := json.Unmarshal(b, &log); err != nil {
  163. fmt.Println("json unmarshal wrong!")
  164. go utils.SendAlarmMsg("用户更新相关标签处理Redis队列消息失败:"+err.Error()+string(b), 2)
  165. }
  166. switch log.SourceType {
  167. case 1:
  168. go ArticleHistoryUserLabelLogReduce(log)
  169. fmt.Println("文章阅读")
  170. break
  171. case 2:
  172. go IndustryFllowUserLabelLogReduce(log)
  173. fmt.Println("2产业关注")
  174. break
  175. case 3:
  176. go ActivityUserLabelLogReduce(log)
  177. fmt.Println("活动到会")
  178. break
  179. case 4:
  180. go CategoryFllowUserLabelLogReduce(log)
  181. fmt.Println("4系列关注")
  182. break
  183. case 5:
  184. go ActivitySpecialUserLabelLogReduce(log)
  185. fmt.Println("5专项调研活动到会")
  186. break
  187. default:
  188. fmt.Println(string(b))
  189. go utils.SendAlarmMsg("用户更新相关标签处理Redis队列消息失败:"+string(b), 2)
  190. }
  191. })
  192. }
  193. }
  194. // 1:文章阅读
  195. func ArticleHistoryUserLabelLogReduce(log models.CygxUserLabelLogRedis) (err error) {
  196. defer func() {
  197. if err != nil {
  198. fmt.Println(err)
  199. go utils.SendAlarmMsg("用户文章阅读更新相关标签,处理Redis队列消息失败:"+err.Error()+fmt.Sprint("articleId", log.SourceId, "userId", log.UserId), 2)
  200. }
  201. }()
  202. articleId := log.SourceId
  203. userId := log.UserId
  204. var source int
  205. user, e := models.GetWxUserItemByUserId(userId)
  206. if e != nil {
  207. err = errors.New("GetWxUserItemByUserId" + e.Error())
  208. return
  209. }
  210. var condition string
  211. var pars []interface{}
  212. condition = ` AND article_id = ? `
  213. pars = append(pars, articleId)
  214. industrialList, e := models.GetIndustrialArticleGroupManagementList(condition, pars)
  215. if e != nil && e.Error() != utils.ErrNoRow() {
  216. err = errors.New("GetIndustrialArticleGroupManagementList, Err: " + e.Error())
  217. return
  218. }
  219. var items []*models.CygxUserLabel
  220. if len(industrialList) == 0 {
  221. //如果没有行产业归类就按照行业报告处理
  222. source = 2
  223. detailArticle, e := models.GetArticleDetailById(articleId)
  224. if e != nil {
  225. err = errors.New("GetArticleDetailById, Err: " + e.Error())
  226. return
  227. }
  228. labelDetail, e := models.GetdetailByCategoryIdLabel(detailArticle.CategoryId)
  229. if e != nil && e.Error() != utils.ErrNoRow() {
  230. err = errors.New("GetdetailByCategoryIdLabel, Err: " + e.Error())
  231. return
  232. }
  233. if labelDetail == nil {
  234. return
  235. }
  236. label := labelDetail.MatchTypeName
  237. industrialManagementId := labelDetail.Id
  238. var condition string
  239. var pars []interface{}
  240. condition += ` AND source_id=? AND source = ? AND user_id = ? `
  241. pars = append(pars, industrialManagementId, source, userId)
  242. total, e := models.GetCygxUserLabelCount(condition, pars)
  243. if e != nil {
  244. err = errors.New("GetCygxProductInteriorCount" + e.Error())
  245. return
  246. }
  247. item := new(models.CygxUserLabel)
  248. item.UserId = user.UserId
  249. item.CompanyId = user.CompanyId
  250. item.RealName = user.RealName
  251. item.Mobile = user.Mobile
  252. item.Email = user.Email
  253. item.Label = label
  254. item.SourceId = industrialManagementId
  255. item.Source = source
  256. item.Weight = 1
  257. item.CreateTime = time.Now()
  258. item.ModifyTime = time.Now()
  259. items = append(items, item)
  260. if total == 0 {
  261. _, e = models.AddCygxUserLabel(item)
  262. if e != nil {
  263. err = errors.New("AddCygxUserLabel" + e.Error())
  264. return
  265. }
  266. } else {
  267. //source 来源1:产业、2:系列
  268. e = models.UpdateCygxUserLabelWeight(industrialManagementId, source, userId, label)
  269. if e != nil {
  270. err = errors.New("UpdateCygxUserLabelWeight" + e.Error())
  271. return
  272. }
  273. }
  274. } else {
  275. //如果有行产业归类就按照产业报告处理
  276. var topCond string
  277. var topPars []interface{}
  278. var industrialManagementIds []int
  279. for _, v := range industrialList {
  280. industrialManagementIds = append(industrialManagementIds, v.IndustrialManagementId)
  281. }
  282. idsLen := len(industrialManagementIds)
  283. if idsLen > 0 {
  284. topCond = ` AND industrial_management_id IN (` + utils.GetOrmInReplace(idsLen) + `)`
  285. topPars = append(topPars, industrialManagementIds)
  286. } else {
  287. return
  288. }
  289. industrNamelist, e := models.GetTopOneMonthArtReadNumIndustryAll(topCond, topPars)
  290. if e != nil {
  291. err = errors.New("GetTopOneMonthArtReadNumIndustryAll, Err: " + e.Error())
  292. return
  293. }
  294. source = 1
  295. for _, v := range industrNamelist {
  296. label := v.IndustryName
  297. industrialManagementId := v.IndustrialManagementId
  298. var condition string
  299. var pars []interface{}
  300. condition += ` AND source_id=? AND source = ? AND user_id = ? `
  301. pars = append(pars, v.IndustrialManagementId, source, userId)
  302. total, e := models.GetCygxUserLabelCount(condition, pars)
  303. if e != nil {
  304. err = errors.New("GetCygxProductInteriorCount" + e.Error())
  305. return
  306. }
  307. item := new(models.CygxUserLabel)
  308. item.UserId = user.UserId
  309. item.CompanyId = user.CompanyId
  310. item.RealName = user.RealName
  311. item.Mobile = user.Mobile
  312. item.Email = user.Email
  313. item.Label = label
  314. item.SourceId = industrialManagementId
  315. item.Source = source
  316. item.Weight = 1
  317. item.CreateTime = time.Now()
  318. item.ModifyTime = time.Now()
  319. items = append(items, item)
  320. if total == 0 {
  321. _, e = models.AddCygxUserLabel(item)
  322. if e != nil {
  323. err = errors.New("AddCygxUserLabel" + e.Error())
  324. return
  325. }
  326. } else {
  327. //source 来源1:产业、2:系列
  328. e = models.UpdateCygxUserLabelWeight(industrialManagementId, source, userId, label)
  329. if e != nil {
  330. err = errors.New("UpdateCygxUserLabelWeight" + e.Error())
  331. return
  332. }
  333. }
  334. }
  335. }
  336. if len(items) > 0 {
  337. go AddArticleHistoryUserLabelLog(items, articleId)
  338. }
  339. updateUserLabelByUserId(userId)
  340. return
  341. }
  342. // 添加文章阅读记录日志,处理文章阅读三个月之内标签权重有效逻辑
  343. func AddArticleHistoryUserLabelLog(items []*models.CygxUserLabel, articleId int) (err error) {
  344. defer func() {
  345. if err != nil {
  346. fmt.Println(err)
  347. go utils.SendAlarmMsg("添加文章阅读记录日志,处理文章阅读三个月之内标签权重有效逻辑失败:"+err.Error(), 2)
  348. }
  349. }()
  350. if len(items) == 0 {
  351. return
  352. }
  353. for _, v := range items {
  354. item := new(models.CygxUserLabelArticle)
  355. item.UserId = v.UserId
  356. item.CompanyId = v.CompanyId
  357. item.RealName = v.RealName
  358. item.Mobile = v.Mobile
  359. item.Email = v.Email
  360. item.Label = v.Label
  361. item.SourceId = v.SourceId
  362. item.Source = v.Source
  363. item.ArticleId = articleId
  364. item.CreateTime = time.Now()
  365. item.ModifyTime = time.Now()
  366. _, e := models.AddCygxUserLabelArticle(item)
  367. if e != nil {
  368. err = errors.New("AddCygxUserLabelArticle" + e.Error())
  369. return
  370. }
  371. }
  372. return
  373. }
  374. // 2:产业关注
  375. func IndustryFllowUserLabelLogReduce(log models.CygxUserLabelLogRedis) (err error) {
  376. defer func() {
  377. if err != nil {
  378. fmt.Println(err)
  379. go utils.SendAlarmMsg("用户关注产业更新相关标签,处理Redis队列消息失败:"+err.Error(), 2)
  380. }
  381. }()
  382. isFllow := log.IsFllow
  383. industrialManagementId := log.SourceId
  384. userId := log.UserId
  385. source := 1
  386. detailIndustrial, e := models.GetIndustrialManagementDetail(industrialManagementId)
  387. if e != nil {
  388. err = errors.New("GetIndustrialManagementDetail" + e.Error())
  389. return
  390. }
  391. label := detailIndustrial.IndustryName
  392. if isFllow == 0 {
  393. e = models.UpdateCygxUserLabelIsFollow(isFllow, industrialManagementId, source, userId, label)
  394. if e != nil {
  395. err = errors.New("UpdateCygxUserLabelIsFollow" + e.Error())
  396. return
  397. }
  398. } else {
  399. var condition string
  400. var pars []interface{}
  401. condition += ` AND source_id=? AND source = ? AND user_id = ?`
  402. pars = append(pars, industrialManagementId, source, userId)
  403. total, e := models.GetCygxUserLabelCount(condition, pars)
  404. if e != nil {
  405. err = errors.New("GetCygxProductInteriorCount" + e.Error())
  406. return
  407. }
  408. if total == 0 {
  409. user, e := models.GetWxUserItemByUserId(userId)
  410. if e != nil {
  411. err = errors.New("GetWxUserItemByUserId" + e.Error())
  412. return
  413. }
  414. item := new(models.CygxUserLabel)
  415. item.UserId = user.UserId
  416. item.CompanyId = user.CompanyId
  417. item.RealName = user.RealName
  418. item.Mobile = user.Mobile
  419. item.Email = user.Email
  420. item.Label = label
  421. item.SourceId = industrialManagementId
  422. item.Source = source
  423. item.IsFollow = isFllow
  424. item.CreateTime = time.Now()
  425. item.ModifyTime = time.Now()
  426. _, e = models.AddCygxUserLabel(item)
  427. if e != nil {
  428. err = errors.New("AddCygxUserLabel" + e.Error())
  429. return
  430. }
  431. } else {
  432. //source 来源1:产业、2:系列
  433. e = models.UpdateCygxUserLabelIsFollow(isFllow, industrialManagementId, source, userId, label)
  434. if e != nil {
  435. err = errors.New("UpdateCygxUserLabelIsFollow" + e.Error())
  436. return
  437. }
  438. }
  439. }
  440. updateUserLabelByUserId(userId)
  441. return
  442. }
  443. // 3:活动到会
  444. func ActivityUserLabelLogReduce(log models.CygxUserLabelLogRedis) (err error) {
  445. defer func() {
  446. if err != nil {
  447. fmt.Println(err)
  448. go utils.SendAlarmMsg("用户活动到会更新相关标签,处理Redis队列消息失败:"+err.Error(), 2)
  449. }
  450. }()
  451. activityId := log.SourceId
  452. userId := log.UserId
  453. var source int
  454. user, e := models.GetWxUserItemByUserId(userId)
  455. if e != nil {
  456. err = errors.New("GetWxUserItemByUserId" + e.Error())
  457. return
  458. }
  459. var condition string
  460. var pars []interface{}
  461. condition = ` AND activity_id = ? `
  462. pars = append(pars, activityId)
  463. industrialList, e := models.GetCygxIndustrialActivityGroupManagementList(condition+" AND source = 1 ", pars)
  464. if e != nil {
  465. err = errors.New("GetCygxIndustrialActivityGroupManagementList, Err: " + e.Error())
  466. return
  467. }
  468. //如果有行产业归类就按照产业报告处理
  469. var topCond string
  470. var topPars []interface{}
  471. var industrialManagementIds []int
  472. for _, v := range industrialList {
  473. industrialManagementIds = append(industrialManagementIds, v.IndustrialManagementId)
  474. }
  475. idsLen := len(industrialManagementIds)
  476. if idsLen > 0 {
  477. topCond = ` AND industrial_management_id IN (` + utils.GetOrmInReplace(idsLen) + `)`
  478. topPars = append(topPars, industrialManagementIds)
  479. } else {
  480. return
  481. }
  482. industrNamelist, e := models.GetTopOneMonthArtReadNumIndustryAll(topCond, topPars)
  483. if e != nil {
  484. err = errors.New("GetTopOneMonthArtReadNumIndustryAll, Err: " + e.Error())
  485. return
  486. }
  487. source = 1
  488. for _, v := range industrNamelist {
  489. label := v.IndustryName
  490. industrialManagementId := v.IndustrialManagementId
  491. var condition string
  492. var pars []interface{}
  493. condition += ` AND source_id=? AND source = ? AND user_id = ? `
  494. pars = append(pars, v.IndustrialManagementId, source, userId)
  495. total, e := models.GetCygxUserLabelCount(condition, pars)
  496. if e != nil {
  497. err = errors.New("GetCygxProductInteriorCount" + e.Error())
  498. return
  499. }
  500. if total == 0 {
  501. item := new(models.CygxUserLabel)
  502. item.UserId = user.UserId
  503. item.CompanyId = user.CompanyId
  504. item.RealName = user.RealName
  505. item.Mobile = user.Mobile
  506. item.Email = user.Email
  507. item.Label = label
  508. item.SourceId = industrialManagementId
  509. item.Source = source
  510. item.Weight = 1
  511. item.CreateTime = time.Now()
  512. item.ModifyTime = time.Now()
  513. _, e = models.AddCygxUserLabel(item)
  514. if e != nil {
  515. err = errors.New("AddCygxUserLabel" + e.Error())
  516. return
  517. }
  518. } else {
  519. //source 来源1:产业、2:系列
  520. e = models.UpdateCygxUserLabelWeight(industrialManagementId, source, userId, label)
  521. if e != nil {
  522. err = errors.New("UpdateCygxUserLabelWeight" + e.Error())
  523. return
  524. }
  525. }
  526. }
  527. updateUserLabelByUserId(userId)
  528. return
  529. }
  530. // 4:系列关注
  531. func CategoryFllowUserLabelLogReduce(log models.CygxUserLabelLogRedis) (err error) {
  532. defer func() {
  533. if err != nil {
  534. fmt.Println(err)
  535. go utils.SendAlarmMsg("用户关注系列更新相关标签,处理Redis队列消息失败:"+err.Error()+fmt.Sprint(log), 2)
  536. }
  537. }()
  538. isFllow := log.IsFllow
  539. industrialManagementId := log.SourceId
  540. userId := log.UserId
  541. source := 2
  542. detailIndustrial, e := models.GetCygxReportMappingById(industrialManagementId)
  543. if e != nil {
  544. err = errors.New("GetCygxReportMappingById" + e.Error())
  545. return
  546. }
  547. label := detailIndustrial.MatchTypeName
  548. if isFllow == 0 {
  549. e = models.UpdateCygxUserLabelIsFollow(isFllow, industrialManagementId, source, userId, label)
  550. if e != nil {
  551. err = errors.New("UpdateCygxUserLabelIsFollow" + e.Error())
  552. return
  553. }
  554. } else {
  555. var condition string
  556. var pars []interface{}
  557. condition += ` AND source_id=? AND source = ? AND user_id = ?`
  558. pars = append(pars, industrialManagementId, source, userId)
  559. total, e := models.GetCygxUserLabelCount(condition, pars)
  560. if e != nil {
  561. err = errors.New("GetCygxProductInteriorCount" + e.Error())
  562. return
  563. }
  564. if total == 0 {
  565. user, e := models.GetWxUserItemByUserId(userId)
  566. if e != nil {
  567. err = errors.New("GetWxUserItemByUserId" + e.Error())
  568. return
  569. }
  570. item := new(models.CygxUserLabel)
  571. item.UserId = user.UserId
  572. item.CompanyId = user.CompanyId
  573. item.RealName = user.RealName
  574. item.Mobile = user.Mobile
  575. item.Email = user.Email
  576. item.Label = label
  577. item.SourceId = industrialManagementId
  578. item.Source = source
  579. item.IsFollow = isFllow
  580. item.CreateTime = time.Now()
  581. item.ModifyTime = time.Now()
  582. _, e = models.AddCygxUserLabel(item)
  583. if e != nil {
  584. err = errors.New("AddCygxUserLabel" + e.Error())
  585. return
  586. }
  587. } else {
  588. //source 来源1:产业、2:系列
  589. e = models.UpdateCygxUserLabelIsFollow(isFllow, industrialManagementId, source, userId, label)
  590. if e != nil {
  591. err = errors.New("UpdateCygxUserLabelIsFollow" + e.Error())
  592. return
  593. }
  594. }
  595. }
  596. updateUserLabelByUserId(userId)
  597. return
  598. }
  599. // 5:专项调研活动到会
  600. func ActivitySpecialUserLabelLogReduce(log models.CygxUserLabelLogRedis) (err error) {
  601. defer func() {
  602. if err != nil {
  603. fmt.Println(err)
  604. go utils.SendAlarmMsg("用户活动到会更新相关标签,处理Redis队列消息失败:"+err.Error(), 2)
  605. }
  606. }()
  607. activityId := log.SourceId
  608. userId := log.UserId
  609. var source int
  610. user, e := models.GetWxUserItemByUserId(userId)
  611. if e != nil {
  612. err = errors.New("GetWxUserItemByUserId" + e.Error())
  613. return
  614. }
  615. var condition string
  616. var pars []interface{}
  617. condition = ` AND activity_id = ? `
  618. pars = append(pars, activityId)
  619. industrialList, e := models.GetCygxIndustrialActivityGroupManagementList(condition+" AND source = 2 ", pars)
  620. if e != nil {
  621. err = errors.New("GetCygxIndustrialActivityGroupManagementList, Err: " + e.Error())
  622. return
  623. }
  624. //如果有行产业归类就按照产业报告处理
  625. var topCond string
  626. var topPars []interface{}
  627. var industrialManagementIds []int
  628. for _, v := range industrialList {
  629. industrialManagementIds = append(industrialManagementIds, v.IndustrialManagementId)
  630. }
  631. idsLen := len(industrialManagementIds)
  632. if idsLen > 0 {
  633. topCond = ` AND industrial_management_id IN (` + utils.GetOrmInReplace(idsLen) + `)`
  634. topPars = append(topPars, industrialManagementIds)
  635. } else {
  636. return
  637. }
  638. industrNamelist, e := models.GetTopOneMonthArtReadNumIndustryAll(topCond, topPars)
  639. if e != nil {
  640. err = errors.New("GetTopOneMonthArtReadNumIndustryAll, Err: " + e.Error())
  641. return
  642. }
  643. source = 1
  644. for _, v := range industrNamelist {
  645. label := v.IndustryName
  646. industrialManagementId := v.IndustrialManagementId
  647. var condition string
  648. var pars []interface{}
  649. condition += ` AND source_id=? AND source = ? AND user_id = ? `
  650. pars = append(pars, v.IndustrialManagementId, source, userId)
  651. total, e := models.GetCygxUserLabelCount(condition, pars)
  652. if e != nil {
  653. err = errors.New("GetCygxProductInteriorCount" + e.Error())
  654. return
  655. }
  656. if total == 0 {
  657. item := new(models.CygxUserLabel)
  658. item.UserId = user.UserId
  659. item.CompanyId = user.CompanyId
  660. item.RealName = user.RealName
  661. item.Mobile = user.Mobile
  662. item.Email = user.Email
  663. item.Label = label
  664. item.SourceId = industrialManagementId
  665. item.Source = source
  666. item.Weight = 1
  667. item.CreateTime = time.Now()
  668. item.ModifyTime = time.Now()
  669. _, e = models.AddCygxUserLabel(item)
  670. if e != nil {
  671. err = errors.New("AddCygxUserLabel" + e.Error())
  672. return
  673. }
  674. } else {
  675. //source 来源1:产业、2:系列
  676. e = models.UpdateCygxUserLabelWeight(industrialManagementId, source, userId, label)
  677. if e != nil {
  678. err = errors.New("UpdateCygxUserLabelWeight" + e.Error())
  679. return
  680. }
  681. }
  682. }
  683. updateUserLabelByUserId(userId)
  684. return
  685. }
  686. //func init() {
  687. // var condition string
  688. // var pars []interface{}
  689. // pars = make([]interface{}, 0)
  690. // condition = ` GROUP BY user_id`
  691. // listUser, _ := models.GetCygxUserLabelList(condition, pars)
  692. // for k, v := range listUser {
  693. // fmt.Println(k)
  694. // updateUserLabelByUserId(v.UserId)
  695. // }
  696. // //updateUserLabelByUserId(13269)
  697. //}
  698. // 更新用户标签
  699. func updateUserLabelByUserId(userId int) (err error) {
  700. defer func() {
  701. if err != nil {
  702. fmt.Println(err)
  703. utils.FileLog.Info("更新用户标签,Err:%s", err.Error(), userId)
  704. go utils.SendAlarmMsg("更新用户标签失败:"+err.Error()+"userId:"+strconv.Itoa(userId), 2)
  705. }
  706. }()
  707. //加200毫秒的延迟
  708. time.Sleep(200 * time.Millisecond)
  709. var condition string
  710. var pars []interface{}
  711. condition = ` AND is_follow=1 AND user_id = ? AND modify_time > DATE_SUB(CURDATE(), INTERVAL 3 MONTH) `
  712. pars = append(pars, userId)
  713. totalSource1, e := models.GetCygxUserLabelCount(condition+" AND source = 1 ", pars)
  714. if e != nil {
  715. err = errors.New("GetCygxProductInteriorCount" + e.Error())
  716. return
  717. }
  718. totalSource2, e := models.GetCygxUserLabelCount(condition+" AND source = 2 ", pars)
  719. if e != nil {
  720. err = errors.New("GetCygxProductInteriorCount" + e.Error())
  721. return
  722. }
  723. condition = ` AND user_id = ? `
  724. var list []*models.CygxUserLabel
  725. //当产业关注数量不超过10 ,系列关注数量不超过5
  726. if totalSource1 <= 10 && totalSource2 <= 5 {
  727. condition += ` AND ( is_follow > 0 OR weight > 0 ) ORDER BY is_follow DESC, weight DESC `
  728. list, e = models.GetCygxUserLabelList(condition, pars)
  729. if e != nil {
  730. err = errors.New("GetCygxUserLabelList当产业关注数量不超过10 ,系列关注数量不超过5" + e.Error())
  731. return
  732. }
  733. }
  734. //当产业关注数量超过10 ,系列关注数量超过5
  735. if totalSource1 > 10 && totalSource2 > 5 {
  736. condition += ` AND weight > 0 ORDER BY weight DESC `
  737. list, e = models.GetCygxUserLabelList(condition, pars)
  738. if e != nil {
  739. err = errors.New("GetCygxUserLabelList 当产业关注数量超过10 ,系列关注数量超过5" + e.Error())
  740. return
  741. }
  742. }
  743. //当产业关注数量不超过10 ,系列关注数量超过5
  744. if totalSource1 <= 10 && totalSource2 > 5 {
  745. condition += ` AND source = 1 AND is_follow = 1 ORDER BY weight DESC `
  746. listfollow, e := models.GetCygxUserLabelList(condition, pars)
  747. if e != nil {
  748. err = errors.New("GetCygxUserLabelList" + e.Error())
  749. return
  750. }
  751. idMap := make(map[int]bool)
  752. for _, v := range listfollow {
  753. idMap[v.Id] = true
  754. list = append(list, v)
  755. }
  756. pars = make([]interface{}, 0)
  757. condition = ` AND user_id = ? `
  758. pars = append(pars, userId)
  759. condition += ` AND weight > 0 ORDER BY weight DESC `
  760. listother, e := models.GetCygxUserLabelList(condition, pars)
  761. if e != nil {
  762. err = errors.New("GetCygxUserLabelList 当产业关注数量不超过10 ,系列关注数量超过5" + e.Error())
  763. return
  764. }
  765. for _, v := range listother {
  766. if idMap[v.Id] {
  767. continue
  768. }
  769. list = append(list, v)
  770. }
  771. }
  772. //当产业关注数量超过10 ,系列关注数量不超过5
  773. if totalSource1 > 10 && totalSource2 <= 5 {
  774. condition += ` AND source = 2 AND is_follow = 1 ORDER BY weight DESC `
  775. listfollow, e := models.GetCygxUserLabelList(condition, pars)
  776. if e != nil {
  777. err = errors.New("GetCygxUserLabelList 当产业关注数量超过10 ,系列关注数量不超过5" + e.Error())
  778. return
  779. }
  780. idMap := make(map[int]bool)
  781. for _, v := range listfollow {
  782. idMap[v.Id] = true
  783. list = append(list, v)
  784. }
  785. pars = make([]interface{}, 0)
  786. condition = ` AND user_id = ? `
  787. pars = append(pars, userId)
  788. condition += ` AND weight > 0 ORDER BY weight DESC `
  789. listother, e := models.GetCygxUserLabelList(condition, pars)
  790. if e != nil {
  791. err = errors.New("GetCygxUserLabelList 当产业关注数量超过10 ,系列关注数量不超过5" + e.Error())
  792. return
  793. }
  794. for _, v := range listother {
  795. if idMap[v.Id] {
  796. continue
  797. }
  798. list = append(list, v)
  799. }
  800. }
  801. var labelUser string
  802. for _, v := range list {
  803. labelUser += v.Label + ","
  804. }
  805. labelUser = strings.TrimRight(labelUser, ",")
  806. e = models.UpdateUserLabel(labelUser, userId)
  807. if e != nil {
  808. err = errors.New("UpdateUserLabel" + e.Error())
  809. return err
  810. }
  811. return
  812. }
  813. //func init() {
  814. // UpdateUserLabelWeight()
  815. //}
  816. // 更新用户标签权重
  817. func UpdateUserLabelWeight() (err error) {
  818. defer func() {
  819. if err != nil {
  820. fmt.Println(err)
  821. go utils.SendAlarmMsg("更新用户标签权重失败:"+err.Error(), 2)
  822. }
  823. }()
  824. var condition string
  825. var pars []interface{}
  826. list, e := models.GetCygxUserLabelList(condition, pars)
  827. if e != nil {
  828. err = errors.New("GetCygxUserLabelList" + e.Error())
  829. return
  830. }
  831. updateTime := time.Now().AddDate(0, -3, 0)
  832. condition = ` AND create_time > ?`
  833. pars = append(pars, updateTime)
  834. listArticle, e := models.GetCygxUserLabelArticleList(condition, pars)
  835. if e != nil {
  836. err = errors.New("GetCygxUserLabelArticleList" + e.Error())
  837. return
  838. }
  839. listActivity, e := models.GetCygxUserLabelActivity(condition, pars)
  840. if e != nil {
  841. err = errors.New("GetCygxUserLabelActivity" + e.Error())
  842. return
  843. }
  844. listActivitySpecial, e := models.GetCygxUserLabelActivitySpecial(condition, pars)
  845. if e != nil {
  846. err = errors.New("GetCygxUserLabelActivitySpecial" + e.Error())
  847. return
  848. }
  849. userLabel := make(map[string]bool)
  850. userLabelWeight := make(map[string]int)
  851. for _, v := range list {
  852. userLabel[fmt.Sprint("user_id{|}", v.UserId, "{|}source_id{|}", v.SourceId, "{|}source{|}", v.Source)] = true
  853. }
  854. fmt.Println(userLabel)
  855. //处理文章标签
  856. var items []*models.CygxUserLabel
  857. for _, user := range listArticle {
  858. if !userLabel[fmt.Sprint("user_id{|}", user.UserId, "{|}source_id{|}", user.SourceId, "{|}source{|}", user.Source)] {
  859. item := new(models.CygxUserLabel)
  860. item.UserId = user.UserId
  861. item.CompanyId = user.CompanyId
  862. item.RealName = user.RealName
  863. item.Mobile = user.Mobile
  864. item.Email = user.Email
  865. item.Label = user.Label
  866. item.SourceId = user.SourceId
  867. item.Source = user.Source
  868. item.CreateTime = user.CreateTime
  869. item.ModifyTime = time.Now()
  870. items = append(items, item)
  871. userLabel[fmt.Sprint("user_id{|}", user.UserId, "{|}source_id{|}", user.SourceId, "{|}source{|}", user.Source)] = true
  872. }
  873. userLabelWeight[fmt.Sprint("user_id{|}", user.UserId, "{|}source_id{|}", user.SourceId, "{|}source{|}", user.Source)]++
  874. }
  875. //处理活动标签
  876. for _, user := range listActivity {
  877. if !userLabel[fmt.Sprint("user_id{|}", user.UserId, "{|}source_id{|}", user.IndustrialManagementId, "{|}source{|}", 1)] {
  878. item := new(models.CygxUserLabel)
  879. item.UserId = user.UserId
  880. item.CompanyId = user.CompanyId
  881. item.RealName = user.RealName
  882. item.Mobile = user.Mobile
  883. item.Email = user.Email
  884. item.Label = user.Label
  885. item.SourceId = user.IndustrialManagementId
  886. item.Source = 1
  887. item.CreateTime = user.CreateTime
  888. item.ModifyTime = time.Now()
  889. items = append(items, item)
  890. userLabel[fmt.Sprint("user_id{|}", user.UserId, "{|}source_id{|}", user.IndustrialManagementId, "{|}source{|}", 1)] = true
  891. }
  892. userLabelWeight[fmt.Sprint("user_id{|}", user.UserId, "{|}source_id{|}", user.IndustrialManagementId, "{|}source{|}", 1)]++
  893. }
  894. //处理专项调研活动标签
  895. for _, user := range listActivitySpecial {
  896. if !userLabel[fmt.Sprint("user_id{|}", user.UserId, "{|}source_id{|}", user.IndustrialManagementId, "{|}source{|}", 1)] {
  897. item := new(models.CygxUserLabel)
  898. item.UserId = user.UserId
  899. item.CompanyId = user.CompanyId
  900. item.RealName = user.RealName
  901. item.Mobile = user.Mobile
  902. item.Email = user.Email
  903. item.Label = user.Label
  904. item.SourceId = user.IndustrialManagementId
  905. item.Source = 1
  906. item.CreateTime = user.CreateTime
  907. item.ModifyTime = time.Now()
  908. items = append(items, item)
  909. userLabel[fmt.Sprint("user_id{|}", user.UserId, "{|}source_id{|}", user.IndustrialManagementId, "{|}source{|}", 1)] = true
  910. }
  911. userLabelWeight[fmt.Sprint("user_id{|}", user.UserId, "{|}source_id{|}", user.IndustrialManagementId, "{|}source{|}", 1)]++
  912. }
  913. var itemLimt []*models.CygxUserLabel
  914. if len(items) > 0 {
  915. for _, v := range items {
  916. itemLimt = append(itemLimt, v)
  917. if len(itemLimt)%5000 == 0 {
  918. err = models.CygxUserLabelMulti(itemLimt)
  919. fmt.Println(err)
  920. itemLimt = make([]*models.CygxUserLabel, 0)
  921. }
  922. }
  923. }
  924. if len(itemLimt) > 0 {
  925. err = models.CygxUserLabelMulti(itemLimt)
  926. fmt.Println(err)
  927. }
  928. var itemsUpdate []*models.CygxUserLabel
  929. for k, v := range userLabelWeight {
  930. sliceUser := strings.Split(k, "{|}")
  931. userid, _ := strconv.Atoi(sliceUser[1])
  932. label := sliceUser[len(sliceUser)-1]
  933. item := new(models.CygxUserLabel)
  934. item.UserId = userid
  935. item.Label = label
  936. item.Weight = v
  937. itemsUpdate = append(itemsUpdate, item)
  938. }
  939. e = models.UpdateCygxUserLabelWeightAll()
  940. if e != nil {
  941. err = errors.New("UpdateCygxUserLabelWeightAll" + e.Error())
  942. return
  943. }
  944. e = models.UpdateCygxUserLabelMulti(itemsUpdate)
  945. if e != nil {
  946. err = errors.New("UpdateCygxUserLabelMulti" + e.Error())
  947. return
  948. }
  949. pars = make([]interface{}, 0)
  950. condition = ` GROUP BY user_id`
  951. listUser, e := models.GetCygxUserLabelList(condition, pars)
  952. for _, v := range listUser {
  953. go updateUserLabelByUserId(v.UserId)
  954. }
  955. return
  956. }