query.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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. "strings"
  9. )
  10. // GetLatestClassReportsByClassifyIdSeconds 根据用户已购买的分类权限查询个分类最新的报告
  11. func GetLatestClassReportsByClassifyIdSeconds(classifyIdSeconds []int) (reportList []*Report, err error) {
  12. sql := `SELECT t1.id,
  13. t1.classify_id_first,
  14. t1.classify_id_second,
  15. t1.stage,
  16. t1.title,
  17. t1.classify_name_first,
  18. t1.classify_name_second,
  19. t1.publish_time
  20. FROM report t1
  21. INNER JOIN
  22. (
  23. SELECT classify_id_first, max( publish_time ) AS max_publish_time
  24. FROM report
  25. WHERE
  26. state IN (2, 6)
  27. AND classify_name_first !="权益研报"
  28. AND classify_id_second IN ?
  29. GROUP BY
  30. classify_id_first
  31. ) t2
  32. ON t1.classify_id_first = t2.classify_id_first AND
  33. t1.publish_time = t2.max_publish_time
  34. WHERE t1.state IN (2, 6)
  35. AND t1.classify_name_first !="权益研报"
  36. AND t1.classify_id_second IN ?`
  37. err = global.MYSQL["rddp"].Raw(sql, classifyIdSeconds, classifyIdSeconds).Scan(&reportList).Error
  38. return
  39. }
  40. // GetReportsByClassifyIdSecondsAndDate 根据时间和报告分类筛选出合适的记录
  41. func GetReportsByClassifyIdSecondsAndDate(classifyIdSeconds []int, publishTime string) (reportList []*Report, err error) {
  42. err = global.MYSQL["rddp"].Model(Report{}).
  43. Select("id, classify_name_first").
  44. Where("classify_id_second in (?) and state IN (2, 6) and publish_time > ? ", classifyIdSeconds, publishTime).Scan(&reportList).Error
  45. return
  46. }
  47. // GetListByIDsAndClassifyIdFirst 分页查询
  48. func GetListByIDsAndClassifyIdFirst(ids []int, classifyIdFirst int, offset, limit int) (reportList []*Report, 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 IN (2, 6) ", ids, classifyIdFirst).
  52. Order("publish_time desc, id desc").
  53. Offset(offset).
  54. Limit(limit).
  55. Scan(&reportList).Error
  56. return
  57. }
  58. // GetListCountByIDsAndClassifyIdFirst 分页查询
  59. func GetListCountByIDsAndClassifyIdFirst(ids []int, classifyIdFirst int) (total int64, err error) {
  60. err = global.MYSQL["rddp"].Model(Report{}).
  61. Select("id, classify_id_first, classify_name_first, classify_id_second, classify_name_second, title, stage, publish_time").
  62. Where("id in (?) and classify_id_first=? and state IN (2, 6) ", ids, classifyIdFirst).
  63. Count(&total).Error
  64. return
  65. }
  66. // GetListByClassifyIdSeconds 分页查询
  67. func GetListByClassifyIdSeconds(classifyIdSeconds []int, offset, limit int) (reportList []*Report, 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 IN (2, 6) ", classifyIdSeconds).
  71. Order("publish_time desc, id desc").
  72. Offset(offset).
  73. Limit(limit).
  74. Scan(&reportList).Error
  75. return
  76. }
  77. // GetListCountByClassifyIdSeconds
  78. func GetListCountByClassifyIdSeconds(classifyIdSeconds []int) (total int64, err error) {
  79. err = global.MYSQL["rddp"].Model(Report{}).
  80. Select("id, classify_id_first, classify_name_first, classify_id_second, classify_name_second, title, stage, publish_time").
  81. Where("classify_id_second in (?) and state IN (2, 6) ", classifyIdSeconds).
  82. Count(&total).Error
  83. return
  84. }
  85. // GetListByClassifyIdFirst 按照类型分页查询
  86. func GetListByClassifyIdFirst(classifyIdFirst int, offset, limit int) (reportList []*Report, 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 IN (2, 6) ", classifyIdFirst).
  90. Order("publish_time desc, id desc").
  91. Offset(offset).
  92. Limit(limit).
  93. Scan(&reportList).Error
  94. return
  95. }
  96. // GetListCountByClassifyIdFirst 按照类型查询报告总数
  97. func GetListCountByClassifyIdFirst(classifyIdFirst int) (total int64, err error) {
  98. err = global.MYSQL["rddp"].Model(Report{}).
  99. Select("id, classify_id_first, classify_name_first, classify_id_second, classify_name_second, title, stage, publish_time").
  100. Where("classify_id_first=? and state IN (2, 6) ", classifyIdFirst).
  101. Count(&total).Error
  102. return
  103. }
  104. // GetLatestByReportIds 根据ids获取报告
  105. func GetLatestByReportIds(ids []int) (item *Report, err error) {
  106. err = global.MYSQL["rddp"].Where("id in ? and state IN (2, 6)", ids).Order("publish_time desc, id desc").First(&item).Error
  107. if err == utils.ErrNoRow {
  108. err = nil
  109. }
  110. return
  111. }
  112. // GetPublishByReportId 根据id获取报告
  113. func GetPublishByReportId(id int) (item *Report, err error) {
  114. err = global.MYSQL["rddp"].Where("id = ? and state IN (2, 6)", id).First(&item).Error
  115. if err == utils.ErrNoRow {
  116. err = nil
  117. }
  118. return
  119. }
  120. // GetByReportId 根据id获取报告
  121. func GetByReportId(id int) (item *Report, err error) {
  122. err = global.MYSQL["rddp"].Where("id = ? ", id).First(&item).Error
  123. if err == utils.ErrNoRow {
  124. err = nil
  125. }
  126. return
  127. }
  128. // GetByReportIds 根据id获取报告
  129. func GetByReportIds(ids []int) (list []*Report, err error) {
  130. err = global.MYSQL["rddp"].Model(Report{}).
  131. Where("id in (?) and state IN (2, 6)", ids).
  132. Select("id, create_time").
  133. Scan(&list).Error
  134. if err == utils.ErrNoRow {
  135. err = nil
  136. }
  137. return
  138. }
  139. // GetLatestDay 获取最新的晨报
  140. func GetLatestDay() (item *Report, err error) {
  141. err = global.MYSQL["rddp"].Where("state = 2 and classify_name_first= '晨报'").Order("publish_time desc, id desc").First(&item).Error
  142. if err == utils.ErrNoRow {
  143. err = nil
  144. }
  145. return
  146. }
  147. // GetLatestReportsByClassifyIdFirst 查询当前一级分类下,二级分类中,最新的报告
  148. func GetLatestReportsByClassifyIdFirst(classifyIdFirst int, classifyIdSeconds []int) (reportList []*Report, err error) {
  149. sql := `
  150. SELECT t1.id,
  151. t1.classify_id_first,
  152. t1.classify_id_second,
  153. t1.stage,
  154. t1.classify_name_first,
  155. t1.classify_name_second,
  156. t1.publish_time
  157. FROM report t1
  158. INNER JOIN
  159. (
  160. SELECT classify_id_first, classify_id_second, max( publish_time ) AS max_publish_time
  161. FROM report
  162. WHERE
  163. state IN (2, 6)
  164. AND classify_id_first = ?
  165. AND classify_id_second IN ?
  166. GROUP BY
  167. classify_id_second
  168. ) t2
  169. ON t1.classify_id_second = t2.classify_id_second AND
  170. t1.classify_id_first = t2.classify_id_first AND
  171. t1.publish_time = t2.max_publish_time
  172. WHERE t1.state IN (2, 6)
  173. AND t1.classify_id_first = ?
  174. AND t1.classify_id_second IN ?`
  175. err = global.MYSQL["rddp"].Raw(sql, classifyIdFirst, classifyIdSeconds, classifyIdFirst, classifyIdSeconds).Scan(&reportList).Error
  176. return
  177. }
  178. // GetReportListByCondition 获取报告列表
  179. func GetReportListByCondition(condition string, pars []interface{}) (list []*Report, err error) {
  180. err = global.MYSQL["rddp"].Select("id").Model(Report{}).Where(condition, pars...).
  181. Scan(&list).Error
  182. return
  183. }
  184. // GetListByClassifyIdSecond 按照二级类型分页查询
  185. func GetListByClassifyIdSecond(classifyIdSecond int, offset, limit int) (reportList []*Report, err error) {
  186. err = global.MYSQL["rddp"].Model(Report{}).
  187. 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").
  188. Where("classify_id_second = ? and state IN (2, 6) ", classifyIdSecond).
  189. Order("publish_time desc, id desc").
  190. Offset(offset).
  191. Limit(limit).
  192. Scan(&reportList).Error
  193. if err == utils.ErrNoRow {
  194. err = nil
  195. }
  196. return
  197. }
  198. // GetListCountByClassifyIdSecond 按照二级分类总条数
  199. func GetListCountByClassifyIdSecond(classifyIdSecond int) (total int64, err error) {
  200. err = global.MYSQL["rddp"].Model(Report{}).
  201. Where("classify_id_second=? and state IN (2, 6) ", classifyIdSecond).Count(&total).Error
  202. if err == utils.ErrNoRow {
  203. err = nil
  204. }
  205. return
  206. }
  207. // GetReportList 获取报告列表
  208. func GetReportList(condition string, pars []interface{}, offset, limit int) (list []*Report, err error) {
  209. err = global.MYSQL["rddp"].Model(Report{}).
  210. Select("id, classify_id_first, classify_name_first, classify_id_second, classify_name_second,classify_id_third, classify_name_third, title, stage, publish_time, author, create_time, video_url, video_name, video_play_seconds, abstract,has_chapter,head_img,end_img,head_resource_id,end_resource_id").
  211. Where(condition, pars...).
  212. Order("publish_time desc, id desc").
  213. Offset(offset).
  214. Limit(limit).
  215. Scan(&list).Error
  216. return
  217. }
  218. // GetReportListCount 获取报告总数
  219. func GetReportListCount(condition string, pars []interface{}) (total int64, err error) {
  220. err = global.MYSQL["rddp"].Model(Report{}).Where(condition, pars...).
  221. Count(&total).Error
  222. return
  223. }
  224. // GetReportCollectListByPermission 根据权限相关的分类查询报告和章节
  225. func GetReportCollectListByPermission(classifyIdSeconds []int, typeIds []int, offset, limit int) (list []*response.ReportCollectListItem, err error) {
  226. sql := `( SELECT
  227. id AS report_id,
  228. 0 AS report_chapter_id,
  229. classify_id_first,
  230. classify_id_second,
  231. classify_name_first,
  232. classify_name_second,
  233. 0 as report_chapter_type_id,
  234. title,
  235. content_sub,
  236. publish_time
  237. FROM
  238. report
  239. WHERE
  240. classify_name_first != "晨报"
  241. AND classify_name_first != "周报"
  242. AND classify_id_second in ?
  243. AND state IN (2, 6)
  244. )
  245. UNION
  246. ( SELECT
  247. report_id,
  248. report_chapter_id,
  249. classify_id_first,
  250. 0 as classify_id_second,
  251. classify_name_first,
  252. null as classify_name_second,
  253. type_id as report_chapter_type_id,
  254. title,
  255. content_sub,
  256. publish_time
  257. FROM
  258. report_chapter
  259. WHERE
  260. publish_state = 2
  261. AND type_id in ?
  262. )
  263. ORDER BY publish_time DESC, report_id desc LIMIT ? OFFSET ?
  264. `
  265. err = global.MYSQL["rddp"].Raw(sql, classifyIdSeconds, typeIds, limit, offset).Scan(&list).Error
  266. return
  267. }
  268. // GetReportCollectCountByPermission 查询汇总报告总页数
  269. func GetReportCollectCountByPermission(classifyIdSeconds []int, typeIds []int) (total int64, err error) {
  270. sql := `select count(*) from ( ( SELECT
  271. id AS report_id,
  272. 0 AS report_chapter_id
  273. FROM
  274. report
  275. WHERE
  276. classify_name_first != "晨报"
  277. AND classify_name_first != "周报"
  278. AND classify_id_second in ?
  279. AND state IN (2, 6)
  280. )
  281. UNION
  282. ( SELECT
  283. report_id,
  284. report_chapter_id
  285. FROM
  286. report_chapter
  287. WHERE
  288. publish_state = 2
  289. AND type_id in ?
  290. )
  291. ) as ru
  292. `
  293. err = global.MYSQL["rddp"].Raw(sql, classifyIdSeconds, typeIds).Count(&total).Error
  294. return
  295. }
  296. // GetReportByOldReportId 根据老后台的research_report_id查找新的报告ID
  297. func GetReportByOldReportId(oldReportId uint64) (item *Report, err error) {
  298. err = global.MYSQL["rddp"].Model(Report{}).
  299. Where("old_report_id=?", oldReportId).First(&item).Error
  300. if err == utils.ErrNoRow {
  301. err = nil
  302. }
  303. return
  304. }
  305. // GetLatestReportByPermission 根据权限相关的分类查询最新的三篇专栏报告
  306. func GetLatestReportByPermission(classifyIdSeconds []int) (list []*pc.LatestReport, err error) {
  307. sql := `SELECT
  308. id AS report_id,
  309. 0 AS report_chapter_id,
  310. classify_id_first,
  311. classify_id_second,
  312. classify_name_first,
  313. classify_name_second,
  314. 0 as report_chapter_type_id,
  315. title,
  316. content_sub,
  317. stage,
  318. publish_time
  319. FROM
  320. report
  321. WHERE
  322. classify_name_first != "晨报"
  323. AND classify_name_first != "周报"
  324. AND classify_id_second in ?
  325. AND state IN (2, 6)
  326. ORDER BY publish_time DESC LIMIT 3
  327. `
  328. err = global.MYSQL["rddp"].Raw(sql, classifyIdSeconds).Scan(&list).Error
  329. return
  330. }
  331. // GetLatestReport 获取最新专栏信息
  332. func GetLatestReport() (list *pc.LatestReport, err error) {
  333. sql := `SELECT
  334. r.id AS report_id,
  335. 0 AS report_chapter_id,
  336. classify_id_first,
  337. classify_id_second,
  338. classify_name_first,
  339. classify_name_second,
  340. 0 as report_chapter_type_id,
  341. title,
  342. content_sub,
  343. stage,
  344. publish_time
  345. FROM
  346. report AS r
  347. INNER JOIN
  348. classify AS c
  349. WHERE
  350. r.classify_name_first = c.classify_name
  351. AND c.show_type = 2
  352. AND c.is_show = 1
  353. AND c.enabled = 1
  354. AND state IN (2, 6)
  355. ORDER BY publish_time DESC LIMIT 1
  356. `
  357. err = global.MYSQL["rddp"].Raw(sql).First(&list).Error
  358. return
  359. }
  360. func GetLatestReportByClassifyName(firstName string, secondId int) (items *pc.LatestReportBanner, err error) {
  361. sql := `SELECT
  362. a.id AS report_id,
  363. classify_name_second,
  364. classify_id_second,
  365. classify_name_first,
  366. classify_id_first,
  367. author,
  368. stage,
  369. report_author,
  370. vip_title
  371. FROM
  372. report as a
  373. JOIN classify as b
  374. WHERE
  375. a.state IN (2, 6)
  376. AND a.classify_name_first = "%v"
  377. AND a.classify_id_second = %v
  378. AND a.classify_id_second = b.id
  379. ORDER BY
  380. publish_time DESC `
  381. sql = fmt.Sprintf(sql, firstName, secondId)
  382. err = global.MYSQL["rddp"].Raw(sql).First(&items).Error
  383. return
  384. }
  385. // GetCommoditiesReportCollectListByPermission 根据权限相关的分类查询大宗商品报告
  386. func GetCommoditiesReportCollectListByPermission(classifyIdSeconds []int, offset, limit int) (list []*Report, err error) {
  387. sql := ` SELECT
  388. 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
  389. FROM
  390. report
  391. WHERE
  392. classify_name_first != "晨报"
  393. AND classify_name_first != "周报"
  394. AND classify_id_second in ?
  395. AND state IN (2, 6)
  396. ORDER BY publish_time DESC, id desc LIMIT ? OFFSET ?
  397. `
  398. err = global.MYSQL["rddp"].Raw(sql, classifyIdSeconds, limit, offset).Scan(&list).Error
  399. return
  400. }
  401. // GetCommoditiesReportCollectCountByPermission 查询大宗商品报告总页数
  402. func GetCommoditiesReportCollectCountByPermission(classifyIdSeconds []int) (total int64, err error) {
  403. sql := `select count(*)
  404. FROM
  405. report
  406. WHERE
  407. classify_name_first != "晨报"
  408. AND classify_name_first != "周报"
  409. AND classify_id_second in ?
  410. AND state IN (2, 6)
  411. `
  412. err = global.MYSQL["rddp"].Raw(sql, classifyIdSeconds).Count(&total).Error
  413. return
  414. }
  415. // GetListByReportIds 根据IDs获取列表
  416. func GetListByReportIds(reportIds []int) (list []*Report, err error) {
  417. var where string
  418. where = `state IN (2, 6)`
  419. if len(reportIds) > 0 {
  420. where += ` AND id IN (?)`
  421. }
  422. err = global.MYSQL["rddp"].Model(Report{}).
  423. Where(where, reportIds).
  424. Order("id asc").
  425. Scan(&list).Error
  426. return
  427. }
  428. // GetReportCollectListByPermissionV1
  429. // @Description: 根据权限相关的分类查询报告和章节
  430. // @author: Roc
  431. // @datetime 2024-06-21 10:24:14
  432. // @param chartPermissionId int
  433. // @param classifyIdSeconds []int
  434. // @param offset int
  435. // @param limit int
  436. // @return list []*response.ReportCollectListItem
  437. // @return err error
  438. func GetReportCollectListByPermissionV1(chartPermissionId int, firstClassifyIdList, secondClassifyIdList, thirdClassifyIdList []int, offset, limit int) (list []*response.ReportCollectListItem, err error) {
  439. conditionList := make([]string, 0)
  440. pars := make([]interface{}, 0)
  441. if len(firstClassifyIdList) > 0 {
  442. conditionList = append(conditionList, "classify_id_first in ?")
  443. pars = append(pars, firstClassifyIdList)
  444. }
  445. if len(secondClassifyIdList) > 0 {
  446. conditionList = append(conditionList, "classify_id_second in ?")
  447. pars = append(pars, secondClassifyIdList)
  448. }
  449. if len(thirdClassifyIdList) > 0 {
  450. conditionList = append(conditionList, "classify_id_third in ?")
  451. pars = append(pars, thirdClassifyIdList)
  452. }
  453. condition := " classify_id_first = 0 "
  454. if len(conditionList) > 0 {
  455. condition = strings.Join(conditionList, " OR ")
  456. }
  457. pars = append(pars, chartPermissionId, limit, offset)
  458. sql := `( SELECT
  459. id AS report_id,
  460. 0 AS report_chapter_id,
  461. classify_id_first,
  462. classify_id_second,
  463. classify_name_first,
  464. classify_name_second,
  465. 0 as report_chapter_type_id,
  466. title,
  467. content_sub,
  468. publish_time
  469. FROM
  470. report
  471. WHERE
  472. has_chapter != 1
  473. AND ( ` + condition + ` )
  474. AND state IN (2, 6)
  475. )
  476. UNION
  477. ( SELECT
  478. a.report_id,
  479. a.report_chapter_id,
  480. a.classify_id_first,
  481. 0 as classify_id_second,
  482. a.classify_name_first,
  483. null as classify_name_second,
  484. a.type_id as report_chapter_type_id,
  485. a.title,
  486. a.content_sub,
  487. a.publish_time
  488. FROM
  489. report_chapter AS a
  490. JOIN report_chapter_permission_mapping AS b on a.report_chapter_id=b.report_chapter_id
  491. WHERE
  492. a.publish_state = 2
  493. AND b.chart_permission_id = ?
  494. )
  495. ORDER BY publish_time DESC, report_id desc LIMIT ? OFFSET ?
  496. `
  497. err = global.MYSQL["rddp"].Raw(sql, pars...).Scan(&list).Error
  498. return
  499. }
  500. // GetReportCollectCountByPermission 查询汇总报告总页数
  501. func GetReportCollectCountByPermissionV1(chartPermissionId int, classifyIdSeconds []int) (total int64, err error) {
  502. sql := `select count(*) from ( (( SELECT
  503. id AS report_id,
  504. 0 AS report_chapter_id
  505. FROM
  506. report
  507. WHERE
  508. has_chapter != 1
  509. AND classify_id_second in ?
  510. AND state IN (2, 6)
  511. )
  512. UNION
  513. ( SELECT
  514. a.report_id,
  515. a.report_chapter_id
  516. FROM
  517. report_chapter AS a
  518. JOIN report_chapter_permission_mapping AS b on a.report_chapter_id=b.report_chapter_id
  519. WHERE
  520. a.publish_state = 2
  521. AND b.chart_permission_id = ?
  522. )
  523. )
  524. ) as ru
  525. `
  526. err = global.MYSQL["rddp"].Raw(sql, classifyIdSeconds, chartPermissionId).Count(&total).Error
  527. return
  528. }
  529. // GetLatestReportByClassifyId
  530. // @Description: 根据分类id获取最近一期报告
  531. // @author: Roc
  532. // @datetime 2024-06-24 14:10:05
  533. // @param firstId int
  534. // @param secondId int
  535. // @param thirdId int
  536. // @return items *pc.LatestReportBanner
  537. // @return err error
  538. func GetLatestReportByClassifyId(firstId, secondId, thirdId int) (items *pc.LatestReportBanner, err error) {
  539. sql := `SELECT
  540. a.id AS report_id,
  541. classify_name_second,
  542. classify_id_second,
  543. classify_name_first,
  544. classify_id_first,
  545. author,
  546. stage
  547. FROM
  548. report as a
  549. WHERE
  550. a.state IN (2, 6)
  551. AND a.classify_id_first = ?
  552. AND a.classify_id_second = ?
  553. AND a.classify_id_third = ?
  554. ORDER BY
  555. publish_time DESC `
  556. err = global.MYSQL["rddp"].Raw(sql, firstId, secondId, thirdId).First(&items).Error
  557. return
  558. }