report.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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. report, err := models.GetReportById(reportId)
  174. if err != nil {
  175. if err.Error() == utils.ErrNoRow() {
  176. br.Ret = 200
  177. br.Data = new(response.ReportDetailResp)
  178. br.Success = true
  179. br.Msg = "该报告已删除或不存在"
  180. return
  181. }
  182. br.Msg = "该报告已删除"
  183. br.ErrMsg = "获取报告详情失败,Err:" + err.Error()
  184. return
  185. }
  186. if report == nil {
  187. br.Msg = "报告不存在"
  188. return
  189. }
  190. // 版头版尾
  191. if report.HeadResourceId > 0 || report.EndResourceId > 0 {
  192. headImg, err := models.GetSmartReportResourceById(report.HeadResourceId)
  193. if err != nil && err.Error() != utils.ErrNoRow() {
  194. utils.FileLog.Warn("版头数据获取失败,Err:" + err.Error())
  195. }
  196. endImg, err := models.GetSmartReportResourceById(report.EndResourceId)
  197. if err != nil && err.Error() != utils.ErrNoRow() {
  198. utils.FileLog.Warn("版尾数据获取失败,Err:" + err.Error())
  199. }
  200. if headImg != nil {
  201. report.HeadResource = headImg
  202. }
  203. if endImg != nil {
  204. report.EndResource = endImg
  205. }
  206. }
  207. // 章节
  208. chapters := make([]*models.ReportChapter, 0)
  209. if report.HasChapter == 1 {
  210. chapterList, e := models.GetReportChapterList(report.Id)
  211. if e != nil {
  212. br.Msg = "获取失败"
  213. br.ErrMsg = fmt.Sprintf("获取章节列表失败, %v", e)
  214. return
  215. }
  216. for _, v := range chapterList {
  217. v.Content = html.UnescapeString(v.Content)
  218. }
  219. chapters = chapterList
  220. }
  221. report.ContentSub = html.UnescapeString(report.ContentSub)
  222. report.Content = html.UnescapeString(report.Content)
  223. // 图表授权token
  224. isOpenChartExpired, e := services.CheckIsOpenChartExpired()
  225. if e != nil {
  226. br.Msg = "获取失败"
  227. br.ErrMsg = fmt.Sprintf("获取图表是否开启鉴权失败, %v", e)
  228. return
  229. }
  230. if isOpenChartExpired {
  231. tokenMap := make(map[string]string)
  232. report.Content = services.HandleReportContent(report.Content, "add", tokenMap)
  233. report.ContentSub = services.HandleReportContent(report.ContentSub, "add", tokenMap)
  234. for _, v := range chapters {
  235. v.Content = html.UnescapeString(v.Content)
  236. v.Content = services.HandleReportContent(v.Content, "add", tokenMap)
  237. }
  238. }
  239. resp := new(response.ReportDetailResp)
  240. report.ChapterContent = chapters
  241. resp.Report = report
  242. br.Data = resp
  243. br.Ret = 200
  244. br.Msg = "获取成功"
  245. br.Success = true
  246. }
  247. // OutsideDetail
  248. // @Title 研报详情H5-文档管理库
  249. // @Description 研报详情H5-文档管理库
  250. // @Param ReportId query int true "报告ID"
  251. // @Success 200 {object} models.OutsideReportItem
  252. // @router /outside_detail [get]
  253. func (this *ReportOpenController) OutsideDetail() {
  254. br := new(models.BaseResponse).Init()
  255. defer func() {
  256. this.Data["json"] = br
  257. this.ServeJSON()
  258. }()
  259. reportId, _ := this.GetInt("ReportId")
  260. if reportId <= 0 {
  261. br.Msg = "参数有误"
  262. return
  263. }
  264. outsideReport, e := models.GetOutsideReportById(reportId)
  265. if e != nil {
  266. if e.Error() == utils.ErrNoRow() {
  267. br.Msg = "报告不存在,请刷新页面"
  268. return
  269. }
  270. br.Msg = "操作失败"
  271. br.ErrMsg = fmt.Sprintf("获取外部报告失败, %v", e)
  272. return
  273. }
  274. br.Data = outsideReport.Format2Item()
  275. br.Ret = 200
  276. br.Msg = "获取成功"
  277. br.Success = true
  278. }
  279. // ReadRecord
  280. // @Title 新增报告阅读记录
  281. // @Description 新增报告阅读记录
  282. // @Param request body request.ReportReadRecordReq true "type json string"
  283. // @Success 200 操作成功
  284. // @router /read_record [post]
  285. func (this *ReportController) ReadRecord() {
  286. br := new(models.BaseResponse).Init()
  287. defer func() {
  288. this.Data["json"] = br
  289. this.ServeJSON()
  290. }()
  291. users := this.User
  292. if users == nil {
  293. br.Msg = "请登录"
  294. br.ErrMsg = "请登录,用户信息为空"
  295. br.Ret = 403
  296. return
  297. }
  298. var req request.ReportReadRecordReq
  299. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  300. br.Msg = "参数解析异常"
  301. br.ErrMsg = fmt.Sprintf("参数解析异常, %v", e)
  302. return
  303. }
  304. if req.ReportId <= 0 {
  305. br.Msg = "参数有误"
  306. br.ErrMsg = fmt.Sprintf("参数有误, ReportId: %d", req.ReportId)
  307. return
  308. }
  309. if req.ReportSource != utils.ReportSourceDefault && req.ReportSource != utils.ReportSourceOutside {
  310. br.Msg = "参数有误"
  311. br.ErrMsg = fmt.Sprintf("参数有误, ReportSource: %d", req.ReportSource)
  312. return
  313. }
  314. // 获取报告分类信息
  315. var (
  316. title string
  317. classifyIdFirst int
  318. classifyIdSecond int
  319. classifyIdThird int
  320. classifyNameFirst string
  321. classifyNameSecond string
  322. classifyNameThird string
  323. )
  324. if req.ReportSource == utils.ReportSourceDefault {
  325. reportOb := new(models.Report)
  326. report, e := reportOb.GetItemById(req.ReportId)
  327. if e != nil {
  328. if e.Error() == utils.ErrNoRow() {
  329. br.Msg = "报告不存在,请刷新页面"
  330. return
  331. }
  332. br.Msg = "操作失败"
  333. br.ErrMsg = fmt.Sprintf("获取报告失败, %v", e)
  334. return
  335. }
  336. title = report.Title
  337. classifyIdFirst = report.ClassifyIdFirst
  338. classifyIdSecond = report.ClassifyIdSecond
  339. classifyIdThird = report.ClassifyIdThird
  340. classifyNameFirst = report.ClassifyNameFirst
  341. classifyNameSecond = report.ClassifyNameSecond
  342. classifyNameThird = report.ClassifyNameThird
  343. }
  344. if req.ReportSource == utils.ReportSourceOutside {
  345. outsideReport, e := models.GetOutsideReportById(req.ReportId)
  346. if e != nil {
  347. if e.Error() == utils.ErrNoRow() {
  348. br.Msg = "报告不存在,请刷新页面"
  349. return
  350. }
  351. br.Msg = "操作失败"
  352. br.ErrMsg = fmt.Sprintf("获取外部报告失败, %v", e)
  353. return
  354. }
  355. title = outsideReport.Title
  356. // 根据分类层级取出所有分类ID和名称
  357. classifyOb := new(models.MiniClassify)
  358. classifies, e := classifyOb.GetItemsByCondition(``, make([]interface{}, 0), []string{}, "")
  359. if e != nil {
  360. br.Msg = "操作失败"
  361. br.ErrMsg = fmt.Sprintf("获取报告分类失败, %v", e)
  362. return
  363. }
  364. classifyMapping := make(map[int]*models.MiniClassify)
  365. for _, v := range classifies {
  366. classifyMapping[v.Id] = v
  367. }
  368. thisClassify := classifyMapping[outsideReport.ClassifyId]
  369. if thisClassify == nil {
  370. br.Msg = "操作失败"
  371. br.ErrMsg = fmt.Sprintf("报告分类不存在, ClassifyId: %d", outsideReport.ClassifyId)
  372. return
  373. }
  374. levelArr := strings.Split(thisClassify.LevelPath, ",")
  375. if len(levelArr) <= 0 {
  376. br.Msg = "操作失败"
  377. br.ErrMsg = fmt.Sprintf("报告分类层级异常, ClassifyId: %d, LevelPath: %s", thisClassify.Id, thisClassify.LevelPath)
  378. return
  379. }
  380. if len(levelArr) > 0 {
  381. classifyIdFirst, _ = strconv.Atoi(levelArr[0])
  382. firstClassify := classifyMapping[classifyIdFirst]
  383. if firstClassify != nil {
  384. classifyNameFirst = firstClassify.ClassifyName
  385. }
  386. // 测试环境统一放在了一个一级分类下
  387. if classifyNameFirst == "" && utils.RunMode == "debug" {
  388. classifyNameFirst = "金瑞小程序分类(勿删)"
  389. }
  390. }
  391. if len(levelArr) > 1 {
  392. classifyIdSecond, _ = strconv.Atoi(levelArr[1])
  393. secondClassify := classifyMapping[classifyIdSecond]
  394. if secondClassify != nil {
  395. classifyNameSecond = secondClassify.ClassifyName
  396. }
  397. }
  398. if len(levelArr) > 2 {
  399. classifyIdThird, _ = strconv.Atoi(levelArr[2])
  400. thirdClassify := classifyMapping[classifyIdThird]
  401. if thirdClassify != nil {
  402. classifyNameThird = thirdClassify.ClassifyName
  403. }
  404. }
  405. }
  406. // 记录不存在则新增,存在则更新结束阅读时间
  407. var recordId int
  408. nowTime := time.Now().Local()
  409. if req.RecordId <= 0 {
  410. newRecord := &models.UserReadRecord{
  411. UserId: users.UserId,
  412. ReportId: req.ReportId,
  413. ReportTitle: title,
  414. ClassifyIdFirst: classifyIdFirst,
  415. ClassifyNameFirst: classifyNameFirst,
  416. ClassifyIdSecond: classifyIdSecond,
  417. ClassifyNameSecond: classifyNameSecond,
  418. ClassifyIdThird: classifyIdThird,
  419. ClassifyNameThird: classifyNameThird,
  420. StartTimestamp: int(nowTime.Unix()),
  421. CreateTime: nowTime,
  422. ReportSource: req.ReportSource,
  423. }
  424. if e := newRecord.Create(); e != nil {
  425. br.Msg = "操作失败"
  426. br.ErrMsg = fmt.Sprintf("新增阅读记录失败, %v", e)
  427. return
  428. }
  429. recordId = newRecord.Id
  430. } else {
  431. recordOb := new(models.UserReadRecord)
  432. readRecord, e := recordOb.GetItemById(req.RecordId)
  433. if e != nil {
  434. if e.Error() == utils.ErrNoRow() {
  435. br.Msg = "阅读记录不存在"
  436. return
  437. }
  438. br.Msg = "操作失败"
  439. br.ErrMsg = fmt.Sprintf("获取阅读记录失败, %v", e)
  440. return
  441. }
  442. readRecord.EndTimestamp = int(nowTime.Unix())
  443. if e = readRecord.Update([]string{recordOb.Cols().EndTimestamp}); e != nil {
  444. br.Msg = "操作失败"
  445. br.ErrMsg = fmt.Sprintf("更新阅读记录失败, %v", e)
  446. return
  447. }
  448. recordId = readRecord.Id
  449. // 更新用户阅读次数及最后一次阅读时间
  450. usersOb := new(models.Users)
  451. if e = usersOb.UpdateUserReadTimes(users.UserId); e != nil {
  452. br.Msg = "操作失败"
  453. br.ErrMsg = fmt.Sprintf("更新用户阅读次数失败, %v", e)
  454. return
  455. }
  456. }
  457. resp := new(response.UserReadRecordResp)
  458. resp.RecordId = recordId
  459. br.Data = resp
  460. br.Ret = 200
  461. br.Msg = "操作成功"
  462. br.Success = true
  463. }