user_label.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  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. if len(industrialList) == 0 {
  192. //如果没有行产业归类就按照行业报告处理
  193. source = 2
  194. detailArticle, e := models.GetArticleDetailById(articleId)
  195. if e != nil {
  196. err = errors.New("GetIndustrialArticleGroupManagementList, Err: " + e.Error())
  197. return
  198. }
  199. labelDetail, e := models.GetdetailByCategoryIdLabel(detailArticle.CategoryId)
  200. if e != nil {
  201. err = errors.New("GetIndustrialArticleGroupManagementList, Err: " + e.Error())
  202. return
  203. }
  204. label := labelDetail.MatchTypeName
  205. industrialManagementId := labelDetail.Id
  206. var condition string
  207. var pars []interface{}
  208. condition += ` AND source_id=? AND source = ? AND user_id = ? `
  209. pars = append(pars, industrialManagementId, source, userId)
  210. total, e := models.GetCygxUserLabelCount(condition, pars)
  211. if e != nil {
  212. err = errors.New("GetCygxProductInteriorCount" + e.Error())
  213. return
  214. }
  215. if total == 0 {
  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. _, e = models.AddCygxUserLabel(item)
  229. if e != nil {
  230. err = errors.New("AddCygxUserLabel" + e.Error())
  231. return
  232. }
  233. } else {
  234. //source 来源1:产业、2:系列
  235. e = models.UpdateCygxUserLabelWeight(industrialManagementId, source, userId, label)
  236. if e != nil {
  237. err = errors.New("UpdateCygxUserLabelWeight" + e.Error())
  238. return
  239. }
  240. }
  241. } else {
  242. //如果有行产业归类就按照产业报告处理
  243. var topCond string
  244. var topPars []interface{}
  245. var industrialManagementIds []int
  246. for _, v := range industrialList {
  247. industrialManagementIds = append(industrialManagementIds, v.IndustrialManagementId)
  248. }
  249. idsLen := len(industrialManagementIds)
  250. if idsLen > 0 {
  251. topCond = ` AND industrial_management_id IN (` + utils.GetOrmInReplace(idsLen) + `)`
  252. topPars = append(topPars, industrialManagementIds)
  253. } else {
  254. return
  255. }
  256. industrNamelist, e := models.GetTopOneMonthArtReadNumIndustryAll(topCond, topPars)
  257. if e != nil {
  258. err = errors.New("GetTopOneMonthArtReadNumIndustryAll, Err: " + e.Error())
  259. return
  260. }
  261. source = 1
  262. for _, v := range industrNamelist {
  263. label := v.IndustryName
  264. industrialManagementId := v.IndustrialManagementId
  265. var condition string
  266. var pars []interface{}
  267. condition += ` AND source_id=? AND source = ? AND user_id = ? `
  268. pars = append(pars, v.IndustrialManagementId, source, userId)
  269. total, e := models.GetCygxUserLabelCount(condition, pars)
  270. if e != nil {
  271. err = errors.New("GetCygxProductInteriorCount" + e.Error())
  272. return
  273. }
  274. if total == 0 {
  275. item := new(models.CygxUserLabel)
  276. item.UserId = user.UserId
  277. item.CompanyId = user.CompanyId
  278. item.RealName = user.RealName
  279. item.Mobile = user.Mobile
  280. item.Email = user.Email
  281. item.Label = label
  282. item.SourceId = industrialManagementId
  283. item.Source = source
  284. item.Weight = 1
  285. item.CreateTime = time.Now()
  286. item.ModifyTime = time.Now()
  287. _, e = models.AddCygxUserLabel(item)
  288. if e != nil {
  289. err = errors.New("AddCygxUserLabel" + e.Error())
  290. return
  291. }
  292. } else {
  293. //source 来源1:产业、2:系列
  294. e = models.UpdateCygxUserLabelWeight(industrialManagementId, source, userId, label)
  295. if e != nil {
  296. err = errors.New("UpdateCygxUserLabelWeight" + e.Error())
  297. return
  298. }
  299. }
  300. }
  301. }
  302. return
  303. }
  304. // 2:产业关注
  305. func IndustryFllowUserLabelLogReduce(log models.CygxUserLabelLogRedis) (err error) {
  306. defer func() {
  307. if err != nil {
  308. fmt.Println(err)
  309. go utils.SendAlarmMsg("用户关注产业更新相关标签,处理Redis队列消息失败:"+err.Error(), 2)
  310. }
  311. }()
  312. isFllow := log.IsFllow
  313. industrialManagementId := log.SourceId
  314. userId := log.UserId
  315. source := 1
  316. detailIndustrial, e := models.GetIndustrialManagementDetail(industrialManagementId)
  317. if e != nil {
  318. err = errors.New("GetIndustrialManagementDetail" + e.Error())
  319. return
  320. }
  321. label := detailIndustrial.IndustryName
  322. if isFllow == 0 {
  323. e = models.UpdateCygxUserLabelIsFollow(isFllow, industrialManagementId, source, userId, label)
  324. if e != nil {
  325. err = errors.New("UpdateCygxUserLabelIsFollow" + e.Error())
  326. return
  327. }
  328. } else {
  329. var condition string
  330. var pars []interface{}
  331. condition += ` AND source_id=? AND source = ? AND user_id = ?`
  332. pars = append(pars, industrialManagementId, source, userId)
  333. total, e := models.GetCygxUserLabelCount(condition, pars)
  334. if e != nil {
  335. err = errors.New("GetCygxProductInteriorCount" + e.Error())
  336. return
  337. }
  338. if total == 0 {
  339. user, e := models.GetWxUserItemByUserId(userId)
  340. if e != nil {
  341. err = errors.New("GetWxUserItemByUserId" + e.Error())
  342. return
  343. }
  344. item := new(models.CygxUserLabel)
  345. item.UserId = user.UserId
  346. item.CompanyId = user.CompanyId
  347. item.RealName = user.RealName
  348. item.Mobile = user.Mobile
  349. item.Email = user.Email
  350. item.Label = label
  351. item.SourceId = industrialManagementId
  352. item.Source = source
  353. item.IsFollow = isFllow
  354. item.CreateTime = time.Now()
  355. item.ModifyTime = time.Now()
  356. _, e = models.AddCygxUserLabel(item)
  357. if e != nil {
  358. err = errors.New("AddCygxUserLabel" + e.Error())
  359. return
  360. }
  361. } else {
  362. //source 来源1:产业、2:系列
  363. e = models.UpdateCygxUserLabelIsFollow(isFllow, industrialManagementId, source, userId, label)
  364. if e != nil {
  365. err = errors.New("UpdateCygxUserLabelIsFollow" + e.Error())
  366. return
  367. }
  368. }
  369. }
  370. return
  371. }
  372. // 3:活动到会
  373. func ActivityUserLabelLogReduce(log models.CygxUserLabelLogRedis) (err error) {
  374. defer func() {
  375. if err != nil {
  376. fmt.Println(err)
  377. go utils.SendAlarmMsg("用户活动到会更新相关标签,处理Redis队列消息失败:"+err.Error(), 2)
  378. }
  379. }()
  380. activityId := log.SourceId
  381. userId := log.UserId
  382. var source int
  383. user, e := models.GetWxUserItemByUserId(userId)
  384. if e != nil {
  385. err = errors.New("GetWxUserItemByUserId" + e.Error())
  386. return
  387. }
  388. var condition string
  389. var pars []interface{}
  390. condition = ` AND activity_id = ? `
  391. pars = append(pars, activityId)
  392. industrialList, e := models.GetCygxIndustrialActivityGroupManagementList(condition+" AND source = 1 ", pars)
  393. if e != nil {
  394. err = errors.New("GetCygxIndustrialActivityGroupManagementList, Err: " + e.Error())
  395. return
  396. }
  397. //如果有行产业归类就按照产业报告处理
  398. var topCond string
  399. var topPars []interface{}
  400. var industrialManagementIds []int
  401. for _, v := range industrialList {
  402. industrialManagementIds = append(industrialManagementIds, v.IndustrialManagementId)
  403. }
  404. idsLen := len(industrialManagementIds)
  405. if idsLen > 0 {
  406. topCond = ` AND industrial_management_id IN (` + utils.GetOrmInReplace(idsLen) + `)`
  407. topPars = append(topPars, industrialManagementIds)
  408. } else {
  409. return
  410. }
  411. industrNamelist, e := models.GetTopOneMonthArtReadNumIndustryAll(topCond, topPars)
  412. if e != nil {
  413. err = errors.New("GetTopOneMonthArtReadNumIndustryAll, Err: " + e.Error())
  414. return
  415. }
  416. source = 1
  417. for _, v := range industrNamelist {
  418. label := v.IndustryName
  419. industrialManagementId := v.IndustrialManagementId
  420. var condition string
  421. var pars []interface{}
  422. condition += ` AND source_id=? AND source = ? AND user_id = ? `
  423. pars = append(pars, v.IndustrialManagementId, source, userId)
  424. total, e := models.GetCygxUserLabelCount(condition, pars)
  425. if e != nil {
  426. err = errors.New("GetCygxProductInteriorCount" + e.Error())
  427. return
  428. }
  429. if total == 0 {
  430. item := new(models.CygxUserLabel)
  431. item.UserId = user.UserId
  432. item.CompanyId = user.CompanyId
  433. item.RealName = user.RealName
  434. item.Mobile = user.Mobile
  435. item.Email = user.Email
  436. item.Label = label
  437. item.SourceId = industrialManagementId
  438. item.Source = source
  439. item.Weight = 1
  440. item.CreateTime = time.Now()
  441. item.ModifyTime = time.Now()
  442. _, e = models.AddCygxUserLabel(item)
  443. if e != nil {
  444. err = errors.New("AddCygxUserLabel" + e.Error())
  445. return
  446. }
  447. } else {
  448. //source 来源1:产业、2:系列
  449. e = models.UpdateCygxUserLabelWeight(industrialManagementId, source, userId, label)
  450. if e != nil {
  451. err = errors.New("UpdateCygxUserLabelWeight" + e.Error())
  452. return
  453. }
  454. }
  455. }
  456. return
  457. }
  458. // 4:系列关注
  459. func CategoryFllowUserLabelLogReduce(log models.CygxUserLabelLogRedis) (err error) {
  460. defer func() {
  461. if err != nil {
  462. fmt.Println(err)
  463. go utils.SendAlarmMsg("用户关注系列更新相关标签,处理Redis队列消息失败:"+err.Error(), 2)
  464. }
  465. }()
  466. isFllow := log.IsFllow
  467. industrialManagementId := log.SourceId
  468. userId := log.UserId
  469. source := 2
  470. detailIndustrial, e := models.GetCygxReportMappingById(industrialManagementId)
  471. if e != nil {
  472. err = errors.New("GetCygxReportMappingById" + e.Error())
  473. return
  474. }
  475. label := detailIndustrial.MatchTypeName
  476. if isFllow == 0 {
  477. e = models.UpdateCygxUserLabelIsFollow(isFllow, industrialManagementId, source, userId, label)
  478. if e != nil {
  479. err = errors.New("UpdateCygxUserLabelIsFollow" + e.Error())
  480. return
  481. }
  482. } else {
  483. var condition string
  484. var pars []interface{}
  485. condition += ` AND source_id=? AND source = ? AND user_id = ?`
  486. pars = append(pars, industrialManagementId, source, userId)
  487. total, e := models.GetCygxUserLabelCount(condition, pars)
  488. if e != nil {
  489. err = errors.New("GetCygxProductInteriorCount" + e.Error())
  490. return
  491. }
  492. if total == 0 {
  493. user, e := models.GetWxUserItemByUserId(userId)
  494. if e != nil {
  495. err = errors.New("GetWxUserItemByUserId" + e.Error())
  496. return
  497. }
  498. item := new(models.CygxUserLabel)
  499. item.UserId = user.UserId
  500. item.CompanyId = user.CompanyId
  501. item.RealName = user.RealName
  502. item.Mobile = user.Mobile
  503. item.Email = user.Email
  504. item.Label = label
  505. item.SourceId = industrialManagementId
  506. item.Source = source
  507. item.IsFollow = isFllow
  508. item.CreateTime = time.Now()
  509. item.ModifyTime = time.Now()
  510. _, e = models.AddCygxUserLabel(item)
  511. if e != nil {
  512. err = errors.New("AddCygxUserLabel" + e.Error())
  513. return
  514. }
  515. } else {
  516. //source 来源1:产业、2:系列
  517. e = models.UpdateCygxUserLabelIsFollow(isFllow, industrialManagementId, source, userId, label)
  518. if e != nil {
  519. err = errors.New("UpdateCygxUserLabelIsFollow" + e.Error())
  520. return
  521. }
  522. }
  523. }
  524. return
  525. }
  526. // 5:专项调研活动到会
  527. func ActivitySpecialUserLabelLogReduce(log models.CygxUserLabelLogRedis) (err error) {
  528. defer func() {
  529. if err != nil {
  530. fmt.Println(err)
  531. go utils.SendAlarmMsg("用户活动到会更新相关标签,处理Redis队列消息失败:"+err.Error(), 2)
  532. }
  533. }()
  534. activityId := log.SourceId
  535. userId := log.UserId
  536. var source int
  537. user, e := models.GetWxUserItemByUserId(userId)
  538. if e != nil {
  539. err = errors.New("GetWxUserItemByUserId" + e.Error())
  540. return
  541. }
  542. var condition string
  543. var pars []interface{}
  544. condition = ` AND activity_id = ? `
  545. pars = append(pars, activityId)
  546. industrialList, e := models.GetCygxIndustrialActivityGroupManagementList(condition+" AND source = 2 ", pars)
  547. if e != nil {
  548. err = errors.New("GetCygxIndustrialActivityGroupManagementList, Err: " + e.Error())
  549. return
  550. }
  551. //如果有行产业归类就按照产业报告处理
  552. var topCond string
  553. var topPars []interface{}
  554. var industrialManagementIds []int
  555. for _, v := range industrialList {
  556. industrialManagementIds = append(industrialManagementIds, v.IndustrialManagementId)
  557. }
  558. idsLen := len(industrialManagementIds)
  559. if idsLen > 0 {
  560. topCond = ` AND industrial_management_id IN (` + utils.GetOrmInReplace(idsLen) + `)`
  561. topPars = append(topPars, industrialManagementIds)
  562. } else {
  563. return
  564. }
  565. industrNamelist, e := models.GetTopOneMonthArtReadNumIndustryAll(topCond, topPars)
  566. if e != nil {
  567. err = errors.New("GetTopOneMonthArtReadNumIndustryAll, Err: " + e.Error())
  568. return
  569. }
  570. source = 1
  571. for _, v := range industrNamelist {
  572. label := v.IndustryName
  573. industrialManagementId := v.IndustrialManagementId
  574. var condition string
  575. var pars []interface{}
  576. condition += ` AND source_id=? AND source = ? AND user_id = ? `
  577. pars = append(pars, v.IndustrialManagementId, source, userId)
  578. total, e := models.GetCygxUserLabelCount(condition, pars)
  579. if e != nil {
  580. err = errors.New("GetCygxProductInteriorCount" + e.Error())
  581. return
  582. }
  583. if total == 0 {
  584. item := new(models.CygxUserLabel)
  585. item.UserId = user.UserId
  586. item.CompanyId = user.CompanyId
  587. item.RealName = user.RealName
  588. item.Mobile = user.Mobile
  589. item.Email = user.Email
  590. item.Label = label
  591. item.SourceId = industrialManagementId
  592. item.Source = source
  593. item.Weight = 1
  594. item.CreateTime = time.Now()
  595. item.ModifyTime = time.Now()
  596. _, e = models.AddCygxUserLabel(item)
  597. if e != nil {
  598. err = errors.New("AddCygxUserLabel" + e.Error())
  599. return
  600. }
  601. } else {
  602. //source 来源1:产业、2:系列
  603. e = models.UpdateCygxUserLabelWeight(industrialManagementId, source, userId, label)
  604. if e != nil {
  605. err = errors.New("UpdateCygxUserLabelWeight" + e.Error())
  606. return
  607. }
  608. }
  609. }
  610. return
  611. }
  612. // func init() {
  613. // updateUserLabelByUserId(53095)
  614. // }
  615. // 更新用户标签
  616. func updateUserLabelByUserId(userId int) (err error) {
  617. defer func() {
  618. if err != nil {
  619. fmt.Println(err)
  620. go utils.SendAlarmMsg("更新用户标签失败:"+err.Error()+"userId:"+strconv.Itoa(userId), 2)
  621. }
  622. }()
  623. var condition string
  624. var pars []interface{}
  625. condition = ` AND is_follow=1 AND user_id = ? `
  626. pars = append(pars, userId)
  627. totalSource1, e := models.GetCygxUserLabelCount(condition+" AND source = 1 ", pars)
  628. if e != nil {
  629. err = errors.New("GetCygxProductInteriorCount" + e.Error())
  630. return
  631. }
  632. totalSource2, e := models.GetCygxUserLabelCount(condition+" AND source = 2 ", pars)
  633. if e != nil {
  634. err = errors.New("GetCygxProductInteriorCount" + e.Error())
  635. return
  636. }
  637. condition = ` AND user_id = ? `
  638. var list []*models.CygxUserLabel
  639. //当产业关注数量不超过10 ,系列关注数量不超过5
  640. if totalSource1 <= 10 && totalSource2 <= 5 {
  641. condition += ` AND ( is_follow > 0 OR weight > 0 ) ORDER BY is_follow DESC, weight DESC `
  642. list, e = models.GetCygxUserLabelList(condition, pars)
  643. if e != nil {
  644. err = errors.New("GetCygxUserLabelList" + e.Error())
  645. return
  646. }
  647. }
  648. //当产业关注数量超过10 ,系列关注数量超过5
  649. if totalSource1 > 10 && totalSource2 > 1 {
  650. condition += ` AND weight > 0 ORDER BY weight DESC `
  651. list, e = models.GetCygxUserLabelList(condition, pars)
  652. if e != nil {
  653. err = errors.New("GetCygxUserLabelList" + e.Error())
  654. return
  655. }
  656. }
  657. //当产业关注数量不超过10 ,系列关注数量超过5
  658. if totalSource1 <= 10 && totalSource2 > 5 {
  659. fmt.Println("当产业关注数量不超过10 ,系列关注数量超过5")
  660. condition += ` AND source = 1 AND is_follow = 1 ORDER BY weight DESC `
  661. listfollow, e := models.GetCygxUserLabelList(condition, pars)
  662. if e != nil {
  663. err = errors.New("GetCygxUserLabelList" + e.Error())
  664. return
  665. }
  666. idMap := make(map[int]bool)
  667. for _, v := range listfollow {
  668. idMap[v.Id] = true
  669. list = append(list, v)
  670. }
  671. pars = make([]interface{}, 0)
  672. condition = ` AND user_id = ? `
  673. pars = append(pars, userId)
  674. condition += ` AND weight > 0 ORDER BY weight DESC `
  675. listother, e := models.GetCygxUserLabelList(condition, pars)
  676. if e != nil {
  677. err = errors.New("GetCygxUserLabelList" + e.Error())
  678. return
  679. }
  680. for _, v := range listother {
  681. if idMap[v.Id] {
  682. continue
  683. }
  684. list = append(list, v)
  685. }
  686. }
  687. //当产业关注数量超过10 ,系列关注数量不超过5
  688. if totalSource1 > 10 && totalSource2 <= 5 {
  689. condition += ` AND source = 2 AND is_follow = 1 ORDER BY weight DESC `
  690. listfollow, e := models.GetCygxUserLabelList(condition, pars)
  691. if e != nil {
  692. err = errors.New("GetCygxUserLabelList" + e.Error())
  693. return
  694. }
  695. idMap := make(map[int]bool)
  696. for _, v := range listfollow {
  697. idMap[v.Id] = true
  698. list = append(list, v)
  699. }
  700. pars = make([]interface{}, 0)
  701. condition = ` AND user_id = ? `
  702. pars = append(pars, userId)
  703. condition += ` AND weight > 0 ORDER BY weight DESC `
  704. listother, e := models.GetCygxUserLabelList(condition, pars)
  705. if e != nil {
  706. err = errors.New("GetCygxUserLabelList" + e.Error())
  707. return
  708. }
  709. for _, v := range listother {
  710. if idMap[v.Id] {
  711. continue
  712. }
  713. list = append(list, v)
  714. }
  715. }
  716. var labelUser string
  717. for _, v := range list {
  718. labelUser += v.Label + ","
  719. }
  720. labelUser = strings.TrimRight(labelUser, ",")
  721. e = models.UpdateUserLabel(labelUser, userId)
  722. if e != nil {
  723. err = errors.New("UpdateUserLabel" + e.Error())
  724. return err
  725. }
  726. return
  727. }