From 0e3e56f088466ecacad6fe66751f0d4e3aa07d5e Mon Sep 17 00:00:00 2001 From: lihongjie0209 Date: Mon, 14 Aug 2023 16:31:04 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=8A=A8=E6=89=AB=E6=8F=8Furl?= =?UTF-8?q?=E8=B5=84=E6=BA=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lihongjie/coal/entity/ResourceEntity.java | 78 +++++++++++++++++-- .../coal/entity/base/BaseEntity.java | 9 ++- .../lihongjie/coal/runner/InitDataRunner.java | 2 +- .../coal/service/ResourceService.java | 55 +++++++++++-- 4 files changed, 131 insertions(+), 13 deletions(-) diff --git a/src/main/java/cn/lihongjie/coal/entity/ResourceEntity.java b/src/main/java/cn/lihongjie/coal/entity/ResourceEntity.java index 013637cf..5b04e421 100644 --- a/src/main/java/cn/lihongjie/coal/entity/ResourceEntity.java +++ b/src/main/java/cn/lihongjie/coal/entity/ResourceEntity.java @@ -1,22 +1,27 @@ package cn.lihongjie.coal.entity; import cn.lihongjie.coal.entity.base.CommonEntity; +import io.vavr.collection.Stream; import jakarta.persistence.*; import lombok.Data; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; import org.hibernate.annotations.Comment; +import java.util.ArrayList; import java.util.List; @Data @Entity @Comment("资源") +@Slf4j public class ResourceEntity extends CommonEntity { @ManyToMany private List permissions; - @OneToMany(mappedBy = "parent") + @OneToMany(mappedBy = "parent", cascade = CascadeType.ALL) private List children; @ManyToOne @@ -28,28 +33,91 @@ public class ResourceEntity extends CommonEntity { private String type; - @Comment("资源地址") private String url; - @Comment("需要机构管理员权限") private Boolean orgAdmin; - @Comment("需要系统管理员权限") private Boolean sysAdmin; - @Comment("匿名用户可以访问") private Boolean anonymous; + public void addChildren(ResourceEntity entity) { + + log.info("addChildren {} {}", this.getUrl(), entity.getUrl()); + if (this.children == null) { + this.children = new ArrayList<>(); + } + + if (entity.getUrl().equals(this.getUrl())) { + return; + } + for (ResourceEntity child : this.children) { + + if (this.getUrl().startsWith(child.getUrl())) { + + + child.addChildren(entity); + + return; + + } + + } + + + String parent = getParent(entity.url); + + if (isSamePath(parent, this.getUrl())) { + + this.children.add(entity); + entity.setParent(this); + + } else { + + ResourceEntity r = new ResourceEntity(); + + String tmp = entity.getUrl(); + + while (!isSamePath(this.getUrl(), getParent(tmp))) { + tmp = getParent(tmp); + } + + + r.setUrl(tmp); + + r.setName(r.getUrl()); + r.setType("3"); + r.setParent(this); + r.setChildren(new ArrayList<>()); + r.setCode(r.getUrl()); + + + this.children.add(r); + + + r.addChildren(entity); + } + + + } + + private boolean isSamePath(String a, String b) { + return a.equalsIgnoreCase(b) || (a + "/").equalsIgnoreCase(b); + } + + private static String getParent(String path) { + return "/" + StringUtils.join(Stream.of(StringUtils.split(path, "/")).dropRight(1), "/"); + } } diff --git a/src/main/java/cn/lihongjie/coal/entity/base/BaseEntity.java b/src/main/java/cn/lihongjie/coal/entity/base/BaseEntity.java index ac336fd5..7559c1c9 100644 --- a/src/main/java/cn/lihongjie/coal/entity/base/BaseEntity.java +++ b/src/main/java/cn/lihongjie/coal/entity/base/BaseEntity.java @@ -7,7 +7,6 @@ import lombok.Getter; import lombok.Setter; import org.apache.commons.lang3.StringUtils; import org.hibernate.annotations.*; -import org.springframework.data.annotation.CreatedDate; import java.time.LocalDateTime; @@ -50,12 +49,20 @@ public class BaseEntity { this.createUserId = Ctx.isLoggedIn() ? Ctx.getUserId() : ""; } + + if (this.createTime == null) { + this.createTime = LocalDateTime.now(); + } } @PreUpdate public void preUpdate() { this.updateUserId = Ctx.isLoggedIn() ? Ctx.getUserId() : ""; + + if (this.updateTime == null) { + this.updateTime = LocalDateTime.now(); + } } diff --git a/src/main/java/cn/lihongjie/coal/runner/InitDataRunner.java b/src/main/java/cn/lihongjie/coal/runner/InitDataRunner.java index 8d2a3eb6..aeaf7fab 100644 --- a/src/main/java/cn/lihongjie/coal/runner/InitDataRunner.java +++ b/src/main/java/cn/lihongjie/coal/runner/InitDataRunner.java @@ -47,7 +47,7 @@ public class InitDataRunner implements CommandLineRunner { coalParameterDefService.initDefault(); -// resourceService.initUrlResource(); + resourceService.initUrlResource(); } finally { SecurityContextHolder.clearContext(); diff --git a/src/main/java/cn/lihongjie/coal/service/ResourceService.java b/src/main/java/cn/lihongjie/coal/service/ResourceService.java index 429db50a..0503ebfe 100644 --- a/src/main/java/cn/lihongjie/coal/service/ResourceService.java +++ b/src/main/java/cn/lihongjie/coal/service/ResourceService.java @@ -5,12 +5,13 @@ import cn.lihongjie.coal.dto.*; import cn.lihongjie.coal.entity.ResourceEntity; import cn.lihongjie.coal.mapper.ResourceMapper; import jakarta.annotation.PostConstruct; +import jakarta.persistence.EntityManager; +import jakarta.persistence.PersistenceContext; import jakarta.persistence.criteria.CriteriaBuilder; import jakarta.persistence.criteria.CriteriaQuery; import jakarta.persistence.criteria.Predicate; import jakarta.persistence.criteria.Root; import lombok.extern.slf4j.Slf4j; -import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.convert.ConversionService; import org.springframework.data.domain.Page; @@ -18,6 +19,7 @@ import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Sort; import org.springframework.data.jpa.domain.Specification; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import org.springframework.web.method.HandlerMethod; import org.springframework.web.servlet.mvc.method.RequestMappingInfo; import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; @@ -27,7 +29,6 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Optional; -import java.util.stream.Collectors; @Service @Slf4j @@ -110,19 +111,58 @@ public class ResourceService extends BaseService all = findAll().stream().filter(x -> StringUtils.equals(x.getType(), "3")).toList(); + List all = findAll(new Specification() { + @Override + public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder criteriaBuilder) { + return criteriaBuilder.and(criteriaBuilder.equal(root.get("url"), "/"), criteriaBuilder.equal(root.get("type"), "3")); + } + }); - Map map = all.stream().collect(Collectors.toMap(e -> e.getCode(), e -> e)); + ResourceEntity root = null; + + if (all.isEmpty()) { + + ResourceEntity entity = new ResourceEntity(); + entity.setCode("/"); + entity.setType("3"); + entity.setName("接口资源"); + entity.setUrl("/"); + this.save(entity); + root = entity; + }else { + + root = all.get(0); + + } - getAllUrls(); + + List allUrls = getAllUrls(); + + + for (String allUrl : allUrls) { + ResourceEntity entity = new ResourceEntity(); + entity.setCode(allUrl); + entity.setType("3"); + entity.setName(""); + entity.setUrl(allUrl); + root.addChildren(entity); + + } + + + this.save(root); } - private void getAllUrls() { + private List getAllUrls() { Map handlerMethods = requestMappingHandlerMapping.getHandlerMethods(); List urls = new ArrayList<>(); for (Map.Entry entry : handlerMethods.entrySet()) { @@ -135,9 +175,12 @@ public class ResourceService extends BaseService