permission_service.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package config
  2. import (
  3. logger "eta/eta_mini_ht_api/common/component/log"
  4. "eta/eta_mini_ht_api/common/exception"
  5. permissionDao "eta/eta_mini_ht_api/models/config"
  6. )
  7. type PermissionDTO struct {
  8. PermissionId int `json:"permissionId"`
  9. PermissionName string `json:"permissionName"`
  10. ParentId int `json:"parentId"`
  11. RiskLevel string `json:"riskLevel"`
  12. Sort int `json:"sort"`
  13. }
  14. type PermissionClassifyMappingDTO struct {
  15. PermissionId int
  16. ClassifyId int
  17. }
  18. type PermissionNode struct {
  19. ID int `json:"id"`
  20. Name string `json:"name"`
  21. ParentID int `json:"parentId"`
  22. Children []*PermissionNode `json:"children,omitempty"`
  23. }
  24. func assemblePermissionNode(list []PermissionDTO, node *PermissionNode, current int, level int) {
  25. if node != nil && current < level {
  26. for _, permission := range list {
  27. if permission.ParentId == node.ID {
  28. childNode := &PermissionNode{
  29. ID: permission.PermissionId,
  30. Name: permission.PermissionName,
  31. ParentID: permission.ParentId,
  32. }
  33. node.Children = append(node.Children, childNode)
  34. assemblePermissionNode(list, childNode, current+1, level)
  35. }
  36. }
  37. }
  38. }
  39. func GetPermissionListWithRisk() (dtos []PermissionDTO, err error) {
  40. permissions, err := permissionDao.GetPermissionListWithRisk()
  41. if err != nil {
  42. logger.Error("获取设置风险等级的品种列表失败:%v", err)
  43. }
  44. for _, permission := range permissions {
  45. dtos = append(dtos, PermissionDTO{
  46. PermissionId: permission.PermissionId,
  47. PermissionName: permission.Name,
  48. ParentId: permission.ParentId,
  49. RiskLevel: permission.RiskLevel,
  50. Sort: permission.Sort,
  51. })
  52. }
  53. return
  54. }
  55. func GetPermissionListByIds(permissionIds []int) (dtos []PermissionDTO, err error) {
  56. permissions, err := permissionDao.GetPermissionListByIds(permissionIds)
  57. if err != nil {
  58. logger.Error("通过ID查找品种列表失败:%v", err)
  59. }
  60. for _, permission := range permissions {
  61. dtos = append(dtos, PermissionDTO{
  62. PermissionId: permission.PermissionId,
  63. PermissionName: permission.Name,
  64. ParentId: permission.ParentId,
  65. RiskLevel: permission.RiskLevel,
  66. Sort: permission.Sort,
  67. })
  68. }
  69. return
  70. }
  71. func GetPermissionList() (root *PermissionNode, err error) {
  72. list, err := permissionDao.GetPermissionList()
  73. if err != nil {
  74. logger.Error("获取品种列表失败:%v", err)
  75. err = exception.New(exception.GetPermissionListFailed)
  76. return
  77. }
  78. root = &PermissionNode{
  79. ID: 0,
  80. ParentID: 0,
  81. }
  82. var dtoList []PermissionDTO
  83. for _, permission := range list {
  84. dtoList = append(dtoList, PermissionDTO{
  85. PermissionId: permission.PermissionId,
  86. PermissionName: permission.Name,
  87. ParentId: permission.ParentId,
  88. Sort: permission.Sort,
  89. })
  90. }
  91. assemblePermissionNode(dtoList, root, 0, 2)
  92. return
  93. }
  94. func SyncPermission(list []PermissionDTO) (err error) {
  95. var permissionList []permissionDao.Permission
  96. for _, dto := range list {
  97. permission := convert(dto)
  98. permissionList = append(permissionList, permission)
  99. }
  100. return permissionDao.BatchInsertOrUpdate(permissionList)
  101. }
  102. func convert(dto PermissionDTO) permissionDao.Permission {
  103. return permissionDao.Permission{
  104. PermissionId: dto.PermissionId,
  105. Name: dto.PermissionName,
  106. ParentId: dto.ParentId,
  107. Sort: dto.Sort,
  108. }
  109. }
  110. func convertMapping(dto PermissionClassifyMappingDTO) permissionDao.PermissionClassifyMapping {
  111. return permissionDao.PermissionClassifyMapping{
  112. PermissionID: dto.PermissionId,
  113. ClassifyID: dto.ClassifyId,
  114. }
  115. }
  116. func SyncPermissionClassifyMapping(list []PermissionClassifyMappingDTO) error {
  117. var permissionMappingList []permissionDao.PermissionClassifyMapping
  118. for _, dto := range list {
  119. permissionMapping := convertMapping(dto)
  120. permissionMappingList = append(permissionMappingList, permissionMapping)
  121. }
  122. return permissionDao.BatchInsertOrUpdateMapping(permissionMappingList)
  123. }
  124. func GetPermissionByName(name string) (dto PermissionDTO, err error) {
  125. permission, err := permissionDao.GetPermissionByName(name)
  126. if err != nil {
  127. return
  128. }
  129. dto = convertPermissionToDTO(permission)
  130. return
  131. }
  132. func convertPermissionToDTO(permission permissionDao.Permission) PermissionDTO {
  133. return PermissionDTO{
  134. PermissionId: permission.PermissionId,
  135. PermissionName: permission.Name,
  136. ParentId: permission.ParentId,
  137. RiskLevel: permission.RiskLevel,
  138. Sort: permission.Sort,
  139. }
  140. }
  141. func GetSecondPermissionsByClassifyId(id int) (dtoList []PermissionDTO, err error) {
  142. permissionList, err := permissionDao.GetSecondPermissionsByClassifyID(id)
  143. if err != nil {
  144. return
  145. }
  146. for _, permission := range permissionList {
  147. dtoList = append(dtoList, convertPermissionToDTO(permission))
  148. }
  149. return
  150. }