query.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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,video_url,video_name,video_play_seconds,video_size").
  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, author, stage, publish_time, content, content_sub, video_url, video_name, video_play_seconds, video_size, report_type, video_size,report_layout").
  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, report_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. // GetChapterByReportIdTypeId 根据报告ID和章节类型ID查找章节
  107. func GetChapterByReportIdTypeId(reportId, typeId int) (chapter *ReportChapter, err error) {
  108. err = global.MYSQL["rddp"].Model(ReportChapter{}).
  109. Select("report_id, type_id, report_chapter_id").
  110. Where("report_id = ? and type_id = ?", reportId, typeId).
  111. First(&chapter).Error
  112. if err == utils.ErrNoRow {
  113. err = nil
  114. }
  115. return
  116. }
  117. func GetWeekRecommendList(reportId int, firstName string) (items []*ReportChapter, err error) {
  118. sql := `SELECT * FROM (SELECT
  119. a.report_id,
  120. a.report_chapter_id,
  121. a.classify_name_first,
  122. a.title,
  123. a.stage,
  124. a.publish_time
  125. FROM
  126. report_chapter AS a,
  127. report AS b
  128. WHERE
  129. a.publish_state = 2
  130. AND a.report_id <> %v
  131. AND a.classify_name_first = "%v"
  132. AND a.report_id=b.id
  133. AND b.state IN (2, 6)
  134. ORDER BY
  135. publish_time DESC) t
  136. GROUP BY report_id
  137. ORDER BY publish_time desc LIMIT 3 `
  138. sql = fmt.Sprintf(sql, reportId, firstName)
  139. err = global.MYSQL["rddp"].Raw(sql).Scan(&items).Error
  140. return
  141. }
  142. func GetLatestChapterByClassifyName(firstName string) (items *ReportChapter, err error) {
  143. sql := `SELECT
  144. report_id,
  145. classify_name_first,
  146. classify_id_first,
  147. stage
  148. FROM
  149. report_chapter
  150. WHERE
  151. publish_state = 2
  152. AND classify_name_first = "%v"
  153. ORDER BY
  154. publish_time DESC `
  155. sql = fmt.Sprintf(sql, firstName)
  156. err = global.MYSQL["rddp"].Raw(sql).First(&items).Error
  157. return
  158. }
  159. // GetItemById 主键获取章节
  160. func GetItemById(chapterId int) (item *ReportChapter, err error) {
  161. err = global.MYSQL["rddp"].Model(ReportChapter{}).
  162. Where("report_chapter_id = ? AND publish_state = 2", chapterId).
  163. First(&item).Error
  164. return
  165. }
  166. // GetListByChapterIds 根据章节IDs获取列表
  167. func GetListByChapterIds(chapterIds []int) (list []*ReportChapter, err error) {
  168. var where string
  169. where = `publish_state = 2`
  170. if len(chapterIds) > 0 {
  171. where += ` AND report_chapter_id IN (?)`
  172. }
  173. err = global.MYSQL["rddp"].Model(ReportChapter{}).
  174. //Select("report_id, type_id, report_chapter_id, classify_name_first, classify_id_first, video_url, video_name, video_play_seconds, video_size, sort").
  175. Where(where, chapterIds).
  176. Order("sort asc, report_chapter_id asc").
  177. Scan(&list).Error
  178. return
  179. }
  180. // GetLatestChapterByMinClassifyId
  181. // @Description: 根据分类id获取最新的章节信息
  182. // @author: Roc
  183. // @datetime 2024-06-24 14:00:46
  184. // @param minClassifyId int
  185. // @return items *ReportChapter
  186. // @return err error
  187. func GetLatestChapterByMinClassifyId(minClassifyId int) (items *ReportChapter, err error) {
  188. sql := `SELECT
  189. report_id,
  190. classify_name_first,
  191. classify_id_first,
  192. stage
  193. FROM
  194. report_chapter
  195. WHERE
  196. publish_state = 2
  197. AND classify_id_first = ?
  198. ORDER BY
  199. publish_time DESC `
  200. err = global.MYSQL["rddp"].Raw(sql, minClassifyId).First(&items).Error
  201. return
  202. }
  203. // GetWeekRecommendListV2
  204. // @Description: 获取推荐列表
  205. // @author: Roc
  206. // @datetime 2024-06-24 14:20:09
  207. // @param reportId int
  208. // @param firstId int
  209. // @param secondId int
  210. // @param thirdId int
  211. // @return items []*ReportChapter
  212. // @return err error
  213. func GetWeekRecommendListV2(reportId, minClassifyId int) (items []*ReportChapter, err error) {
  214. sql := `SELECT * FROM (SELECT
  215. a.report_id,
  216. a.report_chapter_id,
  217. a.classify_name_first,
  218. a.title,
  219. a.stage,
  220. a.publish_time
  221. FROM
  222. report_chapter AS a,
  223. report AS b
  224. WHERE
  225. a.publish_state = 2
  226. AND a.report_id <> ?
  227. AND a.classify_id_first = ?
  228. AND a.report_id=b.id
  229. AND b.state IN (2, 6)
  230. ORDER BY
  231. publish_time DESC) t
  232. GROUP BY report_id
  233. ORDER BY publish_time desc LIMIT 3 `
  234. err = global.MYSQL["rddp"].Raw(sql, reportId, minClassifyId).Scan(&items).Error
  235. return
  236. }
  237. // GetByPermissionIdListAndReportList
  238. // @Description: 根据报告id和品种权限获取所有的章节
  239. // @param permissionIdList
  240. // @param reportIds
  241. // @return list
  242. // @return err
  243. func GetByPermissionIdListAndReportList(permissionIdList, reportIds []int) (list []*ReportChapter, err error) {
  244. sql := ` SELECT a.report_id, a.type_id, a.report_chapter_id, a.classify_name_first, a.classify_id_first, a.video_url, a.video_name, a.video_play_seconds, a.video_size, a.sort FROM report_chapter a
  245. INNER JOIN report_chapter_permission_mapping b ON a.report_chapter_id = b.report_chapter_id
  246. WHERE a.report_id in (?) AND b.chart_permission_id IN (?) AND a.is_edit = 1 AND a.publish_state = 2 `
  247. sql += ` order by a.sort asc,a.report_chapter_id asc `
  248. err = global.MYSQL["rddp"].Raw(sql, reportIds, permissionIdList).Scan(&list).Error
  249. for _, v := range list {
  250. reportIds = append(reportIds, v.ReportId)
  251. }
  252. return
  253. }