query.go 9.2 KB

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