Commit 30db74bf7eecd1814d4680e25e82d0ade5a53853
1 parent
8f018e3b
day05-路线规划之Neo4j入门
Showing
22 changed files
with
705 additions
and
1 deletions
.idea/.gitignore renamed to .gitignore
| 1 | # 默认忽略的文件 | 1 | # 默认忽略的文件 |
| 2 | /shelf/ | 2 | /shelf/ |
| 3 | -/workspace.xml | 3 | +/.idea/workspace.xml |
| 4 | # 基于编辑器的 HTTP 客户端请求 | 4 | # 基于编辑器的 HTTP 客户端请求 |
| 5 | /httpRequests/ | 5 | /httpRequests/ |
| 6 | # Datasource local storage ignored files | 6 | # Datasource local storage ignored files |
| @@ -8,3 +8,11 @@ | @@ -8,3 +8,11 @@ | ||
| 8 | /dataSources.local.xml | 8 | /dataSources.local.xml |
| 9 | # Zeppelin 忽略的文件 | 9 | # Zeppelin 忽略的文件 |
| 10 | /ZeppelinRemoteNotebooks/ | 10 | /ZeppelinRemoteNotebooks/ |
| 11 | +### Example user template template | ||
| 12 | +### Example user template | ||
| 13 | + | ||
| 14 | +# IntelliJ project files | ||
| 15 | +.idea | ||
| 16 | +*.iml | ||
| 17 | +out | ||
| 18 | +gen |
sl-express-sdn/pom.xml
0 → 100644
| 1 | +<?xml version="1.0" encoding="UTF-8"?> | ||
| 2 | +<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
| 3 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| 4 | + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| 5 | + <modelVersion>4.0.0</modelVersion> | ||
| 6 | + | ||
| 7 | + <parent> | ||
| 8 | + <groupId>com.sl-express</groupId> | ||
| 9 | + <artifactId>sl-express-parent</artifactId> | ||
| 10 | + <version>1.4</version> | ||
| 11 | + </parent> | ||
| 12 | + | ||
| 13 | + <groupId>com.sl-express.sdn</groupId> | ||
| 14 | + <artifactId>sl-express-sdn</artifactId> | ||
| 15 | + <version>1.0-SNAPSHOT</version> | ||
| 16 | + | ||
| 17 | + <properties> | ||
| 18 | + <maven.compiler.source>11</maven.compiler.source> | ||
| 19 | + <maven.compiler.target>11</maven.compiler.target> | ||
| 20 | + <sl-express-common.version>1.2-SNAPSHOT</sl-express-common.version> | ||
| 21 | + </properties> | ||
| 22 | + | ||
| 23 | + <dependencies> | ||
| 24 | + <dependency> | ||
| 25 | + <groupId>com.sl-express.common</groupId> | ||
| 26 | + <artifactId>sl-express-common</artifactId> | ||
| 27 | + <version>${sl-express-common.version}</version> | ||
| 28 | + </dependency> | ||
| 29 | + <!--SDN依赖--> | ||
| 30 | + <dependency> | ||
| 31 | + <groupId>org.springframework.boot</groupId> | ||
| 32 | + <artifactId>spring-boot-starter-data-neo4j</artifactId> | ||
| 33 | + </dependency> | ||
| 34 | + </dependencies> | ||
| 35 | + | ||
| 36 | +</project> | ||
| 0 | \ No newline at end of file | 37 | \ No newline at end of file |
sl-express-sdn/src/main/java/com/sl/sdn/SDNApplication.java
0 → 100644
| 1 | +package com.sl.sdn; | ||
| 2 | + | ||
| 3 | +import org.springframework.boot.SpringApplication; | ||
| 4 | +import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
| 5 | + | ||
| 6 | +@SpringBootApplication | ||
| 7 | +public class SDNApplication { | ||
| 8 | + | ||
| 9 | + public static void main(String[] args) { | ||
| 10 | + SpringApplication.run(SDNApplication.class, args); | ||
| 11 | + } | ||
| 12 | +} | ||
| 0 | \ No newline at end of file | 13 | \ No newline at end of file |
sl-express-sdn/src/main/java/com/sl/sdn/dto/OrganDTO.java
0 → 100644
| 1 | +package com.sl.sdn.dto; | ||
| 2 | + | ||
| 3 | +import cn.hutool.core.annotation.Alias; | ||
| 4 | +import io.swagger.annotations.ApiModelProperty; | ||
| 5 | +import lombok.Data; | ||
| 6 | + | ||
| 7 | +/** | ||
| 8 | + * 机构数据对象,网点、一级转运、二级转运都是看作是机构 | ||
| 9 | + * BaseEntity中的location无法序列化,需要将经纬度拆开封装对象 | ||
| 10 | + */ | ||
| 11 | +@Data | ||
| 12 | +public class OrganDTO { | ||
| 13 | + | ||
| 14 | + @Alias("bid") //业务id作为id进行封装 | ||
| 15 | + @ApiModelProperty(value = "机构id", required = true) | ||
| 16 | + private Long id; | ||
| 17 | + @ApiModelProperty(value = "名称", required = true) | ||
| 18 | + private String name; | ||
| 19 | + @ApiModelProperty(value = "类型,1:一级转运,2:二级转运,3:网点", required = true) | ||
| 20 | + private Integer type; | ||
| 21 | + @ApiModelProperty(value = "电话", required = true) | ||
| 22 | + private String phone; | ||
| 23 | + @ApiModelProperty(value = "地址", required = true) | ||
| 24 | + private String address; | ||
| 25 | + @ApiModelProperty(value = "纬度", required = true) | ||
| 26 | + private Double latitude; | ||
| 27 | + @ApiModelProperty(value = "经度", required = true) | ||
| 28 | + private Double longitude; | ||
| 29 | + | ||
| 30 | +} |
sl-express-sdn/src/main/java/com/sl/sdn/dto/TransportLineNodeDTO.java
0 → 100644
| 1 | +package com.sl.sdn.dto; | ||
| 2 | + | ||
| 3 | +import io.swagger.annotations.ApiModelProperty; | ||
| 4 | +import lombok.Data; | ||
| 5 | + | ||
| 6 | +import java.util.ArrayList; | ||
| 7 | +import java.util.List; | ||
| 8 | + | ||
| 9 | +/** | ||
| 10 | + * 运输路线对象 | ||
| 11 | + */ | ||
| 12 | +@Data | ||
| 13 | +public class TransportLineNodeDTO { | ||
| 14 | + | ||
| 15 | + @ApiModelProperty(value = "节点列表", required = true) | ||
| 16 | + private List<OrganDTO> nodeList = new ArrayList<>(); | ||
| 17 | + | ||
| 18 | + @ApiModelProperty(value = "路线成本", required = true) | ||
| 19 | + private Double cost = 0d; | ||
| 20 | + | ||
| 21 | +} |
sl-express-sdn/src/main/java/com/sl/sdn/entity/line/TransportLine.java
0 → 100644
| 1 | +package com.sl.sdn.entity.line; | ||
| 2 | + | ||
| 3 | +import lombok.AllArgsConstructor; | ||
| 4 | +import lombok.Builder; | ||
| 5 | +import lombok.Data; | ||
| 6 | +import lombok.NoArgsConstructor; | ||
| 7 | + | ||
| 8 | +/** | ||
| 9 | + * 运输路线实体 | ||
| 10 | + */ | ||
| 11 | +@Data | ||
| 12 | +@Builder | ||
| 13 | +@NoArgsConstructor | ||
| 14 | +@AllArgsConstructor | ||
| 15 | +public class TransportLine { | ||
| 16 | + | ||
| 17 | + private Long id; | ||
| 18 | + private Double cost; //成本 | ||
| 19 | + | ||
| 20 | +} |
sl-express-sdn/src/main/java/com/sl/sdn/entity/node/AgencyEntity.java
0 → 100644
| 1 | +package com.sl.sdn.entity.node; | ||
| 2 | + | ||
| 3 | + | ||
| 4 | +import com.sl.sdn.enums.OrganTypeEnum; | ||
| 5 | +import lombok.Data; | ||
| 6 | +import lombok.NoArgsConstructor; | ||
| 7 | +import lombok.ToString; | ||
| 8 | +import lombok.experimental.SuperBuilder; | ||
| 9 | +import org.springframework.data.neo4j.core.schema.Node; | ||
| 10 | + | ||
| 11 | +/** | ||
| 12 | + * 网点实体 | ||
| 13 | + */ | ||
| 14 | +@Node("AGENCY") | ||
| 15 | +@Data | ||
| 16 | +@ToString(callSuper = true) | ||
| 17 | +@SuperBuilder(toBuilder = true) | ||
| 18 | +@NoArgsConstructor | ||
| 19 | +public class AgencyEntity extends BaseEntity { | ||
| 20 | + | ||
| 21 | + @Override | ||
| 22 | + public OrganTypeEnum getAgencyType() { | ||
| 23 | + return OrganTypeEnum.AGENCY; | ||
| 24 | + } | ||
| 25 | +} |
sl-express-sdn/src/main/java/com/sl/sdn/entity/node/BaseEntity.java
0 → 100644
| 1 | +package com.sl.sdn.entity.node; | ||
| 2 | + | ||
| 3 | +import com.sl.sdn.enums.OrganTypeEnum; | ||
| 4 | +import io.swagger.annotations.ApiModelProperty; | ||
| 5 | +import lombok.AllArgsConstructor; | ||
| 6 | +import lombok.Data; | ||
| 7 | +import lombok.NoArgsConstructor; | ||
| 8 | +import lombok.experimental.SuperBuilder; | ||
| 9 | +import org.neo4j.driver.types.Point; | ||
| 10 | +import org.springframework.data.neo4j.core.schema.GeneratedValue; | ||
| 11 | +import org.springframework.data.neo4j.core.schema.Id; | ||
| 12 | + | ||
| 13 | +@Data | ||
| 14 | +@SuperBuilder(toBuilder = true) | ||
| 15 | +@NoArgsConstructor | ||
| 16 | +@AllArgsConstructor | ||
| 17 | +public abstract class BaseEntity { | ||
| 18 | + | ||
| 19 | + @Id | ||
| 20 | + @GeneratedValue | ||
| 21 | + @ApiModelProperty(value = "Neo4j ID", hidden = true) | ||
| 22 | + private Long id; | ||
| 23 | + @ApiModelProperty(value = "业务id", required = true) | ||
| 24 | + private Long bid; | ||
| 25 | + @ApiModelProperty(value = "名称", required = true) | ||
| 26 | + private String name; | ||
| 27 | + @ApiModelProperty(value = "电话", required = true) | ||
| 28 | + private String phone; | ||
| 29 | + @ApiModelProperty(value = "地址", required = true) | ||
| 30 | + private String address; | ||
| 31 | + @ApiModelProperty(value = "位置坐标, x: 纬度,y: 经度", required = true) | ||
| 32 | + private Point location; | ||
| 33 | + | ||
| 34 | + //机构类型 | ||
| 35 | + public abstract OrganTypeEnum getAgencyType(); | ||
| 36 | + | ||
| 37 | +} | ||
| 0 | \ No newline at end of file | 38 | \ No newline at end of file |
sl-express-sdn/src/main/java/com/sl/sdn/entity/node/OLTEntity.java
0 → 100644
| 1 | +package com.sl.sdn.entity.node; | ||
| 2 | + | ||
| 3 | +import com.sl.sdn.enums.OrganTypeEnum; | ||
| 4 | +import lombok.Data; | ||
| 5 | +import lombok.NoArgsConstructor; | ||
| 6 | +import lombok.ToString; | ||
| 7 | +import lombok.experimental.SuperBuilder; | ||
| 8 | +import org.springframework.data.neo4j.core.schema.Node; | ||
| 9 | + | ||
| 10 | +/** | ||
| 11 | + * 一级转运中心实体 (OneLevelTransportEntity) | ||
| 12 | + */ | ||
| 13 | +@Node("OLT") | ||
| 14 | +@Data | ||
| 15 | +@ToString(callSuper = true) | ||
| 16 | +@SuperBuilder(toBuilder = true) | ||
| 17 | +@NoArgsConstructor | ||
| 18 | +public class OLTEntity extends BaseEntity { | ||
| 19 | + | ||
| 20 | + @Override | ||
| 21 | + public OrganTypeEnum getAgencyType() { | ||
| 22 | + return OrganTypeEnum.OLT; | ||
| 23 | + } | ||
| 24 | +} |
sl-express-sdn/src/main/java/com/sl/sdn/entity/node/TLTEntity.java
0 → 100644
| 1 | +package com.sl.sdn.entity.node; | ||
| 2 | + | ||
| 3 | +import com.sl.sdn.enums.OrganTypeEnum; | ||
| 4 | +import lombok.Data; | ||
| 5 | +import lombok.NoArgsConstructor; | ||
| 6 | +import lombok.ToString; | ||
| 7 | +import lombok.experimental.SuperBuilder; | ||
| 8 | +import org.springframework.data.neo4j.core.schema.Node; | ||
| 9 | + | ||
| 10 | +/** | ||
| 11 | + * 二级转运中心实体(TwoLevelTransportEntity) | ||
| 12 | + */ | ||
| 13 | +@Node("TLT") | ||
| 14 | +@Data | ||
| 15 | +@ToString(callSuper = true) | ||
| 16 | +@SuperBuilder(toBuilder = true) | ||
| 17 | +@NoArgsConstructor | ||
| 18 | +public class TLTEntity extends BaseEntity { | ||
| 19 | + | ||
| 20 | + @Override | ||
| 21 | + public OrganTypeEnum getAgencyType() { | ||
| 22 | + return OrganTypeEnum.TLT; | ||
| 23 | + } | ||
| 24 | +} | ||
| 0 | \ No newline at end of file | 25 | \ No newline at end of file |
sl-express-sdn/src/main/java/com/sl/sdn/enums/OrganTypeEnum.java
0 → 100644
| 1 | +package com.sl.sdn.enums; | ||
| 2 | + | ||
| 3 | +import cn.hutool.core.util.EnumUtil; | ||
| 4 | +import com.sl.transport.common.enums.BaseEnum; | ||
| 5 | + | ||
| 6 | +/** | ||
| 7 | + * 机构类型枚举 | ||
| 8 | + */ | ||
| 9 | +public enum OrganTypeEnum implements BaseEnum { | ||
| 10 | + | ||
| 11 | + OLT(1, "一级转运中心"), | ||
| 12 | + TLT(2, "二级转运中心"), | ||
| 13 | + AGENCY(3, "网点"); | ||
| 14 | + | ||
| 15 | + /** | ||
| 16 | + * 类型编码 | ||
| 17 | + */ | ||
| 18 | + private final Integer code; | ||
| 19 | + | ||
| 20 | + /** | ||
| 21 | + * 类型值 | ||
| 22 | + */ | ||
| 23 | + private final String value; | ||
| 24 | + | ||
| 25 | + OrganTypeEnum(Integer code, String value) { | ||
| 26 | + this.code = code; | ||
| 27 | + this.value = value; | ||
| 28 | + } | ||
| 29 | + | ||
| 30 | + public Integer getCode() { | ||
| 31 | + return code; | ||
| 32 | + } | ||
| 33 | + | ||
| 34 | + public String getValue() { | ||
| 35 | + return value; | ||
| 36 | + } | ||
| 37 | + | ||
| 38 | + public static OrganTypeEnum codeOf(Integer code) { | ||
| 39 | + return EnumUtil.getBy(OrganTypeEnum::getCode, code); | ||
| 40 | + } | ||
| 41 | +} |
sl-express-sdn/src/main/java/com/sl/sdn/repository/AgencyRepository.java
0 → 100644
| 1 | +package com.sl.sdn.repository; | ||
| 2 | + | ||
| 3 | +import com.sl.sdn.entity.node.AgencyEntity; | ||
| 4 | +import org.springframework.data.neo4j.repository.Neo4jRepository; | ||
| 5 | +import org.springframework.stereotype.Repository; | ||
| 6 | + | ||
| 7 | +/** | ||
| 8 | + * 网点操作 | ||
| 9 | + */ | ||
| 10 | +@Repository | ||
| 11 | +public interface AgencyRepository extends Neo4jRepository<AgencyEntity,Long> { | ||
| 12 | + /** | ||
| 13 | + * 根据bid查询 | ||
| 14 | + * @param bid | ||
| 15 | + * @return | ||
| 16 | + */ | ||
| 17 | + AgencyEntity findByBid(Long bid); | ||
| 18 | + | ||
| 19 | + /** | ||
| 20 | + * 根据bid删除 | ||
| 21 | + * @param bid | ||
| 22 | + * @return | ||
| 23 | + */ | ||
| 24 | + Long deleteByBid(Long bid); | ||
| 25 | + | ||
| 26 | +} |
sl-express-sdn/src/main/java/com/sl/sdn/repository/OLTRepository.java
0 → 100644
| 1 | +package com.sl.sdn.repository; | ||
| 2 | + | ||
| 3 | +import com.sl.sdn.entity.node.OLTEntity; | ||
| 4 | +import org.springframework.data.neo4j.repository.Neo4jRepository; | ||
| 5 | +import org.springframework.stereotype.Repository; | ||
| 6 | + | ||
| 7 | +/** | ||
| 8 | + * 一级转运中心数据操作 | ||
| 9 | + */ | ||
| 10 | +@Repository | ||
| 11 | +public interface OLTRepository extends Neo4jRepository<OLTEntity, Long> { | ||
| 12 | + | ||
| 13 | + /** | ||
| 14 | + * 根据bid查询 | ||
| 15 | + * @param bid | ||
| 16 | + * @return | ||
| 17 | + */ | ||
| 18 | + OLTEntity findByBid(Long bid); | ||
| 19 | + | ||
| 20 | + /** | ||
| 21 | + * 根据bid删除 | ||
| 22 | + * @param bid | ||
| 23 | + * @return | ||
| 24 | + */ | ||
| 25 | + Long deleteByBid(Long bid); | ||
| 26 | +} |
sl-express-sdn/src/main/java/com/sl/sdn/repository/OrganRepository.java
0 → 100644
| 1 | +package com.sl.sdn.repository; | ||
| 2 | + | ||
| 3 | +import com.sl.sdn.dto.OrganDTO; | ||
| 4 | + | ||
| 5 | +import java.util.List; | ||
| 6 | + | ||
| 7 | +/** | ||
| 8 | + * 通过机构查询 | ||
| 9 | + */ | ||
| 10 | +public interface OrganRepository { | ||
| 11 | + | ||
| 12 | + /** | ||
| 13 | + * 无需指定type,根据id查询 | ||
| 14 | + * @param bid | ||
| 15 | + * @return | ||
| 16 | + */ | ||
| 17 | + OrganDTO findByBid(Long bid); | ||
| 18 | + | ||
| 19 | + | ||
| 20 | + /** | ||
| 21 | + * 查询所有的机构,如果name不为空的按照name模糊查询 | ||
| 22 | + * @param name | ||
| 23 | + * @return | ||
| 24 | + */ | ||
| 25 | + List<OrganDTO> findAll(String name); | ||
| 26 | +} |
sl-express-sdn/src/main/java/com/sl/sdn/repository/TLTRepository.java
0 → 100644
| 1 | +package com.sl.sdn.repository; | ||
| 2 | + | ||
| 3 | +import com.sl.sdn.entity.node.TLTEntity; | ||
| 4 | +import org.springframework.data.neo4j.repository.Neo4jRepository; | ||
| 5 | +import org.springframework.stereotype.Repository; | ||
| 6 | + | ||
| 7 | +/** | ||
| 8 | + * 二级转运中心数据操作 | ||
| 9 | + */ | ||
| 10 | +@Repository | ||
| 11 | +public interface TLTRepository extends Neo4jRepository<TLTEntity,Long> { | ||
| 12 | + | ||
| 13 | + /** | ||
| 14 | + * 根据Bid查询 | ||
| 15 | + * @param bid | ||
| 16 | + * @return | ||
| 17 | + */ | ||
| 18 | + TLTEntity findByBid(Long bid); | ||
| 19 | + | ||
| 20 | + | ||
| 21 | + /** | ||
| 22 | + * 根据bid删除 | ||
| 23 | + * @param bid | ||
| 24 | + * @return | ||
| 25 | + */ | ||
| 26 | + Long deleteByBid(Long bid); | ||
| 27 | + | ||
| 28 | +} |
sl-express-sdn/src/main/java/com/sl/sdn/repository/TransportLineRepository.java
0 → 100644
| 1 | +package com.sl.sdn.repository; | ||
| 2 | + | ||
| 3 | +import com.sl.sdn.dto.TransportLineNodeDTO; | ||
| 4 | +import com.sl.sdn.entity.node.AgencyEntity; | ||
| 5 | + | ||
| 6 | +/** | ||
| 7 | + * 运输路线相关操作 | ||
| 8 | + */ | ||
| 9 | +public interface TransportLineRepository { | ||
| 10 | + | ||
| 11 | + /** | ||
| 12 | + * 查询两个网点之间最短的路线,查询深度为:10 | ||
| 13 | + * @param start | ||
| 14 | + * @param end | ||
| 15 | + * @return | ||
| 16 | + */ | ||
| 17 | + TransportLineNodeDTO findShortTestPath(AgencyEntity start, AgencyEntity end); | ||
| 18 | +} |
sl-express-sdn/src/main/java/com/sl/sdn/repository/impl/OrganRepositoryImpl.java
0 → 100644
| 1 | +package com.sl.sdn.repository.impl; | ||
| 2 | + | ||
| 3 | +import cn.hutool.core.collection.CollUtil; | ||
| 4 | +import com.sl.sdn.dto.OrganDTO; | ||
| 5 | +import com.sl.sdn.enums.OrganTypeEnum; | ||
| 6 | +import com.sl.sdn.repository.OrganRepository; | ||
| 7 | +import com.sl.transport.common.util.BeanUtil; | ||
| 8 | +import org.neo4j.driver.internal.value.NodeValue; | ||
| 9 | +import org.springframework.beans.factory.annotation.Autowired; | ||
| 10 | +import org.springframework.data.neo4j.core.Neo4jClient; | ||
| 11 | +import org.springframework.stereotype.Repository; | ||
| 12 | + | ||
| 13 | +import java.util.Collection; | ||
| 14 | +import java.util.List; | ||
| 15 | +import java.util.Map; | ||
| 16 | +import java.util.Optional; | ||
| 17 | + | ||
| 18 | +/** | ||
| 19 | + * 通过机构查询 | ||
| 20 | + */ | ||
| 21 | +@Repository | ||
| 22 | +public class OrganRepositoryImpl implements OrganRepository { | ||
| 23 | + | ||
| 24 | + @Autowired | ||
| 25 | + private Neo4jClient neo4jClient; | ||
| 26 | + | ||
| 27 | + /** | ||
| 28 | + * 无需指定type,根据id查询 | ||
| 29 | + * @param bid | ||
| 30 | + * @return | ||
| 31 | + */ | ||
| 32 | + @Override | ||
| 33 | + public OrganDTO findByBid(Long bid) { | ||
| 34 | + String cypherQuery = "MATCH(n) where n.bid=$bid RETURN n"; | ||
| 35 | + | ||
| 36 | + Optional<OrganDTO> organDTOOptional = neo4jClient.query(cypherQuery) | ||
| 37 | + .bind(bid).to("bid") | ||
| 38 | + .fetchAs(OrganDTO.class) | ||
| 39 | + .mappedBy(((typeSystem, record) -> { | ||
| 40 | + NodeValue nodeValue = (NodeValue) record.get(0); | ||
| 41 | + Map<String, Object> map = nodeValue.asMap(); | ||
| 42 | + OrganDTO organDTO = BeanUtil.toBeanIgnoreError(map, OrganDTO.class); | ||
| 43 | + organDTO.setType(OrganTypeEnum.valueOf(CollUtil.getFirst(nodeValue.asNode().labels())).getCode()); | ||
| 44 | + organDTO.setLatitude(BeanUtil.getProperty(map.get("location"), "y")); | ||
| 45 | + organDTO.setLongitude(BeanUtil.getProperty(map.get("location"), "x")); | ||
| 46 | + return organDTO; | ||
| 47 | + })).one(); | ||
| 48 | + return organDTOOptional.orElse(null); | ||
| 49 | + } | ||
| 50 | + | ||
| 51 | + /** | ||
| 52 | + * 查询所有的机构,如果name不为空的按照name模糊查询 | ||
| 53 | + * @param name | ||
| 54 | + * @return | ||
| 55 | + */ | ||
| 56 | + @Override | ||
| 57 | + public List<OrganDTO> findAll(String name) { | ||
| 58 | + String cypherQuery = "MATCH(n) where n.name CONTAINS $name RETURN n"; | ||
| 59 | + | ||
| 60 | + Collection<OrganDTO> organDTOCollection = neo4jClient.query(cypherQuery) | ||
| 61 | + .bind(name).to("name") | ||
| 62 | + .fetchAs(OrganDTO.class) | ||
| 63 | + .mappedBy(((typeSystem, record) -> { | ||
| 64 | + NodeValue nodeValue = (NodeValue) record.get(0); | ||
| 65 | + Map<String, Object> map = nodeValue.asMap(); | ||
| 66 | + OrganDTO organDTO = BeanUtil.toBeanIgnoreError(map, OrganDTO.class); | ||
| 67 | + organDTO.setType(OrganTypeEnum.valueOf(CollUtil.getFirst(nodeValue.asNode().labels())).getCode()); | ||
| 68 | + organDTO.setLatitude(BeanUtil.getProperty(map.get("location"), "y")); | ||
| 69 | + organDTO.setLongitude(BeanUtil.getProperty(map.get("location"), "x")); | ||
| 70 | + return organDTO; | ||
| 71 | + })).all(); | ||
| 72 | + return (List<OrganDTO>) organDTOCollection; | ||
| 73 | + } | ||
| 74 | +} |
sl-express-sdn/src/main/java/com/sl/sdn/repository/impl/TransportLineRepositoryImpl.java
0 → 100644
| 1 | +package com.sl.sdn.repository.impl; | ||
| 2 | + | ||
| 3 | +import cn.hutool.core.collection.CollUtil; | ||
| 4 | +import cn.hutool.core.util.NumberUtil; | ||
| 5 | +import cn.hutool.core.util.StrUtil; | ||
| 6 | +import com.sl.sdn.dto.OrganDTO; | ||
| 7 | +import com.sl.sdn.dto.TransportLineNodeDTO; | ||
| 8 | +import com.sl.sdn.entity.node.AgencyEntity; | ||
| 9 | +import com.sl.sdn.enums.OrganTypeEnum; | ||
| 10 | +import com.sl.sdn.repository.TransportLineRepository; | ||
| 11 | +import com.sl.transport.common.util.BeanUtil; | ||
| 12 | +import org.neo4j.driver.internal.value.PathValue; | ||
| 13 | +import org.neo4j.driver.types.Path; | ||
| 14 | +import org.springframework.beans.factory.annotation.Autowired; | ||
| 15 | +import org.springframework.data.neo4j.core.Neo4jClient; | ||
| 16 | +import org.springframework.data.neo4j.core.schema.Node; | ||
| 17 | +import org.springframework.stereotype.Repository; | ||
| 18 | + | ||
| 19 | +import java.util.Map; | ||
| 20 | +import java.util.Optional; | ||
| 21 | + | ||
| 22 | +/** | ||
| 23 | + * 运输路线相关操作 | ||
| 24 | + */ | ||
| 25 | +@Repository | ||
| 26 | +public class TransportLineRepositoryImpl implements TransportLineRepository { | ||
| 27 | + | ||
| 28 | + @Autowired | ||
| 29 | + private Neo4jClient neo4jClient; | ||
| 30 | + | ||
| 31 | + | ||
| 32 | + /** | ||
| 33 | + * 查询两个网点之间最短的路线,查询深度为:10 | ||
| 34 | + * @param start | ||
| 35 | + * @param end | ||
| 36 | + * @return | ||
| 37 | + */ | ||
| 38 | + @Override | ||
| 39 | + public TransportLineNodeDTO findShortTestPath(AgencyEntity start, AgencyEntity end) { | ||
| 40 | +// 获取网点数据在Neo4j中的类型 | ||
| 41 | + String type = AgencyEntity.class.getAnnotation(Node.class).value()[0]; | ||
| 42 | + | ||
| 43 | +// 构造查询语句 | ||
| 44 | + String cypherQuery = StrUtil.format("MATCH path = shortestPath((start:{}) -[*..10]-> (end:{}))\n" + | ||
| 45 | + "WHERE start.bid = $startId AND end.bid = $endId \n" + | ||
| 46 | + "RETURN path", type, type); | ||
| 47 | + | ||
| 48 | +// 执行查询语句 | ||
| 49 | + Optional<TransportLineNodeDTO> transportLineNodeDTOOptional = neo4jClient.query(cypherQuery) | ||
| 50 | + .bind(start.getBid()).to("startId") // 设置参数 | ||
| 51 | + .bind(end.getBid()).to("endId") // 设置参数 | ||
| 52 | + .fetchAs(TransportLineNodeDTO.class) // 设置想要数据类型 | ||
| 53 | + .mappedBy(((typeSystem, record) -> { // 对响应结果进行处理 | ||
| 54 | + PathValue pathValue = (PathValue) record.get(0); | ||
| 55 | + Path path = pathValue.asPath(); | ||
| 56 | + TransportLineNodeDTO transportLineNodeDTO = new TransportLineNodeDTO(); | ||
| 57 | + | ||
| 58 | +// 读取节点数据 | ||
| 59 | + path.nodes().forEach(node -> { | ||
| 60 | + Map<String, Object> map = node.asMap(); | ||
| 61 | + OrganDTO organDTO = BeanUtil.toBeanIgnoreError(map, OrganDTO.class); | ||
| 62 | + | ||
| 63 | +// 读取第一个标签作为类型 | ||
| 64 | + organDTO.setType(OrganTypeEnum.valueOf(CollUtil.getFirst(node.labels())).getCode()); | ||
| 65 | + | ||
| 66 | +// 查询处理的数据源,x:经度,y:纬度 | ||
| 67 | + organDTO.setLatitude(BeanUtil.getProperty(map.get("location"), "y")); | ||
| 68 | + organDTO.setLongitude(BeanUtil.getProperty(map.get("location"), "x")); | ||
| 69 | + transportLineNodeDTO.getNodeList().add(organDTO); | ||
| 70 | + }); | ||
| 71 | + | ||
| 72 | +// 取2位小数 | ||
| 73 | + transportLineNodeDTO.setCost(NumberUtil.round(transportLineNodeDTO.getCost(), 2).doubleValue()); | ||
| 74 | + return transportLineNodeDTO; | ||
| 75 | + | ||
| 76 | + })).one(); | ||
| 77 | + | ||
| 78 | + | ||
| 79 | + return transportLineNodeDTOOptional.orElse(null); | ||
| 80 | + } | ||
| 81 | + | ||
| 82 | +} |
sl-express-sdn/src/main/resources/application.yml
0 → 100644
| 1 | +server: | ||
| 2 | + port: 9902 | ||
| 3 | +logging: | ||
| 4 | + level: | ||
| 5 | + org.springframework.data.neo4j: debug | ||
| 6 | +spring: | ||
| 7 | + application: | ||
| 8 | + name: sl-express-sdn | ||
| 9 | + mvc: | ||
| 10 | + pathmatch: | ||
| 11 | + #解决异常:swagger Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException | ||
| 12 | + #因为Springfox使用的路径匹配是基于AntPathMatcher的,而Spring Boot 2.6.X使用的是PathPatternMatcher | ||
| 13 | + matching-strategy: ant_path_matcher | ||
| 14 | + data: | ||
| 15 | + neo4j: | ||
| 16 | + database: neo4j | ||
| 17 | + neo4j: | ||
| 18 | + authentication: | ||
| 19 | + username: neo4j | ||
| 20 | + password: neo4j123 | ||
| 21 | + uri: neo4j://192.168.150.101:7687 | ||
| 0 | \ No newline at end of file | 22 | \ No newline at end of file |
sl-express-sdn/src/test/java/com/sl/sdn/repository/AgencyRepositoryTest.java
0 → 100644
| 1 | +package com.sl.sdn.repository; | ||
| 2 | + | ||
| 3 | +import com.sl.sdn.entity.node.AgencyEntity; | ||
| 4 | +import org.junit.jupiter.api.Test; | ||
| 5 | +import org.springframework.beans.factory.annotation.Autowired; | ||
| 6 | +import org.springframework.boot.test.context.SpringBootTest; | ||
| 7 | +import org.springframework.data.domain.Page; | ||
| 8 | +import org.springframework.data.domain.PageRequest; | ||
| 9 | +import org.springframework.data.domain.Sort; | ||
| 10 | + | ||
| 11 | +import java.util.List; | ||
| 12 | + | ||
| 13 | +@SpringBootTest | ||
| 14 | +class AgencyRepositoryTest { | ||
| 15 | + | ||
| 16 | + @Autowired | ||
| 17 | + private AgencyRepository agencyRepository; | ||
| 18 | + | ||
| 19 | + @Test | ||
| 20 | + void findByBid() { | ||
| 21 | + AgencyEntity agencyEntity = agencyRepository.findByBid(9001L); | ||
| 22 | + System.out.println(agencyEntity); | ||
| 23 | + } | ||
| 24 | + | ||
| 25 | + @Test | ||
| 26 | + void deleteByBid() { | ||
| 27 | + AgencyEntity agencyEntity = new AgencyEntity(); | ||
| 28 | + agencyEntity.setBid(9001L); | ||
| 29 | + agencyEntity.setName("测试节点"); | ||
| 30 | + agencyEntity.setPhone("122222222222"); | ||
| 31 | + agencyEntity.setAddress("测试数据地址"); | ||
| 32 | + | ||
| 33 | + agencyRepository.save(agencyEntity); | ||
| 34 | + System.out.println(agencyEntity); | ||
| 35 | + | ||
| 36 | + } | ||
| 37 | + | ||
| 38 | + @Test | ||
| 39 | + public void testUpdate() { | ||
| 40 | + AgencyEntity agencyEntity = agencyRepository.findByBid(9001L); | ||
| 41 | + agencyEntity.setName("测试节点1"); | ||
| 42 | + | ||
| 43 | + agencyRepository.save(agencyEntity); | ||
| 44 | + System.out.println(agencyEntity); | ||
| 45 | + | ||
| 46 | + } | ||
| 47 | + | ||
| 48 | + @Test | ||
| 49 | + public void testDeleteByBid() { | ||
| 50 | + Long count = agencyRepository.deleteByBid(9001L); | ||
| 51 | + System.out.println(count); | ||
| 52 | + } | ||
| 53 | + | ||
| 54 | + | ||
| 55 | + @Test | ||
| 56 | + public void testFindAll() { | ||
| 57 | + List<AgencyEntity> list = agencyRepository.findAll(); | ||
| 58 | + for (AgencyEntity agencyEntity : list) { | ||
| 59 | + System.out.println(agencyEntity); | ||
| 60 | + } | ||
| 61 | + } | ||
| 62 | + | ||
| 63 | + | ||
| 64 | + @Test | ||
| 65 | + public void testPage() { | ||
| 66 | + PageRequest pageRequest = PageRequest.of(1, 2, Sort.by(Sort.Direction.DESC, "bid")); | ||
| 67 | + Page<AgencyEntity> page = agencyRepository.findAll(pageRequest); | ||
| 68 | + page.getContent().forEach(System.out::println); | ||
| 69 | + } | ||
| 70 | + | ||
| 71 | +} | ||
| 0 | \ No newline at end of file | 72 | \ No newline at end of file |
sl-express-sdn/src/test/java/com/sl/sdn/repository/impl/OrganRepositoryImplTest.java
0 → 100644
| 1 | +package com.sl.sdn.repository.impl; | ||
| 2 | + | ||
| 3 | +import com.sl.sdn.dto.OrganDTO; | ||
| 4 | +import com.sl.sdn.repository.OrganRepository; | ||
| 5 | +import org.junit.jupiter.api.Test; | ||
| 6 | +import org.springframework.beans.factory.annotation.Autowired; | ||
| 7 | +import org.springframework.boot.test.context.SpringBootTest; | ||
| 8 | + | ||
| 9 | +import java.util.List; | ||
| 10 | + | ||
| 11 | +import static org.junit.jupiter.api.Assertions.*; | ||
| 12 | + | ||
| 13 | +@SpringBootTest | ||
| 14 | +class OrganRepositoryImplTest { | ||
| 15 | + | ||
| 16 | + @Autowired | ||
| 17 | + private OrganRepository organRepository; | ||
| 18 | + | ||
| 19 | + @Test | ||
| 20 | + void findByBid() { | ||
| 21 | + OrganDTO byBid = organRepository.findByBid(100280L); | ||
| 22 | + System.out.println(byBid); | ||
| 23 | + } | ||
| 24 | + | ||
| 25 | + @Test | ||
| 26 | + void findAll() { | ||
| 27 | + List<OrganDTO> organRepositoryAll = organRepository.findAll("北京"); | ||
| 28 | + System.out.println(organRepositoryAll); | ||
| 29 | + } | ||
| 30 | +} | ||
| 0 | \ No newline at end of file | 31 | \ No newline at end of file |
sl-express-sdn/src/test/java/com/sl/sdn/repository/impl/TransportLineRepositoryImplTest.java
0 → 100644
| 1 | +package com.sl.sdn.repository.impl; | ||
| 2 | + | ||
| 3 | +import com.sl.sdn.dto.TransportLineNodeDTO; | ||
| 4 | +import com.sl.sdn.entity.node.AgencyEntity; | ||
| 5 | +import com.sl.sdn.repository.TransportLineRepository; | ||
| 6 | +import org.junit.jupiter.api.Test; | ||
| 7 | +import org.springframework.beans.factory.annotation.Autowired; | ||
| 8 | +import org.springframework.boot.test.context.SpringBootTest; | ||
| 9 | + | ||
| 10 | +@SpringBootTest | ||
| 11 | +class TransportLineRepositoryImplTest { | ||
| 12 | + | ||
| 13 | + @Autowired | ||
| 14 | + private TransportLineRepository transportLineRepository; | ||
| 15 | + | ||
| 16 | + @Test | ||
| 17 | + void findShortTestPath() { | ||
| 18 | + AgencyEntity start = AgencyEntity.builder().bid(100280L).build(); | ||
| 19 | + AgencyEntity end = AgencyEntity.builder().bid(210057L).build(); | ||
| 20 | + | ||
| 21 | + TransportLineNodeDTO shortTestPath = transportLineRepository.findShortTestPath(start, end); | ||
| 22 | + System.out.println(shortTestPath); | ||
| 23 | + } | ||
| 24 | +} | ||
| 0 | \ No newline at end of file | 25 | \ No newline at end of file |