query.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package report_chapter
  2. import (
  3. "fmt"
  4. "hongze/hongze_yb/global"
  5. "hongze/hongze_yb/utils"
  6. )
  7. func GetLatestChaptersByTypeIdsAndClass(typeIds []int, classifyNameFirst string) (list []*ReportChapter, err error) {
  8. sql := `SELECT
  9. DISTINCT report_id
  10. FROM
  11. report_chapter
  12. WHERE
  13. type_id IN (?)
  14. AND publish_state = 2
  15. AND is_edit = 1
  16. AND report_type = ?
  17. ORDER BY
  18. publish_time desc, report_chapter_id desc limit 30
  19. `
  20. err = global.MYSQL["rddp"].Model(ReportChapter{}).Raw(sql, typeIds, classifyNameFirst).Scan(&list).Error
  21. return
  22. }
  23. // GetListByReportId 根据报告ID获取章节列表
  24. func GetListByReportId(reportId int, classifyNameFirst string) (list []*ReportChapter, err error) {
  25. var where string
  26. if classifyNameFirst == "周报"{
  27. where = "report_id = ? AND is_edit = 1 AND publish_state = 2"
  28. }else{
  29. where = "report_id = ? AND publish_state = 2"
  30. }
  31. err = global.MYSQL["rddp"].Model(ReportChapter{}).
  32. Select("report_chapter_id, report_id, type_id, type_name, abstract, title, author, publish_time, trend").
  33. Where(where, reportId).
  34. Order("sort asc, report_chapter_id asc").
  35. Scan(&list).Error
  36. return
  37. }
  38. // GetListByReportIdTypeIds 根据报告ID、章节类型ID获取章节列表
  39. func GetListByReportIdTypeIds(reportId int, typeIds []int, classifyNameFirst string) (list []*ReportChapter, err error) {
  40. var where string
  41. if classifyNameFirst == "周报"{
  42. where = "report_id = ? AND type_id in (?) AND is_edit = 1 AND publish_state = 2 "
  43. }else{
  44. where = "report_id = ? AND type_id in (?) AND publish_state = 2 "
  45. }
  46. err = global.MYSQL["rddp"].Model(ReportChapter{}).
  47. Select("report_chapter_id, report_id, type_id, type_name, abstract, title, author, publish_time").
  48. Where(where, reportId, typeIds).
  49. Order("sort asc, report_chapter_id asc").
  50. Scan(&list).Error
  51. return
  52. }
  53. // GetContentById 根据ID获取章节详情
  54. func GetContentById(id int, typeIds []int) (info *ReportChapter, err error) {
  55. err = global.MYSQL["rddp"].Model(ReportChapter{}).Select("report_chapter_id, report_id, is_edit, classify_name_first, classify_id_first, content, trend, type_id, type_name, abstract, title, abstract, author, stage, publish_time, content, content_sub, video_url, video_name, video_play_seconds, video_size").
  56. Where("report_chapter_id = ? and type_id in (?) AND publish_state = 2 ", id, typeIds).
  57. First(&info).Error
  58. if err == utils.ErrNoRow {
  59. err = nil
  60. }
  61. return
  62. }
  63. // GetReportIdsByTypeIdsAndClass 根据章节ID获取报告ID
  64. func GetReportIdsByTypeIdsAndClass(typeIds []int, classifyNameFirst string) (reportIds []int, err error) {
  65. var list []*ReportChapter
  66. err = global.MYSQL["rddp"].Model(ReportChapter{}).Select("DISTINCT report_id").Where("type_id in (?) and publish_state = 2 AND is_edit = 1 AND classify_name_first= ? ", typeIds, classifyNameFirst).Scan(&list).Error
  67. if err != nil {
  68. return
  69. }
  70. for _, v := range list {
  71. reportIds = append(reportIds, v.ReportId)
  72. }
  73. return
  74. }
  75. // GetTypeIdById 根据ID获取章节类型
  76. func GetTypeIdById(id int) (info *ReportChapter, err error) {
  77. err = global.MYSQL["rddp"].Model(ReportChapter{}).Select("report_chapter_id, type_id").
  78. Where("report_chapter_id = ? AND publish_state = 2 ", id).
  79. First(&info).Error
  80. if err == utils.ErrNoRow {
  81. err = nil
  82. }
  83. return
  84. }
  85. // GetByTypeIdsAndReportIds 根据章节ID和ReportIds查询报告
  86. func GetByTypeIdsAndReportIds(typeIds []int, reportIds []int, classifyNameFirst string) (list []*ReportChapter, err error) {
  87. var where string
  88. if classifyNameFirst == "周报"{
  89. where = "report_id in (?) AND type_id in (?) AND is_edit = 1 AND publish_state = 2 "
  90. }else{
  91. where = "report_id in (?) AND type_id in (?) AND publish_state = 2 "
  92. }
  93. err = global.MYSQL["rddp"].Model(ReportChapter{}).
  94. Select("report_id, type_id, report_chapter_id, classify_name_first, classify_id_first, video_url, video_name, video_play_seconds, video_size, sort").
  95. Where(where, reportIds, typeIds).
  96. Order("sort asc, report_chapter_id asc").
  97. Scan(&list).Error
  98. if err != nil {
  99. return
  100. }
  101. for _, v := range list {
  102. reportIds = append(reportIds, v.ReportId)
  103. }
  104. return
  105. }
  106. func GetWeekRecommendList(reportId int, firstName string) (items []*ReportChapter, err error) {
  107. sql := `SELECT * FROM (SELECT
  108. a.report_id,
  109. a.report_chapter_id,
  110. a.classify_name_first,
  111. a.title,
  112. a.stage,
  113. a.publish_time
  114. FROM
  115. report_chapter AS a,
  116. report AS b
  117. WHERE
  118. a.publish_state = 2
  119. AND a.report_id <> %v
  120. AND a.classify_name_first = "%v"
  121. AND a.report_id=b.id
  122. AND b.state = 2
  123. ORDER BY
  124. publish_time DESC) t
  125. GROUP BY report_id
  126. ORDER BY publish_time desc LIMIT 3 `
  127. sql = fmt.Sprintf(sql, reportId, firstName)
  128. err = global.MYSQL["rddp"].Raw(sql).Scan(&items).Error
  129. return
  130. }
  131. func GetLatestChapterByClassifyName(firstName string) (items *ReportChapter, err error) {
  132. sql := `SELECT
  133. report_id,
  134. classify_name_first,
  135. classify_id_first,
  136. stage
  137. FROM
  138. report_chapter
  139. WHERE
  140. publish_state = 2
  141. AND classify_name_first = "%v"
  142. ORDER BY
  143. publish_time DESC `
  144. sql = fmt.Sprintf(sql, firstName)
  145. err = global.MYSQL["rddp"].Raw(sql).First(&items).Error
  146. return
  147. }