package role import ( roleModel "busniess-user-center/internal/models/role" "busniess-user-center/internal/repo" "busniess-user-center/internal/service/util" "strconv" ) func getOrgAuthorAndRole(roleId uint, authorOrgs []roleModel.OrgAuthorBrief, allOrg []*repo.Organization) (authors []repo.OrganizationRoleAuthor, orgRole []repo.OrganizationRole) { _, orgchildrenMaps := util.GetOrgMap(allOrg) for _, item := range authorOrgs { orgRole = append(orgRole, genOrgRole(roleId, item.Id)) authors = append(authors, genOrgAuthor(roleId, item)) if item.Type == roleModel.IncludeChildrenOrgAuthor { childOrgs := util.GetChildren(item.Id, orgchildrenMaps) for _, child := range childOrgs { orgRole = append(orgRole, genOrgRole(roleId, child.ID)) } } } return } func genOrgRole(roleId, orgId uint) repo.OrganizationRole { return repo.OrganizationRole{ OrgID: orgId, RoleID: roleId, } } func genOrgAuthor(roleId uint, orgAuthor roleModel.OrgAuthorBrief) repo.OrganizationRoleAuthor { return repo.OrganizationRoleAuthor{ OrgID: orgAuthor.Id, Option: int(orgAuthor.Type), RoleID: roleId, } } func filterOrgAuthorRole(authors, dbAuthors []repo.OrganizationRoleAuthor, orgRole, dbOrgRoles []repo.OrganizationRole) (fAuthors []repo.OrganizationRoleAuthor, fOrgRole []repo.OrganizationRole) { dbAuthorMap := make(map[string]repo.OrganizationRoleAuthor, 0) dbOrgRoleMap := make(map[string]repo.OrganizationRole, 0) for _, item := range dbAuthors { key := strconv.FormatUint(uint64(item.RoleID), 10) + strconv.FormatUint(uint64(item.OrgID), 10) dbAuthorMap[key] = item } for _, item := range dbOrgRoles { key := strconv.FormatUint(uint64(item.RoleID), 10) + strconv.FormatUint(uint64(item.OrgID), 10) dbOrgRoleMap[key] = item } for _, item := range authors { key := strconv.FormatUint(uint64(item.RoleID), 10) + strconv.FormatUint(uint64(item.OrgID), 10) if _, ok := dbAuthorMap[key]; !ok { dbAuthorMap[key] = item fAuthors = append(fAuthors, item) } } for _, item := range orgRole { key := strconv.FormatUint(uint64(item.RoleID), 10) + strconv.FormatUint(uint64(item.OrgID), 10) if _, ok := dbOrgRoleMap[key]; !ok { dbOrgRoleMap[key] = item fOrgRole = append(fOrgRole, item) } } return } func genRemoveOrgAuthorAndRole(roleId uint, orgIds []uint, allOrg []*repo.Organization, allAuthor []repo.OrganizationRoleAuthor) (authors []repo.OrganizationRoleAuthor, orgRole []repo.OrganizationRole) { orgMaps, orgchildrenMaps := util.GetOrgMap(allOrg) authorMaps := make(map[string]repo.OrganizationRoleAuthor, 0) for _, item := range allAuthor { key := strconv.FormatUint(uint64(item.RoleID), 10) + strconv.FormatUint(uint64(item.OrgID), 10) authorMaps[key] = item } removeOrgMap := make(map[uint]bool) for _, orgId := range orgIds { removeOrgMap[orgId] = true } for _, orgId := range orgIds { // 判断是否移除当前组织的角色权限:1 父级组织没有包含子类的授权 2 组织自己没有记录 满足着两条可以删除组织角色权限 key := strconv.FormatUint(uint64(roleId), 10) + strconv.FormatUint(uint64(orgId), 10) if author, ok := authorMaps[key]; ok { authors = append(authors, repo.OrganizationRoleAuthor{ OrgID: orgId, RoleID: roleId, }) if whetherRemoveOrgRole(false, author, authorMaps, orgMaps, removeOrgMap) { orgRole = append(orgRole, repo.OrganizationRole{ OrgID: orgId, RoleID: roleId, }) } if roleModel.OrgAuthorRoleType(author.Option) == roleModel.IncludeChildrenOrgAuthor { childOrgs := util.GetChildren(orgId, orgchildrenMaps) for _, child := range childOrgs { cAuthor := repo.OrganizationRoleAuthor{ OrgID: child.ID, RoleID: roleId, } if whetherRemoveOrgRole(true, cAuthor, authorMaps, orgMaps, removeOrgMap) { orgRole = append(orgRole, genOrgRole(roleId, child.ID)) } } } } } return } func whetherRemoveOrgRole(child bool, author repo.OrganizationRoleAuthor, authorMaps map[string]repo.OrganizationRoleAuthor, orgMaps map[uint]*repo.Organization, delOrgMaps map[uint]bool) bool { if child { // 如果是要删除组织自己就不用判断自己是否授权过,如果是要删除组织的子组织,则需要判断子组织是否添加 key := strconv.FormatUint(uint64(author.RoleID), 10) + strconv.FormatUint(uint64(author.OrgID), 10) if _, ok := authorMaps[key]; ok { return false } } // 判断父级是否添加 parentOrg := util.GetParents(author.OrgID, orgMaps) for _, org := range parentOrg { // 父级不在当前要删除的组织内 if _, ok := delOrgMaps[org.ID]; !ok { key := strconv.FormatUint(uint64(author.RoleID), 10) + strconv.FormatUint(uint64(org.ID), 10) if pAuthor, ok := authorMaps[key]; ok { if pAuthor.Option == int(roleModel.IncludeChildrenOrgAuthor) { return false } } } } return true }