123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- package config
- import (
- logger "eta/eta_mini_ht_api/common/component/log"
- "eta/eta_mini_ht_api/common/exception"
- permissionDao "eta/eta_mini_ht_api/models/config"
- )
- type PermissionDTO struct {
- PermissionId int `json:"permissionId"`
- PermissionName string `json:"permissionName"`
- ParentId int `json:"parentId"`
- RiskLevel string `json:"riskLevel"`
- Sort int `json:"sort"`
- }
- type PermissionClassifyMappingDTO struct {
- PermissionId int
- ClassifyId int
- }
- type PermissionNode struct {
- ID int `json:"id"`
- Name string `json:"name"`
- ParentID int `json:"parentId"`
- Children []*PermissionNode `json:"children,omitempty"`
- }
- func assemblePermissionNode(list []PermissionDTO, node *PermissionNode, current int, level int) {
- if node != nil && current < level {
- for _, permission := range list {
- if permission.ParentId == node.ID {
- childNode := &PermissionNode{
- ID: permission.PermissionId,
- Name: permission.PermissionName,
- ParentID: permission.ParentId,
- }
- node.Children = append(node.Children, childNode)
- assemblePermissionNode(list, childNode, current+1, level)
- }
- }
- }
- }
- func GetPermissionListWithRisk() (dtos []PermissionDTO, err error) {
- permissions, err := permissionDao.GetPermissionListWithRisk()
- if err != nil {
- logger.Error("获取设置风险等级的品种列表失败:%v", err)
- }
- for _, permission := range permissions {
- dtos = append(dtos, PermissionDTO{
- PermissionId: permission.PermissionId,
- PermissionName: permission.Name,
- ParentId: permission.ParentId,
- RiskLevel: permission.RiskLevel,
- Sort: permission.Sort,
- })
- }
- return
- }
- func GetPermissionListByIds(permissionIds []int) (dtos []PermissionDTO, err error) {
- permissions, err := permissionDao.GetPermissionListByIds(permissionIds)
- if err != nil {
- logger.Error("通过ID查找品种列表失败:%v", err)
- }
- for _, permission := range permissions {
- dtos = append(dtos, PermissionDTO{
- PermissionId: permission.PermissionId,
- PermissionName: permission.Name,
- ParentId: permission.ParentId,
- RiskLevel: permission.RiskLevel,
- Sort: permission.Sort,
- })
- }
- return
- }
- func GetPermissionList() (root *PermissionNode, err error) {
- list, err := permissionDao.GetPermissionList()
- if err != nil {
- logger.Error("获取品种列表失败:%v", err)
- err = exception.New(exception.GetPermissionListFailed)
- return
- }
- root = &PermissionNode{
- ID: 0,
- ParentID: 0,
- }
- var dtoList []PermissionDTO
- for _, permission := range list {
- dtoList = append(dtoList, PermissionDTO{
- PermissionId: permission.PermissionId,
- PermissionName: permission.Name,
- ParentId: permission.ParentId,
- Sort: permission.Sort,
- })
- }
- assemblePermissionNode(dtoList, root, 0, 2)
- return
- }
- func SyncPermission(list []PermissionDTO) (err error) {
- var permissionList []permissionDao.Permission
- for _, dto := range list {
- permission := convert(dto)
- permissionList = append(permissionList, permission)
- }
- return permissionDao.BatchInsertOrUpdate(permissionList)
- }
- func convert(dto PermissionDTO) permissionDao.Permission {
- return permissionDao.Permission{
- PermissionId: dto.PermissionId,
- Name: dto.PermissionName,
- ParentId: dto.ParentId,
- Sort: dto.Sort,
- }
- }
- func convertMapping(dto PermissionClassifyMappingDTO) permissionDao.PermissionClassifyMapping {
- return permissionDao.PermissionClassifyMapping{
- PermissionID: dto.PermissionId,
- ClassifyID: dto.ClassifyId,
- }
- }
- func SyncPermissionClassifyMapping(list []PermissionClassifyMappingDTO) error {
- var permissionMappingList []permissionDao.PermissionClassifyMapping
- for _, dto := range list {
- permissionMapping := convertMapping(dto)
- permissionMappingList = append(permissionMappingList, permissionMapping)
- }
- return permissionDao.BatchInsertOrUpdateMapping(permissionMappingList)
- }
- func GetPermissionByName(name string) (dto PermissionDTO, err error) {
- permission, err := permissionDao.GetPermissionByName(name)
- if err != nil {
- return
- }
- dto = convertPermissionToDTO(permission)
- return
- }
- func convertPermissionToDTO(permission permissionDao.Permission) PermissionDTO {
- return PermissionDTO{
- PermissionId: permission.PermissionId,
- PermissionName: permission.Name,
- ParentId: permission.ParentId,
- RiskLevel: permission.RiskLevel,
- Sort: permission.Sort,
- }
- }
- func GetSecondPermissionsByClassifyId(id int) (dtoList []PermissionDTO, err error) {
- permissionList, err := permissionDao.GetSecondPermissionsByClassifyID(id)
- if err != nil {
- return
- }
- for _, permission := range permissionList {
- dtoList = append(dtoList, convertPermissionToDTO(permission))
- }
- return
- }
|