report.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_mini_api/models"
  5. "eta/eta_mini_api/models/request"
  6. "eta/eta_mini_api/models/response"
  7. "eta/eta_mini_api/services"
  8. "eta/eta_mini_api/utils"
  9. "fmt"
  10. "github.com/rdlucklib/rdluck_tools/paging"
  11. "html"
  12. "strconv"
  13. "strings"
  14. "time"
  15. )
  16. type ReportController struct {
  17. BaseAuthController
  18. }
  19. type ReportOpenController struct {
  20. BaseCommonController
  21. }
  22. // List
  23. // @Title 研报列表
  24. // @Description 研报列表
  25. // @Param request body request.ReportListForm true "type json string"
  26. // @Success 200 {object} response.ReportList
  27. // @router /list [get]
  28. func (this *ReportController) List() {
  29. br := new(models.BaseResponse).Init()
  30. defer func() {
  31. this.Data["json"] = br
  32. this.ServeJSON()
  33. }()
  34. params := new(request.ReportListForm)
  35. if e := this.ParseForm(params); e != nil {
  36. br.Msg = "参数解析异常"
  37. br.ErrMsg = fmt.Sprintf("参数解析异常, %v", e)
  38. return
  39. }
  40. var (
  41. reportCond, outsideCond string
  42. reportPars, outsidePars []interface{}
  43. )
  44. // 仅取常规和智能布局的报告
  45. reportCond += ` AND r.report_layout IN (1,2)`
  46. params.Keywords = strings.TrimSpace(params.Keywords)
  47. if params.Keywords != "" {
  48. kw := fmt.Sprint("%", params.Keywords, "%")
  49. reportCond += ` AND r.title LIKE ?`
  50. reportPars = append(reportPars, kw)
  51. outsideCond += ` AND o.title LIKE ?`
  52. outsidePars = append(outsidePars, kw)
  53. }
  54. // 分类名称map
  55. classifyOb := new(models.MiniClassify)
  56. classifies, e := classifyOb.GetItemsByCondition(``, make([]interface{}, 0), []string{}, "")
  57. if e != nil {
  58. br.Msg = "获取失败"
  59. br.ErrMsg = fmt.Sprintf("获取分类失败, %v", e)
  60. return
  61. }
  62. classifyMapping := make(map[int]*models.MiniClassify)
  63. for _, v := range classifies {
  64. classifyMapping[v.Id] = v
  65. }
  66. if params.ClassifyId > 0 {
  67. classify := classifyMapping[params.ClassifyId]
  68. if classify == nil || (classify != nil && classify.Enabled != 1) {
  69. br.Msg = "分类不存在,请刷新页面"
  70. return
  71. }
  72. switch classify.Level {
  73. case 1:
  74. reportCond += ` AND r.classify_id_first = ?`
  75. case 2:
  76. reportCond += ` AND r.classify_id_second = ?`
  77. case 3:
  78. reportCond += ` AND r.classify_id_third = ?`
  79. default:
  80. br.Msg = "分类异常"
  81. return
  82. }
  83. reportPars = append(reportPars, params.ClassifyId)
  84. // 获取子分类IDs
  85. childIds, e := classifyOb.GetChildClassifyIdsByParentId(params.ClassifyId)
  86. if e != nil {
  87. br.Msg = "获取失败"
  88. br.ErrMsg = fmt.Sprintf("获取子分类失败, %v", e)
  89. return
  90. }
  91. if len(childIds) > 0 {
  92. outsideCond += fmt.Sprintf(` AND o.classify_id IN (%s)`, utils.GetOrmInReplace(len(childIds)))
  93. outsidePars = append(outsidePars, childIds)
  94. } else {
  95. outsideCond += ` AND o.classify_id = ?`
  96. outsidePars = append(outsidePars, params.ClassifyId)
  97. }
  98. }
  99. resp := new(response.ReportList)
  100. // 分页
  101. var startSize int
  102. if params.PageSize <= 0 {
  103. params.PageSize = utils.PageSize20
  104. }
  105. if params.CurrentIndex <= 0 {
  106. params.CurrentIndex = 1
  107. }
  108. startSize = utils.StartIndex(params.CurrentIndex, params.PageSize)
  109. total, e := models.GetReportAndOutsideReportCount(reportCond, outsideCond, reportPars, outsidePars)
  110. if e != nil {
  111. br.Msg = "获取失败"
  112. br.ErrMsg = fmt.Sprintf("获取报告总数失败, %v", e)
  113. return
  114. }
  115. page := paging.GetPaging(params.CurrentIndex, params.PageSize, total)
  116. resp.Paging = page
  117. list, e := models.GetReportAndOutsideReportByCondition(reportCond, outsideCond, reportPars, outsidePars, startSize, params.PageSize)
  118. if e != nil {
  119. br.Msg = "获取失败"
  120. br.ErrMsg = fmt.Sprintf("获取报告列表失败, %v", e)
  121. return
  122. }
  123. for _, v := range list {
  124. t := new(models.UnionReportItem)
  125. t.Id = v.Id
  126. t.Title = v.Title
  127. t.Abstract = v.Abstract
  128. t.PublishTime = utils.TimeTransferString(utils.FormatDateTime, v.PublishTime)
  129. t.ReportSource = v.ReportSource
  130. t.ReportFile = v.ReportFile
  131. t.Author = v.Author
  132. // 报告分类名称
  133. if v.ReportSource == utils.ReportSourceDefault {
  134. if v.ClassifyIdThird > 0 {
  135. t.ClassifyId = v.ClassifyIdThird
  136. } else if v.ClassifyIdSecond > 0 {
  137. t.ClassifyId = v.ClassifyIdSecond
  138. } else {
  139. t.ClassifyId = v.ClassifyIdFirst
  140. }
  141. }
  142. if v.ReportSource == utils.ReportSourceOutside {
  143. t.ClassifyId = v.ClassifyIdThird
  144. }
  145. cs := classifyMapping[t.ClassifyId]
  146. if cs != nil {
  147. t.ClassifyName = cs.ClassifyName
  148. }
  149. resp.List = append(resp.List, t)
  150. }
  151. br.Data = resp
  152. br.Ret = 200
  153. br.Msg = "获取成功"
  154. br.Success = true
  155. }
  156. // Detail
  157. // @Title 研报详情H5
  158. // @Description 研报详情H5
  159. // @Param ReportId query int true "报告ID"
  160. // @Success 200 {object} models.ReportDetailResp
  161. // @router /detail [get]
  162. func (this *ReportOpenController) Detail() {
  163. br := new(models.BaseResponse).Init()
  164. defer func() {
  165. this.Data["json"] = br
  166. this.ServeJSON()
  167. }()
  168. reportId, _ := this.GetInt("ReportId")
  169. if reportId <= 0 {
  170. br.Msg = "参数有误"
  171. return
  172. }
  173. resp := new(response.ReportDetailResp)
  174. // 报告权限
  175. users := this.User
  176. if users != nil {
  177. resp.Status = users.AuthStatus
  178. }
  179. report, err := models.GetReportById(reportId)
  180. if err != nil {
  181. if err.Error() == utils.ErrNoRow() {
  182. br.Ret = 200
  183. br.Data = new(response.ReportDetailResp)
  184. br.Success = true
  185. br.Msg = "该报告已删除或不存在"
  186. return
  187. }
  188. br.Msg = "该报告已删除"
  189. br.ErrMsg = "获取报告详情失败,Err:" + err.Error()
  190. return
  191. }
  192. if report == nil {
  193. br.Msg = "报告不存在"
  194. return
  195. }
  196. // 版头版尾
  197. if report.HeadResourceId > 0 || report.EndResourceId > 0 {
  198. headImg, err := models.GetSmartReportResourceById(report.HeadResourceId)
  199. if err != nil && err.Error() != utils.ErrNoRow() {
  200. utils.FileLog.Warn("版头数据获取失败,Err:" + err.Error())
  201. }
  202. endImg, err := models.GetSmartReportResourceById(report.EndResourceId)
  203. if err != nil && err.Error() != utils.ErrNoRow() {
  204. utils.FileLog.Warn("版尾数据获取失败,Err:" + err.Error())
  205. }
  206. if headImg != nil {
  207. report.HeadResource = headImg
  208. }
  209. if endImg != nil {
  210. report.EndResource = endImg
  211. }
  212. }
  213. // 章节
  214. chapters := make([]*models.ReportChapter, 0)
  215. if report.HasChapter == 1 {
  216. chapterList, e := models.GetReportChapterList(report.Id)
  217. if e != nil {
  218. br.Msg = "获取失败"
  219. br.ErrMsg = fmt.Sprintf("获取章节列表失败, %v", e)
  220. return
  221. }
  222. for _, v := range chapterList {
  223. v.Content = html.UnescapeString(v.Content)
  224. }
  225. chapters = chapterList
  226. }
  227. report.ContentSub = html.UnescapeString(report.ContentSub)
  228. report.Content = html.UnescapeString(report.Content)
  229. // 图表授权token
  230. isOpenChartExpired, e := services.CheckIsOpenChartExpired()
  231. if e != nil {
  232. br.Msg = "获取失败"
  233. br.ErrMsg = fmt.Sprintf("获取图表是否开启鉴权失败, %v", e)
  234. return
  235. }
  236. if isOpenChartExpired {
  237. tokenMap := make(map[string]string)
  238. report.Content = services.HandleReportContent(report.Content, "add", tokenMap)
  239. report.ContentSub = services.HandleReportContent(report.ContentSub, "add", tokenMap)
  240. for _, v := range chapters {
  241. v.Content = services.HandleReportContent(v.Content, "add", tokenMap)
  242. }
  243. }
  244. report.ChapterContent = chapters
  245. resp.Report = report
  246. br.Data = resp
  247. br.Ret = 200
  248. br.Msg = "获取成功"
  249. br.Success = true
  250. }
  251. // OutsideDetail
  252. // @Title 研报详情H5-文档管理库
  253. // @Description 研报详情H5-文档管理库
  254. // @Param ReportId query int true "报告ID"
  255. // @Success 200 {object} models.OutsideReportItem
  256. // @router /outside_detail [get]
  257. func (this *ReportOpenController) OutsideDetail() {
  258. br := new(models.BaseResponse).Init()
  259. defer func() {
  260. this.Data["json"] = br
  261. this.ServeJSON()
  262. }()
  263. users := this.User
  264. reportId, _ := this.GetInt("ReportId")
  265. if reportId <= 0 {
  266. br.Msg = "参数有误"
  267. return
  268. }
  269. outsideReport, e := models.GetOutsideReportById(reportId)
  270. if e != nil {
  271. if e.Error() == utils.ErrNoRow() {
  272. br.Msg = "报告不存在,请刷新页面"
  273. return
  274. }
  275. br.Msg = "操作失败"
  276. br.ErrMsg = fmt.Sprintf("获取外部报告失败, %v", e)
  277. return
  278. }
  279. resp := outsideReport.Format2Item()
  280. if users != nil {
  281. resp.Status = users.AuthStatus
  282. }
  283. br.Data = resp
  284. br.Ret = 200
  285. br.Msg = "获取成功"
  286. br.Success = true
  287. }
  288. // ReadRecord
  289. // @Title 新增报告阅读记录
  290. // @Description 新增报告阅读记录
  291. // @Param request body request.ReportReadRecordReq true "type json string"
  292. // @Success 200 操作成功
  293. // @router /read_record [post]
  294. func (this *ReportController) ReadRecord() {
  295. br := new(models.BaseResponse).Init()
  296. defer func() {
  297. this.Data["json"] = br
  298. this.ServeJSON()
  299. }()
  300. users := this.User
  301. if users == nil {
  302. br.Msg = "请登录"
  303. br.ErrMsg = "请登录,用户信息为空"
  304. br.Ret = 403
  305. return
  306. }
  307. var req request.ReportReadRecordReq
  308. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  309. br.Msg = "参数解析异常"
  310. br.ErrMsg = fmt.Sprintf("参数解析异常, %v", e)
  311. return
  312. }
  313. if req.ReportId <= 0 {
  314. br.Msg = "参数有误"
  315. br.ErrMsg = fmt.Sprintf("参数有误, ReportId: %d", req.ReportId)
  316. return
  317. }
  318. if req.ReportSource != utils.ReportSourceDefault && req.ReportSource != utils.ReportSourceOutside {
  319. br.Msg = "参数有误"
  320. br.ErrMsg = fmt.Sprintf("参数有误, ReportSource: %d", req.ReportSource)
  321. return
  322. }
  323. // 获取报告分类信息
  324. var (
  325. title string
  326. classifyIdFirst int
  327. classifyIdSecond int
  328. classifyIdThird int
  329. classifyNameFirst string
  330. classifyNameSecond string
  331. classifyNameThird string
  332. )
  333. if req.ReportSource == utils.ReportSourceDefault {
  334. reportOb := new(models.Report)
  335. report, e := reportOb.GetItemById(req.ReportId)
  336. if e != nil {
  337. if e.Error() == utils.ErrNoRow() {
  338. br.Msg = "报告不存在,请刷新页面"
  339. return
  340. }
  341. br.Msg = "操作失败"
  342. br.ErrMsg = fmt.Sprintf("获取报告失败, %v", e)
  343. return
  344. }
  345. title = report.Title
  346. classifyIdFirst = report.ClassifyIdFirst
  347. classifyIdSecond = report.ClassifyIdSecond
  348. classifyIdThird = report.ClassifyIdThird
  349. classifyNameFirst = report.ClassifyNameFirst
  350. classifyNameSecond = report.ClassifyNameSecond
  351. classifyNameThird = report.ClassifyNameThird
  352. }
  353. if req.ReportSource == utils.ReportSourceOutside {
  354. outsideReport, e := models.GetOutsideReportById(req.ReportId)
  355. if e != nil {
  356. if e.Error() == utils.ErrNoRow() {
  357. br.Msg = "报告不存在,请刷新页面"
  358. return
  359. }
  360. br.Msg = "操作失败"
  361. br.ErrMsg = fmt.Sprintf("获取外部报告失败, %v", e)
  362. return
  363. }
  364. title = outsideReport.Title
  365. // 根据分类层级取出所有分类ID和名称
  366. classifyOb := new(models.MiniClassify)
  367. classifies, e := classifyOb.GetItemsByCondition(``, make([]interface{}, 0), []string{}, "")
  368. if e != nil {
  369. br.Msg = "操作失败"
  370. br.ErrMsg = fmt.Sprintf("获取报告分类失败, %v", e)
  371. return
  372. }
  373. classifyMapping := make(map[int]*models.MiniClassify)
  374. for _, v := range classifies {
  375. classifyMapping[v.Id] = v
  376. }
  377. thisClassify := classifyMapping[outsideReport.ClassifyId]
  378. if thisClassify == nil {
  379. br.Msg = "操作失败"
  380. br.ErrMsg = fmt.Sprintf("报告分类不存在, ClassifyId: %d", outsideReport.ClassifyId)
  381. return
  382. }
  383. levelArr := strings.Split(thisClassify.LevelPath, ",")
  384. if len(levelArr) <= 0 {
  385. br.Msg = "操作失败"
  386. br.ErrMsg = fmt.Sprintf("报告分类层级异常, ClassifyId: %d, LevelPath: %s", thisClassify.Id, thisClassify.LevelPath)
  387. return
  388. }
  389. if len(levelArr) > 0 {
  390. classifyIdFirst, _ = strconv.Atoi(levelArr[0])
  391. firstClassify := classifyMapping[classifyIdFirst]
  392. if firstClassify != nil {
  393. classifyNameFirst = firstClassify.ClassifyName
  394. }
  395. // 测试环境统一放在了一个一级分类下
  396. if classifyNameFirst == "" && utils.RunMode == "debug" {
  397. classifyNameFirst = "金瑞小程序分类(勿删)"
  398. }
  399. }
  400. if len(levelArr) > 1 {
  401. classifyIdSecond, _ = strconv.Atoi(levelArr[1])
  402. secondClassify := classifyMapping[classifyIdSecond]
  403. if secondClassify != nil {
  404. classifyNameSecond = secondClassify.ClassifyName
  405. }
  406. }
  407. if len(levelArr) > 2 {
  408. classifyIdThird, _ = strconv.Atoi(levelArr[2])
  409. thirdClassify := classifyMapping[classifyIdThird]
  410. if thirdClassify != nil {
  411. classifyNameThird = thirdClassify.ClassifyName
  412. }
  413. }
  414. }
  415. // 记录不存在则新增,存在则更新结束阅读时间
  416. var recordId int
  417. nowTime := time.Now().Local()
  418. if req.RecordId <= 0 {
  419. newRecord := &models.UserReadRecord{
  420. UserId: users.UserId,
  421. ReportId: req.ReportId,
  422. ReportTitle: title,
  423. ClassifyIdFirst: classifyIdFirst,
  424. ClassifyNameFirst: classifyNameFirst,
  425. ClassifyIdSecond: classifyIdSecond,
  426. ClassifyNameSecond: classifyNameSecond,
  427. ClassifyIdThird: classifyIdThird,
  428. ClassifyNameThird: classifyNameThird,
  429. StartTimestamp: int(nowTime.Unix()),
  430. CreateTime: nowTime,
  431. ReportSource: req.ReportSource,
  432. }
  433. if e := newRecord.Create(); e != nil {
  434. br.Msg = "操作失败"
  435. br.ErrMsg = fmt.Sprintf("新增阅读记录失败, %v", e)
  436. return
  437. }
  438. recordId = newRecord.Id
  439. } else {
  440. recordOb := new(models.UserReadRecord)
  441. readRecord, e := recordOb.GetItemById(req.RecordId)
  442. if e != nil {
  443. if e.Error() == utils.ErrNoRow() {
  444. br.Msg = "阅读记录不存在"
  445. return
  446. }
  447. br.Msg = "操作失败"
  448. br.ErrMsg = fmt.Sprintf("获取阅读记录失败, %v", e)
  449. return
  450. }
  451. readRecord.EndTimestamp = int(nowTime.Unix())
  452. if e = readRecord.Update([]string{recordOb.Cols().EndTimestamp}); e != nil {
  453. br.Msg = "操作失败"
  454. br.ErrMsg = fmt.Sprintf("更新阅读记录失败, %v", e)
  455. return
  456. }
  457. recordId = readRecord.Id
  458. // 更新用户阅读次数及最后一次阅读时间
  459. usersOb := new(models.Users)
  460. if e = usersOb.UpdateUserReadTimes(users.UserId); e != nil {
  461. br.Msg = "操作失败"
  462. br.ErrMsg = fmt.Sprintf("更新用户阅读次数失败, %v", e)
  463. return
  464. }
  465. }
  466. resp := new(response.UserReadRecordResp)
  467. resp.RecordId = recordId
  468. br.Data = resp
  469. br.Ret = 200
  470. br.Msg = "操作成功"
  471. br.Success = true
  472. }