user_label.go 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  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(), 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. time.Sleep(1 * time.Second)
  708. var condition string
  709. var pars []interface{}
  710. condition = ` AND is_follow=1 AND user_id = ? `
  711. pars = append(pars, userId)
  712. totalSource1, e := models.GetCygxUserLabelCount(condition+" AND source = 1 ", pars)
  713. if e != nil {
  714. err = errors.New("GetCygxProductInteriorCount" + e.Error())
  715. return
  716. }
  717. totalSource2, e := models.GetCygxUserLabelCount(condition+" AND source = 2 ", pars)
  718. if e != nil {
  719. err = errors.New("GetCygxProductInteriorCount" + e.Error())
  720. return
  721. }
  722. condition = ` AND user_id = ? `
  723. var list []*models.CygxUserLabel
  724. //当产业关注数量不超过10 ,系列关注数量不超过5
  725. if totalSource1 <= 10 && totalSource2 <= 5 {
  726. condition += ` AND ( is_follow > 0 OR weight > 0 ) ORDER BY is_follow DESC, weight DESC `
  727. list, e = models.GetCygxUserLabelList(condition, pars)
  728. if e != nil {
  729. err = errors.New("GetCygxUserLabelList当产业关注数量不超过10 ,系列关注数量不超过5" + e.Error())
  730. return
  731. }
  732. }
  733. //当产业关注数量超过10 ,系列关注数量超过5
  734. if totalSource1 > 10 && totalSource2 > 5 {
  735. condition += ` AND weight > 0 ORDER BY weight DESC `
  736. list, e = models.GetCygxUserLabelList(condition, pars)
  737. if e != nil {
  738. err = errors.New("GetCygxUserLabelList 当产业关注数量超过10 ,系列关注数量超过5" + e.Error())
  739. return
  740. }
  741. }
  742. //当产业关注数量不超过10 ,系列关注数量超过5
  743. if totalSource1 <= 10 && totalSource2 > 5 {
  744. condition += ` AND source = 1 AND is_follow = 1 ORDER BY weight DESC `
  745. listfollow, e := models.GetCygxUserLabelList(condition, pars)
  746. if e != nil {
  747. err = errors.New("GetCygxUserLabelList" + e.Error())
  748. return
  749. }
  750. idMap := make(map[int]bool)
  751. for _, v := range listfollow {
  752. idMap[v.Id] = true
  753. list = append(list, v)
  754. }
  755. pars = make([]interface{}, 0)
  756. condition = ` AND user_id = ? `
  757. pars = append(pars, userId)
  758. condition += ` AND weight > 0 ORDER BY weight DESC `
  759. listother, e := models.GetCygxUserLabelList(condition, pars)
  760. if e != nil {
  761. err = errors.New("GetCygxUserLabelList 当产业关注数量不超过10 ,系列关注数量超过5" + e.Error())
  762. return
  763. }
  764. for _, v := range listother {
  765. if idMap[v.Id] {
  766. continue
  767. }
  768. list = append(list, v)
  769. }
  770. }
  771. //当产业关注数量超过10 ,系列关注数量不超过5
  772. if totalSource1 > 10 && totalSource2 <= 5 {
  773. condition += ` AND source = 2 AND is_follow = 1 ORDER BY weight DESC `
  774. listfollow, e := models.GetCygxUserLabelList(condition, pars)
  775. if e != nil {
  776. err = errors.New("GetCygxUserLabelList 当产业关注数量超过10 ,系列关注数量不超过5" + e.Error())
  777. return
  778. }
  779. idMap := make(map[int]bool)
  780. for _, v := range listfollow {
  781. idMap[v.Id] = true
  782. list = append(list, v)
  783. }
  784. pars = make([]interface{}, 0)
  785. condition = ` AND user_id = ? `
  786. pars = append(pars, userId)
  787. condition += ` AND weight > 0 ORDER BY weight DESC `
  788. listother, e := models.GetCygxUserLabelList(condition, pars)
  789. if e != nil {
  790. err = errors.New("GetCygxUserLabelList 当产业关注数量超过10 ,系列关注数量不超过5" + e.Error())
  791. return
  792. }
  793. for _, v := range listother {
  794. if idMap[v.Id] {
  795. continue
  796. }
  797. list = append(list, v)
  798. }
  799. }
  800. var labelUser string
  801. for _, v := range list {
  802. labelUser += v.Label + ","
  803. }
  804. labelUser = strings.TrimRight(labelUser, ",")
  805. e = models.UpdateUserLabel(labelUser, userId)
  806. if e != nil {
  807. err = errors.New("UpdateUserLabel" + e.Error())
  808. return err
  809. }
  810. return
  811. }
  812. //func init() {
  813. // UpdateUserLabelWeight()
  814. //}
  815. // 更新用户标签权重
  816. func UpdateUserLabelWeight() (err error) {
  817. defer func() {
  818. if err != nil {
  819. fmt.Println(err)
  820. go utils.SendAlarmMsg("更新用户标签权重失败:"+err.Error(), 2)
  821. }
  822. }()
  823. var condition string
  824. var pars []interface{}
  825. list, e := models.GetCygxUserLabelList(condition, pars)
  826. if e != nil {
  827. err = errors.New("GetCygxUserLabelList" + e.Error())
  828. return
  829. }
  830. updateTime := time.Now().AddDate(0, -3, 0)
  831. condition = ` AND create_time > ?`
  832. pars = append(pars, updateTime)
  833. listArticle, e := models.GetCygxUserLabelArticleList(condition, pars)
  834. if e != nil {
  835. err = errors.New("GetCygxUserLabelArticleList" + e.Error())
  836. return
  837. }
  838. listActivity, e := models.GetCygxUserLabelActivity(condition, pars)
  839. if e != nil {
  840. err = errors.New("GetCygxUserLabelActivity" + e.Error())
  841. return
  842. }
  843. listActivitySpecial, e := models.GetCygxUserLabelActivitySpecial(condition, pars)
  844. if e != nil {
  845. err = errors.New("GetCygxUserLabelActivitySpecial" + e.Error())
  846. return
  847. }
  848. userLabel := make(map[string]bool)
  849. userLabelWeight := make(map[string]int)
  850. for _, v := range list {
  851. userLabel[fmt.Sprint("user_id{|}", v.UserId, "{|}source_id{|}", v.SourceId, "{|}source{|}", v.Source)] = true
  852. }
  853. fmt.Println(userLabel)
  854. //处理文章标签
  855. var items []*models.CygxUserLabel
  856. for _, user := range listArticle {
  857. if !userLabel[fmt.Sprint("user_id{|}", user.UserId, "{|}source_id{|}", user.SourceId, "{|}source{|}", user.Source)] {
  858. item := new(models.CygxUserLabel)
  859. item.UserId = user.UserId
  860. item.CompanyId = user.CompanyId
  861. item.RealName = user.RealName
  862. item.Mobile = user.Mobile
  863. item.Email = user.Email
  864. item.Label = user.Label
  865. item.SourceId = user.SourceId
  866. item.Source = user.Source
  867. item.CreateTime = user.CreateTime
  868. item.ModifyTime = time.Now()
  869. items = append(items, item)
  870. userLabel[fmt.Sprint("user_id{|}", user.UserId, "{|}source_id{|}", user.SourceId, "{|}source{|}", user.Source)] = true
  871. }
  872. userLabelWeight[fmt.Sprint("user_id{|}", user.UserId, "{|}label{|}", user.Label)]++
  873. }
  874. //处理活动标签
  875. for _, user := range listActivity {
  876. if !userLabel[fmt.Sprint("user_id{|}", user.UserId, "{|}source_id{|}", user.IndustrialManagementId, "{|}source{|}", 1)] {
  877. item := new(models.CygxUserLabel)
  878. item.UserId = user.UserId
  879. item.CompanyId = user.CompanyId
  880. item.RealName = user.RealName
  881. item.Mobile = user.Mobile
  882. item.Email = user.Email
  883. item.Label = user.Label
  884. item.SourceId = user.IndustrialManagementId
  885. item.Source = 1
  886. item.CreateTime = user.CreateTime
  887. item.ModifyTime = time.Now()
  888. items = append(items, item)
  889. userLabel[fmt.Sprint("user_id{|}", user.UserId, "{|}source_id{|}", user.IndustrialManagementId, "{|}source{|}", 1)] = true
  890. }
  891. userLabelWeight[fmt.Sprint("user_id{|}", user.UserId, "{|}label{|}", user.Label)]++
  892. }
  893. //处理专项调研活动标签
  894. for _, user := range listActivitySpecial {
  895. if !userLabel[fmt.Sprint("user_id{|}", user.UserId, "{|}source_id{|}", user.IndustrialManagementId, "{|}source{|}", 1)] {
  896. item := new(models.CygxUserLabel)
  897. item.UserId = user.UserId
  898. item.CompanyId = user.CompanyId
  899. item.RealName = user.RealName
  900. item.Mobile = user.Mobile
  901. item.Email = user.Email
  902. item.Label = user.Label
  903. item.SourceId = user.IndustrialManagementId
  904. item.Source = 1
  905. item.CreateTime = user.CreateTime
  906. item.ModifyTime = time.Now()
  907. items = append(items, item)
  908. userLabel[fmt.Sprint("user_id{|}", user.UserId, "{|}source_id{|}", user.IndustrialManagementId, "{|}source{|}", 1)] = true
  909. }
  910. userLabelWeight[fmt.Sprint("user_id{|}", user.UserId, "{|}label{|}", user.Label)]++
  911. }
  912. var itemLimt []*models.CygxUserLabel
  913. fmt.Println(len(items))
  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. fmt.Println(len(itemsUpdate))
  945. e = models.UpdateCygxUserLabelMulti(itemsUpdate)
  946. if e != nil {
  947. err = errors.New("UpdateCygxUserLabelMulti" + e.Error())
  948. return
  949. }
  950. pars = make([]interface{}, 0)
  951. condition = ` GROUP BY user_id`
  952. listUser, e := models.GetCygxUserLabelList(condition, pars)
  953. for _, v := range listUser {
  954. go updateUserLabelByUserId(v.UserId)
  955. }
  956. return
  957. }