article_top_history_record.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package models
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "time"
  5. )
  6. // ArticleTopHistoryRecord 报告阅读飙升榜
  7. type ArticleTopHistoryRecord struct {
  8. Id int `orm:"column(id);pk"`
  9. ArticleId int `description:"文章ID"`
  10. Title string `description:"文章标题"`
  11. PublishDate string `description:"发布日期"`
  12. ChartPermissionId int `description:"行业ID"`
  13. PermissionName string `description:"行业名称"`
  14. Pv int `description:"PV"`
  15. CreateTime time.Time `description:"创建时间"`
  16. }
  17. func (item *ArticleTopHistoryRecord) TableName() string {
  18. return "cygx_article_top_history_record"
  19. }
  20. // GetTopReadRecordArticleListFromSource 获取阅读量排行榜源报告
  21. func GetTopReadRecordArticleListFromSource(startTime, endTime time.Time) (list []*ArticleTopHistoryRecord, err error) {
  22. sql := `SELECT
  23. a.article_id,
  24. a.title,
  25. date_format(a.publish_date, '%Y-%m-%d') AS publish_date,
  26. m.chart_permission_id,
  27. m.chart_permission_name as permission_name,
  28. COUNT(1) AS pv
  29. FROM
  30. cygx_article_history_record_newpv AS l
  31. JOIN cygx_article AS a ON a.article_id = l.article_id
  32. JOIN cygx_report_mapping AS m ON m.category_id = a.category_id
  33. WHERE
  34. a.publish_status = 1 AND l.create_time BETWEEN ? AND ?
  35. GROUP BY
  36. l.article_id
  37. ORDER BY
  38. pv DESC,
  39. a.publish_date DESC`
  40. _, err = orm.NewOrm().Raw(sql, startTime, endTime).QueryRows(&list)
  41. return
  42. }
  43. // UpdateTopReadRecordArticleList 更新榜单
  44. func UpdateTopReadRecordArticleList(items []*ArticleTopHistoryRecord) (err error) {
  45. o := orm.NewOrm()
  46. tx, err := o.Begin()
  47. if err != nil {
  48. return
  49. }
  50. defer func() {
  51. if err != nil {
  52. _ = tx.Rollback()
  53. } else {
  54. _ = tx.Commit()
  55. }
  56. }()
  57. // 重置榜单
  58. sql := `DELETE FROM cygx_article_top_history_record WHERE 1 = 1 `
  59. _, err = tx.Raw(sql).Exec()
  60. if err != nil {
  61. return
  62. }
  63. // 新增今日榜单
  64. _, err = tx.InsertMulti(len(items), items)
  65. if err != nil {
  66. return
  67. }
  68. return
  69. }
  70. // GetTopReadRecordArticleListByCondition 获取阅读量排行榜
  71. func GetTopReadRecordArticleListByCondition(limit int, condition string, pars []interface{}) (list []*ArticleTopHistoryRecord, err error) {
  72. sql := `SELECT * FROM cygx_article_top_history_record WHERE 1 = 1 `
  73. if condition != `` {
  74. sql += condition
  75. }
  76. sql += ` ORDER BY pv DESC, publish_date DESC LIMIT ?`
  77. _, err = orm.NewOrm().Raw(sql, pars, limit).QueryRows(&list)
  78. return
  79. }