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 }