report.go 15 KB

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