123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508 |
- package controllers
- import (
- "eta/eta_mini_bridge/models"
- "eta/eta_mini_bridge/services"
- "eta/eta_mini_bridge/utils"
- "fmt"
- "sort"
- "strconv"
- )
- type ChartPermissionController struct {
- BaseAuthController
- }
- // List
- // @Title 系统品种一级列表
- // @Description 系统品种一级列表
- // @Success 200 {object} models.LoginResp
- // @router /list [get]
- func (this *ChartPermissionController) List() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- items, err := models.GetChildChartPermissionListById(0)
- if err != nil {
- br.Msg = "权限列表获取失败"
- br.ErrMsg = "权限列表获取失败,系统错误,Err:" + err.Error()
- return
- }
- br.Ret = 200
- br.Data = items
- br.Msg = "列表获取成功"
- br.Success = true
- }
- // SecondList
- // @Title 系统品种二级列表
- // @Description 系统品种二级列表
- // @Param chartPermissonId query int true "品种ID"
- // @Success 200 {object} []models.ChartPermission
- // @router /second/list [get]
- func (this *ChartPermissionController) SecondList() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- id, _ := this.GetInt("chartPermissonId", 0)
- if id <= 0 {
- br.Msg = "品种id错误,获取失败"
- return
- }
- items, err := models.GetChildChartPermissionListById(id)
- if err != nil {
- br.Msg = "权限列表获取失败"
- br.ErrMsg = "权限列表获取失败,系统错误,Err:" + err.Error()
- return
- }
- br.Ret = 200
- br.Data = items
- br.Msg = "列表获取成功"
- br.Success = true
- }
- // AllList
- // @Title 系统所有品种列表
- // @Description 系统所有品种列表
- // @Success 200 {object} []models.ChartPermission
- // @router /allList [get]
- func (this *ChartPermissionController) AllList() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- items, err := models.GetChartPermissionList()
- if err != nil {
- br.Msg = "权限列表获取失败"
- br.ErrMsg = "权限列表获取失败,系统错误,Err:" + err.Error()
- return
- }
- br.Ret = 200
- br.Data = items
- br.Msg = "列表获取成功"
- br.Success = true
- }
- // Public
- // @Title 获取公有共有权限列表
- // @Description 获取所有公有权限列表
- // @Success 200 {object} []models.ChartPermission
- // @router /public/list [get]
- func (this *ChartPermissionController) Public() {
- br := new(models.BaseResponse).Init()
- defer func() {
- if err := recover(); err != nil {
- fmt.Println(err)
- return
- }
- this.Data["json"] = br
- this.ServeJSON()
- }()
- items, err := models.GetChartPermissionList()
- if err != nil {
- br.Msg = "权限列表获取失败"
- br.ErrMsg = "权限列表获取失败,系统错误,Err:" + err.Error()
- return
- }
- rootMap := make(map[int]*models.ChartPermission)
- publicList := make([]*models.ChartPermission, 0)
- for _, item := range items {
- if item.ParentId == 0 {
- rootMap[item.ChartPermissionId] = item
- } else if item.IsPublic == 1 {
- publicList = append(publicList, item)
- }
- }
- // 公有权限
- publicView := getChartPermissionTree(rootMap, publicList)
- chartList := make([]*models.ChartPermissionView, 0)
- for _, v := range publicView {
- chartList = append(chartList, v)
- }
- sort.Sort(models.BySortChartPermissionView(chartList))
- br.Msg = "列表获取成功"
- br.Data = chartList
- br.Success = true
- br.Ret = 200
- }
- // Private
- // @Title 获取私有共有权限列表
- // @Description 获取所有私有权限列表
- // @Success 200 {object} []models.ChartPermission
- // @router /private/list [get]
- func (this *ChartPermissionController) Private() {
- br := new(models.BaseResponse).Init()
- defer func() {
- if err := recover(); err != nil {
- utils.ApiLog.Error("私有权限列表获取失败,系统错误,Err:%s", err)
- return
- }
- this.Data["json"] = br
- this.ServeJSON()
- }()
- items, err := models.GetChartPermissionList()
- if err != nil {
- br.Msg = "权限列表获取失败"
- br.ErrMsg = "权限列表获取失败,系统错误,Err:" + err.Error()
- return
- }
- rootMap := make(map[int]*models.ChartPermission)
- privateList := make([]*models.ChartPermission, 0)
- for _, item := range items {
- if item.ParentId == 0 {
- rootMap[item.ChartPermissionId] = item
- } else if item.IsPublic == 0 {
- privateList = append(privateList, item)
- }
- }
- // 私有权限
- privateView := getChartPermissionTree(rootMap, privateList)
- chartList := make([]*models.ChartPermissionView, 0)
- for _, v := range privateView {
- chartList = append(chartList, v)
- }
- sort.Sort(models.BySortChartPermissionView(chartList))
- br.Msg = "列表获取成功"
- br.Data = chartList
- br.Success = true
- br.Ret = 200
- }
- // DetailByClassify
- // @Title 获取品种权限根据分类id
- // @Description 获取品种权限根据分类id
- // @Param ClassifyId query int true "分类的id"
- // @Param Scope query int true "0:获取二级权限,1:获取一级权限,2:获取所有权限"
- // @Success 200 {object} []models.ChartPermission
- // @router /classify/detail [get]
- func (this *ChartPermissionController) DetailByClassify() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- classifyId, _ := this.GetInt("ClassifyId")
- if classifyId <= 0 {
- br.Msg = "参数错误"
- br.ErrMsg = "参数错误,分类id小于等于0"
- return
- }
- scope, _ := this.GetInt("Scope")
- reportChartPermissionIds, err := models.GetChartPermissionIdsListByClassifyId(classifyId)
- if err != nil {
- br.Msg = "获取研报权限失败"
- br.ErrMsg = "获取研报权限失败,Err:" + err.Error()
- return
- }
- chartPermissionList, err := models.GetChartPermissionListByStrIds(reportChartPermissionIds)
- if err != nil {
- br.Msg = "获取研报权限失败"
- br.ErrMsg = "获取研报权限失败,Err:" + err.Error()
- return
- }
- if scope == 1 || scope == 2 {
- fisrtPermissionIds := make([]string, 0)
- for _, chartPermission := range chartPermissionList {
- fisrtPermissionIds = append(fisrtPermissionIds, strconv.Itoa(chartPermission.ParentId))
- }
- fisrtPermissionIds = utils.Unique(fisrtPermissionIds)
- firstPermissionList, err := models.GetChartPermissionListByStrIds(fisrtPermissionIds)
- if err != nil {
- br.Msg = "一级品种获取失败"
- br.ErrMsg = "一级品种获取失败,系统错误,Err:" + err.Error()
- return
- }
- if scope == 1 {
- chartPermissionList = firstPermissionList
- } else if scope == 2 {
- chartPermissionList = append(chartPermissionList, firstPermissionList...)
- }
- }
- br.Data = chartPermissionList
- br.Msg = "权限列表获取成功"
- br.Success = true
- br.Ret = 200
- }
- // Detail
- // @Title 获取研报的品种权限列表
- // @Description 获取研报的品种权限列表
- // @Param ReportId query int true "研报id"
- // @Success 200 {object} []models.ChartPermission
- // @router /detail [get]
- func (this *ChartPermissionController) Detail() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- reportId, _ := this.GetInt("ReportId")
- if reportId <= 0 {
- br.Msg = "参数错误"
- br.ErrMsg = "参数错误,报告id小于等于0"
- return
- }
- report, err := models.GetReportById(reportId)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "该报告已删除"
- br.ErrMsg = "获取报告详情失败,Err:" + err.Error()
- return
- }
- if report == nil {
- br.Msg = "报告不存在"
- return
- }
- reportChartPermissionIds, err := models.GetChartPermissionIdsListByClassifyId(report.ClassifyIdSecond)
- if err != nil {
- br.Msg = "获取研报权限失败"
- br.ErrMsg = "获取研报权限失败,Err:" + err.Error()
- return
- }
- chartPermissionList, err := models.GetChartPermissionListByStrIds(reportChartPermissionIds)
- if err != nil {
- br.Msg = "获取研报权限失败"
- br.ErrMsg = "获取研报权限失败,Err:" + err.Error()
- return
- }
- br.Data = chartPermissionList
- br.Msg = "权限列表获取成功"
- br.Success = true
- br.Ret = 200
- }
- // ClassifyTree
- // @Title 获取品种下的分类权限树形列表
- // @Description 获取研报的品种权限树形列表
- // @Param ChartPermissionId query int true "品种权限id"
- // @Success 200 {object} []models.ChartPermission
- // @router /classify/tree [get]
- func (this *ChartPermissionController) ClassifyTree() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- chartPermissionId, _ := this.GetInt("ChartPermissionId")
- if chartPermissionId < 0 {
- br.Msg = "品种权限错误"
- return
- }
- var finalList []*models.ClassifyView
- if chartPermissionId == 0 {
- // 如果没有品种id那么返回所有分类
- classifyList, err := models.GetClassifyEnabledList()
- if err != nil {
- br.Msg = "获取分类失败"
- br.ErrMsg = "获取分类失败,系统错误,Err:" + err.Error()
- return
- }
- finalList = classifyList
- } else {
- chartPermission, err := models.GetChartPermissionById(chartPermissionId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- br.Msg = "品种权限不存在"
- return
- }
- br.Msg = "品种权限错误"
- br.ErrMsg = "品种权限错误,系统错误,Err:" + err.Error()
- return
- }
- var classifyIds []int
- if chartPermission.ParentId == 0 {
- // 传过来的是一级品种权限的id,那就获取子品种权限的id,通过子品种权限寻找分类id
- childChartPermissionIdsList, err := models.GetChildChartPermissionIdsById(chartPermission.ChartPermissionId)
- if err != nil {
- br.Msg = "获取品种权限失败"
- br.ErrMsg = "获取子品种权限失败,系统错误,Err:" + err.Error()
- return
- }
- classifyIds, err = models.GetClassifyIdsListByIds(childChartPermissionIdsList)
- if err != nil {
- br.Msg = "获取分类失败"
- br.ErrMsg = "获取类别失败,系统错误,Err:" + err.Error()
- return
- }
- } else {
- classifyIds, err = models.GetClassifyIdsListById(chartPermission.ChartPermissionId)
- if err != nil {
- br.Msg = "获取分类失败"
- br.ErrMsg = "获取类别失败,系统错误,Err:" + err.Error()
- return
- }
- }
- classifyList, err := models.GetClassifyListByIds(classifyIds)
- if err != nil {
- br.Msg = "获取分类失败"
- br.ErrMsg = "获取类别信息失败,系统错误,Err:" + err.Error()
- return
- }
- var parentClassifyIds []int
- for _, v := range classifyList {
- if v.ParentId != 0 {
- parentClassifyIds = append(parentClassifyIds, v.ParentId)
- }
- }
- parentClassifyList, err := models.GetClassifyListByIds(parentClassifyIds)
- if err != nil {
- br.Msg = "获取分类失败"
- br.ErrMsg = "获取类别信息失败,系统错误,Err:" + err.Error()
- return
- }
- var rootClassifyIds []int
- for _, v := range parentClassifyList {
- if v.ParentId != 0 {
- rootClassifyIds = append(rootClassifyIds, v.ParentId)
- }
- }
- classifyIds = append(classifyIds, parentClassifyIds...)
- classifyIds = append(classifyIds, rootClassifyIds...)
- classifyIds = utils.Unique(classifyIds)
- finalList, err = models.GetClassifyListByIds(classifyIds)
- if err != nil {
- br.Msg = "获取分类失败"
- br.ErrMsg = "获取类别信息失败,系统错误,Err:" + err.Error()
- return
- }
- }
- finalList = services.GetClassifyTree(finalList, 0)
- br.Msg = "分类列表获取成功"
- br.Data = finalList
- br.Success = true
- br.Ret = 200
- }
- // ClassifyList
- // @Title 获取品种下的分类权限列表
- // @Description 获取研报的品种权限列表
- // @Param ChartPermissionId query int true "品种权限id"
- // @Success 200 {object} []models.ChartPermission
- // @router /classify/list [get]
- func (this *ChartPermissionController) ClassifyList() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- chartPermissionId, _ := this.GetInt("ChartPermissionId")
- if chartPermissionId <= 0 {
- br.Msg = "品种权限错误"
- return
- }
- chartPermission, err := models.GetChartPermissionById(chartPermissionId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- br.Msg = "品种权限不存在"
- return
- }
- br.Msg = "品种权限错误"
- br.ErrMsg = "品种权限错误,系统错误,Err:" + err.Error()
- return
- }
- var classifyIds []int
- if chartPermission.ParentId == 0 {
- // 传过来的是一级品种权限的id,那就获取子品种权限的id,通过子品种权限寻找分类id
- childChartPermissionIdsList, err := models.GetChildChartPermissionIdsById(chartPermission.ChartPermissionId)
- if err != nil {
- br.Msg = "获取品种权限失败"
- br.ErrMsg = "获取子品种权限失败,系统错误,Err:" + err.Error()
- return
- }
- classifyIds, err = models.GetClassifyIdsListByIds(childChartPermissionIdsList)
- if err != nil {
- br.Msg = "获取分类失败"
- br.ErrMsg = "获取类别失败,系统错误,Err:" + err.Error()
- return
- }
- } else {
- classifyIds, err = models.GetClassifyIdsListById(chartPermission.ChartPermissionId)
- if err != nil {
- br.Msg = "获取分类失败"
- br.ErrMsg = "获取类别失败,系统错误,Err:" + err.Error()
- return
- }
- }
- classifyIds = utils.Unique(classifyIds)
- classifyList, err := models.GetClassifyListByIds(classifyIds)
- if err != nil {
- br.Msg = "获取分类失败"
- br.ErrMsg = "获取类别信息失败,系统错误,Err:" + err.Error()
- return
- }
- br.Msg = "分类列表获取成功"
- br.Data = classifyList
- br.Success = true
- br.Ret = 200
- }
- // getChartPermissionTree获得品种权限的树形结构
- func getChartPermissionTree(rootMap map[int]*models.ChartPermission, childChartPermissionList []*models.ChartPermission) map[int]*models.ChartPermissionView {
- resultMap := make(map[int]*models.ChartPermissionView)
- for _, v := range childChartPermissionList {
- if perm, ok := resultMap[v.ParentId]; ok {
- perm.Child = append(perm.Child, &models.ChartPermissionView{
- ChartPermissionId: v.ChartPermissionId,
- PermissionName: v.PermissionName,
- IsPublic: v.IsPublic,
- ParentId: v.ParentId,
- Sort: v.Sort,
- })
- } else {
- if root, ok := rootMap[v.ParentId]; ok {
- resultMap[v.ParentId] = &models.ChartPermissionView{
- ChartPermissionId: root.ChartPermissionId,
- PermissionName: root.PermissionName,
- IsPublic: root.IsPublic,
- Sort: root.Sort,
- ParentId: root.ParentId,
- Child: make([]*models.ChartPermissionView, 0),
- }
- resultMap[v.ParentId].Child = append(resultMap[v.ParentId].Child, &models.ChartPermissionView{
- ChartPermissionId: v.ChartPermissionId,
- PermissionName: v.PermissionName,
- IsPublic: v.IsPublic,
- ParentId: v.ParentId,
- Sort: v.Sort,
- })
- } else {
- utils.ApiLog.Info("私有权限列表获取失败,未找到根节点,权限ID:" + strconv.Itoa(v.ChartPermissionId))
- }
- }
- }
- return resultMap
- }
- func getClassifyTree(classifyList []*models.ClassifyView, parentClassifyList []*models.ClassifyView) []*models.ClassifyView {
- for _, v := range parentClassifyList {
- for _, v1 := range classifyList {
- if v.Id == v1.ParentId {
- v.Child = append(v.Child, v1)
- }
- }
- }
- return parentClassifyList
- }
|