CommonID 基本Domain类java
public interface CommonID<ID, E> extends Serializable { ID getId(); ID getParentId(); void setParent(E parent); void setChildren(List<E> children); }
TreeUtils 树形结构处理类node
import com.google.common.collect.Lists; import com.pingan.haofang.gov.sm.account.common.query.CommonID; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.SerializationUtils; import java.lang.reflect.ParameterizedType; import java.util.Collections; import java.util.List; import java.util.Objects; import java.util.stream.Collectors; public class TreeUtils<T extends CommonID<ID, T>, ID> { private List<T> sourceTree; private List<T> _back; public TreeUtils(List<T> _sourceTree) { this.sourceTree = _sourceTree; } /** * 获取当前节点的全部上级节点和全部子节点,拼装树形结构 * * @param id 当前节点ID * @return 当前节点全部子节点和全部父节点的树 */ public T getTree(ID id) { if (CollectionUtils.isEmpty(_back)){ rebuildBack(); } T current = this.getCurrent(id); if (Objects.isNull(current)) { return null; } this.buildUpChild(current); this.buildUpParents(current); return current; } private void rebuildBack() { if (CollectionUtils.isNotEmpty(_back)) { _back.clear(); } this._back = ListUtils.deepCopyList(sourceTree); } /** * 获取当前节点的全部上级节点和全部子节点,放入同一个List中 * * @param id 当前节点ID * @return 当前节点全部子节点和全部父节点的集合 */ public List<T> getFamily(ID id) { if (CollectionUtils.isEmpty(_back)){ rebuildBack(); } List<T> family = Lists.newArrayList(); T current = this.getCurrent(id); if (Objects.nonNull(current)) { this.getParentList(current, family); this.getChildrenList(current, family); family.add(current); } return family; } private void buildUpParents(T current) { T parent = getParentNode(current.getId()); if (Objects.isNull(parent)) { return; } T p = SerializationUtils.clone(parent); current.setParent(p); buildUpParents(parent); } private void buildUpChildren(List<T> list) { if (CollectionUtils.isNotEmpty(list)) { list.forEach(this::buildUpChild); } } private void buildUpChild(T current) { List<T> children = getChildNode(current.getId()); if (CollectionUtils.isEmpty(children)) { return; } List<T> ts = ListUtils.deepCopyList(children); current.setChildren(ts); buildUpChildren(children); } public T getParentNode(ID id) { if (CollectionUtils.isEmpty(_back)){ rebuildBack(); } if (CollectionUtils.isNotEmpty(_back)) { T currentNode = this.getCurrent(id); if (Objects.nonNull(currentNode)) { List<T> collect = _back.stream().filter(node -> currentNode.getParentId().equals(node.getId())).distinct().collect(Collectors.toList()); return CollectionUtils.isEmpty(collect) ? null : collect.get(0); } } return null; } public List<T> getChildNode(ID parentID) { if (CollectionUtils.isEmpty(_back)){ rebuildBack(); } if (CollectionUtils.isNotEmpty(_back)) { return _back.stream().filter(node -> parentID.equals(node.getParentId())).distinct() .collect(Collectors.toList()); } return Collections.emptyList(); } public T getCurrent(ID id) { if (CollectionUtils.isEmpty(_back)){ rebuildBack(); } if (CollectionUtils.isNotEmpty(_back)) { List<T> currentNodes = _back.stream().filter(node -> id.equals(node.getId())).collect(Collectors.toList()); if (CollectionUtils.isNotEmpty(currentNodes)) { T currentNode = currentNodes.get(0); // T instance = getInstance(); // BeanUtils.copyProperties(currentNode, instance); return currentNode; } } return null; } public void getChildrenList(T current, List<T> family) { if (CollectionUtils.isEmpty(_back)){ rebuildBack(); } if (Objects.isNull(current)) { return; } List<T> childNodes = this.getChildNode(current.getId()); if (CollectionUtils.isEmpty(childNodes)) { return; } family.addAll(childNodes); childNodes.forEach(node -> getChildrenList(node, family)); } public void getParentList(T current, List<T> family) { if (CollectionUtils.isEmpty(_back)){ rebuildBack(); } if (Objects.isNull(current)) { return; } T parentNode = this.getParentNode(current.getId()); if (Objects.isNull(parentNode)) { return; } family.add(parentNode); getParentList(parentNode, family); } private T getInstance() { T instance; try { ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass(); Class<T> tCls = (Class<T>) pt.getActualTypeArguments()[0]; instance = tCls.newInstance(); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException("TreeUtils==> 获取实例异常"); } return instance; } }
构造树形结构的业务方法spring
@Override public List<PermissionDto> getPermissionTreeBySubsystemId(Long subsystemId) { // 查出表的List集合 List<Permission> permissions = permissionService.findBySubsystemId(subsystemId); if (CollectionUtils.isNotEmpty(permissions)) { // 这只是一个转换 实体转换为DTO 不用管 List<PermissionDto> dtos = permissions.stream().map(permissionSupportService::transfer2Dto).collect(Collectors.toList()); // 构造树形结构 TreeUtils<PermissionDto, Long> utils = new TreeUtils<>(dtos); return permissions.stream() .filter(p -> NumberUtils.LONG_ZERO.equals(p.getParentId())) .map(Permission::getId) .map(utils::getTree) .collect(Collectors.toList()); } return Lists.newArrayListWithCapacity(NumberUtils.INTEGER_ZERO); }
权限实体DOMAINsql
package com.pingan.haofang.gov.sm.account.entity.domain; import org.hibernate.annotations.DynamicInsert; import org.hibernate.annotations.DynamicUpdate; import org.hibernate.envers.Audited; import org.springframework.data.jpa.domain.support.AuditingEntityListener; import javax.persistence.*; import java.sql.Timestamp; /** * 权限表 */ @DynamicInsert @DynamicUpdate @Entity @Table(name = "t_standard_permission") @EntityListeners({AuditingEntityListener.class}) @Audited public class Permission { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "id") private Long id; /** * 权限名称 */ @Column(name = "name") private String name; /** * 权限编码 */ @Column(name = "code") private String code; /** * 权限key */ @Column(name = "permission_key") private String permissionKey; /** * 排序字段 */ @Column(name = "order_no") private Integer orderNo; /** * 父级id */ @Column(name = "parent_id") private Long parentId; /** * 权限描述 */ @Column(name = "permission_desc") private String permissionDesc; /** * 资源地址 */ @Column(name = "request_url") private String requestUrl; /** * http请求方式: GET, POST... */ @Column(name = "request_method") private String requestMethod; /** * 系统来源, 0: ERP端, 1 C端,2 GM端 */ @Column(name = "source_type") private Integer sourceType; /** * 系统模块,用户模块, 房源模块等... */ @Column(name = "module") private String module; /** * 权限类型,0:功能按钮,1:菜单,2 资源url */ @Column(name = "permission_type") private Integer permissionType; /** * 子系统id */ @Column(name = "subsystem_id") private Long subsystemId; /** * 建立人id */ @Column(name = "create_by") private Long createBy; /** * 建立时间 */ @Column(name = "create_time") private Timestamp createTime; /** * 更新人id */ @Column(name = "update_by") private Long updateBy; /** * 更新时间 */ @Column(name = "update_time") private Timestamp updateTime; /** * 是否删除 1是 0否 */ @Column(name = "is_deleted") private Integer isDeleted; /** * 启用状态:1正常,0禁用 */ @Column(name = "status") private Integer status; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public Long getParentId() { return parentId; } public void setParentId(Long parentId) { this.parentId = parentId; } public String getPermissionDesc() { return permissionDesc; } public void setPermissionDesc(String permissionDesc) { this.permissionDesc = permissionDesc; } public String getRequestUrl() { return requestUrl; } public void setRequestUrl(String requestUrl) { this.requestUrl = requestUrl; } public String getRequestMethod() { return requestMethod; } public void setRequestMethod(String requestMethod) { this.requestMethod = requestMethod; } public Integer getSourceType() { return sourceType; } public void setSourceType(Integer sourceType) { this.sourceType = sourceType; } public String getModule() { return module; } public void setModule(String module) { this.module = module; } public Integer getPermissionType() { return permissionType; } public void setPermissionType(Integer permissionType) { this.permissionType = permissionType; } public Long getCreateBy() { return createBy; } public void setCreateBy(Long createBy) { this.createBy = createBy; } public Timestamp getCreateTime() { return createTime; } public void setCreateTime(Timestamp createTime) { this.createTime = createTime; } public Long getUpdateBy() { return updateBy; } public void setUpdateBy(Long updateBy) { this.updateBy = updateBy; } public Timestamp getUpdateTime() { return updateTime; } public void setUpdateTime(Timestamp updateTime) { this.updateTime = updateTime; } public Integer getIsDeleted() { return isDeleted; } public void setIsDeleted(Integer isDeleted) { this.isDeleted = isDeleted; } public Long getSubsystemId() { return subsystemId; } public void setSubsystemId(Long subsystemId) { this.subsystemId = subsystemId; } public String getPermissionKey() { return permissionKey; } public void setPermissionKey(String permissionKey) { this.permissionKey = permissionKey; } public Integer getOrderNo() { return orderNo; } public void setOrderNo(Integer orderNo) { this.orderNo = orderNo; } public Integer getStatus() { return status; } public void setStatus(Integer status) { this.status = status; } }
权限DTO PermissionDtoapache
package com.pingan.haofang.gov.sm.account.entity.dto.role; import java.sql.Timestamp; import java.util.List; import com.pingan.haofang.gov.sm.account.common.query.CommonID; import io.swagger.annotations.ApiModelProperty; import lombok.Data; @Data public class PermissionDto implements CommonID<Long, PermissionDto> { @ApiModelProperty(value = "主键id") private Long id; @ApiModelProperty(value = "选中状态, 1:选择, 0:未选中.") private Boolean selected; @ApiModelProperty(value = "权限名称") private String name; @ApiModelProperty(value = "权限编码") private String code; @ApiModelProperty(value = "权限key") private String permissionKey; @ApiModelProperty(value = "排序") private Integer orderNo; @ApiModelProperty(value = "父权限id") private Long parentId; @ApiModelProperty(value = "权限描述") private String permissionDesc; @ApiModelProperty(value = "权限路径") private String requestUrl; @ApiModelProperty(value = "请求方式") private String requestMethod; @ApiModelProperty(value = "系统来源") private Integer sourceType; @ApiModelProperty(value = "模块") private String module; @ApiModelProperty(value = "子系统id") private Long subsystemId; @ApiModelProperty(value = "权限类型") private Integer permissionType; @ApiModelProperty(value = "建立人id") private Long createBy; @ApiModelProperty(value = "建立时间") private Timestamp createTime; @ApiModelProperty(value = "更新人id") private Long updateBy; @ApiModelProperty(value = "更新时间") private Timestamp updateTime; @ApiModelProperty(value = "是否删除 1 是, 0 否") private Integer isDeleted; @ApiModelProperty(value = "子权限列表") private List<PermissionDto> subList; @ApiModelProperty(value = "生效状态:1 有效,0 失效") private Integer status; private PermissionDto parent; private List<PermissionDto> children; }