query.go 8.4 KB

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