chart_permission.go 15 KB

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