chart_permission.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. package controllers
  2. import (
  3. "eta/eta_mini_bridge/models"
  4. "eta/eta_mini_bridge/utils"
  5. "fmt"
  6. "sort"
  7. "strconv"
  8. )
  9. type ChartPermissionController struct {
  10. BaseAuthController
  11. }
  12. // List
  13. // @Title 系统品种一级列表
  14. // @Description 系统品种一级列表
  15. // @Success 200 {object} models.LoginResp
  16. // @router /list [get]
  17. func (this *ChartPermissionController) List() {
  18. br := new(models.BaseResponse).Init()
  19. defer func() {
  20. this.Data["json"] = br
  21. this.ServeJSON()
  22. }()
  23. items, err := models.GetChildChartPermissionListById(0)
  24. if err != nil {
  25. br.Msg = "权限列表获取失败"
  26. br.ErrMsg = "权限列表获取失败,系统错误,Err:" + err.Error()
  27. return
  28. }
  29. br.Ret = 200
  30. br.Data = items
  31. br.Msg = "列表获取成功"
  32. br.Success = true
  33. }
  34. // SecondList
  35. // @Title 系统品种二级列表
  36. // @Description 系统品种二级列表
  37. // @Param chartPermissonId query int true "品种ID"
  38. // @Success 200 {object} []models.ChartPermission
  39. // @router /second/list [get]
  40. func (this *ChartPermissionController) SecondList() {
  41. br := new(models.BaseResponse).Init()
  42. defer func() {
  43. this.Data["json"] = br
  44. this.ServeJSON()
  45. }()
  46. id, _ := this.GetInt("chartPermissonId", 0)
  47. if id <= 0 {
  48. br.Msg = "品种id错误,获取失败"
  49. return
  50. }
  51. items, err := models.GetChildChartPermissionListById(id)
  52. if err != nil {
  53. br.Msg = "权限列表获取失败"
  54. br.ErrMsg = "权限列表获取失败,系统错误,Err:" + err.Error()
  55. return
  56. }
  57. br.Ret = 200
  58. br.Data = items
  59. br.Msg = "列表获取成功"
  60. br.Success = true
  61. }
  62. // AllList
  63. // @Title 系统所有品种列表
  64. // @Description 系统所有品种列表
  65. // @Success 200 {object} []models.ChartPermission
  66. // @router /allList [get]
  67. func (this *ChartPermissionController) AllList() {
  68. br := new(models.BaseResponse).Init()
  69. defer func() {
  70. this.Data["json"] = br
  71. this.ServeJSON()
  72. }()
  73. items, err := models.GetChartPermissionList()
  74. if err != nil {
  75. br.Msg = "权限列表获取失败"
  76. br.ErrMsg = "权限列表获取失败,系统错误,Err:" + err.Error()
  77. return
  78. }
  79. br.Ret = 200
  80. br.Data = items
  81. br.Msg = "列表获取成功"
  82. br.Success = true
  83. }
  84. // Public
  85. // @Title 获取公有共有权限列表
  86. // @Description 获取所有公有权限列表
  87. // @Success 200 {object} []models.ChartPermission
  88. // @router /public/list [get]
  89. func (this *ChartPermissionController) Public() {
  90. br := new(models.BaseResponse).Init()
  91. defer func() {
  92. if err := recover(); err != nil {
  93. fmt.Println(err)
  94. return
  95. }
  96. this.Data["json"] = br
  97. this.ServeJSON()
  98. }()
  99. items, err := models.GetChartPermissionList()
  100. if err != nil {
  101. br.Msg = "权限列表获取失败"
  102. br.ErrMsg = "权限列表获取失败,系统错误,Err:" + err.Error()
  103. return
  104. }
  105. rootMap := make(map[int]*models.ChartPermission)
  106. publicList := make([]*models.ChartPermission, 0)
  107. for _, item := range items {
  108. if item.ParentId == 0 {
  109. rootMap[item.ChartPermissionId] = item
  110. } else if item.IsPublic == 1 {
  111. publicList = append(publicList, item)
  112. }
  113. }
  114. // 公有权限
  115. publicView := getChartPermissionTree(rootMap, publicList)
  116. chartList := make([]*models.ChartPermissionView, 0)
  117. for _, v := range publicView {
  118. chartList = append(chartList, v)
  119. }
  120. sort.Sort(models.BySortChartPermissionView(chartList))
  121. br.Msg = "列表获取成功"
  122. br.Data = chartList
  123. br.Success = true
  124. br.Ret = 200
  125. }
  126. // Private
  127. // @Title 获取私有共有权限列表
  128. // @Description 获取所有私有权限列表
  129. // @Success 200 {object} []models.ChartPermission
  130. // @router /private/list [get]
  131. func (this *ChartPermissionController) Private() {
  132. br := new(models.BaseResponse).Init()
  133. defer func() {
  134. if err := recover(); err != nil {
  135. utils.ApiLog.Error("私有权限列表获取失败,系统错误,Err:%s", err)
  136. return
  137. }
  138. this.Data["json"] = br
  139. this.ServeJSON()
  140. }()
  141. items, err := models.GetChartPermissionList()
  142. if err != nil {
  143. br.Msg = "权限列表获取失败"
  144. br.ErrMsg = "权限列表获取失败,系统错误,Err:" + err.Error()
  145. return
  146. }
  147. rootMap := make(map[int]*models.ChartPermission)
  148. privateList := make([]*models.ChartPermission, 0)
  149. for _, item := range items {
  150. if item.ParentId == 0 {
  151. rootMap[item.ChartPermissionId] = item
  152. } else if item.IsPublic == 0 {
  153. privateList = append(privateList, item)
  154. }
  155. }
  156. // 私有权限
  157. privateView := getChartPermissionTree(rootMap, privateList)
  158. chartList := make([]*models.ChartPermissionView, 0)
  159. for _, v := range privateView {
  160. chartList = append(chartList, v)
  161. }
  162. sort.Sort(models.BySortChartPermissionView(chartList))
  163. br.Msg = "列表获取成功"
  164. br.Data = chartList
  165. br.Success = true
  166. br.Ret = 200
  167. }
  168. // DetailByClassify
  169. // @Title 获取品种权限根据分类id
  170. // @Description 获取品种权限根据分类id
  171. // @Param ClassifyId query int true "分类的id"
  172. // @Param Scope query int true "0:获取二级权限,1:获取一级权限,2:获取所有权限"
  173. // @Success 200 {object} []models.ChartPermission
  174. // @router /classify/detail [get]
  175. func (this *ChartPermissionController) DetailByClassify() {
  176. br := new(models.BaseResponse).Init()
  177. defer func() {
  178. this.Data["json"] = br
  179. this.ServeJSON()
  180. }()
  181. classifyId, _ := this.GetInt("ClassifyId")
  182. if classifyId <= 0 {
  183. br.Msg = "参数错误"
  184. br.ErrMsg = "参数错误,分类id小于等于0"
  185. return
  186. }
  187. scope, _ := this.GetInt("Scope")
  188. reportChartPermissionIds, err := models.GetChartPermissionIdsListByClassifyId(classifyId)
  189. if err != nil {
  190. br.Msg = "获取研报权限失败"
  191. br.ErrMsg = "获取研报权限失败,Err:" + err.Error()
  192. return
  193. }
  194. chartPermissionList, err := models.GetChartPermissionListByStrIds(reportChartPermissionIds)
  195. if err != nil {
  196. br.Msg = "获取研报权限失败"
  197. br.ErrMsg = "获取研报权限失败,Err:" + err.Error()
  198. return
  199. }
  200. if scope == 1 || scope == 2 {
  201. fisrtPermissionIds := make([]string, 0)
  202. for _, chartPermission := range chartPermissionList {
  203. fisrtPermissionIds = append(fisrtPermissionIds, strconv.Itoa(chartPermission.ParentId))
  204. }
  205. fisrtPermissionIds = utils.Unique(fisrtPermissionIds)
  206. firstPermissionList, err := models.GetChartPermissionListByStrIds(fisrtPermissionIds)
  207. if err != nil {
  208. br.Msg = "一级品种获取失败"
  209. br.ErrMsg = "一级品种获取失败,系统错误,Err:" + err.Error()
  210. return
  211. }
  212. if scope == 1 {
  213. chartPermissionList = firstPermissionList
  214. } else if scope == 2 {
  215. chartPermissionList = append(chartPermissionList, firstPermissionList...)
  216. }
  217. }
  218. br.Data = chartPermissionList
  219. br.Msg = "权限列表获取成功"
  220. br.Success = true
  221. br.Ret = 200
  222. }
  223. // Detail
  224. // @Title 获取研报的品种权限列表
  225. // @Description 获取研报的品种权限列表
  226. // @Param ReportId query int true "研报id"
  227. // @Success 200 {object} []models.ChartPermission
  228. // @router /detail [get]
  229. func (this *ChartPermissionController) Detail() {
  230. br := new(models.BaseResponse).Init()
  231. defer func() {
  232. this.Data["json"] = br
  233. this.ServeJSON()
  234. }()
  235. reportId, _ := this.GetInt("ReportId")
  236. if reportId <= 0 {
  237. br.Msg = "参数错误"
  238. br.ErrMsg = "参数错误,报告id小于等于0"
  239. return
  240. }
  241. report, err := models.GetReportById(reportId)
  242. if err != nil && err.Error() != utils.ErrNoRow() {
  243. br.Msg = "该报告已删除"
  244. br.ErrMsg = "获取报告详情失败,Err:" + err.Error()
  245. return
  246. }
  247. if report == nil {
  248. br.Msg = "报告不存在"
  249. return
  250. }
  251. reportChartPermissionIds, err := models.GetChartPermissionIdsListByClassifyId(report.ClassifyIdSecond)
  252. if err != nil {
  253. br.Msg = "获取研报权限失败"
  254. br.ErrMsg = "获取研报权限失败,Err:" + err.Error()
  255. return
  256. }
  257. chartPermissionList, err := models.GetChartPermissionListByStrIds(reportChartPermissionIds)
  258. if err != nil {
  259. br.Msg = "获取研报权限失败"
  260. br.ErrMsg = "获取研报权限失败,Err:" + err.Error()
  261. return
  262. }
  263. br.Data = chartPermissionList
  264. br.Msg = "权限列表获取成功"
  265. br.Success = true
  266. br.Ret = 200
  267. }
  268. // ClassifyTree
  269. // @Title 获取品种下的分类权限树形列表
  270. // @Description 获取研报的品种权限树形列表
  271. // @Param ChartPermissionId query int true "品种权限id"
  272. // @Success 200 {object} []models.ChartPermission
  273. // @router /classify/tree [get]
  274. func (this *ChartPermissionController) ClassifyTree() {
  275. br := new(models.BaseResponse).Init()
  276. defer func() {
  277. this.Data["json"] = br
  278. this.ServeJSON()
  279. }()
  280. chartPermissionId, _ := this.GetInt("ChartPermissionId")
  281. if chartPermissionId <= 0 {
  282. br.Msg = "品种权限错误"
  283. return
  284. }
  285. chartPermission, err := models.GetChartPermissionById(chartPermissionId)
  286. if err != nil {
  287. if err.Error() == utils.ErrNoRow() {
  288. br.Msg = "品种权限不存在"
  289. return
  290. }
  291. br.Msg = "品种权限错误"
  292. br.ErrMsg = "品种权限错误,系统错误,Err:" + err.Error()
  293. return
  294. }
  295. var classifyIds []int
  296. if chartPermission.ParentId == 0 {
  297. // 传过来的是一级品种权限的id,那就获取子品种权限的id,通过子品种权限寻找分类id
  298. childChartPermissionIdsList, err := models.GetChildChartPermissionIdsById(chartPermission.ChartPermissionId)
  299. if err != nil {
  300. br.Msg = "获取品种权限失败"
  301. br.ErrMsg = "获取子品种权限失败,系统错误,Err:" + err.Error()
  302. return
  303. }
  304. classifyIds, err = models.GetClassifyIdsListByIds(childChartPermissionIdsList)
  305. if err != nil {
  306. br.Msg = "获取分类失败"
  307. br.ErrMsg = "获取类别失败,系统错误,Err:" + err.Error()
  308. return
  309. }
  310. } else {
  311. classifyIds, err = models.GetClassifyIdsListById(chartPermission.ChartPermissionId)
  312. if err != nil {
  313. br.Msg = "获取分类失败"
  314. br.ErrMsg = "获取类别失败,系统错误,Err:" + err.Error()
  315. return
  316. }
  317. }
  318. classifyIds = utils.Unique(classifyIds)
  319. classifyList, err := models.GetClassifyListByIds(classifyIds)
  320. if err != nil {
  321. br.Msg = "获取分类失败"
  322. br.ErrMsg = "获取类别信息失败,系统错误,Err:" + err.Error()
  323. return
  324. }
  325. var parentClassifyIds []int
  326. for _, v := range classifyList {
  327. if v.ParentId != 0 {
  328. parentClassifyIds = append(parentClassifyIds, v.ParentId)
  329. }
  330. }
  331. parentClassifyIds = utils.Unique(parentClassifyIds)
  332. parentClassifyList, err := models.GetClassifyListByIds(parentClassifyIds)
  333. if err != nil {
  334. br.Msg = "获取分类失败"
  335. br.ErrMsg = "获取类别信息失败,系统错误,Err:" + err.Error()
  336. return
  337. }
  338. classifyList = getClassifyTree(classifyList, parentClassifyList)
  339. br.Msg = "分类列表获取成功"
  340. br.Data = classifyList
  341. br.Success = true
  342. br.Ret = 200
  343. }
  344. // ClassifyList
  345. // @Title 获取品种下的分类权限列表
  346. // @Description 获取研报的品种权限列表
  347. // @Param ChartPermissionId query int true "品种权限id"
  348. // @Success 200 {object} []models.ChartPermission
  349. // @router /classify/list [get]
  350. func (this *ChartPermissionController) ClassifyList() {
  351. br := new(models.BaseResponse).Init()
  352. defer func() {
  353. this.Data["json"] = br
  354. this.ServeJSON()
  355. }()
  356. chartPermissionId, _ := this.GetInt("ChartPermissionId")
  357. if chartPermissionId <= 0 {
  358. br.Msg = "品种权限错误"
  359. return
  360. }
  361. chartPermission, err := models.GetChartPermissionById(chartPermissionId)
  362. if err != nil {
  363. if err.Error() == utils.ErrNoRow() {
  364. br.Msg = "品种权限不存在"
  365. return
  366. }
  367. br.Msg = "品种权限错误"
  368. br.ErrMsg = "品种权限错误,系统错误,Err:" + err.Error()
  369. return
  370. }
  371. var classifyIds []int
  372. if chartPermission.ParentId == 0 {
  373. // 传过来的是一级品种权限的id,那就获取子品种权限的id,通过子品种权限寻找分类id
  374. childChartPermissionIdsList, err := models.GetChildChartPermissionIdsById(chartPermission.ChartPermissionId)
  375. if err != nil {
  376. br.Msg = "获取品种权限失败"
  377. br.ErrMsg = "获取子品种权限失败,系统错误,Err:" + err.Error()
  378. return
  379. }
  380. classifyIds, err = models.GetClassifyIdsListByIds(childChartPermissionIdsList)
  381. if err != nil {
  382. br.Msg = "获取分类失败"
  383. br.ErrMsg = "获取类别失败,系统错误,Err:" + err.Error()
  384. return
  385. }
  386. } else {
  387. classifyIds, err = models.GetClassifyIdsListById(chartPermission.ChartPermissionId)
  388. if err != nil {
  389. br.Msg = "获取分类失败"
  390. br.ErrMsg = "获取类别失败,系统错误,Err:" + err.Error()
  391. return
  392. }
  393. }
  394. classifyIds = utils.Unique(classifyIds)
  395. classifyList, err := models.GetClassifyListByIds(classifyIds)
  396. if err != nil {
  397. br.Msg = "获取分类失败"
  398. br.ErrMsg = "获取类别信息失败,系统错误,Err:" + err.Error()
  399. return
  400. }
  401. br.Msg = "分类列表获取成功"
  402. br.Data = classifyList
  403. br.Success = true
  404. br.Ret = 200
  405. }
  406. // getChartPermissionTree获得品种权限的树形结构
  407. func getChartPermissionTree(rootMap map[int]*models.ChartPermission, childChartPermissionList []*models.ChartPermission) map[int]*models.ChartPermissionView {
  408. resultMap := make(map[int]*models.ChartPermissionView)
  409. for _, v := range childChartPermissionList {
  410. if perm, ok := resultMap[v.ParentId]; ok {
  411. perm.Child = append(perm.Child, &models.ChartPermissionView{
  412. ChartPermissionId: v.ChartPermissionId,
  413. PermissionName: v.PermissionName,
  414. IsPublic: v.IsPublic,
  415. ParentId: v.ParentId,
  416. Sort: v.Sort,
  417. })
  418. } else {
  419. if root, ok := rootMap[v.ParentId]; ok {
  420. resultMap[v.ParentId] = &models.ChartPermissionView{
  421. ChartPermissionId: root.ChartPermissionId,
  422. PermissionName: root.PermissionName,
  423. IsPublic: root.IsPublic,
  424. Sort: root.Sort,
  425. ParentId: root.ParentId,
  426. Child: make([]*models.ChartPermissionView, 0),
  427. }
  428. resultMap[v.ParentId].Child = append(resultMap[v.ParentId].Child, &models.ChartPermissionView{
  429. ChartPermissionId: v.ChartPermissionId,
  430. PermissionName: v.PermissionName,
  431. IsPublic: v.IsPublic,
  432. ParentId: v.ParentId,
  433. Sort: v.Sort,
  434. })
  435. } else {
  436. utils.ApiLog.Info("私有权限列表获取失败,未找到根节点,权限ID:" + strconv.Itoa(v.ChartPermissionId))
  437. }
  438. }
  439. }
  440. return resultMap
  441. }
  442. func getClassifyTree(classifyList []*models.ClassifyView, parentClassifyList []*models.ClassifyView) []*models.ClassifyView {
  443. for _, v := range parentClassifyList {
  444. for _, v1 := range classifyList {
  445. if v.Id == v1.ParentId {
  446. v.Child = append(v.Child, v1)
  447. }
  448. }
  449. }
  450. return parentClassifyList
  451. }