user_label.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  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队列消息失败:"+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. userList, e := models.GetWxUserByOutboundMobiles(mobileArr)
  86. if e != nil {
  87. err = errors.New("GetWxUserByOutboundMobiles" + e.Error())
  88. return
  89. }
  90. listActivityHistory, e := models.GetCygxUserLabelActivity(condition, pars, 0, 9999)
  91. if e != nil {
  92. err = errors.New("GetCygxUserLabelActivity" + e.Error())
  93. return
  94. }
  95. activityHistoryMap := make(map[int]bool)
  96. for _, v := range listActivityHistory {
  97. activityHistoryMap[v.UserId] = true
  98. }
  99. var items []*models.CygxUserLabelActivity
  100. for _, user := range userList {
  101. //已经提交到会的活动写入标签的不做二次添加处理
  102. if activityHistoryMap[user.UserId] {
  103. continue
  104. }
  105. // SourceType 1:文章阅读、 2产业关注、3:活动到会、4系列关注、5专项调研活动到会。
  106. log := &models.CygxUserLabelLogRedis{UserId: user.UserId, SourceId: activityId, SourceType: 3, CreateTime: time.Now()}
  107. if utils.Re == nil {
  108. err := utils.Rc.LPush(utils.CYGX_USER_KEY_LABEL, log)
  109. if err != nil {
  110. fmt.Println("CygxUserLabelLogRedis LPush Err:" + err.Error())
  111. }
  112. }
  113. item := new(models.CygxUserLabelActivity)
  114. item.UserId = user.UserId
  115. item.CompanyId = user.CompanyId
  116. item.RealName = user.RealName
  117. item.Mobile = user.Mobile
  118. item.Email = user.Email
  119. item.ActivityId = activityId
  120. item.CreateTime = time.Now()
  121. item.ModifyTime = time.Now()
  122. items = append(items, item)
  123. }
  124. if len(items) > 0 {
  125. _, err = models.AddCygxUserLabelActivityList(items)
  126. }
  127. return
  128. }
  129. func UserLabelLogReduce() (err error) {
  130. for {
  131. //SourceType int `description:"1:文章阅读、 2产业关注、3:活动到会、4系列关注、5专项调研活动到会。"`
  132. utils.Rc.Brpop(utils.CYGX_USER_KEY_LABEL, func(b []byte) {
  133. var log models.CygxUserLabelLogRedis
  134. if err := json.Unmarshal(b, &log); err != nil {
  135. fmt.Println("json unmarshal wrong!")
  136. go utils.SendAlarmMsg("用户更新相关标签处理Redis队列消息失败:"+err.Error()+string(b), 2)
  137. }
  138. switch log.SourceType {
  139. case 1:
  140. go ArticleHistoryUserLabelLogReduce(log)
  141. fmt.Println("文章阅读")
  142. break
  143. case 2:
  144. go IndustryFllowUserLabelLogReduce(log)
  145. fmt.Println("2产业关注")
  146. break
  147. case 3:
  148. go ActivityUserLabelLogReduce(log)
  149. fmt.Println("活动到会")
  150. break
  151. case 4:
  152. go CategoryFllowUserLabelLogReduce(log)
  153. fmt.Println("4系列关注")
  154. break
  155. case 5:
  156. go ActivitySpecialUserLabelLogReduce(log)
  157. fmt.Println("5专项调研活动到会")
  158. break
  159. default:
  160. fmt.Println(string(b))
  161. go utils.SendAlarmMsg("用户更新相关标签处理Redis队列消息失败:"+string(b), 2)
  162. }
  163. })
  164. }
  165. }
  166. // 1:文章阅读
  167. func ArticleHistoryUserLabelLogReduce(log models.CygxUserLabelLogRedis) (err error) {
  168. defer func() {
  169. if err != nil {
  170. fmt.Println(err)
  171. go utils.SendAlarmMsg("用户文章阅读更新相关标签,处理Redis队列消息失败:"+err.Error(), 2)
  172. }
  173. }()
  174. articleId := log.SourceId
  175. userId := log.UserId
  176. var source int
  177. user, e := models.GetWxUserItemByUserId(userId)
  178. if e != nil {
  179. err = errors.New("GetWxUserItemByUserId" + e.Error())
  180. return
  181. }
  182. var condition string
  183. var pars []interface{}
  184. condition = ` AND article_id = ? `
  185. pars = append(pars, articleId)
  186. industrialList, e := models.GetIndustrialArticleGroupManagementList(condition, pars)
  187. if e != nil && e.Error() != utils.ErrNoRow() {
  188. err = errors.New("GetIndustrialArticleGroupManagementList, Err: " + e.Error())
  189. return
  190. }
  191. var items []*models.CygxUserLabel
  192. if len(industrialList) == 0 {
  193. //如果没有行产业归类就按照行业报告处理
  194. source = 2
  195. detailArticle, e := models.GetArticleDetailById(articleId)
  196. if e != nil {
  197. err = errors.New("GetIndustrialArticleGroupManagementList, Err: " + e.Error())
  198. return
  199. }
  200. labelDetail, e := models.GetdetailByCategoryIdLabel(detailArticle.CategoryId)
  201. if e != nil {
  202. err = errors.New("GetIndustrialArticleGroupManagementList, Err: " + e.Error())
  203. return
  204. }
  205. label := labelDetail.MatchTypeName
  206. industrialManagementId := labelDetail.Id
  207. var condition string
  208. var pars []interface{}
  209. condition += ` AND source_id=? AND source = ? AND user_id = ? `
  210. pars = append(pars, industrialManagementId, source, userId)
  211. total, e := models.GetCygxUserLabelCount(condition, pars)
  212. if e != nil {
  213. err = errors.New("GetCygxProductInteriorCount" + e.Error())
  214. return
  215. }
  216. item := new(models.CygxUserLabel)
  217. item.UserId = user.UserId
  218. item.CompanyId = user.CompanyId
  219. item.RealName = user.RealName
  220. item.Mobile = user.Mobile
  221. item.Email = user.Email
  222. item.Label = label
  223. item.SourceId = industrialManagementId
  224. item.Source = source
  225. item.Weight = 1
  226. item.CreateTime = time.Now()
  227. item.ModifyTime = time.Now()
  228. items = append(items, item)
  229. if total == 0 {
  230. _, e = models.AddCygxUserLabel(item)
  231. if e != nil {
  232. err = errors.New("AddCygxUserLabel" + e.Error())
  233. return
  234. }
  235. } else {
  236. //source 来源1:产业、2:系列
  237. e = models.UpdateCygxUserLabelWeight(industrialManagementId, source, userId, label)
  238. if e != nil {
  239. err = errors.New("UpdateCygxUserLabelWeight" + e.Error())
  240. return
  241. }
  242. }
  243. } else {
  244. //如果有行产业归类就按照产业报告处理
  245. var topCond string
  246. var topPars []interface{}
  247. var industrialManagementIds []int
  248. for _, v := range industrialList {
  249. industrialManagementIds = append(industrialManagementIds, v.IndustrialManagementId)
  250. }
  251. idsLen := len(industrialManagementIds)
  252. if idsLen > 0 {
  253. topCond = ` AND industrial_management_id IN (` + utils.GetOrmInReplace(idsLen) + `)`
  254. topPars = append(topPars, industrialManagementIds)
  255. } else {
  256. return
  257. }
  258. industrNamelist, e := models.GetTopOneMonthArtReadNumIndustryAll(topCond, topPars)
  259. if e != nil {
  260. err = errors.New("GetTopOneMonthArtReadNumIndustryAll, Err: " + e.Error())
  261. return
  262. }
  263. source = 1
  264. for _, v := range industrNamelist {
  265. label := v.IndustryName
  266. industrialManagementId := v.IndustrialManagementId
  267. var condition string
  268. var pars []interface{}
  269. condition += ` AND source_id=? AND source = ? AND user_id = ? `
  270. pars = append(pars, v.IndustrialManagementId, source, userId)
  271. total, e := models.GetCygxUserLabelCount(condition, pars)
  272. if e != nil {
  273. err = errors.New("GetCygxProductInteriorCount" + e.Error())
  274. return
  275. }
  276. item := new(models.CygxUserLabel)
  277. item.UserId = user.UserId
  278. item.CompanyId = user.CompanyId
  279. item.RealName = user.RealName
  280. item.Mobile = user.Mobile
  281. item.Email = user.Email
  282. item.Label = label
  283. item.SourceId = industrialManagementId
  284. item.Source = source
  285. item.Weight = 1
  286. item.CreateTime = time.Now()
  287. item.ModifyTime = time.Now()
  288. items = append(items, item)
  289. if total == 0 {
  290. _, e = models.AddCygxUserLabel(item)
  291. if e != nil {
  292. err = errors.New("AddCygxUserLabel" + e.Error())
  293. return
  294. }
  295. } else {
  296. //source 来源1:产业、2:系列
  297. e = models.UpdateCygxUserLabelWeight(industrialManagementId, source, userId, label)
  298. if e != nil {
  299. err = errors.New("UpdateCygxUserLabelWeight" + e.Error())
  300. return
  301. }
  302. }
  303. }
  304. }
  305. if len(items) > 0 {
  306. go AddArticleHistoryUserLabelLog(items, articleId)
  307. }
  308. return
  309. }
  310. // 添加文章阅读记录日志,处理文章阅读三个月之内标签权重有效逻辑
  311. func AddArticleHistoryUserLabelLog(items []*models.CygxUserLabel, articleId int) (err error) {
  312. defer func() {
  313. if err != nil {
  314. fmt.Println(err)
  315. go utils.SendAlarmMsg("添加文章阅读记录日志,处理文章阅读三个月之内标签权重有效逻辑失败:"+err.Error(), 2)
  316. }
  317. }()
  318. if len(items) == 0 {
  319. return
  320. }
  321. for _, v := range items {
  322. item := new(models.CygxUserLabelArticle)
  323. item.UserId = v.UserId
  324. item.CompanyId = v.CompanyId
  325. item.RealName = v.RealName
  326. item.Mobile = v.Mobile
  327. item.Email = v.Email
  328. item.Label = v.Label
  329. item.SourceId = v.SourceId
  330. item.Source = v.Source
  331. item.ArticleId = articleId
  332. item.CreateTime = time.Now()
  333. item.ModifyTime = time.Now()
  334. _, e := models.AddCygxUserLabelArticle(item)
  335. if e != nil {
  336. err = errors.New("AddCygxUserLabelArticle" + e.Error())
  337. return
  338. }
  339. }
  340. return
  341. }
  342. // 2:产业关注
  343. func IndustryFllowUserLabelLogReduce(log models.CygxUserLabelLogRedis) (err error) {
  344. defer func() {
  345. if err != nil {
  346. fmt.Println(err)
  347. go utils.SendAlarmMsg("用户关注产业更新相关标签,处理Redis队列消息失败:"+err.Error(), 2)
  348. }
  349. }()
  350. isFllow := log.IsFllow
  351. industrialManagementId := log.SourceId
  352. userId := log.UserId
  353. source := 1
  354. detailIndustrial, e := models.GetIndustrialManagementDetail(industrialManagementId)
  355. if e != nil {
  356. err = errors.New("GetIndustrialManagementDetail" + e.Error())
  357. return
  358. }
  359. label := detailIndustrial.IndustryName
  360. if isFllow == 0 {
  361. e = models.UpdateCygxUserLabelIsFollow(isFllow, industrialManagementId, source, userId, label)
  362. if e != nil {
  363. err = errors.New("UpdateCygxUserLabelIsFollow" + e.Error())
  364. return
  365. }
  366. } else {
  367. var condition string
  368. var pars []interface{}
  369. condition += ` AND source_id=? AND source = ? AND user_id = ?`
  370. pars = append(pars, industrialManagementId, source, userId)
  371. total, e := models.GetCygxUserLabelCount(condition, pars)
  372. if e != nil {
  373. err = errors.New("GetCygxProductInteriorCount" + e.Error())
  374. return
  375. }
  376. if total == 0 {
  377. user, e := models.GetWxUserItemByUserId(userId)
  378. if e != nil {
  379. err = errors.New("GetWxUserItemByUserId" + e.Error())
  380. return
  381. }
  382. item := new(models.CygxUserLabel)
  383. item.UserId = user.UserId
  384. item.CompanyId = user.CompanyId
  385. item.RealName = user.RealName
  386. item.Mobile = user.Mobile
  387. item.Email = user.Email
  388. item.Label = label
  389. item.SourceId = industrialManagementId
  390. item.Source = source
  391. item.IsFollow = isFllow
  392. item.CreateTime = time.Now()
  393. item.ModifyTime = time.Now()
  394. _, e = models.AddCygxUserLabel(item)
  395. if e != nil {
  396. err = errors.New("AddCygxUserLabel" + e.Error())
  397. return
  398. }
  399. } else {
  400. //source 来源1:产业、2:系列
  401. e = models.UpdateCygxUserLabelIsFollow(isFllow, industrialManagementId, source, userId, label)
  402. if e != nil {
  403. err = errors.New("UpdateCygxUserLabelIsFollow" + e.Error())
  404. return
  405. }
  406. }
  407. }
  408. return
  409. }
  410. // 3:活动到会
  411. func ActivityUserLabelLogReduce(log models.CygxUserLabelLogRedis) (err error) {
  412. defer func() {
  413. if err != nil {
  414. fmt.Println(err)
  415. go utils.SendAlarmMsg("用户活动到会更新相关标签,处理Redis队列消息失败:"+err.Error(), 2)
  416. }
  417. }()
  418. activityId := log.SourceId
  419. userId := log.UserId
  420. var source int
  421. user, e := models.GetWxUserItemByUserId(userId)
  422. if e != nil {
  423. err = errors.New("GetWxUserItemByUserId" + e.Error())
  424. return
  425. }
  426. var condition string
  427. var pars []interface{}
  428. condition = ` AND activity_id = ? `
  429. pars = append(pars, activityId)
  430. industrialList, e := models.GetCygxIndustrialActivityGroupManagementList(condition+" AND source = 1 ", pars)
  431. if e != nil {
  432. err = errors.New("GetCygxIndustrialActivityGroupManagementList, Err: " + e.Error())
  433. return
  434. }
  435. //如果有行产业归类就按照产业报告处理
  436. var topCond string
  437. var topPars []interface{}
  438. var industrialManagementIds []int
  439. for _, v := range industrialList {
  440. industrialManagementIds = append(industrialManagementIds, v.IndustrialManagementId)
  441. }
  442. idsLen := len(industrialManagementIds)
  443. if idsLen > 0 {
  444. topCond = ` AND industrial_management_id IN (` + utils.GetOrmInReplace(idsLen) + `)`
  445. topPars = append(topPars, industrialManagementIds)
  446. } else {
  447. return
  448. }
  449. industrNamelist, e := models.GetTopOneMonthArtReadNumIndustryAll(topCond, topPars)
  450. if e != nil {
  451. err = errors.New("GetTopOneMonthArtReadNumIndustryAll, Err: " + e.Error())
  452. return
  453. }
  454. source = 1
  455. for _, v := range industrNamelist {
  456. label := v.IndustryName
  457. industrialManagementId := v.IndustrialManagementId
  458. var condition string
  459. var pars []interface{}
  460. condition += ` AND source_id=? AND source = ? AND user_id = ? `
  461. pars = append(pars, v.IndustrialManagementId, source, userId)
  462. total, e := models.GetCygxUserLabelCount(condition, pars)
  463. if e != nil {
  464. err = errors.New("GetCygxProductInteriorCount" + e.Error())
  465. return
  466. }
  467. if total == 0 {
  468. item := new(models.CygxUserLabel)
  469. item.UserId = user.UserId
  470. item.CompanyId = user.CompanyId
  471. item.RealName = user.RealName
  472. item.Mobile = user.Mobile
  473. item.Email = user.Email
  474. item.Label = label
  475. item.SourceId = industrialManagementId
  476. item.Source = source
  477. item.Weight = 1
  478. item.CreateTime = time.Now()
  479. item.ModifyTime = time.Now()
  480. _, e = models.AddCygxUserLabel(item)
  481. if e != nil {
  482. err = errors.New("AddCygxUserLabel" + e.Error())
  483. return
  484. }
  485. } else {
  486. //source 来源1:产业、2:系列
  487. e = models.UpdateCygxUserLabelWeight(industrialManagementId, source, userId, label)
  488. if e != nil {
  489. err = errors.New("UpdateCygxUserLabelWeight" + e.Error())
  490. return
  491. }
  492. }
  493. }
  494. return
  495. }
  496. // 4:系列关注
  497. func CategoryFllowUserLabelLogReduce(log models.CygxUserLabelLogRedis) (err error) {
  498. defer func() {
  499. if err != nil {
  500. fmt.Println(err)
  501. go utils.SendAlarmMsg("用户关注系列更新相关标签,处理Redis队列消息失败:"+err.Error(), 2)
  502. }
  503. }()
  504. isFllow := log.IsFllow
  505. industrialManagementId := log.SourceId
  506. userId := log.UserId
  507. source := 2
  508. detailIndustrial, e := models.GetCygxReportMappingById(industrialManagementId)
  509. if e != nil {
  510. err = errors.New("GetCygxReportMappingById" + e.Error())
  511. return
  512. }
  513. label := detailIndustrial.MatchTypeName
  514. if isFllow == 0 {
  515. e = models.UpdateCygxUserLabelIsFollow(isFllow, industrialManagementId, source, userId, label)
  516. if e != nil {
  517. err = errors.New("UpdateCygxUserLabelIsFollow" + e.Error())
  518. return
  519. }
  520. } else {
  521. var condition string
  522. var pars []interface{}
  523. condition += ` AND source_id=? AND source = ? AND user_id = ?`
  524. pars = append(pars, industrialManagementId, source, userId)
  525. total, e := models.GetCygxUserLabelCount(condition, pars)
  526. if e != nil {
  527. err = errors.New("GetCygxProductInteriorCount" + e.Error())
  528. return
  529. }
  530. if total == 0 {
  531. user, e := models.GetWxUserItemByUserId(userId)
  532. if e != nil {
  533. err = errors.New("GetWxUserItemByUserId" + e.Error())
  534. return
  535. }
  536. item := new(models.CygxUserLabel)
  537. item.UserId = user.UserId
  538. item.CompanyId = user.CompanyId
  539. item.RealName = user.RealName
  540. item.Mobile = user.Mobile
  541. item.Email = user.Email
  542. item.Label = label
  543. item.SourceId = industrialManagementId
  544. item.Source = source
  545. item.IsFollow = isFllow
  546. item.CreateTime = time.Now()
  547. item.ModifyTime = time.Now()
  548. _, e = models.AddCygxUserLabel(item)
  549. if e != nil {
  550. err = errors.New("AddCygxUserLabel" + e.Error())
  551. return
  552. }
  553. } else {
  554. //source 来源1:产业、2:系列
  555. e = models.UpdateCygxUserLabelIsFollow(isFllow, industrialManagementId, source, userId, label)
  556. if e != nil {
  557. err = errors.New("UpdateCygxUserLabelIsFollow" + e.Error())
  558. return
  559. }
  560. }
  561. }
  562. return
  563. }
  564. // 5:专项调研活动到会
  565. func ActivitySpecialUserLabelLogReduce(log models.CygxUserLabelLogRedis) (err error) {
  566. defer func() {
  567. if err != nil {
  568. fmt.Println(err)
  569. go utils.SendAlarmMsg("用户活动到会更新相关标签,处理Redis队列消息失败:"+err.Error(), 2)
  570. }
  571. }()
  572. activityId := log.SourceId
  573. userId := log.UserId
  574. var source int
  575. user, e := models.GetWxUserItemByUserId(userId)
  576. if e != nil {
  577. err = errors.New("GetWxUserItemByUserId" + e.Error())
  578. return
  579. }
  580. var condition string
  581. var pars []interface{}
  582. condition = ` AND activity_id = ? `
  583. pars = append(pars, activityId)
  584. industrialList, e := models.GetCygxIndustrialActivityGroupManagementList(condition+" AND source = 2 ", pars)
  585. if e != nil {
  586. err = errors.New("GetCygxIndustrialActivityGroupManagementList, Err: " + e.Error())
  587. return
  588. }
  589. //如果有行产业归类就按照产业报告处理
  590. var topCond string
  591. var topPars []interface{}
  592. var industrialManagementIds []int
  593. for _, v := range industrialList {
  594. industrialManagementIds = append(industrialManagementIds, v.IndustrialManagementId)
  595. }
  596. idsLen := len(industrialManagementIds)
  597. if idsLen > 0 {
  598. topCond = ` AND industrial_management_id IN (` + utils.GetOrmInReplace(idsLen) + `)`
  599. topPars = append(topPars, industrialManagementIds)
  600. } else {
  601. return
  602. }
  603. industrNamelist, e := models.GetTopOneMonthArtReadNumIndustryAll(topCond, topPars)
  604. if e != nil {
  605. err = errors.New("GetTopOneMonthArtReadNumIndustryAll, Err: " + e.Error())
  606. return
  607. }
  608. source = 1
  609. for _, v := range industrNamelist {
  610. label := v.IndustryName
  611. industrialManagementId := v.IndustrialManagementId
  612. var condition string
  613. var pars []interface{}
  614. condition += ` AND source_id=? AND source = ? AND user_id = ? `
  615. pars = append(pars, v.IndustrialManagementId, source, userId)
  616. total, e := models.GetCygxUserLabelCount(condition, pars)
  617. if e != nil {
  618. err = errors.New("GetCygxProductInteriorCount" + e.Error())
  619. return
  620. }
  621. if total == 0 {
  622. item := new(models.CygxUserLabel)
  623. item.UserId = user.UserId
  624. item.CompanyId = user.CompanyId
  625. item.RealName = user.RealName
  626. item.Mobile = user.Mobile
  627. item.Email = user.Email
  628. item.Label = label
  629. item.SourceId = industrialManagementId
  630. item.Source = source
  631. item.Weight = 1
  632. item.CreateTime = time.Now()
  633. item.ModifyTime = time.Now()
  634. _, e = models.AddCygxUserLabel(item)
  635. if e != nil {
  636. err = errors.New("AddCygxUserLabel" + e.Error())
  637. return
  638. }
  639. } else {
  640. //source 来源1:产业、2:系列
  641. e = models.UpdateCygxUserLabelWeight(industrialManagementId, source, userId, label)
  642. if e != nil {
  643. err = errors.New("UpdateCygxUserLabelWeight" + e.Error())
  644. return
  645. }
  646. }
  647. }
  648. return
  649. }
  650. // func init() {
  651. // updateUserLabelByUserId(53095)
  652. // }
  653. // 更新用户标签
  654. func updateUserLabelByUserId(userId int) (err error) {
  655. defer func() {
  656. if err != nil {
  657. fmt.Println(err)
  658. go utils.SendAlarmMsg("更新用户标签失败:"+err.Error()+"userId:"+strconv.Itoa(userId), 2)
  659. }
  660. }()
  661. var condition string
  662. var pars []interface{}
  663. condition = ` AND is_follow=1 AND user_id = ? `
  664. pars = append(pars, userId)
  665. totalSource1, e := models.GetCygxUserLabelCount(condition+" AND source = 1 ", pars)
  666. if e != nil {
  667. err = errors.New("GetCygxProductInteriorCount" + e.Error())
  668. return
  669. }
  670. totalSource2, e := models.GetCygxUserLabelCount(condition+" AND source = 2 ", pars)
  671. if e != nil {
  672. err = errors.New("GetCygxProductInteriorCount" + e.Error())
  673. return
  674. }
  675. condition = ` AND user_id = ? `
  676. var list []*models.CygxUserLabel
  677. //当产业关注数量不超过10 ,系列关注数量不超过5
  678. if totalSource1 <= 10 && totalSource2 <= 5 {
  679. condition += ` AND ( is_follow > 0 OR weight > 0 ) ORDER BY is_follow DESC, weight DESC `
  680. list, e = models.GetCygxUserLabelList(condition, pars)
  681. if e != nil {
  682. err = errors.New("GetCygxUserLabelList" + e.Error())
  683. return
  684. }
  685. }
  686. //当产业关注数量超过10 ,系列关注数量超过5
  687. if totalSource1 > 10 && totalSource2 > 1 {
  688. condition += ` AND weight > 0 ORDER BY weight DESC `
  689. list, e = models.GetCygxUserLabelList(condition, pars)
  690. if e != nil {
  691. err = errors.New("GetCygxUserLabelList" + e.Error())
  692. return
  693. }
  694. }
  695. //当产业关注数量不超过10 ,系列关注数量超过5
  696. if totalSource1 <= 10 && totalSource2 > 5 {
  697. fmt.Println("当产业关注数量不超过10 ,系列关注数量超过5")
  698. condition += ` AND source = 1 AND is_follow = 1 ORDER BY weight DESC `
  699. listfollow, e := models.GetCygxUserLabelList(condition, pars)
  700. if e != nil {
  701. err = errors.New("GetCygxUserLabelList" + e.Error())
  702. return
  703. }
  704. idMap := make(map[int]bool)
  705. for _, v := range listfollow {
  706. idMap[v.Id] = true
  707. list = append(list, v)
  708. }
  709. pars = make([]interface{}, 0)
  710. condition = ` AND user_id = ? `
  711. pars = append(pars, userId)
  712. condition += ` AND weight > 0 ORDER BY weight DESC `
  713. listother, e := models.GetCygxUserLabelList(condition, pars)
  714. if e != nil {
  715. err = errors.New("GetCygxUserLabelList" + e.Error())
  716. return
  717. }
  718. for _, v := range listother {
  719. if idMap[v.Id] {
  720. continue
  721. }
  722. list = append(list, v)
  723. }
  724. }
  725. //当产业关注数量超过10 ,系列关注数量不超过5
  726. if totalSource1 > 10 && totalSource2 <= 5 {
  727. condition += ` AND source = 2 AND is_follow = 1 ORDER BY weight DESC `
  728. listfollow, e := models.GetCygxUserLabelList(condition, pars)
  729. if e != nil {
  730. err = errors.New("GetCygxUserLabelList" + e.Error())
  731. return
  732. }
  733. idMap := make(map[int]bool)
  734. for _, v := range listfollow {
  735. idMap[v.Id] = true
  736. list = append(list, v)
  737. }
  738. pars = make([]interface{}, 0)
  739. condition = ` AND user_id = ? `
  740. pars = append(pars, userId)
  741. condition += ` AND weight > 0 ORDER BY weight DESC `
  742. listother, e := models.GetCygxUserLabelList(condition, pars)
  743. if e != nil {
  744. err = errors.New("GetCygxUserLabelList" + e.Error())
  745. return
  746. }
  747. for _, v := range listother {
  748. if idMap[v.Id] {
  749. continue
  750. }
  751. list = append(list, v)
  752. }
  753. }
  754. var labelUser string
  755. for _, v := range list {
  756. labelUser += v.Label + ","
  757. }
  758. labelUser = strings.TrimRight(labelUser, ",")
  759. e = models.UpdateUserLabel(labelUser, userId)
  760. if e != nil {
  761. err = errors.New("UpdateUserLabel" + e.Error())
  762. return err
  763. }
  764. return
  765. }