recommend.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package service
  2. import (
  3. "context"
  4. "time"
  5. )
  6. func (s *Service) recommendAuthorproc() {
  7. for {
  8. s.calRecommendAuthor()
  9. time.Sleep(time.Hour)
  10. }
  11. }
  12. func (s *Service) calRecommendAuthor() {
  13. var (
  14. mids []int64
  15. err error
  16. m = make(map[int64][]int64)
  17. )
  18. for {
  19. if mids, err = s.dao.MidsByPublishTime(context.TODO(), time.Now().Unix()-86400*s.c.Job.StatDays); err != nil {
  20. time.Sleep(time.Minute)
  21. continue
  22. }
  23. for _, mid := range mids {
  24. if categoris, ok := s.statAuthorInfo(mid); ok {
  25. for _, category := range categoris {
  26. m[category] = append(m[category], mid)
  27. }
  28. s.dao.AddAuthorMostCategories(context.TODO(), mid, categoris)
  29. }
  30. }
  31. s.dao.AddcategoriesAuthors(context.TODO(), m)
  32. return
  33. }
  34. }
  35. func (s *Service) statAuthorInfo(mid int64) (categories []int64, res bool) {
  36. var (
  37. m map[int64][2]int64
  38. err error
  39. words int64
  40. cm = make(map[int64]int64)
  41. max int64
  42. )
  43. if m, err = s.dao.StatByMid(context.TODO(), mid); err != nil {
  44. return
  45. }
  46. for cid, data := range m {
  47. if cate, ok := s.categoriesMap[cid]; ok {
  48. if cate.ParentID != 0 {
  49. cid = cate.ParentID
  50. }
  51. words += data[1]
  52. if _, ok := cm[cid]; ok {
  53. cm[cid] += data[0]
  54. } else {
  55. cm[cid] = data[0]
  56. }
  57. }
  58. }
  59. if words < s.c.Job.Words {
  60. return
  61. }
  62. res = true
  63. for k, v := range cm {
  64. if v > max {
  65. max = v
  66. categories = []int64{k}
  67. } else if v == max {
  68. categories = append(categories, k)
  69. }
  70. }
  71. return
  72. }