report.go 16 KB

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