report.go 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  1. package controllers
  2. import (
  3. "eta/eta_mini_bridge/models"
  4. "eta/eta_mini_bridge/models/response"
  5. "eta/eta_mini_bridge/services"
  6. "eta/eta_mini_bridge/utils"
  7. "fmt"
  8. "html"
  9. "strconv"
  10. "time"
  11. "github.com/rdlucklib/rdluck_tools/paging"
  12. )
  13. type ReportController struct {
  14. BaseAuthController
  15. }
  16. type ReportNoAuthController struct {
  17. BaseCommonController
  18. }
  19. // @Title List
  20. // @Description create users
  21. // @Param ChartPermissionId query int true "品种ID"
  22. // @Param Level query int true "品种层级"
  23. // @Param PageSize query int true "每页数据条数"
  24. // @Param CurrentIndex query int true "当前页页码,从1开始"
  25. // @Param RangeType query string true "范围类型,1-一天内,2-一周内,3-半年内"
  26. // @Param ClassifyId query int true "分类id"
  27. // @Success 200 {object} response.ReportListResp
  28. // @router /list [get]
  29. func (this *ReportController) List() {
  30. br := new(models.BaseResponse).Init()
  31. defer func() {
  32. this.Data["json"] = br
  33. this.ServeJSON()
  34. }()
  35. pageSize, _ := this.GetInt("PageSize")
  36. currentIndex, _ := this.GetInt("CurrentIndex")
  37. chartPermissionId, _ := this.GetInt("ChartPermissionId")
  38. level, _ := this.GetInt("Level")
  39. rangeType, _ := this.GetInt("RangeType")
  40. classifyId, _ := this.GetInt("ClassifyId")
  41. if chartPermissionId <= 0 {
  42. br.Msg = "品种参数错误"
  43. return
  44. }
  45. var condition string
  46. switch rangeType {
  47. case 1:
  48. condition += ` AND DATE(a.modify_time)=DATE(NOW()) `
  49. case 2:
  50. condition += ` AND DATE(a.modify_time) BETWEEN DATE_SUB(NOW(),INTERVAL 1 WEEK) AND NOW() `
  51. case 3:
  52. condition += ` AND DATE(a.modify_time) BETWEEN DATE_SUB(NOW(),INTERVAL 6 MONTH) AND NOW() `
  53. }
  54. var startSize int
  55. if pageSize <= 0 {
  56. pageSize = utils.PageSize20
  57. }
  58. if currentIndex <= 0 {
  59. currentIndex = 1
  60. }
  61. startSize = utils.StartIndex(currentIndex, pageSize)
  62. classifyList, err := models.GetClassifyList()
  63. if err != nil {
  64. br.Msg = "获取数据失败"
  65. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  66. return
  67. }
  68. classifyMap := make(map[int]*models.ClassifyView)
  69. isHas := false
  70. for _, v := range classifyList {
  71. if v.Id == classifyId && classifyId != 0 {
  72. isHas = true
  73. }
  74. classifyMap[v.Id] = v
  75. }
  76. if !isHas && classifyId != 0 {
  77. br.Msg = "分类不存在"
  78. return
  79. }
  80. classifyIdMap := make(map[int]struct{})
  81. if classifyId > 0 {
  82. leafClassifyIds := getLeafClassifyIds(classifyMap, classifyId)
  83. for _, v := range leafClassifyIds {
  84. classifyIdMap[v] = struct{}{}
  85. }
  86. }
  87. var total int
  88. var reportList []*models.ReportList
  89. classifyFirstIds := make([]int, 0)
  90. classifySecondIds := make([]int, 0)
  91. classifyThirdIds := make([]int, 0)
  92. switch level {
  93. case 2:
  94. classifyIds, err := models.GetClassifyIdsListById(chartPermissionId)
  95. if err != nil {
  96. br.Msg = "获取报告列表失败"
  97. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  98. return
  99. }
  100. if len(classifyIds) == 0 {
  101. br.Msg = "该品种下没有绑定分类"
  102. br.ErrMsg = "获取数据失败,品种id:" + strconv.Itoa(chartPermissionId)
  103. return
  104. }
  105. classifyIds = utils.Unique(classifyIds)
  106. // 根据分类筛选报告
  107. var selectedClassifyId []int
  108. if classifyId > 0 {
  109. for _, v := range classifyIds {
  110. if _, ok := classifyIdMap[v]; ok {
  111. selectedClassifyId = append(selectedClassifyId, v)
  112. }
  113. }
  114. } else {
  115. selectedClassifyId = classifyIds
  116. }
  117. classifyList, err := models.GetClassifyListByIds(selectedClassifyId)
  118. if err != nil {
  119. br.Msg = "获取报告列表失败"
  120. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  121. return
  122. }
  123. for _, v := range classifyList {
  124. if v.HasChild == 1 {
  125. continue
  126. }
  127. switch v.Level {
  128. case 1:
  129. classifyFirstIds = append(classifyFirstIds, v.Id)
  130. case 2:
  131. classifySecondIds = append(classifySecondIds, v.Id)
  132. case 3:
  133. classifyThirdIds = append(classifyThirdIds, v.Id)
  134. }
  135. }
  136. tmptotal, err := models.GetReportCountByClassifyIds(classifyFirstIds, classifySecondIds, classifyThirdIds, condition)
  137. if err != nil {
  138. br.Msg = "获取数据失败"
  139. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  140. return
  141. }
  142. total = tmptotal
  143. tmpReportList, err := models.GetReportListByClassifyIds(classifyFirstIds, classifySecondIds, classifyThirdIds, condition, startSize, pageSize)
  144. if err != nil {
  145. br.Msg = "获取报告列表失败"
  146. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  147. return
  148. }
  149. reportList = tmpReportList
  150. case 1:
  151. chartPermissionIds, err := models.GetChildChartPermissionIdsById(chartPermissionId)
  152. if err != nil {
  153. br.Msg = "获取报告列表失败"
  154. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  155. return
  156. }
  157. if len(chartPermissionIds) == 0 {
  158. br.Msg = "该品种下没有绑定分类"
  159. br.ErrMsg = "获取数据失败,品种id:" + strconv.Itoa(chartPermissionId)
  160. return
  161. }
  162. classifyIds, err := models.GetClassifyIdsListByIds(chartPermissionIds)
  163. if err != nil {
  164. br.Msg = "获取报告列表失败"
  165. br.ErrMsg = "获取报告列表失败,Err:" + err.Error()
  166. return
  167. }
  168. if len(classifyIds) == 0 {
  169. br.Msg = "该品种下没有绑定分类"
  170. return
  171. }
  172. classifyIds = utils.Unique(classifyIds)
  173. // 根据分类筛选报告
  174. var selectedClassifyId []int
  175. if classifyId > 0 {
  176. for _, v := range classifyIds {
  177. if _, ok := classifyIdMap[v]; ok {
  178. selectedClassifyId = append(selectedClassifyId, v)
  179. }
  180. }
  181. } else {
  182. selectedClassifyId = classifyIds
  183. }
  184. classifyList, err := models.GetClassifyListByIds(selectedClassifyId)
  185. if err != nil {
  186. br.Msg = "获取报告列表失败"
  187. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  188. return
  189. }
  190. for _, v := range classifyList {
  191. if v.HasChild == 1 {
  192. continue
  193. }
  194. switch v.Level {
  195. case 1:
  196. classifyFirstIds = append(classifyFirstIds, v.Id)
  197. case 2:
  198. classifySecondIds = append(classifySecondIds, v.Id)
  199. case 3:
  200. classifyThirdIds = append(classifyThirdIds, v.Id)
  201. }
  202. }
  203. tmptotal, err := models.GetReportCountByClassifyIds(classifyFirstIds, classifySecondIds, classifyThirdIds, condition)
  204. if err != nil {
  205. br.Msg = "获取数据失败"
  206. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  207. return
  208. }
  209. total = tmptotal
  210. tmpReportList, err := models.GetReportListByClassifyIds(classifyFirstIds, classifySecondIds, classifyThirdIds, condition, startSize, pageSize)
  211. if err != nil {
  212. br.Msg = "获取报告列表失败"
  213. br.ErrMsg = "获取报告列表失败,Err:" + err.Error()
  214. return
  215. }
  216. reportList = tmpReportList
  217. default:
  218. br.Msg = "层级参数错误"
  219. return
  220. }
  221. page := paging.GetPaging(currentIndex, pageSize, total)
  222. resp := new(response.ReportListResp)
  223. resp.Paging = page
  224. resp.List = reportList
  225. br.Ret = 200
  226. br.Success = true
  227. br.Msg = "获取成功"
  228. br.Data = resp
  229. }
  230. func getLeafClassifyIds(classifyMap map[int]*models.ClassifyView, keyId int) []int {
  231. var leafClassifyIds []int
  232. curClassify := classifyMap[keyId]
  233. if curClassify.HasChild == 0 {
  234. leafClassifyIds = append(leafClassifyIds, curClassify.Id)
  235. return leafClassifyIds
  236. }
  237. for _, v := range classifyMap {
  238. if v.ParentId == curClassify.Id {
  239. if v.HasChild == 0 {
  240. leafClassifyIds = append(leafClassifyIds, v.Id)
  241. } else {
  242. leafClassifyIds = append(leafClassifyIds, getLeafClassifyIds(classifyMap, v.Id)...)
  243. }
  244. }
  245. }
  246. return leafClassifyIds
  247. }
  248. // @Title 研报详情
  249. // @Description 研报详情接口
  250. // @Param ReportId query int true "报告id"
  251. // @Param UserId query int true "用户id"
  252. // @Success 200 {object} response.ReportDetailResp
  253. // @router /detail [get]
  254. func (this *ReportController) Detail() {
  255. br := new(models.BaseResponse).Init()
  256. defer func() {
  257. this.Data["json"] = br
  258. this.ServeJSON()
  259. }()
  260. reportId, err := this.GetInt("ReportId")
  261. userId, err := this.GetInt("UserId")
  262. if err != nil {
  263. br.Msg = "参数获取失败"
  264. br.ErrMsg = "参数获取失败,Err:" + err.Error()
  265. return
  266. }
  267. if reportId <= 0 {
  268. br.Msg = "参数错误"
  269. br.ErrMsg = "参数错误,报告id小于等于0"
  270. return
  271. }
  272. if userId <= 0 {
  273. br.Msg = "参数错误"
  274. br.ErrMsg = "参数错误,用户id小于等于0"
  275. return
  276. }
  277. user, err := models.GetUserById(userId)
  278. if err != nil {
  279. br.Msg = "用户不存在"
  280. br.ErrMsg = "用户不存在,系统异常,Err:" + err.Error()
  281. return
  282. }
  283. // 有效期是否到期
  284. var vailStatus int
  285. if user.Status == utils.UserStatusFormal {
  286. if user.ValidEndTime.After(time.Now()) && user.ValidStartTime.Before(time.Now()) {
  287. vailStatus = 2
  288. } else {
  289. // 不在有效期时间则设置为过期, 将用户状态禁用
  290. vailStatus = 1
  291. user.Status = utils.UserStatusNo
  292. }
  293. }
  294. report, err := models.GetReportById(reportId)
  295. if err != nil {
  296. if err.Error() == utils.ErrNoRow() {
  297. br.Ret = 200
  298. br.Data = new(response.ReportDetailResp)
  299. br.Success = true
  300. br.Msg = "该报告已删除或不存在"
  301. return
  302. }
  303. br.Msg = "该报告已删除"
  304. br.ErrMsg = "获取报告详情失败,Err:" + err.Error()
  305. return
  306. }
  307. if report.HeadResourceId > 0 || report.EndResourceId > 0 {
  308. headImg, err := models.GetSmartReportResourceById(report.HeadResourceId)
  309. if err != nil && err.Error() != utils.ErrNoRow() {
  310. utils.FileLog.Warn("版头数据获取失败,Err:" + err.Error())
  311. }
  312. endImg, err := models.GetSmartReportResourceById(report.EndResourceId)
  313. if err != nil && err.Error() != utils.ErrNoRow() {
  314. utils.FileLog.Warn("版尾数据获取失败,Err:" + err.Error())
  315. }
  316. if headImg != nil {
  317. report.HeadResource = headImg
  318. }
  319. if endImg != nil {
  320. report.EndResource = endImg
  321. }
  322. }
  323. if report.HasChapter == 1 {
  324. chapterList, err := models.GetReportChapterList(report.Id)
  325. if err != nil {
  326. br.Msg = "该报告已删除"
  327. br.ErrMsg = "获取章节列表失败,Err:" + err.Error()
  328. return
  329. }
  330. for _, v := range chapterList {
  331. v.Content = html.UnescapeString(v.Content)
  332. }
  333. report.ChapterContent = chapterList
  334. }
  335. report.ContentSub = html.UnescapeString(report.ContentSub)
  336. report.Content = html.UnescapeString(report.Content)
  337. if report == nil {
  338. br.Msg = "报告不存在"
  339. return
  340. }
  341. reportChartPermissionIds, err := models.GetChartPermissionIdsListByClassifyId(report.ClassifyIdSecond)
  342. if err != nil {
  343. br.Msg = "获取研报权限失败"
  344. br.ErrMsg = "获取研报权限失败,Err:" + err.Error()
  345. return
  346. }
  347. chartPermissionList, err := models.GetChartPermissionListByStrIds(reportChartPermissionIds)
  348. if err != nil {
  349. br.Msg = "获取研报权限失败"
  350. br.ErrMsg = "获取研报权限失败,Err:" + err.Error()
  351. return
  352. }
  353. var IsHas bool
  354. var IsPublic bool
  355. for _, v := range chartPermissionList {
  356. if v.IsPublic == 1 {
  357. IsPublic = true
  358. break
  359. }
  360. }
  361. resp := new(response.ReportDetailResp)
  362. if !IsPublic {
  363. // 私有报告
  364. // 用户权限过期或处于禁用状态,直接返回有效期已过
  365. if vailStatus == 1 || user.Status == utils.UserStatusNo {
  366. resp.Report = report
  367. resp.Status = utils.ReportPermissionStatusExpired
  368. report.Content = ""
  369. br.Ret = 200
  370. br.Data = resp
  371. br.Msg = "用户权限不足"
  372. return
  373. }
  374. // 潜在用户,直接返回无阅读报告权限
  375. if user.Status == utils.UserStatusPotential {
  376. resp.Report = report
  377. resp.Status = utils.ReportPermissionStatusNo
  378. report.Content = ""
  379. br.Ret = 200
  380. br.Data = resp
  381. br.Msg = "用户权限不足"
  382. return
  383. }
  384. // 正式用户,判断是否有权限查看
  385. chartPermissionIds, err := models.GetChartPermissionIdByUserId(userId)
  386. if err != nil {
  387. br.Msg = "获取用户权限失败"
  388. br.ErrMsg = "获取用户权限失败,Err:" + err.Error()
  389. return
  390. }
  391. if len(chartPermissionIds) <= 0 {
  392. resp.Report = report
  393. resp.Status = utils.ReportPermissionStatusNoPermission
  394. report.Content = ""
  395. br.Ret = 200
  396. br.Data = resp
  397. br.Msg = "用户权限不足"
  398. return
  399. }
  400. classifyIds, err := models.GetClassifyIdsListByIds(chartPermissionIds)
  401. if err != nil {
  402. br.Msg = "获取用户权限失败"
  403. br.ErrMsg = "获取用户分类权限失败,Err:" + err.Error()
  404. return
  405. }
  406. if len(classifyIds) <= 0 {
  407. resp.Report = report
  408. resp.Status = utils.ReportPermissionStatusNoPermission
  409. report.Content = ""
  410. br.Ret = 200
  411. br.Data = resp
  412. br.Msg = "用户权限不足"
  413. return
  414. }
  415. var reportClassifyId int
  416. if report.ClassifyIdFirst > 0 {
  417. reportClassifyId = report.ClassifyIdFirst
  418. }
  419. if report.ClassifyIdSecond > 0 {
  420. reportClassifyId = report.ClassifyIdSecond
  421. }
  422. if report.ClassifyIdThird > 0 {
  423. reportClassifyId = report.ClassifyIdThird
  424. }
  425. for _, v := range classifyIds {
  426. if v == reportClassifyId {
  427. IsHas = true
  428. break
  429. }
  430. }
  431. if !IsHas {
  432. resp.Report = report
  433. resp.Status = utils.ReportPermissionStatusNoPermission
  434. report.Content = ""
  435. br.Ret = 200
  436. br.Data = resp
  437. br.Msg = "用户权限不足"
  438. return
  439. }
  440. } else {
  441. report.IsPublic = IsPublic
  442. }
  443. resp.Report = report
  444. resp.Status = utils.ReportPermissionStatusHas
  445. br.Data = resp
  446. br.Ret = 200
  447. br.Success = true
  448. br.Msg = "获取成功"
  449. }
  450. // @Title 研报详情
  451. // @Description 研报详情接口
  452. // @Param ReportId query int true "报告id"
  453. // @Success 200 {object} response.ReportDetailResp
  454. // @router /detail/noUser [get]
  455. func (this *ReportController) DetailNoUser() {
  456. br := new(models.BaseResponse).Init()
  457. defer func() {
  458. this.Data["json"] = br
  459. this.ServeJSON()
  460. }()
  461. reportId, err := this.GetInt("ReportId")
  462. if err != nil {
  463. br.Msg = "参数获取失败"
  464. br.ErrMsg = "参数获取失败,Err:" + err.Error()
  465. return
  466. }
  467. if reportId <= 0 {
  468. br.Msg = "参数错误"
  469. br.ErrMsg = "参数错误,报告id小于等于0"
  470. return
  471. }
  472. report, err := models.GetReportById(reportId)
  473. if err != nil {
  474. if err.Error() == utils.ErrNoRow() {
  475. br.Ret = 200
  476. br.Data = new(response.ReportDetailResp)
  477. br.Success = true
  478. br.Msg = "该报告已删除或不存在"
  479. return
  480. }
  481. br.Msg = "该报告已删除"
  482. br.ErrMsg = "获取报告详情失败,Err:" + err.Error()
  483. return
  484. }
  485. report.ContentSub = html.UnescapeString(report.ContentSub)
  486. report.Content = ""
  487. if report == nil {
  488. br.Msg = "报告不存在"
  489. return
  490. }
  491. resp := new(response.ReportDetailResp)
  492. resp.Report = report
  493. resp.Status = utils.ReportPermissionStatusNoUser
  494. br.Data = resp
  495. br.Ret = 200
  496. br.Success = true
  497. br.Msg = "获取成功"
  498. }
  499. // @Title 今日研报列表
  500. // @Description 今日研报列表
  501. // @Success 200 {object} response.ReportListResp
  502. // @router /daily/list [get]
  503. func (this *ReportController) Today() {
  504. br := new(models.BaseResponse).Init()
  505. defer func() {
  506. this.Data["json"] = br
  507. this.ServeJSON()
  508. }()
  509. total, err := models.GetReportDailyListCount()
  510. if err != nil {
  511. br.Msg = "获取数据失败"
  512. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  513. return
  514. }
  515. list, err := models.GetReportDailyList(0, total)
  516. if err != nil {
  517. br.Msg = "获取失败"
  518. br.ErrMsg = "获取失败,Err:" + err.Error()
  519. return
  520. }
  521. classifyIds := make([]string, 0)
  522. for _, v := range list {
  523. classifyIds = append(classifyIds, strconv.Itoa(v.ClassifyIdSecond))
  524. }
  525. classifyIds = utils.Unique(classifyIds)
  526. // 获取二级分类和二级品种权限的映射
  527. chartPermissionMapping, err := models.GetChartPermissionListByClassifyStrIds(classifyIds)
  528. if err != nil {
  529. br.Msg = "获取研报权限失败"
  530. br.ErrMsg = "获取研报权限失败,Err:" + err.Error()
  531. return
  532. }
  533. classifyToPermissionMap2 := make(map[int][]int)
  534. chartPermissionIds := make([]string, 0)
  535. for _, v := range chartPermissionMapping {
  536. classifyToPermissionMap2[v.ClassifyId] = append(classifyToPermissionMap2[v.ClassifyId], v.ChartPermissionId)
  537. chartPermissionIds = append(chartPermissionIds, strconv.Itoa(v.ChartPermissionId))
  538. }
  539. // 获取二级品种的权限,并建立映射
  540. chartPermissionList2, err := models.GetChartPermissionListByStrIds(chartPermissionIds)
  541. if err != nil {
  542. br.Msg = "获取研报二级品种权限失败"
  543. br.ErrMsg = "获取研报二级品种权限失败,Err:" + err.Error()
  544. return
  545. }
  546. chartPermissionViewMap2 := make(map[int]*models.ChartPermission)
  547. for _, v := range chartPermissionList2 {
  548. chartPermissionViewMap2[v.ChartPermissionId] = v
  549. }
  550. // 获取一级品种的权限,并建立映射
  551. chartPermissionList1, err := models.GetChildChartPermissionListById(0)
  552. if err != nil {
  553. br.Msg = "获取研报一级品种权限失败"
  554. br.ErrMsg = "获取研报一级品种权限失败,Err:" + err.Error()
  555. return
  556. }
  557. chartPermissionMap1 := make(map[int]*models.ChartPermission)
  558. for _, v := range chartPermissionList1 {
  559. chartPermissionMap1[v.ChartPermissionId] = v
  560. }
  561. // 组合数据
  562. finalList := make([]*models.ReportList, 0)
  563. for _, v := range list {
  564. var permissionNames []string
  565. for _, vv := range classifyToPermissionMap2[v.ClassifyIdSecond] {
  566. if item, ok := chartPermissionViewMap2[vv]; ok {
  567. parent2 := item.ParentId
  568. permissionNames = append(permissionNames, chartPermissionMap1[parent2].PermissionName)
  569. }
  570. }
  571. v.PermissionNames = utils.Unique(permissionNames)
  572. if len(v.PermissionNames) != 0 {
  573. finalList = append(finalList, v)
  574. }
  575. }
  576. resp := new(response.ReportListResp)
  577. resp.List = finalList
  578. br.Ret = 200
  579. br.Success = true
  580. br.Msg = "获取成功"
  581. br.Data = resp
  582. }
  583. // @Title 最新研报列表
  584. // @Description 最新研报列表
  585. // @Param PageSize query int true "每页数据条数"
  586. // @Param CurrentIndex query int true "当前页页码,从1开始"
  587. // @Success 200 {object} response.ReportListResp
  588. // @router /recent/list [get]
  589. func (this *ReportController) RecentList() {
  590. br := new(models.BaseResponse).Init()
  591. defer func() {
  592. this.Data["json"] = br
  593. this.ServeJSON()
  594. }()
  595. pageSize, _ := this.GetInt("PageSize")
  596. currentIndex, _ := this.GetInt("CurrentIndex")
  597. var startSize int
  598. if pageSize <= 0 {
  599. pageSize = utils.PageSize30
  600. }
  601. if currentIndex <= 0 {
  602. currentIndex = 1
  603. }
  604. startSize = utils.StartIndex(currentIndex, pageSize)
  605. total, err := models.GetReportRecentListCount()
  606. if err != nil {
  607. br.Msg = "获取数据失败"
  608. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  609. return
  610. }
  611. list, err := models.GetReportRecentList(startSize, pageSize)
  612. if err != nil {
  613. br.Msg = "获取失败"
  614. br.ErrMsg = "获取失败,Err:" + err.Error()
  615. return
  616. }
  617. classifyIds := make([]string, 0)
  618. for _, v := range list {
  619. classifyIds = append(classifyIds, strconv.Itoa(v.ClassifyIdSecond))
  620. }
  621. classifyIds = utils.Unique(classifyIds)
  622. // 获取二级分类和二级品种权限的映射
  623. chartPermissionMapping, err := models.GetChartPermissionListByClassifyStrIds(classifyIds)
  624. if err != nil {
  625. br.Msg = "获取研报权限失败"
  626. br.ErrMsg = "获取研报权限失败,Err:" + err.Error()
  627. return
  628. }
  629. classifyToPermissionMap2 := make(map[int][]int)
  630. chartPermissionIds := make([]string, 0)
  631. for _, v := range chartPermissionMapping {
  632. classifyToPermissionMap2[v.ClassifyId] = append(classifyToPermissionMap2[v.ClassifyId], v.ChartPermissionId)
  633. chartPermissionIds = append(chartPermissionIds, strconv.Itoa(v.ChartPermissionId))
  634. }
  635. // 获取二级品种的权限,并建立映射
  636. chartPermissionList2, err := models.GetChartPermissionListByStrIds(chartPermissionIds)
  637. if err != nil {
  638. br.Msg = "获取研报二级品种权限失败"
  639. br.ErrMsg = "获取研报二级品种权限失败,Err:" + err.Error()
  640. return
  641. }
  642. chartPermissionViewMap2 := make(map[int]*models.ChartPermission)
  643. for _, v := range chartPermissionList2 {
  644. chartPermissionViewMap2[v.ChartPermissionId] = v
  645. }
  646. // 获取一级品种的权限,并建立映射
  647. chartPermissionList1, err := models.GetChildChartPermissionListById(0)
  648. if err != nil {
  649. br.Msg = "获取研报一级品种权限失败"
  650. br.ErrMsg = "获取研报一级品种权限失败,Err:" + err.Error()
  651. return
  652. }
  653. chartPermissionMap1 := make(map[int]*models.ChartPermission)
  654. for _, v := range chartPermissionList1 {
  655. chartPermissionMap1[v.ChartPermissionId] = v
  656. }
  657. // 组合数据
  658. finalList := make([]*models.ReportList, 0)
  659. for _, v := range list {
  660. var permissionNames []string
  661. for _, vv := range classifyToPermissionMap2[v.ClassifyIdSecond] {
  662. if item, ok := chartPermissionViewMap2[vv]; ok {
  663. parent2 := item.ParentId
  664. permissionNames = append(permissionNames, chartPermissionMap1[parent2].PermissionName)
  665. }
  666. }
  667. v.PermissionNames = utils.Unique(permissionNames)
  668. if len(v.PermissionNames) != 0 {
  669. finalList = append(finalList, v)
  670. }
  671. }
  672. page := paging.GetPaging(currentIndex, pageSize, total)
  673. resp := new(response.ReportListResp)
  674. resp.Paging = page
  675. resp.List = finalList
  676. br.Ret = 200
  677. br.Success = true
  678. br.Msg = "获取成功"
  679. br.Data = resp
  680. }
  681. // @Title 研报搜索
  682. // @Description 研报搜索
  683. // @Param PageSize query int true "每页数据条数"
  684. // @Param CurrentIndex query int true "当前页页码,从1开始"
  685. // @Param KeyWord query string true "关键字"
  686. // @Success 200 {object} response.ReportSearchResp
  687. // @router /search [get]
  688. func (this *ReportController) Search() {
  689. br := new(models.BaseResponse).Init()
  690. defer func() {
  691. this.Data["json"] = br
  692. this.ServeJSON()
  693. }()
  694. keyWord := this.GetString("KeyWord")
  695. pageSize, _ := this.GetInt("PageSize")
  696. currentIndex, _ := this.GetInt("CurrentIndex")
  697. if pageSize <= 0 {
  698. pageSize = utils.PageSize30
  699. }
  700. if currentIndex <= 0 {
  701. currentIndex = 1
  702. }
  703. if keyWord == "" {
  704. br.Msg = "请输入关键字"
  705. return
  706. }
  707. reportList, err, errMsg := services.SearchReport(keyWord, currentIndex, pageSize)
  708. if err != nil {
  709. br.Msg = "研报搜索失败"
  710. br.ErrMsg = errMsg + ",Err:" + err.Error()
  711. return
  712. }
  713. br.Data = reportList
  714. br.Msg = "查询成功"
  715. br.Ret = 200
  716. br.Success = true
  717. }
  718. // @Title List
  719. // @Description create users
  720. // @Param ChartPermissionId query int true "品种ID"
  721. // @Param Level query int true "品种层级"
  722. // @Param PageSize query int true "每页数据条数"
  723. // @Param CurrentIndex query int true "当前页页码,从1开始"
  724. // @Param RangeType query string true "范围类型,1-一天内,2-一周内,3-半年内"
  725. // @Param ClassifyId query int true "分类id"
  726. // @Param ReportType query int true "报告类型:1-eta报告,2-pdf报告"
  727. // @Success 200 {object} response.ReportListResp
  728. // @router /list [get]
  729. func (this *ReportNoAuthController) List() {
  730. br := new(models.BaseResponse).Init()
  731. defer func() {
  732. this.Data["json"] = br
  733. this.ServeJSON()
  734. }()
  735. pageSize, _ := this.GetInt("PageSize")
  736. currentIndex, _ := this.GetInt("CurrentIndex")
  737. chartPermissionId, _ := this.GetInt("ChartPermissionId")
  738. level, _ := this.GetInt("Level")
  739. rangeType, _ := this.GetInt("RangeType")
  740. classifyId, _ := this.GetInt("ClassifyId")
  741. reportType, _ := this.GetInt("ReportType")
  742. now := time.Now()
  743. todayStart := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local)
  744. var condition string
  745. switch rangeType {
  746. case 1:
  747. condition += fmt.Sprintf(` AND modify_time >= '%s' `, todayStart.Format(utils.FormatDateTime))
  748. case 2:
  749. oneWeekAgo := now.AddDate(0, 0, -7)
  750. condition += fmt.Sprintf(` AND modify_time >= '%s' `, oneWeekAgo.Format(utils.FormatDateTime))
  751. case 3:
  752. sixMonthAgo := now.AddDate(0, -6, 0)
  753. condition += fmt.Sprintf(` AND modify_time >= '%s' `, sixMonthAgo.Format(utils.FormatDateTime))
  754. }
  755. switch reportType {
  756. case utils.ReportTypeEta:
  757. if chartPermissionId < 0 {
  758. br.Msg = "品种参数错误"
  759. return
  760. }
  761. case utils.ReportTypePdf:
  762. default:
  763. br.Msg = "报告类型参数错误"
  764. return
  765. }
  766. var startSize int
  767. if pageSize <= 0 {
  768. pageSize = utils.PageSize20
  769. }
  770. if currentIndex <= 0 {
  771. currentIndex = 1
  772. }
  773. startSize = utils.StartIndex(currentIndex, pageSize)
  774. classifyList, err := models.GetClassifyList()
  775. if err != nil {
  776. br.Msg = "获取数据失败"
  777. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  778. return
  779. }
  780. classifyMap := make(map[int]*models.ClassifyView)
  781. isHas := false
  782. for _, v := range classifyList {
  783. if v.Id == classifyId && classifyId != 0 {
  784. isHas = true
  785. }
  786. classifyMap[v.Id] = v
  787. }
  788. if !isHas && classifyId != 0 {
  789. br.Msg = "分类不存在"
  790. return
  791. }
  792. classifyIdMap := make(map[int]struct{})
  793. if classifyId > 0 {
  794. leafClassifyIds := getLeafClassifyIds(classifyMap, classifyId)
  795. for _, v := range leafClassifyIds {
  796. classifyIdMap[v] = struct{}{}
  797. }
  798. }
  799. var total int
  800. var reportList []*models.ReportPushView
  801. classifyFirstIds := make([]int, 0)
  802. classifySecondIds := make([]int, 0)
  803. classifyThirdIds := make([]int, 0)
  804. switch level {
  805. case 2:
  806. classifyIds, err := models.GetClassifyIdsListById(chartPermissionId)
  807. if err != nil {
  808. br.Msg = "获取报告列表失败"
  809. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  810. return
  811. }
  812. if len(classifyIds) == 0 {
  813. br.Msg = "该品种下没有绑定分类"
  814. br.ErrMsg = "获取数据失败,品种id:" + strconv.Itoa(chartPermissionId)
  815. return
  816. }
  817. classifyIds = utils.Unique(classifyIds)
  818. // 根据分类筛选报告
  819. var selectedClassifyId []int
  820. if classifyId > 0 {
  821. for _, v := range classifyIds {
  822. if _, ok := classifyIdMap[v]; ok {
  823. selectedClassifyId = append(selectedClassifyId, v)
  824. }
  825. }
  826. } else {
  827. selectedClassifyId = classifyIds
  828. }
  829. classifyList, err := models.GetClassifyListByIds(selectedClassifyId)
  830. if err != nil {
  831. br.Msg = "获取报告列表失败"
  832. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  833. return
  834. }
  835. for _, v := range classifyList {
  836. if v.HasChild == 1 {
  837. continue
  838. }
  839. switch v.Level {
  840. case 1:
  841. classifyFirstIds = append(classifyFirstIds, v.Id)
  842. case 2:
  843. classifySecondIds = append(classifySecondIds, v.Id)
  844. case 3:
  845. classifyThirdIds = append(classifyThirdIds, v.Id)
  846. }
  847. }
  848. tmptotal, err := models.GetPushReportCountByClassifyIds(reportType, classifyFirstIds, classifySecondIds, classifyThirdIds, condition)
  849. if err != nil {
  850. br.Msg = "获取数据失败"
  851. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  852. return
  853. }
  854. total = tmptotal
  855. tmpReportList, err := models.GetPushReportListByClassifyIds(reportType, classifyFirstIds, classifySecondIds, classifyThirdIds, condition, startSize, pageSize)
  856. if err != nil {
  857. br.Msg = "获取报告列表失败"
  858. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  859. return
  860. }
  861. reportList = tmpReportList
  862. case 1:
  863. chartPermissionIds, err := models.GetChildChartPermissionIdsById(chartPermissionId)
  864. if err != nil {
  865. br.Msg = "获取报告列表失败"
  866. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  867. return
  868. }
  869. if len(chartPermissionIds) == 0 {
  870. br.Msg = "该品种下没有绑定分类"
  871. br.ErrMsg = "获取数据失败,品种id:" + strconv.Itoa(chartPermissionId)
  872. return
  873. }
  874. classifyIds, err := models.GetClassifyIdsListByIds(chartPermissionIds)
  875. if err != nil {
  876. br.Msg = "获取报告列表失败"
  877. br.ErrMsg = "获取报告列表失败,Err:" + err.Error()
  878. return
  879. }
  880. if len(classifyIds) == 0 {
  881. br.Msg = "该品种下没有绑定分类"
  882. return
  883. }
  884. classifyIds = utils.Unique(classifyIds)
  885. // 根据分类筛选报告
  886. var selectedClassifyId []int
  887. if classifyId > 0 {
  888. for _, v := range classifyIds {
  889. if _, ok := classifyIdMap[v]; ok {
  890. selectedClassifyId = append(selectedClassifyId, v)
  891. }
  892. }
  893. } else {
  894. selectedClassifyId = classifyIds
  895. }
  896. classifyList, err := models.GetClassifyListByIds(selectedClassifyId)
  897. if err != nil {
  898. br.Msg = "获取报告列表失败"
  899. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  900. return
  901. }
  902. for _, v := range classifyList {
  903. if v.HasChild == 1 {
  904. continue
  905. }
  906. switch v.Level {
  907. case 1:
  908. classifyFirstIds = append(classifyFirstIds, v.Id)
  909. case 2:
  910. classifySecondIds = append(classifySecondIds, v.Id)
  911. case 3:
  912. classifyThirdIds = append(classifyThirdIds, v.Id)
  913. }
  914. }
  915. tmptotal, err := models.GetPushReportCountByClassifyIds(reportType, classifyFirstIds, classifySecondIds, classifyThirdIds, condition)
  916. if err != nil {
  917. br.Msg = "获取数据失败"
  918. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  919. return
  920. }
  921. total = tmptotal
  922. tmpReportList, err := models.GetPushReportListByClassifyIds(reportType, classifyFirstIds, classifySecondIds, classifyThirdIds, condition, startSize, pageSize)
  923. if err != nil {
  924. br.Msg = "获取报告列表失败"
  925. br.ErrMsg = "获取报告列表失败,Err:" + err.Error()
  926. return
  927. }
  928. reportList = tmpReportList
  929. case 0:
  930. var selectedClassifyIds []int
  931. for k := range classifyIdMap {
  932. selectedClassifyIds = append(selectedClassifyIds, k)
  933. }
  934. tmptotal, err := models.GetPushReportCount(reportType, selectedClassifyIds, condition)
  935. if err != nil {
  936. br.Msg = "获取数据失败"
  937. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  938. return
  939. }
  940. total = tmptotal
  941. tmpReportList, err := models.GetPushReportListByPage(reportType, selectedClassifyIds, condition, startSize, pageSize)
  942. if err != nil {
  943. br.Msg = "获取报告列表失败"
  944. br.ErrMsg = "获取报告列表失败,Err:" + err.Error()
  945. return
  946. }
  947. reportList = tmpReportList
  948. default:
  949. br.Msg = "层级参数错误"
  950. return
  951. }
  952. page := paging.GetPaging(currentIndex, pageSize, total)
  953. resp := new(response.ReportPushListResp)
  954. resp.Paging = page
  955. resp.List = reportList
  956. br.Ret = 200
  957. br.Success = true
  958. br.Msg = "获取成功"
  959. br.Data = resp
  960. }
  961. // @Title 研报详情
  962. // @Description 研报详情接口
  963. // @Param ReportId query int true "报告id"
  964. // @Param UserId query int true "用户id"
  965. // @Success 200 {object} response.ReportDetailResp
  966. // @router /detail [get]
  967. func (this *ReportNoAuthController) Detail() {
  968. br := new(models.BaseResponse).Init()
  969. defer func() {
  970. this.Data["json"] = br
  971. this.ServeJSON()
  972. }()
  973. reportId, err := this.GetInt("ReportId")
  974. if err != nil {
  975. br.Msg = "参数获取失败"
  976. br.ErrMsg = "参数获取失败,Err:" + err.Error()
  977. return
  978. }
  979. if reportId <= 0 {
  980. br.Msg = "参数错误"
  981. br.ErrMsg = "参数错误,报告id小于等于0"
  982. return
  983. }
  984. report, err := models.GetReportById(reportId)
  985. if err != nil {
  986. if err.Error() == utils.ErrNoRow() {
  987. br.Ret = 200
  988. br.Data = new(response.ReportDetailResp)
  989. br.Success = true
  990. br.Msg = "该报告已删除或不存在"
  991. return
  992. }
  993. br.Msg = "该报告已删除"
  994. br.ErrMsg = "获取报告详情失败,Err:" + err.Error()
  995. return
  996. }
  997. if report.HeadResourceId > 0 || report.EndResourceId > 0 {
  998. headImg, err := models.GetSmartReportResourceById(report.HeadResourceId)
  999. if err != nil && err.Error() != utils.ErrNoRow() {
  1000. utils.FileLog.Warn("版头数据获取失败,Err:" + err.Error())
  1001. }
  1002. endImg, err := models.GetSmartReportResourceById(report.EndResourceId)
  1003. if err != nil && err.Error() != utils.ErrNoRow() {
  1004. utils.FileLog.Warn("版尾数据获取失败,Err:" + err.Error())
  1005. }
  1006. if headImg != nil {
  1007. report.HeadResource = headImg
  1008. }
  1009. if endImg != nil {
  1010. report.EndResource = endImg
  1011. }
  1012. }
  1013. if report.HasChapter == 1 {
  1014. chapterList, err := models.GetReportChapterList(report.Id)
  1015. if err != nil {
  1016. br.Msg = "该报告已删除"
  1017. br.ErrMsg = "获取章节列表失败,Err:" + err.Error()
  1018. return
  1019. }
  1020. for _, v := range chapterList {
  1021. v.Content = html.UnescapeString(v.Content)
  1022. }
  1023. report.ChapterContent = chapterList
  1024. }
  1025. report.ContentSub = html.UnescapeString(report.ContentSub)
  1026. report.Content = html.UnescapeString(report.Content)
  1027. if report == nil {
  1028. br.Msg = "报告不存在"
  1029. return
  1030. }
  1031. resp := new(response.ReportDetailResp)
  1032. resp.Report = report
  1033. br.Data = resp
  1034. br.Ret = 200
  1035. br.Success = true
  1036. br.Msg = "获取成功"
  1037. }