report.go 13 KB

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