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 }