query.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. // GetByReportIds 根据ids获取报告
  102. func GetByReportIds(ids []int) (item *Report, err error) {
  103. err = global.MYSQL["rddp"].Where("id in ? and state = 2", ids).Order("publish_time desc, id desc").First(&item).Error
  104. if err == utils.ErrNoRow {
  105. err = nil
  106. }
  107. return
  108. }
  109. // GetByReportId 根据id获取报告
  110. func GetByReportId(id int) (item *Report, err error) {
  111. err = global.MYSQL["rddp"].Where("id = ? and state = 2", id).First(&item).Error
  112. if err == utils.ErrNoRow {
  113. err = nil
  114. }
  115. return
  116. }
  117. // GetLatestDay 获取最新的晨报
  118. func GetLatestDay() (item *Report, err error) {
  119. err = global.MYSQL["rddp"].Where("state = 2 and classify_name_first= '晨报'").Order("publish_time desc, id desc").First(&item).Error
  120. if err == utils.ErrNoRow {
  121. err = nil
  122. }
  123. return
  124. }
  125. // GetLatestReportsByClassifyIdFirst 查询当前一级分类下,二级分类中,最新的报告
  126. func GetLatestReportsByClassifyIdFirst(classifyIdFirst int, classifyIdSeconds []int) (reportList []*Report, err error) {
  127. sql := `
  128. SELECT t1.id,
  129. t1.classify_id_first,
  130. t1.classify_id_second,
  131. t1.stage,
  132. t1.classify_name_first,
  133. t1.classify_name_second,
  134. t1.publish_time
  135. FROM report t1
  136. INNER JOIN
  137. (
  138. SELECT classify_id_first, classify_id_second, max( publish_time ) AS max_publish_time
  139. FROM report
  140. WHERE
  141. state = 2
  142. AND classify_id_first = ?
  143. AND classify_id_second IN ?
  144. GROUP BY
  145. classify_id_second
  146. ) t2
  147. ON t1.classify_id_second = t2.classify_id_second AND
  148. t1.classify_id_first = t2.classify_id_first AND
  149. t1.publish_time = t2.max_publish_time
  150. WHERE t1.state = 2
  151. AND t1.classify_id_first = ?
  152. AND t1.classify_id_second IN ?`
  153. err = global.MYSQL["rddp"].Raw(sql, classifyIdFirst, classifyIdSeconds, classifyIdFirst, classifyIdSeconds).Scan(&reportList).Error
  154. return
  155. }
  156. // GetReportListByCondition 获取报告列表
  157. func GetReportListByCondition(condition string, pars []interface{}) (list []*Report, err error) {
  158. err = global.MYSQL["rddp"].Select("id").Model(Report{}).Where(condition, pars...).
  159. Scan(&list).Error
  160. return
  161. }
  162. // GetListByClassifyIdSecond 按照二级类型分页查询
  163. func GetListByClassifyIdSecond(classifyIdSecond int, offset , limit int) (reportList []*Report, err error) {
  164. err = global.MYSQL["rddp"].Model(Report{}).
  165. Select("id, classify_id_first, classify_name_first, classify_id_second, classify_name_second, title, stage, publish_time, author, video_url, abstract").
  166. Where("classify_id_second = ? and state = 2 ", classifyIdSecond).
  167. Order("publish_time desc, id desc").
  168. Offset(offset).
  169. Limit(limit).
  170. Scan(&reportList).Error
  171. if err == utils.ErrNoRow {
  172. err = nil
  173. }
  174. return
  175. }
  176. // GetListCountByClassifyIdSecond 按照二级分类总条数
  177. func GetListCountByClassifyIdSecond(classifyIdSecond int) (total int64, err error) {
  178. err = global.MYSQL["rddp"].Model(Report{}).
  179. Select("id, classify_id_first, classify_name_first, classify_id_second, classify_name_second, title, stage, publish_time, author, video_url, abstract").
  180. Where("classify_id_second=? and state = 2 ", classifyIdSecond).Count(&total).Error
  181. if err == utils.ErrNoRow {
  182. err = nil
  183. }
  184. return
  185. }
  186. // GetReportList 获取报告列表
  187. func GetReportList(condition string, pars []interface{}, offset , limit int) (list []*Report, err error) {
  188. err = global.MYSQL["rddp"].Model(Report{}).Where(condition, pars...).
  189. Order("publish_time desc, id desc").
  190. Offset(offset).
  191. Limit(limit).
  192. Scan(&list).Error
  193. return
  194. }
  195. // GetReportListCount 获取报告总数
  196. func GetReportListCount(condition string, pars []interface{}) (total int64, err error) {
  197. err = global.MYSQL["rddp"].Model(Report{}).Where(condition, pars...).
  198. Count(&total).Error
  199. return
  200. }
  201. // GetReportCollectListByPermission 根据权限相关的分类查询报告和章节
  202. func GetReportCollectListByPermission(classifyIdSeconds []int, typeIds []int, offset , limit int) (list []*response.ReportCollectListItem, err error) {
  203. sql := `( SELECT
  204. id AS report_id,
  205. 0 AS report_chapter_id,
  206. classify_id_first,
  207. classify_id_second,
  208. classify_name_first,
  209. classify_name_second,
  210. 0 as report_chapter_type_id,
  211. title,
  212. content_sub,
  213. publish_time
  214. FROM
  215. report
  216. WHERE
  217. classify_name_first != "晨报"
  218. AND classify_name_first != "周报"
  219. AND classify_id_second in (?)
  220. AND state = 2
  221. )
  222. UNION
  223. ( SELECT
  224. report_id,
  225. report_chapter_id,
  226. classify_id_first,
  227. 0 as classify_id_second,
  228. classify_name_first,
  229. null as classify_name_second,
  230. type_id as report_chapter_type_id,
  231. title,
  232. content_sub,
  233. publish_time
  234. FROM
  235. report_chapter
  236. WHERE
  237. publish_state = 2
  238. AND type_id in (?)
  239. )
  240. ORDER BY publish_time DESC, report_id desc LIMIT ? OFFSET ?
  241. `
  242. err = global.MYSQL["rddp"].Raw(sql, classifyIdSeconds, typeIds, limit, offset).Scan(&list).Error
  243. return
  244. }
  245. // GetReportCollectCountByPermission 查询汇总报告总页数
  246. func GetReportCollectCountByPermission(classifyIdSeconds []int, typeIds []int) (total int64, err error) {
  247. sql := `select count(*) from ( ( SELECT
  248. id AS report_id,
  249. 0 AS report_chapter_id
  250. FROM
  251. report
  252. WHERE
  253. classify_name_first != "晨报"
  254. AND classify_name_first != "周报"
  255. AND classify_id_second in (55,35,58,65,61,47)
  256. AND state = 2
  257. )
  258. UNION
  259. ( SELECT
  260. report_id,
  261. report_chapter_id
  262. FROM
  263. report_chapter
  264. WHERE
  265. publish_state = 2
  266. AND type_id in (9,28)
  267. )
  268. ) as ru
  269. `
  270. err = global.MYSQL["rddp"].Raw(sql, classifyIdSeconds, typeIds).Count(&total).Error
  271. return
  272. }