package com.neo4j.controller;
import com.neo4j.domain.Movie;
import com.neo4j.domain.Person;
import com.neo4j.domain.Roles;
import com.neo4j.domain.SystemEntity;
import com.neo4j.repository.MovieRepository;
import com.neo4j.repository.PersonRepository;
import com.neo4j.repository.SystemRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.Collections;
import java.util.List;
@Controller
@RequestMapping("/neo4j")
public class Neo4jController {
@Autowired
private MovieRepository movieRepository;
@Autowired
private PersonRepository personRepository;
@Autowired
private SystemRepository systemRepository;
/**
* 使用repository操作图数据
*
* @return
*/
@GetMapping("/testByRepository")
@ResponseBody
public List
testByRepository() {
// 删除所有节点和关系(删除节点会响应删除关联关系),避免后续创建节点重复影响
movieRepository.deleteAll();
personRepository.deleteAll();
// 创建节点
Movie movie = new Movie("流浪地球", "是由中国电影股份有限公司、北京京西文化旅游股份有限公司、郭帆文化传媒(北京)有限公司、北京登峰国际文化传播有限公司联合出品,由郭帆执导,吴京特别出演、屈楚萧、赵今麦、李光洁、吴孟达等领衔主演的科幻冒险电影");
// 添加关系
movie.getActorsAndRoles().add(new Roles(new Person(1994, "刘启"), Collections.singletonList("初级驾驶员")));
movie.getActorsAndRoles().add(new Roles(new Person(2002, "刘培强"), Collections.singletonList("中国航天员")));
movie.getActorsAndRoles().add(new Roles(new Person(1952, "韩子昂"), Collections.singletonList("高级驾驶员")));
movie.getActorsAndRoles().add(new Roles(new Person(2002, "韩朵朵"), Collections.singletonList("初中生")));
movie.getActorsAndRoles().add(new Roles(new Person(1981, "王磊"), Collections.singletonList("救援队队长")));
movie.getActorsAndRoles().add(new Roles(new Person(1991, "李一一"), Collections.singletonList("技术观察员")));
movie.getActorsAndRoles().add(new Roles(new Person(1974, "何连科"), Collections.singletonList("救援队队员")));
movie.getActorsAndRoles().add(new Roles(new Person(1991, "Tim"), Collections.singletonList("中美混血儿")));
movie.getDirectors().add(new Person(1974, "吴京"));
// 存入图数据库持久化
movieRepository.save(movie);
// 查询
Person person = personRepository.findPersonEntityByName("刘启");
System.out.println(person);
Movie movie1 = movieRepository.findMovieByTitle("流浪地球");
System.out.println(movie1);
Movie movie2 = movieRepository.findMovieById(movie.getId());
System.out.println(movie2);
// 注意:repository的save方法【对应的实体若id一致】则为修改,否则为新建。
person.setBorn(1997);
personRepository.save(person);
person = personRepository.findPersonEntityByName("刘启");
List all = personRepository.findAll();
return all;
}
/**
* 新增SystemEntity节点
*/
@GetMapping("/addSystemNode")
public void addSystemNode() {
systemRepository.deleteAll();
SystemEntity systemEntity = new SystemEntity();
systemEntity.setName("系统A"); // 26
systemRepository.save(systemEntity);
System.out.println("系统A" + "----------" + systemEntity.getId());
SystemEntity systemEntity1 = new SystemEntity();
systemEntity1.setName("系统B");// 27
systemRepository.save(systemEntity1);
System.out.println("系统B" + "----------" + systemEntity1.getId());
SystemEntity systemEntity2 = new SystemEntity();
systemEntity2.setName("系统C");// 28
systemRepository.save(systemEntity2);
System.out.println("系统C" + "----------" + systemEntity2.getId());
SystemEntity systemEntity3 = new SystemEntity();
systemEntity3.setName("系统D");// 29
systemRepository.save(systemEntity3);
System.out.println("系统D" + "----------" + systemEntity3.getId());
SystemEntity systemEntity4 = new SystemEntity();
systemEntity4.setName("系统E");// 30
systemRepository.save(systemEntity4);
System.out.println("系统E" + "----------" + systemEntity4.getId());
SystemEntity systemEntity5 = new SystemEntity();
systemEntity5.setName("系统F");// 31
systemRepository.save(systemEntity5);
System.out.println("系统F" + "----------" + systemEntity5.getId());
}
@GetMapping("/addInvokeRelation")
public void addInvokeRelation() {
systemRepository.addInvokeRelation(26L, 27L);
systemRepository.addInvokeRelation(26L, 28L);
systemRepository.addInvokeRelation(29L, 26L);
systemRepository.addInvokeRelation(29L, 28L);
systemRepository.addInvokeRelation(29L, 28L);
}
@GetMapping("/addConsumeRelation")
public void addConsumeRelation() {
systemRepository.addConsumeRelation(30L, 31L);
systemRepository.addConsumeRelation(29L, 30L);
}
// 分页查询
@GetMapping("/pageListSystem")
@ResponseBody
public List pageListSystem(String name) {
PageRequest pageRequest = PageRequest.of(0, 4, Sort.by(Sort.Direction.DESC, "name"));
Page page = systemRepository.findByName(name, pageRequest);
List list = page.getContent();
list.forEach(System.out::println);
return list;
}
/**
* 删除指定节点直接的关系 DELETE ,,
*
* @return
*/
@DeleteMapping("/deleteConsumeRelation")
@ResponseBody
public String deleteConsumeRelation2() {
try {
Long from = 28L, to = 29L;
systemRepository.deleteConsumeRelation(from, to);
} catch (Exception e) {
e.printStackTrace();
return "删除关系失败";
}
return "删除关系成功";
}
@GetMapping("/findSystemById")
@ResponseBody
public SystemEntity findSystemById(@RequestParam("id") Long id) {
SystemEntity systemEntity = systemRepository.findSystemById(id);
return systemEntity;
}
@GetMapping("/getAllSystemNode")
@ResponseBody
public List getAllSystemNode() {
List all = systemRepository.findAll();
return all;
}
}