query.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. package report
  2. import (
  3. "hongze/hongze_yb/global"
  4. "hongze/hongze_yb/models/response"
  5. "hongze/hongze_yb/models/response/pc"
  6. "hongze/hongze_yb/utils"
  7. )
  8. // GetLatestClassReportsByClassifyIdSeconds 根据用户已购买的分类权限查询个分类最新的报告
  9. func GetLatestClassReportsByClassifyIdSeconds(classifyIdSeconds []int) (reportList []*Report, err error) {
  10. sql := `SELECT t1.id,
  11. t1.classify_id_first,
  12. t1.classify_id_second,
  13. t1.stage,
  14. t1.title,
  15. t1.classify_name_first,
  16. t1.classify_name_second,
  17. t1.publish_time
  18. FROM report t1
  19. INNER JOIN
  20. (
  21. SELECT classify_id_first, max( publish_time ) AS max_publish_time
  22. FROM report
  23. WHERE
  24. state = 2
  25. AND classify_name_first !="权益研报"
  26. AND classify_id_second IN ?
  27. GROUP BY
  28. classify_id_first
  29. ) t2
  30. ON t1.classify_id_first = t2.classify_id_first AND
  31. t1.publish_time = t2.max_publish_time
  32. WHERE t1.state = 2
  33. AND t1.classify_name_first !="权益研报"
  34. AND t1.classify_id_second IN ?`
  35. err = global.MYSQL["rddp"].Raw(sql, classifyIdSeconds, classifyIdSeconds).Scan(&reportList).Error
  36. return
  37. }
  38. // GetReportsByClassifyIdSecondsAndDate 根据时间和报告分类筛选出合适的记录
  39. func GetReportsByClassifyIdSecondsAndDate( classifyIdSeconds []int, publishTime string) (reportList []*Report, err error) {
  40. err = global.MYSQL["rddp"].Model(Report{}).
  41. Select("id, classify_name_first").
  42. Where("classify_id_second in (?) and state = 2 and publish_time > ? ", classifyIdSeconds, publishTime).Scan(&reportList).Error
  43. return
  44. }
  45. // GetListByIDsAndClassifyIdFirst 分页查询
  46. func GetListByIDsAndClassifyIdFirst( ids []int, classifyIdFirst int, offset , limit int) (reportList []*Report, err error) {
  47. err = global.MYSQL["rddp"].Model(Report{}).
  48. Select("id, classify_id_first, classify_name_first, classify_id_second, classify_name_second, title, stage, publish_time").
  49. Where("id in (?) and classify_id_first=? and state = 2 ", ids, classifyIdFirst).
  50. Order("publish_time desc, id desc").
  51. Offset(offset).
  52. Limit(limit).
  53. Scan(&reportList).Error
  54. return
  55. }
  56. // GetListCountByIDsAndClassifyIdFirst 分页查询
  57. func GetListCountByIDsAndClassifyIdFirst( ids []int, classifyIdFirst int) (total int64, err error) {
  58. err = global.MYSQL["rddp"].Model(Report{}).
  59. Select("id, classify_id_first, classify_name_first, classify_id_second, classify_name_second, title, stage, publish_time").
  60. Where("id in (?) and classify_id_first=? and state = 2 ", ids, classifyIdFirst).
  61. Count(&total).Error
  62. return
  63. }
  64. // GetListByClassifyIdSeconds 分页查询
  65. func GetListByClassifyIdSeconds( classifyIdSeconds []int, offset , limit int) (reportList []*Report, err error) {
  66. err = global.MYSQL["rddp"].Model(Report{}).
  67. Select("id, classify_id_first, classify_name_first, classify_id_second, classify_name_second, title, stage, publish_time").
  68. Where("classify_id_second in (?) and state = 2 ", classifyIdSeconds).
  69. Order("publish_time desc, id desc").
  70. Offset(offset).
  71. Limit(limit).
  72. Scan(&reportList).Error
  73. return
  74. }
  75. // GetListCountByClassifyIdSeconds
  76. func GetListCountByClassifyIdSeconds( classifyIdSeconds []int) (total int64, err error) {
  77. err = global.MYSQL["rddp"].Model(Report{}).
  78. Select("id, classify_id_first, classify_name_first, classify_id_second, classify_name_second, title, stage, publish_time").
  79. Where("classify_id_second in (?) and state = 2 ", classifyIdSeconds).
  80. Count(&total).Error
  81. return
  82. }
  83. // GetListByClassifyIdFirst 按照类型分页查询
  84. func GetListByClassifyIdFirst(classifyIdFirst int, offset , limit int) (reportList []*Report, err error) {
  85. err = global.MYSQL["rddp"].Model(Report{}).
  86. Select("id, classify_id_first, classify_name_first, classify_id_second, classify_name_second, title, stage, publish_time").
  87. Where("classify_id_first=? and state = 2 ", classifyIdFirst).
  88. Order("publish_time desc, id desc").
  89. Offset(offset).
  90. Limit(limit).
  91. Scan(&reportList).Error
  92. return
  93. }
  94. // GetListCountByClassifyIdFirst 按照类型查询报告总数
  95. func GetListCountByClassifyIdFirst(classifyIdFirst int) (total int64, err error) {
  96. err = global.MYSQL["rddp"].Model(Report{}).
  97. Select("id, classify_id_first, classify_name_first, classify_id_second, classify_name_second, title, stage, publish_time").
  98. Where("classify_id_first=? and state = 2 ", classifyIdFirst).
  99. Count(&total).Error
  100. return
  101. }
  102. // GetLatestByReportIds 根据ids获取报告
  103. func GetLatestByReportIds(ids []int) (item *Report, err error) {
  104. err = global.MYSQL["rddp"].Where("id in ? and state = 2", ids).Order("publish_time desc, id desc").First(&item).Error
  105. if err == utils.ErrNoRow {
  106. err = nil
  107. }
  108. return
  109. }
  110. // GetPublishByReportId 根据id获取报告
  111. func GetPublishByReportId(id int) (item *Report, err error) {
  112. err = global.MYSQL["rddp"].Where("id = ? and state = 2", id).First(&item).Error
  113. if err == utils.ErrNoRow {
  114. err = nil
  115. }
  116. return
  117. }
  118. // GetByReportId 根据id获取报告
  119. func GetByReportId(id int) (item *Report, err error) {
  120. err = global.MYSQL["rddp"].Where("id = ? ", id).First(&item).Error
  121. if err == utils.ErrNoRow {
  122. err = nil
  123. }
  124. return
  125. }
  126. // GetByReportIds 根据id获取报告
  127. func GetByReportIds(ids []int) (list []*Report, err error) {
  128. err = global.MYSQL["rddp"].Model(Report{}).Where("id in (?) and state = 2", ids).Select("id, create_time").Scan(&list).Error
  129. if err == utils.ErrNoRow {
  130. err = nil
  131. }
  132. return
  133. }
  134. // GetLatestDay 获取最新的晨报
  135. func GetLatestDay() (item *Report, err error) {
  136. err = global.MYSQL["rddp"].Where("state = 2 and classify_name_first= '晨报'").Order("publish_time desc, id desc").First(&item).Error
  137. if err == utils.ErrNoRow {
  138. err = nil
  139. }
  140. return
  141. }
  142. // GetLatestReportsByClassifyIdFirst 查询当前一级分类下,二级分类中,最新的报告
  143. func GetLatestReportsByClassifyIdFirst(classifyIdFirst int, classifyIdSeconds []int) (reportList []*Report, err error) {
  144. sql := `
  145. SELECT t1.id,
  146. t1.classify_id_first,
  147. t1.classify_id_second,
  148. t1.stage,
  149. t1.classify_name_first,
  150. t1.classify_name_second,
  151. t1.publish_time
  152. FROM report t1
  153. INNER JOIN
  154. (
  155. SELECT classify_id_first, classify_id_second, max( publish_time ) AS max_publish_time
  156. FROM report
  157. WHERE
  158. state = 2
  159. AND classify_id_first = ?
  160. AND classify_id_second IN ?
  161. GROUP BY
  162. classify_id_second
  163. ) t2
  164. ON t1.classify_id_second = t2.classify_id_second AND
  165. t1.classify_id_first = t2.classify_id_first AND
  166. t1.publish_time = t2.max_publish_time
  167. WHERE t1.state = 2
  168. AND t1.classify_id_first = ?
  169. AND t1.classify_id_second IN ?`
  170. err = global.MYSQL["rddp"].Raw(sql, classifyIdFirst, classifyIdSeconds, classifyIdFirst, classifyIdSeconds).Scan(&reportList).Error
  171. return
  172. }
  173. // GetReportListByCondition 获取报告列表
  174. func GetReportListByCondition(condition string, pars []interface{}) (list []*Report, err error) {
  175. err = global.MYSQL["rddp"].Select("id").Model(Report{}).Where(condition, pars...).
  176. Scan(&list).Error
  177. return
  178. }
  179. // GetListByClassifyIdSecond 按照二级类型分页查询
  180. func GetListByClassifyIdSecond(classifyIdSecond int, offset , limit int) (reportList []*Report, err error) {
  181. err = global.MYSQL["rddp"].Model(Report{}).
  182. Select("id, classify_id_first, classify_name_first, classify_id_second, classify_name_second, title, stage, publish_time, author, video_name, video_url, video_play_seconds, abstract").
  183. Where("classify_id_second = ? and state = 2 ", classifyIdSecond).
  184. Order("publish_time desc, id desc").
  185. Offset(offset).
  186. Limit(limit).
  187. Scan(&reportList).Error
  188. if err == utils.ErrNoRow {
  189. err = nil
  190. }
  191. return
  192. }
  193. // GetListCountByClassifyIdSecond 按照二级分类总条数
  194. func GetListCountByClassifyIdSecond(classifyIdSecond int) (total int64, err error) {
  195. err = global.MYSQL["rddp"].Model(Report{}).
  196. Where("classify_id_second=? and state = 2 ", classifyIdSecond).Count(&total).Error
  197. if err == utils.ErrNoRow {
  198. err = nil
  199. }
  200. return
  201. }
  202. // GetReportList 获取报告列表
  203. func GetReportList(condition string, pars []interface{}, offset , limit int) (list []*Report, err error) {
  204. err = global.MYSQL["rddp"].Model(Report{}).
  205. Select("id, classify_id_first, classify_name_first, classify_id_second, classify_name_second, title, stage, publish_time, author, create_time, video_url, video_name, video_play_seconds, abstract").
  206. Where(condition, pars...).
  207. Order("publish_time desc, id desc").
  208. Offset(offset).
  209. Limit(limit).
  210. Scan(&list).Error
  211. return
  212. }
  213. // GetReportListCount 获取报告总数
  214. func GetReportListCount(condition string, pars []interface{}) (total int64, err error) {
  215. err = global.MYSQL["rddp"].Model(Report{}).Where(condition, pars...).
  216. Count(&total).Error
  217. return
  218. }
  219. // GetReportCollectListByPermission 根据权限相关的分类查询报告和章节
  220. func GetReportCollectListByPermission(classifyIdSeconds []int, typeIds []int, offset , limit int) (list []*response.ReportCollectListItem, err error) {
  221. sql := `( SELECT
  222. id AS report_id,
  223. 0 AS report_chapter_id,
  224. classify_id_first,
  225. classify_id_second,
  226. classify_name_first,
  227. classify_name_second,
  228. 0 as report_chapter_type_id,
  229. title,
  230. content_sub,
  231. publish_time
  232. FROM
  233. report
  234. WHERE
  235. classify_name_first != "晨报"
  236. AND classify_name_first != "周报"
  237. AND classify_id_second in ?
  238. AND state = 2
  239. )
  240. UNION
  241. ( SELECT
  242. report_id,
  243. report_chapter_id,
  244. classify_id_first,
  245. 0 as classify_id_second,
  246. classify_name_first,
  247. null as classify_name_second,
  248. type_id as report_chapter_type_id,
  249. title,
  250. content_sub,
  251. publish_time
  252. FROM
  253. report_chapter
  254. WHERE
  255. publish_state = 2
  256. AND type_id in ?
  257. )
  258. ORDER BY publish_time DESC, report_id desc LIMIT ? OFFSET ?
  259. `
  260. err = global.MYSQL["rddp"].Raw(sql, classifyIdSeconds, typeIds, limit, offset).Scan(&list).Error
  261. return
  262. }
  263. // GetReportCollectCountByPermission 查询汇总报告总页数
  264. func GetReportCollectCountByPermission(classifyIdSeconds []int, typeIds []int) (total int64, err error) {
  265. sql := `select count(*) from ( ( SELECT
  266. id AS report_id,
  267. 0 AS report_chapter_id
  268. FROM
  269. report
  270. WHERE
  271. classify_name_first != "晨报"
  272. AND classify_name_first != "周报"
  273. AND classify_id_second in ?
  274. AND state = 2
  275. )
  276. UNION
  277. ( SELECT
  278. report_id,
  279. report_chapter_id
  280. FROM
  281. report_chapter
  282. WHERE
  283. publish_state = 2
  284. AND type_id in ?
  285. )
  286. ) as ru
  287. `
  288. err = global.MYSQL["rddp"].Raw(sql, classifyIdSeconds, typeIds).Count(&total).Error
  289. return
  290. }
  291. // GetLatestReportByPermission 根据权限相关的分类查询最新的三篇专栏报告
  292. func GetLatestReportByPermission(classifyIdSeconds []int) (list []*pc.LatestReport, err error) {
  293. sql := `SELECT
  294. id AS report_id,
  295. 0 AS report_chapter_id,
  296. classify_id_first,
  297. classify_id_second,
  298. classify_name_first,
  299. classify_name_second,
  300. 0 as report_chapter_type_id,
  301. title,
  302. content_sub,
  303. stage,
  304. publish_time
  305. FROM
  306. report
  307. WHERE
  308. classify_name_first != "晨报"
  309. AND classify_name_first != "周报"
  310. AND classify_id_second in ?
  311. AND state = 2
  312. ORDER BY publish_time DESC LIMIT 3
  313. `
  314. err = global.MYSQL["rddp"].Raw(sql, classifyIdSeconds).Scan(&list).Error
  315. return
  316. }
  317. // GetLatestReport 获取最新专栏信息
  318. func GetLatestReport() (list *pc.LatestReport, err error) {
  319. sql := `SELECT
  320. id AS report_id,
  321. 0 AS report_chapter_id,
  322. classify_id_first,
  323. classify_id_second,
  324. classify_name_first,
  325. classify_name_second,
  326. 0 as report_chapter_type_id,
  327. title,
  328. content_sub,
  329. stage,
  330. publish_time
  331. FROM
  332. report
  333. WHERE
  334. classify_name_first != "晨报"
  335. AND classify_name_first != "周报"
  336. AND state = 2
  337. ORDER BY publish_time DESC LIMIT 1
  338. `
  339. err = global.MYSQL["rddp"].Raw(sql).First(&list).Error
  340. return
  341. }