report.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. package controllers
  2. import (
  3. "eta/eta_mini_bridge/models"
  4. "eta/eta_mini_bridge/models/response"
  5. "eta/eta_mini_bridge/services"
  6. "eta/eta_mini_bridge/utils"
  7. "html"
  8. "strconv"
  9. "time"
  10. "github.com/rdlucklib/rdluck_tools/paging"
  11. )
  12. type ReportController struct {
  13. BaseAuthController
  14. }
  15. // @Title List
  16. // @Description create users
  17. // @Param ChartPermissionId query int true "品种ID"
  18. // @Param Level query int true "品种层级"
  19. // @Param PageSize query int true "每页数据条数"
  20. // @Param CurrentIndex query int true "当前页页码,从1开始"
  21. // @Param RangeType query string true "范围类型,1-一天内,2-一周内,3-半年内"
  22. // @Success 200 {int} models.User.Id
  23. // @Failure 403 body is empty
  24. // @router /list [get]
  25. func (this *ReportController) List() {
  26. br := new(models.BaseResponse).Init()
  27. defer func() {
  28. this.Data["json"] = br
  29. this.ServeJSON()
  30. }()
  31. pageSize, _ := this.GetInt("PageSize")
  32. currentIndex, _ := this.GetInt("CurrentIndex")
  33. chartPermissionId, _ := this.GetInt("ChartPermissionId")
  34. level, _ := this.GetInt("Level")
  35. rangeType, _ := this.GetInt("RangeType")
  36. if chartPermissionId <= 0 {
  37. br.Msg = "品种参数错误"
  38. return
  39. }
  40. var condition string
  41. switch rangeType {
  42. case 1:
  43. condition += ` AND DATE(a.modify_time)=DATE(NOW()) `
  44. case 2:
  45. condition += ` AND DATE(a.modify_time) BETWEEN DATE_SUB(NOW(),INTERVAL 1 WEEK) AND NOW() `
  46. case 3:
  47. condition += ` AND DATE(a.modify_time) BETWEEN DATE_SUB(NOW(),INTERVAL 6 MONTH) AND NOW() `
  48. }
  49. var startSize int
  50. if pageSize <= 0 {
  51. pageSize = utils.PageSize20
  52. }
  53. if currentIndex <= 0 {
  54. currentIndex = 1
  55. }
  56. startSize = utils.StartIndex(currentIndex, pageSize)
  57. var total int
  58. var reportList []*models.ReportList
  59. switch level {
  60. case 2:
  61. classifyIds, err := models.GetClassifyIdsListById(chartPermissionId)
  62. if err != nil {
  63. br.Msg = "获取报告列表失败"
  64. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  65. return
  66. }
  67. if len(classifyIds) == 0 {
  68. br.Msg = "该品种下没有绑定分类"
  69. br.ErrMsg = "获取数据失败,品种id:" + strconv.Itoa(chartPermissionId)
  70. return
  71. }
  72. tmptotal, err := models.GetReportCountByClassifyIds(classifyIds, condition)
  73. if err != nil {
  74. br.Msg = "获取数据失败"
  75. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  76. return
  77. }
  78. total = tmptotal
  79. tmpReportList, err := models.GetReportListByClassifyIds(classifyIds, condition, startSize, pageSize)
  80. if err != nil {
  81. br.Msg = "获取报告列表失败"
  82. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  83. return
  84. }
  85. reportList = tmpReportList
  86. case 1:
  87. chartPermissionIds, err := models.GetChildChartPermissionIdsById(chartPermissionId)
  88. if err != nil {
  89. br.Msg = "获取报告列表失败"
  90. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  91. return
  92. }
  93. if len(chartPermissionIds) == 0 {
  94. br.Msg = "该品种下没有绑定分类"
  95. br.ErrMsg = "获取数据失败,品种id:" + strconv.Itoa(chartPermissionId)
  96. return
  97. }
  98. classifyIds, err := models.GetClassifyIdsListByIds(chartPermissionIds)
  99. if err != nil {
  100. br.Msg = "获取报告列表失败"
  101. br.ErrMsg = "获取报告列表失败,Err:" + err.Error()
  102. return
  103. }
  104. if len(classifyIds) == 0 {
  105. br.Msg = "该品种下没有绑定分类"
  106. return
  107. }
  108. tmptotal, err := models.GetReportCountByClassifyIds(classifyIds, condition)
  109. if err != nil {
  110. br.Msg = "获取数据失败"
  111. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  112. return
  113. }
  114. total = tmptotal
  115. tmpReportList, err := models.GetReportListByClassifyIds(classifyIds, condition, startSize, pageSize)
  116. if err != nil {
  117. br.Msg = "获取报告列表失败"
  118. br.ErrMsg = "获取报告列表失败,Err:" + err.Error()
  119. return
  120. }
  121. reportList = tmpReportList
  122. default:
  123. br.Msg = "层级参数错误"
  124. return
  125. }
  126. page := paging.GetPaging(currentIndex, pageSize, total)
  127. resp := new(response.ReportListResp)
  128. resp.Paging = page
  129. resp.List = reportList
  130. br.Ret = 200
  131. br.Success = true
  132. br.Msg = "获取成功"
  133. br.Data = resp
  134. }
  135. // @Title 研报详情
  136. // @Description 研报详情接口
  137. // @Param ReportId query int true "报告id"
  138. // @Param UserId query int true "用户id"
  139. // @Success 200 {object} models.ReportDetailResp
  140. // @router /detail [get]
  141. func (this *ReportController) Detail() {
  142. br := new(models.BaseResponse).Init()
  143. defer func() {
  144. this.Data["json"] = br
  145. this.ServeJSON()
  146. }()
  147. reportId, err := this.GetInt("ReportId")
  148. userId, err := this.GetInt("UserId")
  149. if err != nil {
  150. br.Msg = "参数获取失败"
  151. br.ErrMsg = "参数获取失败,Err:" + err.Error()
  152. return
  153. }
  154. if reportId <= 0 {
  155. br.Msg = "参数错误"
  156. br.ErrMsg = "参数错误,报告id小于等于0"
  157. return
  158. }
  159. if userId <= 0 {
  160. br.Msg = "参数错误"
  161. br.ErrMsg = "参数错误,用户id小于等于0"
  162. return
  163. }
  164. user, err := models.GetUserById(userId)
  165. if err != nil {
  166. br.Msg = "用户不存在"
  167. br.ErrMsg = "用户不存在,系统异常,Err:" + err.Error()
  168. return
  169. }
  170. // 有效期是否到期
  171. var IsVail bool
  172. if user.Status == utils.UserStatusFormal {
  173. if user.ValidEndTime.After(time.Now()) && user.ValidStartTime.Before(time.Now()) {
  174. IsVail = true
  175. } else {
  176. IsVail = false
  177. user.Status = utils.UserStatusNo
  178. }
  179. }
  180. report, err := models.GetReportById(reportId)
  181. if err != nil {
  182. if err.Error() == utils.ErrNoRow() {
  183. br.Ret = 200
  184. br.Data = new(response.ReportDetailResp)
  185. br.Success = true
  186. br.Msg = "该报告已删除或不存在"
  187. return
  188. }
  189. br.Msg = "该报告已删除"
  190. br.ErrMsg = "获取报告详情失败,Err:" + err.Error()
  191. return
  192. }
  193. report.ContentSub = html.UnescapeString(report.ContentSub)
  194. report.Content = html.UnescapeString(report.Content)
  195. if report == nil {
  196. br.Msg = "报告不存在"
  197. return
  198. }
  199. reportChartPermissionIds, err := models.GetChartPermissionIdsListByClassifyId(report.ClassifyIdSecond)
  200. if err != nil {
  201. br.Msg = "获取研报权限失败"
  202. br.ErrMsg = "获取研报权限失败,Err:" + err.Error()
  203. return
  204. }
  205. chartPermissionList, err := models.GetChartPermissionIdsByIds(reportChartPermissionIds)
  206. if err != nil {
  207. br.Msg = "获取研报权限失败"
  208. br.ErrMsg = "获取研报权限失败,Err:" + err.Error()
  209. return
  210. }
  211. var IsHas bool
  212. var IsPublic bool
  213. for _, v := range chartPermissionList {
  214. if v.IsPublic == 1 {
  215. IsPublic = true
  216. break
  217. }
  218. }
  219. resp := new(response.ReportDetailResp)
  220. if !IsPublic {
  221. // 如果被禁用或是潜在用户,直接返回无阅读报告权限
  222. if user.Status == utils.UserStatusNo || user.Status == utils.UserStatusPotential {
  223. resp.Report = report
  224. resp.Status = utils.ReportPermissionStatusNo
  225. report.Content = ""
  226. br.Ret = 408
  227. br.Data = resp
  228. br.Msg = "用户权限不足"
  229. return
  230. }
  231. // 如果是私有报告,用户权限过期直接返回有效期已过
  232. if !IsVail {
  233. resp.Report = report
  234. resp.Status = utils.ReportPermissionStatusExpired
  235. report.Content = ""
  236. br.Ret = 408
  237. br.Data = resp
  238. br.Msg = "用户权限不足"
  239. return
  240. }
  241. chartPermissionIds, err := models.GetChartPermissionIdByUserId(userId)
  242. if err != nil {
  243. br.Msg = "获取用户权限失败"
  244. br.ErrMsg = "获取用户权限失败,Err:" + err.Error()
  245. return
  246. }
  247. if len(chartPermissionIds) <= 0 {
  248. resp.Report = report
  249. resp.Status = utils.ReportPermissionStatusNo
  250. report.Content = ""
  251. br.Ret = 408
  252. br.Data = resp
  253. br.Msg = "用户权限不足"
  254. return
  255. }
  256. classifyIds, err := models.GetClassifyIdsListByIds(chartPermissionIds)
  257. if err != nil {
  258. br.Msg = "获取用户权限失败"
  259. br.ErrMsg = "获取用户分类权限失败,Err:" + err.Error()
  260. return
  261. }
  262. if len(classifyIds) <= 0 {
  263. resp.Report = report
  264. resp.Status = utils.ReportPermissionStatusNoPermission
  265. report.Content = ""
  266. br.Ret = 408
  267. br.Data = resp
  268. br.Msg = "用户权限不足"
  269. return
  270. }
  271. reportClassifyIdStr := strconv.Itoa(report.ClassifyIdSecond)
  272. for _, v := range classifyIds {
  273. if v == reportClassifyIdStr {
  274. IsHas = true
  275. }
  276. }
  277. if !IsHas {
  278. resp.Report = report
  279. resp.Status = utils.ReportPermissionStatusNoPermission
  280. report.Content = ""
  281. br.Ret = 408
  282. br.Data = resp
  283. br.Msg = "用户权限不足"
  284. return
  285. }
  286. } else {
  287. report.IsPublic = IsPublic
  288. }
  289. resp.Report = report
  290. resp.Status = utils.ReportPermissionStatusHas
  291. br.Data = resp
  292. br.Ret = 200
  293. br.Success = true
  294. br.Msg = "获取成功"
  295. }
  296. // @Title List
  297. // @Description create users
  298. // @Param body body models.User true "body for user content"
  299. // @Success 200 {int} models.User.Id
  300. // @Failure 403 body is empty
  301. // @router /daily/list [get]
  302. func (this *ReportController) Today() {
  303. br := new(models.BaseResponse).Init()
  304. defer func() {
  305. this.Data["json"] = br
  306. this.ServeJSON()
  307. }()
  308. pageSize, _ := this.GetInt("PageSize")
  309. currentIndex, _ := this.GetInt("CurrentIndex")
  310. var startSize int
  311. if pageSize <= 0 {
  312. pageSize = utils.PageSize30
  313. }
  314. if currentIndex <= 0 {
  315. currentIndex = 1
  316. }
  317. startSize = utils.StartIndex(currentIndex, pageSize)
  318. total, err := models.GetReportDailyListCount()
  319. if err != nil {
  320. br.Msg = "获取数据失败"
  321. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  322. return
  323. }
  324. list, err := models.GetReportDailyList(startSize, pageSize)
  325. if err != nil {
  326. br.Msg = "获取失败"
  327. br.ErrMsg = "获取失败,Err:" + err.Error()
  328. return
  329. }
  330. classifyIds := make([]string, 0)
  331. for _, v := range list {
  332. classifyIds = append(classifyIds, strconv.Itoa(v.ClassifyIdSecond))
  333. }
  334. classifyIds = utils.Unique(classifyIds)
  335. // 获取二级分类和二级品种权限的映射
  336. chartPermissionMapping, err := models.GetChartPermissionIdsListByClassifyIds(classifyIds)
  337. if err != nil {
  338. br.Msg = "获取研报权限失败"
  339. br.ErrMsg = "获取研报权限失败,Err:" + err.Error()
  340. return
  341. }
  342. classifyToPermissionMap2 := make(map[int][]int)
  343. chartPermissionIds := make([]string, 0)
  344. for _, v := range chartPermissionMapping {
  345. classifyToPermissionMap2[v.ClassifyId] = append(classifyToPermissionMap2[v.ClassifyId], v.ChartPermissionId)
  346. chartPermissionIds = append(chartPermissionIds, strconv.Itoa(v.ChartPermissionId))
  347. }
  348. // 获取二级品种的权限,并建立映射
  349. chartPermissionList2, err := models.GetParentChartPermissionListByIds(chartPermissionIds)
  350. if err != nil {
  351. br.Msg = "获取研报二级品种权限失败"
  352. br.ErrMsg = "获取研报二级品种权限失败,Err:" + err.Error()
  353. return
  354. }
  355. chartPermissionViewMap2 := make(map[int]*models.ChartPermission)
  356. for _, v := range chartPermissionList2 {
  357. chartPermissionViewMap2[v.ChartPermissionId] = v
  358. }
  359. // 获取一级品种的权限,并建立映射
  360. chartPermissionList1, err := models.GetChildChartPermissionListById(0)
  361. if err != nil {
  362. br.Msg = "获取研报一级品种权限失败"
  363. br.ErrMsg = "获取研报一级品种权限失败,Err:" + err.Error()
  364. return
  365. }
  366. chartPermissionMap1 := make(map[int]*models.ChartPermission)
  367. for _, v := range chartPermissionList1 {
  368. chartPermissionMap1[v.ChartPermissionId] = v
  369. }
  370. // 组合数据
  371. for _, v := range list {
  372. var permissionNames []string
  373. for _, vv := range classifyToPermissionMap2[v.ClassifyIdSecond] {
  374. parent2 := chartPermissionViewMap2[vv].ParentId
  375. permissionNames = append(permissionNames, chartPermissionMap1[parent2].PermissionName)
  376. }
  377. v.PermissionNames = utils.Unique(permissionNames)
  378. }
  379. page := paging.GetPaging(currentIndex, pageSize, total)
  380. resp := new(response.ReportListResp)
  381. resp.Paging = page
  382. resp.List = list
  383. br.Ret = 200
  384. br.Success = true
  385. br.Msg = "获取成功"
  386. br.Data = resp
  387. }
  388. // @Title 研报搜索
  389. // @Description 研报搜索
  390. // @Param PageSize query int true "每页数据条数"
  391. // @Param CurrentIndex query int true "当前页页码,从1开始"
  392. // @Param KeyWord query string true "关键字"
  393. // @Success 200 {object} models.ReportDetailResp
  394. // @router /search [get]
  395. func (this *ReportController) Search() {
  396. br := new(models.BaseResponse).Init()
  397. defer func() {
  398. this.Data["json"] = br
  399. this.ServeJSON()
  400. }()
  401. keyWord := this.GetString("KeyWord")
  402. pageSize, _ := this.GetInt("PageSize")
  403. currentIndex, _ := this.GetInt("CurrentIndex")
  404. if pageSize <= 0 {
  405. pageSize = utils.PageSize30
  406. }
  407. if currentIndex <= 0 {
  408. currentIndex = 1
  409. }
  410. if keyWord == "" {
  411. br.Msg = "请输入关键字"
  412. return
  413. }
  414. reportList, err, errMsg := services.SearchReport(keyWord, currentIndex, pageSize)
  415. if err != nil {
  416. br.Msg = "研报搜索失败"
  417. br.ErrMsg = errMsg + ",Err:" + err.Error()
  418. return
  419. }
  420. br.Data = reportList
  421. br.Msg = "查询成功"
  422. br.Ret = 200
  423. br.Success = true
  424. }