user_label.go 30 KB

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