user_label.go 22 KB

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