Skip to content

Commit

Permalink
实现图数据库和前端优化
Browse files Browse the repository at this point in the history
  • Loading branch information
Chandler-EZ committed Jan 20, 2025
1 parent a352031 commit 06991e5
Show file tree
Hide file tree
Showing 332 changed files with 965 additions and 78,301 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation group: 'org.neo4j.driver', name: 'neo4j-java-driver', version: '5.27.0'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
runtimeOnly 'com.mysql:mysql-connector-j'
annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/org/eis/frontend/config/Neo4jConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.eis.frontend.config;

import org.neo4j.driver.AuthTokens;
import org.neo4j.driver.Driver;
import org.neo4j.driver.GraphDatabase;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class Neo4jConfig {

@Bean
public Driver neo4jDriver() {
return GraphDatabase.driver(
"bolt://localhost:7687",
AuthTokens.basic("neo4j", "dbvalar412")
);
}
}
63 changes: 49 additions & 14 deletions src/main/java/org/eis/frontend/controller/EnterpriseController.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.PathVariable;
import java.util.List;

@Controller
Expand All @@ -22,26 +23,29 @@ public String index() {
}

// 2. 根据企业名称查询

@GetMapping("/searchByName")
public String searchByName(
@RequestParam(name = "name", required = false) String name,
@RequestParam(name = "searchType", required = false) String searchType,
Model model) {
// 首次加载页面(name 参数为 null)
if (name == null) {
return "enterpriseList"; // 直接返回页面,不设置任何数据
model.addAttribute("searchType", "name");
return "enterpriseList";
}

// 输入为空时,设置提示信息
if (name.trim().isEmpty()) {
model.addAttribute("message", "请输入企业名称进行搜索!");
model.addAttribute("enterprises", List.of()); // 返回空列表
model.addAttribute("enterprises", List.of());
model.addAttribute("searchType", searchType);
return "enterpriseList";
}

// 正常查询
List<Enterprise> enterprises = enterpriseService.searchEnterpriseByName(name);
model.addAttribute("enterprises", enterprises);
model.addAttribute("searchType", searchType);

// 如果结果为空,设置提示信息
if (enterprises.isEmpty()) {
Expand All @@ -51,32 +55,34 @@ public String searchByName(
return "enterpriseList";
}



// 3. 根据企业简称查询
@GetMapping("/searchByAbbreviation")
public String searchByAbbreviation(
@RequestParam(name = "abbreviation", required = false) String name,
@RequestParam(name = "abbreviation", required = false) String abbreviation,
@RequestParam(name = "searchType", required = false) String searchType,
Model model) {
// 首次加载页面(name 参数为 null)
if (name == null) {
return "enterpriseList"; // 直接返回页面,不设置任何数据
// 首次加载页面(abbreviation 参数为 null)
if (abbreviation == null) {
model.addAttribute("searchType", "abbreviation");
return "enterpriseList";
}

// 输入为空时,设置提示信息
if (name.trim().isEmpty()) {
model.addAttribute("message", "请输入企业名称进行搜索!");
model.addAttribute("enterprises", List.of()); // 返回空列表
if (abbreviation.trim().isEmpty()) {
model.addAttribute("message", "请输入企业简称进行搜索!");
model.addAttribute("enterprises", List.of());
model.addAttribute("searchType", searchType);
return "enterpriseList";
}

// 正常查询
List<Enterprise> enterprises = enterpriseService.searchEnterpriseByAbbreviation(name);
List<Enterprise> enterprises = enterpriseService.searchEnterpriseByAbbreviation(abbreviation);
model.addAttribute("enterprises", enterprises);
model.addAttribute("searchType", searchType);

// 如果结果为空,设置提示信息
if (enterprises.isEmpty()) {
model.addAttribute("message", "未找到相关企业,请尝试其他名称!");
model.addAttribute("message", "未找到相关企业,请尝试其他简称!");
}

return "enterpriseList";
Expand All @@ -99,4 +105,33 @@ public String getEnterpriseInfoAnswer() {
return "enterpriseChat"; // 返回问答结果页面
}

// 6. 查看企业详情
@GetMapping("/enterpriseDetail")
public String getEnterpriseDetail(
@RequestParam(name = "name", required = false) String name,
Model model) {
// 首次加载页面(name 参数为 null)
if (name == null) {
return "enterpriseDetail";
}

// 输入为空时,设置提示信息
if (name.trim().isEmpty()) {
model.addAttribute("message", "请输入企业名称!");
model.addAttribute("enterprises", List.of());
return "enterpriseDetail";
}

// 正常查询
List<Enterprise> enterprises = enterpriseService.searchEnterpriseByName(name);
model.addAttribute("enterprises", enterprises);

// 如果结果为空,设置提示信息
if (enterprises.isEmpty()) {
model.addAttribute("message", "未找到相关企业!");
}

return "enterpriseDetail";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.eis.frontend.controller;

import org.neo4j.driver.Driver;
import org.neo4j.driver.Session;
import org.neo4j.driver.Result;
import org.neo4j.driver.Values;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.http.MediaType;
import java.util.*;

@RestController
public class Neo4jEnterpriseController {

private final Driver driver;

public Neo4jEnterpriseController(Driver driver) {
this.driver = driver;
}

@GetMapping(path = "/api/related-enterprises", produces = MediaType.APPLICATION_JSON_VALUE)
public List<String> getRelatedEnterprises(@RequestParam(name = "name") String name) {
try (Session session = driver.session()) {
String query = """
MATCH (c1:企业 {name: $name})-[r]-(c2:企业)
WITH c2,
CASE WHEN '经营范围' IN collect(type(r)) THEN 1 ELSE 0 END as scopeCount,
collect(type(r)) as allTypes
WITH c2,
size([x IN allTypes WHERE x <> '经营范围']) + scopeCount as relationCount,
[x IN allTypes WHERE x <> '经营范围'] +
CASE WHEN '经营范围' IN allTypes THEN ['经营范围'] ELSE [] END as relationTypes
WHERE relationCount >= 3
RETURN c2.name as 关联企业
ORDER BY relationCount DESC
LIMIT 10
""";

Result result = session.run(query, Values.parameters("name", name));
List<String> enterprises = new ArrayList<>();

while (result.hasNext()) {
enterprises.add(result.next().get("关联企业").asString());
}

return enterprises;
} catch (Exception e) {
return new ArrayList<>();
}
}
}
4 changes: 2 additions & 2 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ spring.application.name=frontEnd
spring.thymeleaf.enabled=true

#
spring.datasource.url=jdbc:mysql://localhost:3306/sys?useSSL=false&serverTimezone=UTC
spring.datasource.url=jdbc:mysql://localhost:3306/java?useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=114514
spring.datasource.password=dbvalar412
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.thymeleaf.suffix=.html
# JPA ??
Expand Down
Loading

0 comments on commit 06991e5

Please sign in to comment.