query.go 12 KB

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