report_view_log.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package report
  2. import (
  3. "fmt"
  4. "hongze/hongze_yb/global"
  5. "hongze/hongze_yb/models/tables/rddp/report"
  6. "hongze/hongze_yb/models/tables/rddp/report_view_log"
  7. "time"
  8. )
  9. // GetUnRead 获取未读数
  10. func GetUnRead(classifyIdSeconds []int, userId uint64) (unReadItem map[string]int, err error) {
  11. firstDay := "2022-01-01"
  12. newReports, err := report.GetReportsByClassifyIdSecondsAndDate(classifyIdSeconds, firstDay)
  13. if err != nil {
  14. return
  15. }
  16. var newIds []int
  17. reportMap := make(map[int]*report.UnReadItem)
  18. if len(newReports) > 0 {
  19. for _, v := range newReports {
  20. newIds = append(newIds, v.Id)
  21. temp := new(report.UnReadItem)
  22. temp.ReportID = v.Id
  23. temp.ClassifyNameFirst = v.ClassifyNameFirst
  24. reportMap[v.Id] = temp
  25. }
  26. //获取用户的浏览记录
  27. viewReportIds, tErr := report_view_log.GetByReportIdsAndUserIdAndTime(newIds, userId, firstDay)
  28. if tErr != nil {
  29. err = tErr
  30. return
  31. }
  32. //过滤已经浏览过的报告ID
  33. if len(viewReportIds) > 0 {
  34. for _, item := range viewReportIds {
  35. if _, ok := reportMap[item]; ok {
  36. delete(reportMap, item)
  37. }
  38. }
  39. }
  40. unReadItem = make(map[string]int)
  41. for _, v := range reportMap {
  42. unReadItem[v.ClassifyNameFirst] += 1
  43. }
  44. }
  45. return
  46. }
  47. // BatchInsertReportView 批量新增报告已读记录
  48. func BatchInsertReportView(userId uint64, weekReportIds []int, classifyNameFirst string, classifyIdSeconds []int) {
  49. var err error
  50. defer func() {
  51. if err != nil {
  52. global.LOG.Critical(fmt.Sprintf("Task BatchInsertReportView: userId=%d, err:%s, recover:%s", userId, err.Error(), recover()))
  53. }
  54. }()
  55. firstDay := "2022-01-01"
  56. var newReports []*report.Report
  57. pars := make([]interface{}, 0)
  58. var condition string
  59. // 特殊处理三种分类
  60. if classifyNameFirst == "大事点评" || classifyNameFirst == "会议纪要" || classifyNameFirst == "年报合集" {
  61. condition = "state IN (2, 6) and publish_time > ? "
  62. pars = append(pars, firstDay)
  63. } else {
  64. condition = "state IN (2, 6) and classify_name_first = ? and publish_time > ? "
  65. pars = append(pars, classifyNameFirst, firstDay)
  66. }
  67. if classifyNameFirst == "晨报" {
  68. } else if classifyNameFirst == "周报" {
  69. condition += " and id in (?)"
  70. pars = append(pars, weekReportIds)
  71. } else {
  72. condition += " and classify_id_second in (?)"
  73. pars = append(pars, classifyIdSeconds)
  74. }
  75. newReports, err = report.GetReportListByCondition(condition, pars)
  76. if err != nil {
  77. return
  78. }
  79. var newIds []int
  80. reportMap := make(map[int]int)
  81. if len(newReports) > 0 {
  82. for _, v := range newReports {
  83. newIds = append(newIds, v.Id)
  84. reportMap[v.Id] = v.Id
  85. }
  86. //获取用户的浏览记录
  87. viewReportIds, tErr := report_view_log.GetByReportIdsAndUserIdAndTime(newIds, userId, firstDay)
  88. if tErr != nil {
  89. err = tErr
  90. return
  91. }
  92. //过滤已经浏览过的报告ID
  93. if len(viewReportIds) > 0 {
  94. for _, item := range viewReportIds {
  95. if _, ok := reportMap[item]; ok {
  96. delete(reportMap, item)
  97. }
  98. }
  99. }
  100. var reportLogs []*report_view_log.ReportViewLog
  101. for _, v := range reportMap {
  102. temp := new(report_view_log.ReportViewLog)
  103. temp.ReportId = v
  104. temp.UserId = userId
  105. temp.CreateTime = time.Now()
  106. reportLogs = append(reportLogs, temp)
  107. }
  108. tErr = report_view_log.CreateInBatches(reportLogs)
  109. if tErr != nil {
  110. err = tErr
  111. return
  112. }
  113. //num := len(reportLogs)
  114. }
  115. return
  116. }