user_label.go 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  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. countFllow, e := models.GetCountCygxIndustryFllowByType(userId, industrialManagementId) // 是否重点关注
  395. if e != nil {
  396. err = errors.New("GetCountCygxIndustryFllowByType" + e.Error())
  397. return
  398. }
  399. isFllow = countFllow
  400. label := detailIndustrial.IndustryName
  401. if isFllow == 0 {
  402. e = models.UpdateCygxUserLabelIsFollow(isFllow, industrialManagementId, source, userId, label)
  403. if e != nil {
  404. err = errors.New("UpdateCygxUserLabelIsFollow" + e.Error())
  405. return
  406. }
  407. } else {
  408. var condition string
  409. var pars []interface{}
  410. condition += ` AND source_id=? AND source = ? AND user_id = ?`
  411. pars = append(pars, industrialManagementId, source, userId)
  412. total, e := models.GetCygxUserLabelCount(condition, pars)
  413. if e != nil {
  414. err = errors.New("GetCygxProductInteriorCount" + e.Error())
  415. return
  416. }
  417. if total == 0 {
  418. user, e := models.GetWxUserItemByUserId(userId)
  419. if e != nil {
  420. err = errors.New("GetWxUserItemByUserId" + e.Error())
  421. return
  422. }
  423. item := new(models.CygxUserLabel)
  424. item.UserId = user.UserId
  425. item.CompanyId = user.CompanyId
  426. item.RealName = user.RealName
  427. item.Mobile = user.Mobile
  428. item.Email = user.Email
  429. item.Label = label
  430. item.SourceId = industrialManagementId
  431. item.Source = source
  432. item.IsFollow = isFllow
  433. item.CreateTime = time.Now()
  434. item.ModifyTime = time.Now()
  435. _, e = models.AddCygxUserLabel(item)
  436. if e != nil {
  437. err = errors.New("AddCygxUserLabel" + e.Error())
  438. return
  439. }
  440. } else {
  441. //source 来源1:产业、2:系列
  442. e = models.UpdateCygxUserLabelIsFollow(isFllow, industrialManagementId, source, userId, label)
  443. if e != nil {
  444. err = errors.New("UpdateCygxUserLabelIsFollow" + e.Error())
  445. return
  446. }
  447. }
  448. }
  449. updateUserLabelByUserId(userId)
  450. return
  451. }
  452. // 3:活动到会
  453. func ActivityUserLabelLogReduce(log models.CygxUserLabelLogRedis) (err error) {
  454. defer func() {
  455. if err != nil {
  456. fmt.Println(err)
  457. go utils.SendAlarmMsg("用户活动到会更新相关标签,处理Redis队列消息失败:"+err.Error(), 2)
  458. }
  459. }()
  460. activityId := log.SourceId
  461. userId := log.UserId
  462. var source int
  463. user, e := models.GetWxUserItemByUserId(userId)
  464. if e != nil {
  465. err = errors.New("GetWxUserItemByUserId" + e.Error())
  466. return
  467. }
  468. var condition string
  469. var pars []interface{}
  470. condition = ` AND activity_id = ? `
  471. pars = append(pars, activityId)
  472. industrialList, e := models.GetCygxIndustrialActivityGroupManagementList(condition+" AND source = 1 ", pars)
  473. if e != nil {
  474. err = errors.New("GetCygxIndustrialActivityGroupManagementList, Err: " + e.Error())
  475. return
  476. }
  477. //如果有行产业归类就按照产业报告处理
  478. var topCond string
  479. var topPars []interface{}
  480. var industrialManagementIds []int
  481. for _, v := range industrialList {
  482. industrialManagementIds = append(industrialManagementIds, v.IndustrialManagementId)
  483. }
  484. idsLen := len(industrialManagementIds)
  485. if idsLen > 0 {
  486. topCond = ` AND industrial_management_id IN (` + utils.GetOrmInReplace(idsLen) + `)`
  487. topPars = append(topPars, industrialManagementIds)
  488. } else {
  489. return
  490. }
  491. industrNamelist, e := models.GetTopOneMonthArtReadNumIndustryAll(topCond, topPars)
  492. if e != nil {
  493. err = errors.New("GetTopOneMonthArtReadNumIndustryAll, Err: " + e.Error())
  494. return
  495. }
  496. source = 1
  497. for _, v := range industrNamelist {
  498. label := v.IndustryName
  499. industrialManagementId := v.IndustrialManagementId
  500. var condition string
  501. var pars []interface{}
  502. condition += ` AND source_id=? AND source = ? AND user_id = ? `
  503. pars = append(pars, v.IndustrialManagementId, source, userId)
  504. total, e := models.GetCygxUserLabelCount(condition, pars)
  505. if e != nil {
  506. err = errors.New("GetCygxProductInteriorCount" + e.Error())
  507. return
  508. }
  509. if total == 0 {
  510. item := new(models.CygxUserLabel)
  511. item.UserId = user.UserId
  512. item.CompanyId = user.CompanyId
  513. item.RealName = user.RealName
  514. item.Mobile = user.Mobile
  515. item.Email = user.Email
  516. item.Label = label
  517. item.SourceId = industrialManagementId
  518. item.Source = source
  519. item.Weight = 1
  520. item.CreateTime = time.Now()
  521. item.ModifyTime = time.Now()
  522. _, e = models.AddCygxUserLabel(item)
  523. if e != nil {
  524. err = errors.New("AddCygxUserLabel" + e.Error())
  525. return
  526. }
  527. } else {
  528. //source 来源1:产业、2:系列
  529. e = models.UpdateCygxUserLabelWeight(industrialManagementId, source, userId, label)
  530. if e != nil {
  531. err = errors.New("UpdateCygxUserLabelWeight" + e.Error())
  532. return
  533. }
  534. }
  535. }
  536. updateUserLabelByUserId(userId)
  537. return
  538. }
  539. // 4:系列关注
  540. func CategoryFllowUserLabelLogReduce(log models.CygxUserLabelLogRedis) (err error) {
  541. defer func() {
  542. if err != nil {
  543. fmt.Println(err)
  544. go utils.SendAlarmMsg("用户关注系列更新相关标签,处理Redis队列消息失败:"+err.Error()+fmt.Sprint(log), 2)
  545. }
  546. }()
  547. isFllow := log.IsFllow
  548. industrialManagementId := log.SourceId
  549. userId := log.UserId
  550. source := 2
  551. detailIndustrial, e := models.GetCygxReportMappingById(industrialManagementId)
  552. if e != nil {
  553. err = errors.New("GetCygxReportMappingById" + e.Error())
  554. return
  555. }
  556. label := detailIndustrial.MatchTypeName
  557. countFllow, e := models.GetCountCygxXzsChooseCategoryFllowByType(userId, industrialManagementId) // 是否重点关注
  558. if e != nil {
  559. err = errors.New("GetCountCygxIndustryFllowByType" + e.Error())
  560. return
  561. }
  562. isFllow = countFllow
  563. if isFllow == 0 {
  564. e = models.UpdateCygxUserLabelIsFollow(isFllow, industrialManagementId, source, userId, label)
  565. if e != nil {
  566. err = errors.New("UpdateCygxUserLabelIsFollow" + e.Error())
  567. return
  568. }
  569. } else {
  570. var condition string
  571. var pars []interface{}
  572. condition += ` AND source_id=? AND source = ? AND user_id = ?`
  573. pars = append(pars, industrialManagementId, source, userId)
  574. total, e := models.GetCygxUserLabelCount(condition, pars)
  575. if e != nil {
  576. err = errors.New("GetCygxProductInteriorCount" + e.Error())
  577. return
  578. }
  579. if total == 0 {
  580. user, e := models.GetWxUserItemByUserId(userId)
  581. if e != nil {
  582. err = errors.New("GetWxUserItemByUserId" + e.Error())
  583. return
  584. }
  585. item := new(models.CygxUserLabel)
  586. item.UserId = user.UserId
  587. item.CompanyId = user.CompanyId
  588. item.RealName = user.RealName
  589. item.Mobile = user.Mobile
  590. item.Email = user.Email
  591. item.Label = label
  592. item.SourceId = industrialManagementId
  593. item.Source = source
  594. item.IsFollow = isFllow
  595. item.CreateTime = time.Now()
  596. item.ModifyTime = time.Now()
  597. _, e = models.AddCygxUserLabel(item)
  598. if e != nil {
  599. err = errors.New("AddCygxUserLabel" + e.Error())
  600. return
  601. }
  602. } else {
  603. //source 来源1:产业、2:系列
  604. e = models.UpdateCygxUserLabelIsFollow(isFllow, industrialManagementId, source, userId, label)
  605. if e != nil {
  606. err = errors.New("UpdateCygxUserLabelIsFollow" + e.Error())
  607. return
  608. }
  609. }
  610. }
  611. updateUserLabelByUserId(userId)
  612. return
  613. }
  614. // 5:专项调研活动到会
  615. func ActivitySpecialUserLabelLogReduce(log models.CygxUserLabelLogRedis) (err error) {
  616. defer func() {
  617. if err != nil {
  618. fmt.Println(err)
  619. go utils.SendAlarmMsg("用户活动到会更新相关标签,处理Redis队列消息失败:"+err.Error(), 2)
  620. }
  621. }()
  622. activityId := log.SourceId
  623. userId := log.UserId
  624. var source int
  625. user, e := models.GetWxUserItemByUserId(userId)
  626. if e != nil {
  627. err = errors.New("GetWxUserItemByUserId" + e.Error())
  628. return
  629. }
  630. var condition string
  631. var pars []interface{}
  632. condition = ` AND activity_id = ? `
  633. pars = append(pars, activityId)
  634. industrialList, e := models.GetCygxIndustrialActivityGroupManagementList(condition+" AND source = 2 ", pars)
  635. if e != nil {
  636. err = errors.New("GetCygxIndustrialActivityGroupManagementList, Err: " + e.Error())
  637. return
  638. }
  639. //如果有行产业归类就按照产业报告处理
  640. var topCond string
  641. var topPars []interface{}
  642. var industrialManagementIds []int
  643. for _, v := range industrialList {
  644. industrialManagementIds = append(industrialManagementIds, v.IndustrialManagementId)
  645. }
  646. idsLen := len(industrialManagementIds)
  647. if idsLen > 0 {
  648. topCond = ` AND industrial_management_id IN (` + utils.GetOrmInReplace(idsLen) + `)`
  649. topPars = append(topPars, industrialManagementIds)
  650. } else {
  651. return
  652. }
  653. industrNamelist, e := models.GetTopOneMonthArtReadNumIndustryAll(topCond, topPars)
  654. if e != nil {
  655. err = errors.New("GetTopOneMonthArtReadNumIndustryAll, Err: " + e.Error())
  656. return
  657. }
  658. source = 1
  659. for _, v := range industrNamelist {
  660. label := v.IndustryName
  661. industrialManagementId := v.IndustrialManagementId
  662. var condition string
  663. var pars []interface{}
  664. condition += ` AND source_id=? AND source = ? AND user_id = ? `
  665. pars = append(pars, v.IndustrialManagementId, source, userId)
  666. total, e := models.GetCygxUserLabelCount(condition, pars)
  667. if e != nil {
  668. err = errors.New("GetCygxProductInteriorCount" + e.Error())
  669. return
  670. }
  671. if total == 0 {
  672. item := new(models.CygxUserLabel)
  673. item.UserId = user.UserId
  674. item.CompanyId = user.CompanyId
  675. item.RealName = user.RealName
  676. item.Mobile = user.Mobile
  677. item.Email = user.Email
  678. item.Label = label
  679. item.SourceId = industrialManagementId
  680. item.Source = source
  681. item.Weight = 1
  682. item.CreateTime = time.Now()
  683. item.ModifyTime = time.Now()
  684. _, e = models.AddCygxUserLabel(item)
  685. if e != nil {
  686. err = errors.New("AddCygxUserLabel" + e.Error())
  687. return
  688. }
  689. } else {
  690. //source 来源1:产业、2:系列
  691. e = models.UpdateCygxUserLabelWeight(industrialManagementId, source, userId, label)
  692. if e != nil {
  693. err = errors.New("UpdateCygxUserLabelWeight" + e.Error())
  694. return
  695. }
  696. }
  697. }
  698. updateUserLabelByUserId(userId)
  699. return
  700. }
  701. //func init() {
  702. // var condition string
  703. // var pars []interface{}
  704. // pars = make([]interface{}, 0)
  705. // condition = ` GROUP BY user_id`
  706. // listUser, _ := models.GetCygxUserLabelList(condition, pars)
  707. // for k, v := range listUser {
  708. // fmt.Println(k)
  709. // updateUserLabelByUserId(v.UserId)
  710. // }
  711. // //updateUserLabelByUserId(13269)
  712. //}
  713. // 更新用户标签
  714. func updateUserLabelByUserId(userId int) (err error) {
  715. defer func() {
  716. if err != nil {
  717. fmt.Println(err)
  718. utils.FileLog.Info("更新用户标签,Err:%s", err.Error(), userId)
  719. go utils.SendAlarmMsg("更新用户标签失败:"+err.Error()+"userId:"+strconv.Itoa(userId), 2)
  720. }
  721. }()
  722. //加200毫秒的延迟
  723. time.Sleep(200 * time.Millisecond)
  724. var condition string
  725. var pars []interface{}
  726. condition = ` AND is_follow=1 AND user_id = ? AND modify_time > DATE_SUB(CURDATE(), INTERVAL 3 MONTH) `
  727. pars = append(pars, userId)
  728. totalSource1, e := models.GetCygxUserLabelCount(condition+" AND source = 1 ", pars)
  729. if e != nil {
  730. err = errors.New("GetCygxProductInteriorCount" + e.Error())
  731. return
  732. }
  733. totalSource2, e := models.GetCygxUserLabelCount(condition+" AND source = 2 ", pars)
  734. if e != nil {
  735. err = errors.New("GetCygxProductInteriorCount" + e.Error())
  736. return
  737. }
  738. condition = ` AND user_id = ? `
  739. var list []*models.CygxUserLabel
  740. //当产业关注数量不超过10 ,系列关注数量不超过5
  741. if totalSource1 <= 10 && totalSource2 <= 5 {
  742. condition += ` AND ( is_follow > 0 OR weight > 0 ) ORDER BY is_follow DESC, weight DESC `
  743. list, e = models.GetCygxUserLabelList(condition, pars)
  744. if e != nil {
  745. err = errors.New("GetCygxUserLabelList当产业关注数量不超过10 ,系列关注数量不超过5" + e.Error())
  746. return
  747. }
  748. }
  749. //当产业关注数量超过10 ,系列关注数量超过5
  750. if totalSource1 > 10 && totalSource2 > 5 {
  751. condition += ` AND weight > 0 ORDER BY weight DESC `
  752. list, e = models.GetCygxUserLabelList(condition, pars)
  753. if e != nil {
  754. err = errors.New("GetCygxUserLabelList 当产业关注数量超过10 ,系列关注数量超过5" + e.Error())
  755. return
  756. }
  757. }
  758. //当产业关注数量不超过10 ,系列关注数量超过5
  759. if totalSource1 <= 10 && totalSource2 > 5 {
  760. condition += ` AND source = 1 AND is_follow = 1 ORDER BY weight DESC `
  761. listfollow, e := models.GetCygxUserLabelList(condition, pars)
  762. if e != nil {
  763. err = errors.New("GetCygxUserLabelList" + e.Error())
  764. return
  765. }
  766. idMap := make(map[int]bool)
  767. for _, v := range listfollow {
  768. idMap[v.Id] = true
  769. list = append(list, v)
  770. }
  771. pars = make([]interface{}, 0)
  772. condition = ` AND user_id = ? `
  773. pars = append(pars, userId)
  774. condition += ` AND weight > 0 ORDER BY weight DESC `
  775. listother, e := models.GetCygxUserLabelList(condition, pars)
  776. if e != nil {
  777. err = errors.New("GetCygxUserLabelList 当产业关注数量不超过10 ,系列关注数量超过5" + e.Error())
  778. return
  779. }
  780. for _, v := range listother {
  781. if idMap[v.Id] {
  782. continue
  783. }
  784. list = append(list, v)
  785. }
  786. }
  787. //当产业关注数量超过10 ,系列关注数量不超过5
  788. if totalSource1 > 10 && totalSource2 <= 5 {
  789. condition += ` AND source = 2 AND is_follow = 1 ORDER BY weight DESC `
  790. listfollow, e := models.GetCygxUserLabelList(condition, pars)
  791. if e != nil {
  792. err = errors.New("GetCygxUserLabelList 当产业关注数量超过10 ,系列关注数量不超过5" + e.Error())
  793. return
  794. }
  795. idMap := make(map[int]bool)
  796. for _, v := range listfollow {
  797. idMap[v.Id] = true
  798. list = append(list, v)
  799. }
  800. pars = make([]interface{}, 0)
  801. condition = ` AND user_id = ? `
  802. pars = append(pars, userId)
  803. condition += ` AND weight > 0 ORDER BY weight DESC `
  804. listother, e := models.GetCygxUserLabelList(condition, pars)
  805. if e != nil {
  806. err = errors.New("GetCygxUserLabelList 当产业关注数量超过10 ,系列关注数量不超过5" + e.Error())
  807. return
  808. }
  809. for _, v := range listother {
  810. if idMap[v.Id] {
  811. continue
  812. }
  813. list = append(list, v)
  814. }
  815. }
  816. var labelUser string
  817. for _, v := range list {
  818. labelUser += v.Label + ","
  819. }
  820. labelUser = strings.TrimRight(labelUser, ",")
  821. e = models.UpdateUserLabel(labelUser, userId)
  822. if e != nil {
  823. err = errors.New("UpdateUserLabel" + e.Error())
  824. return err
  825. }
  826. return
  827. }
  828. //func init() {
  829. // UpdateUserLabelWeight()
  830. //}
  831. // 更新用户标签权重
  832. func UpdateUserLabelWeight() (err error) {
  833. defer func() {
  834. if err != nil {
  835. fmt.Println(err)
  836. go utils.SendAlarmMsg("更新用户标签权重失败:"+err.Error(), 2)
  837. }
  838. }()
  839. var condition string
  840. var pars []interface{}
  841. list, e := models.GetCygxUserLabelList(condition, pars)
  842. if e != nil {
  843. err = errors.New("GetCygxUserLabelList" + e.Error())
  844. return
  845. }
  846. updateTime := time.Now().AddDate(0, -3, 0)
  847. condition = ` AND create_time > ?`
  848. pars = append(pars, updateTime)
  849. listArticle, e := models.GetCygxUserLabelArticleList(condition, pars)
  850. if e != nil {
  851. err = errors.New("GetCygxUserLabelArticleList" + e.Error())
  852. return
  853. }
  854. listActivity, e := models.GetCygxUserLabelActivity(condition, pars)
  855. if e != nil {
  856. err = errors.New("GetCygxUserLabelActivity" + e.Error())
  857. return
  858. }
  859. listActivitySpecial, e := models.GetCygxUserLabelActivitySpecial(condition, pars)
  860. if e != nil {
  861. err = errors.New("GetCygxUserLabelActivitySpecial" + e.Error())
  862. return
  863. }
  864. userLabel := make(map[string]bool)
  865. userLabelWeight := make(map[string]int)
  866. for _, v := range list {
  867. userLabel[fmt.Sprint("user_id{|}", v.UserId, "{|}source_id{|}", v.SourceId, "{|}source{|}", v.Source)] = true
  868. }
  869. fmt.Println(userLabel)
  870. //处理文章标签
  871. var items []*models.CygxUserLabel
  872. for _, user := range listArticle {
  873. if !userLabel[fmt.Sprint("user_id{|}", user.UserId, "{|}source_id{|}", user.SourceId, "{|}source{|}", user.Source)] {
  874. item := new(models.CygxUserLabel)
  875. item.UserId = user.UserId
  876. item.CompanyId = user.CompanyId
  877. item.RealName = user.RealName
  878. item.Mobile = user.Mobile
  879. item.Email = user.Email
  880. item.Label = user.Label
  881. item.SourceId = user.SourceId
  882. item.Source = user.Source
  883. item.CreateTime = user.CreateTime
  884. item.ModifyTime = time.Now()
  885. items = append(items, item)
  886. userLabel[fmt.Sprint("user_id{|}", user.UserId, "{|}source_id{|}", user.SourceId, "{|}source{|}", user.Source)] = true
  887. }
  888. userLabelWeight[fmt.Sprint("user_id{|}", user.UserId, "{|}source_id{|}", user.SourceId, "{|}source{|}", user.Source)]++
  889. }
  890. //处理活动标签
  891. for _, user := range listActivity {
  892. if !userLabel[fmt.Sprint("user_id{|}", user.UserId, "{|}source_id{|}", user.IndustrialManagementId, "{|}source{|}", 1)] {
  893. item := new(models.CygxUserLabel)
  894. item.UserId = user.UserId
  895. item.CompanyId = user.CompanyId
  896. item.RealName = user.RealName
  897. item.Mobile = user.Mobile
  898. item.Email = user.Email
  899. item.Label = user.Label
  900. item.SourceId = user.IndustrialManagementId
  901. item.Source = 1
  902. item.CreateTime = user.CreateTime
  903. item.ModifyTime = time.Now()
  904. items = append(items, item)
  905. userLabel[fmt.Sprint("user_id{|}", user.UserId, "{|}source_id{|}", user.IndustrialManagementId, "{|}source{|}", 1)] = true
  906. }
  907. userLabelWeight[fmt.Sprint("user_id{|}", user.UserId, "{|}source_id{|}", user.IndustrialManagementId, "{|}source{|}", 1)]++
  908. }
  909. //处理专项调研活动标签
  910. for _, user := range listActivitySpecial {
  911. if !userLabel[fmt.Sprint("user_id{|}", user.UserId, "{|}source_id{|}", user.IndustrialManagementId, "{|}source{|}", 1)] {
  912. item := new(models.CygxUserLabel)
  913. item.UserId = user.UserId
  914. item.CompanyId = user.CompanyId
  915. item.RealName = user.RealName
  916. item.Mobile = user.Mobile
  917. item.Email = user.Email
  918. item.Label = user.Label
  919. item.SourceId = user.IndustrialManagementId
  920. item.Source = 1
  921. item.CreateTime = user.CreateTime
  922. item.ModifyTime = time.Now()
  923. items = append(items, item)
  924. userLabel[fmt.Sprint("user_id{|}", user.UserId, "{|}source_id{|}", user.IndustrialManagementId, "{|}source{|}", 1)] = true
  925. }
  926. userLabelWeight[fmt.Sprint("user_id{|}", user.UserId, "{|}source_id{|}", user.IndustrialManagementId, "{|}source{|}", 1)]++
  927. }
  928. var itemLimt []*models.CygxUserLabel
  929. if len(items) > 0 {
  930. for _, v := range items {
  931. itemLimt = append(itemLimt, v)
  932. if len(itemLimt)%5000 == 0 {
  933. err = models.CygxUserLabelMulti(itemLimt)
  934. fmt.Println(err)
  935. itemLimt = make([]*models.CygxUserLabel, 0)
  936. }
  937. }
  938. }
  939. if len(itemLimt) > 0 {
  940. err = models.CygxUserLabelMulti(itemLimt)
  941. fmt.Println(err)
  942. }
  943. var itemsUpdate []*models.CygxUserLabel
  944. for k, v := range userLabelWeight {
  945. sliceUser := strings.Split(k, "{|}")
  946. userid, _ := strconv.Atoi(sliceUser[1])
  947. label := sliceUser[len(sliceUser)-1]
  948. item := new(models.CygxUserLabel)
  949. item.UserId = userid
  950. item.Label = label
  951. item.Weight = v
  952. itemsUpdate = append(itemsUpdate, item)
  953. }
  954. e = models.UpdateCygxUserLabelWeightAll()
  955. if e != nil {
  956. err = errors.New("UpdateCygxUserLabelWeightAll" + e.Error())
  957. return
  958. }
  959. e = models.UpdateCygxUserLabelMulti(itemsUpdate)
  960. if e != nil {
  961. err = errors.New("UpdateCygxUserLabelMulti" + e.Error())
  962. return
  963. }
  964. pars = make([]interface{}, 0)
  965. condition = ` GROUP BY user_id`
  966. listUser, e := models.GetCygxUserLabelList(condition, pars)
  967. for _, v := range listUser {
  968. go updateUserLabelByUserId(v.UserId)
  969. }
  970. return
  971. }