query.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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. // GetReportByOldReportId 根据老后台的research_report_id查找新的报告ID
  293. func GetReportByOldReportId(oldReportId uint64)(item *Report, err error) {
  294. err = global.MYSQL["rddp"].Model(Report{}).
  295. Where("old_report_id=?", oldReportId).First(&item).Error
  296. if err == utils.ErrNoRow {
  297. err = nil
  298. }
  299. return
  300. }
  301. // GetLatestReportByPermission 根据权限相关的分类查询最新的三篇专栏报告
  302. func GetLatestReportByPermission(classifyIdSeconds []int) (list []*pc.LatestReport, err error) {
  303. sql := `SELECT
  304. id AS report_id,
  305. 0 AS report_chapter_id,
  306. classify_id_first,
  307. classify_id_second,
  308. classify_name_first,
  309. classify_name_second,
  310. 0 as report_chapter_type_id,
  311. title,
  312. content_sub,
  313. stage,
  314. publish_time
  315. FROM
  316. report
  317. WHERE
  318. classify_name_first != "晨报"
  319. AND classify_name_first != "周报"
  320. AND classify_id_second in ?
  321. AND state = 2
  322. ORDER BY publish_time DESC LIMIT 3
  323. `
  324. err = global.MYSQL["rddp"].Raw(sql, classifyIdSeconds).Scan(&list).Error
  325. return
  326. }
  327. // GetLatestReport 获取最新专栏信息
  328. func GetLatestReport() (list *pc.LatestReport, err error) {
  329. sql := `SELECT
  330. r.id AS report_id,
  331. 0 AS report_chapter_id,
  332. classify_id_first,
  333. classify_id_second,
  334. classify_name_first,
  335. classify_name_second,
  336. 0 as report_chapter_type_id,
  337. title,
  338. content_sub,
  339. stage,
  340. publish_time
  341. FROM
  342. report AS r
  343. INNER JOIN
  344. classify AS c
  345. WHERE
  346. r.classify_name_first = c.classify_name
  347. AND c.show_type = 2
  348. AND c.is_show = 1
  349. AND state = 2
  350. ORDER BY publish_time DESC LIMIT 1
  351. `
  352. err = global.MYSQL["rddp"].Raw(sql).First(&list).Error
  353. return
  354. }
  355. func GetLatestReportByClassifyName(firstName string, secondId int) (items *pc.LatestReportBanner, err error) {
  356. sql := `SELECT
  357. a.id AS report_id,
  358. classify_name_second,
  359. classify_id_second,
  360. classify_name_first,
  361. classify_id_first,
  362. author,
  363. stage,
  364. report_author,
  365. vip_title
  366. FROM
  367. report as a
  368. JOIN classify as b
  369. WHERE
  370. a.state = 2
  371. AND a.classify_name_first = "%v"
  372. AND a.classify_id_second = %v
  373. AND a.classify_id_second = b.id
  374. ORDER BY
  375. publish_time DESC `
  376. sql = fmt.Sprintf(sql, firstName, secondId)
  377. err = global.MYSQL["rddp"].Raw(sql).First(&items).Error
  378. return
  379. }
  380. // GetCommoditiesReportCollectListByPermission 根据权限相关的分类查询大宗商品报告
  381. func GetCommoditiesReportCollectListByPermission(classifyIdSeconds []int, offset , limit int) (list []*Report, err error) {
  382. sql := ` SELECT
  383. 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
  384. FROM
  385. report
  386. WHERE
  387. classify_name_first != "晨报"
  388. AND classify_name_first != "周报"
  389. AND classify_id_second in ?
  390. AND state = 2
  391. ORDER BY publish_time DESC, id desc LIMIT ? OFFSET ?
  392. `
  393. err = global.MYSQL["rddp"].Raw(sql, classifyIdSeconds, limit, offset).Scan(&list).Error
  394. return
  395. }
  396. // GetCommoditiesReportCollectCountByPermission 查询大宗商品报告总页数
  397. func GetCommoditiesReportCollectCountByPermission(classifyIdSeconds []int) (total int64, err error) {
  398. sql := `select count(*)
  399. FROM
  400. report
  401. WHERE
  402. classify_name_first != "晨报"
  403. AND classify_name_first != "周报"
  404. AND classify_id_second in ?
  405. AND state = 2
  406. `
  407. err = global.MYSQL["rddp"].Raw(sql, classifyIdSeconds).Count(&total).Error
  408. return
  409. }