一、封装信息类
将前端传到后端的模型图的json数据封装成类,方便对其进行创新方向,关系列表的生成。
分析前端传来的json数据:rootId存放模型图的根节点;nodes存放所有节点的信息,包括id、name、表示节点性质的颜色color以及父节点的id;links存放所有关系的信息,包括关系的父节点id、子节点id、关系信息:需要或产生、关系是否隐藏。
创建model.java
@Data
public class Model {
//根节点id
private String rootId;
//结点
private List<Node> nodes;
//关系
private List<Link> links;
}
创建Node.java
@Data
public class Node {
//结点id
private String id;
//结点名称
private String name;
//结点性质:#89B8CA有用、#ca8989有害
private String color;
//父结点
private String preId;
}
创建Link.java
@Data
public class Link {
private String from;
private String to;
//“需要”是连接到根节点,“产生”是其他节点
private String text;
//是否隐藏
private boolean isHide = false;
}
封装返回的创新方向数据 Direction.java
public class Direction {
private String id;
private String type;
private String clash;
private String clashMessage;
private List<String> suggestion;
}
二、封装工具类
创建DirectionUtil.java,封装处理前端传入的json数据、生成创新方向的方法。
transform方法
将json字符串转为Model对象并且将link列表中text属性为需要和isHide为true的Link去掉。
public Model transform (String jsonString) throws JsonProcessingException {
JSONObject json = JSONUtil.parseObj(jsonString);
Model model = JSONUtil.toBean(json,Model.class);
JSONArray jsonArray1 = new JSONArray(json.getObj("nodes"));
JSONArray jsonArray2 = new JSONArray(json.getObj("links"));
model.setNodes(jsonArray1.toList(Node.class));
List<Link> linkList = jsonArray2.toList(Link.class);
//数据清洗
linkList.removeIf(link -> link.getText().equals("需要") || link.isHide());
model.setLinks(linkList);
return model;
}
createList方法
解析传入的Model对象,生成创新方向列表
public List<Direction> createList(Model model){
List<Direction> directionList = new ArrayList<>();
//遍历结点
for (Node node : model.getNodes()) {
//对每个结点创建Direction对象、suggestion列表和StringBuffer对象
Direction direction = new Direction();
StringBuffer stringBuffer = new StringBuffer();
StringBuffer stringClash= new StringBuffer();
List<String> suggestion = new ArrayList<>();
//对每个结点创建两个list,存储指向结点的id
List<String> goodNodes = new ArrayList<>();
List<String> badNodes = new ArrayList<>();
//若结点为根节点、跳过
if (node.getId().equals("root")) {
}
else {
if (node.getColor().equals("#89B8CA")) { //如果结点性质为有用,存储id,添加方向
direction.setId(node.getId());
stringBuffer.append("保证 ").append(direction.getId()).append(" 功能的实现");
suggestion.add(stringBuffer.toString());
stringBuffer.delete(0,stringBuffer.length());
//
childNodes(model,node,goodNodes,badNodes);
//技术冲突
if (!goodNodes.isEmpty() && !badNodes.isEmpty()) {
stringClash.append("技术冲突: 系统应具有 ").append(node.getId());
techClash(direction,node,stringBuffer,stringClash,suggestion,goodNodes,badNodes);
}
else if (goodNodes.isEmpty() && badNodes.isEmpty()) {
direction.setType("#e0e5df");
}
//物理冲突
else if (goodNodes.isEmpty()){
direction.setClash("物理冲突");
direction.setType("#f8ebd8");
stringBuffer.append("避免 ");
stringClash.append("物理冲突: 系统中应具有 ").append(direction.getId()).append(",但同时会伴生 ");
for (String bad : badNodes){
stringBuffer.append(bad).append(" ");
stringClash.append(bad).append(" ");
}
stringBuffer.append("的产生");
suggestion.add(stringBuffer.toString());
stringBuffer.delete(0,stringBuffer.length());
}
//普通有用类
else {
direction.setType("#e0e5df");
stringBuffer.append("不会消除 ");
for (String good : goodNodes)
stringBuffer.append(good).append(" ");
stringBuffer.append("的功能");
suggestion.add(stringBuffer.toString());
stringBuffer.delete(0,stringBuffer.length());
}
if (node.getPreId() != null){
stringBuffer.append("尽可能不依靠 ").append(node.getPreId());
suggestion.add(stringBuffer.toString());
}
}
else if (node.getColor().equals("#ca8989")) {
direction.setId(node.getId());
stringBuffer.append("尽可能避免 ").append(direction.getId()).append(" 的负面影响");
suggestion.add(stringBuffer.toString());
stringBuffer.delete(0,stringBuffer.length());
//
childNodes(model,node,goodNodes,badNodes);
//技术冲突
if (!goodNodes.isEmpty() && !badNodes.isEmpty()) {
stringClash.append("技术冲突: 系统中本身不应该存在 ").append(node.getId());
techClash(direction,node,stringBuffer,stringClash,suggestion,goodNodes,badNodes);
}
//无子节点
else if (goodNodes.isEmpty() && badNodes.isEmpty()) {
direction.setType("#ead0d1");
}
//物理冲突
else if (!goodNodes.isEmpty()){
direction.setClash("物理冲突");
direction.setType("#f8ebd8");
stringBuffer.append("不会消除 ");
stringClash.append("物理冲突: 系统中本身不应该存在 ").append(direction.getId()).append(",但又需要用于提供 ");
for (String good : goodNodes) {
stringBuffer.append(good).append(" ");
stringClash.append(good).append(" ");
}
stringBuffer.append("的功能");
suggestion.add(stringBuffer.toString());
stringBuffer.delete(0,stringBuffer.length());
}
//普通有害类
else {
direction.setType("#ead0d1");
stringBuffer.append("避免 ");
for (String bad : badNodes)
stringBuffer.append(bad).append(" ");
stringBuffer.append("的产生");
suggestion.add(stringBuffer.toString());
stringBuffer.delete(0,stringBuffer.length());
}
if (node.getPreId() != null){
stringBuffer.append("尽可能保留 ").append(node.getPreId());
suggestion.add(stringBuffer.toString());
}
}
direction.setSuggestion(suggestion);
direction.setClashMessage(stringClash.toString());
directionList.add(direction);
}
}
return directionList;
}
childNodes方法
遍历link列表,找到传入节点的父节点,找到传入节点的子节点、判断其性质并将其添加到对应的子节点列表
public void childNodes(Model model, Node node, List<String> gNodes, List<String> bNodes){
for (Link link : model.getLinks()) { //对关系进行遍历
if (link.getTo().equals(node.getId())) { //找到当前结点的父结点
node.setPreId(link.getFrom());
}
if (link.getFrom().equals(node.getId())) { //找到父结点为当前结点的关系
for (Node node1 : model.getNodes()){ //对结点进行遍历
if (node1.getId().equals(link.getTo())) { //找到子节点
if (node1.getColor().equals("#89B8CA")){ //对子结点的性质进行判断,并将其添加到对应列表中
gNodes.add(node1.getId());
}
else {
bNodes.add(node1.getId());
}
}
}
}
}
}
techClash方法
对传入的direction设置冲突类型和节点类型,设置创新方向对象的suggestion
public void techClash(Direction direction,Node node,StringBuffer sb,StringBuffer sc,List<String> strings,List<String> goodNodes,List<String> badNodes){
StringBuffer temp1 = new StringBuffer();
StringBuffer temp2 = new StringBuffer();
direction.setClash("技术冲突");
direction.setType("#f8ebd8");
sb.append("不会消除 ");
if (node.getColor().equals("#89B8CA"))
temp1.append(",用于提供 ");
else
temp2.append(",但需要用于提供 ");
for (String good : goodNodes) {
sb.append(good).append(" ");
temp1.append(good).append(" ");
temp2.append(good).append(" ");
}
if (node.getColor().equals("#89B8CA"))
sc.append(temp1.toString());
sb.append("的功能");
strings.add(sb.toString());
sb.delete(0,sb.length());
sb.append("避免 ");
if (node.getColor().equals("#89B8CA"))
sc.append(",但同时会伴生 ");
else
sc.append(",且会伴生 ");
for (String bad : badNodes){
sb.append(bad).append(" ");
sc.append(bad).append(" ");
}
sb.append("的产生");
strings.add(sb.toString());
sb.delete(0,sb.length());
if (node.getColor().equals("#ca8989"))
sc.append(temp2.toString());
}
linksToString方法
根据模型图json字符串生成关系信息列表
public List<String> linksToString(String jsonString) throws JsonProcessingException {
Model model = transform(jsonString);
StringBuffer stringBuffer = new StringBuffer();
List<String> result = new ArrayList<>();
List<Link> linkList = model.getLinks();
for (Link link : linkList) {
for (Node node : model.getNodes()){
if (node.getId().equals(link.getFrom())){
if (node.getColor().equals("#89B8CA")){
stringBuffer.append("有用特性 ").append(node.getId());
}
else if (node.getColor().equals("#ca8989")){
stringBuffer.append("有害特性 ").append(node.getId());
}
}
}
for (Node node : model.getNodes()){
if (node.getId().equals(link.getTo())){
if (node.getColor().equals("#89B8CA")){
stringBuffer.append("产生了有用特性 ").append(node.getId());
}
else if (node.getColor().equals("#ca8989")){
stringBuffer.append("产生了有害特性 ").append(node.getId());
}
}
}
result.add(stringBuffer.toString());
stringBuffer.delete(0,stringBuffer.length());
}
return result;
}
三、service实现类
创建引导方法
@Override
public Result insertGuide(Guide guide) {
QueryWrapper<Guide> queryWrapper1 = new QueryWrapper<>();
queryWrapper1.eq("user_id",guide.getUserId());
queryWrapper1.eq("name",guide.getName());
Guide one = guideService.getOne(queryWrapper1);
//根据用户id和引导名查找,如果找到,插入失败
if (one != null) {
return Result.error(Constants.CODE_600,"您已创建过相同名称的创新引导,请前往个人中心查看。");
}
//若未在数据库中找到重复数据,将数据插入数据库,返回引导信息
else {
guideService.save(guide);
one = guideService.getOne(queryWrapper1);
return Result.success("创新引导创建成功,已为您生成专属笔记。",one);
}
}
搜索引导方法
@Override
public List<Guide> findByName(int userId, String guideName) {
LambdaQueryWrapper<Guide> wrapper= Wrappers.lambdaQuery();
wrapper.eq(Guide::getUserId,userId);
if (StrUtil.isNotBlank(guideName)){
wrapper.like(Guide::getName,guideName);
}
return guideService.list(wrapper);
}
根据引导id生成关系信息列表方法
@Override
public Result getModelLinks(int id) throws JsonProcessingException {
DirectionUtil directionUtil = new DirectionUtil();
Guide guide = guideService.getById(id);
return Result.success(directionUtil.linksToString(guide.getModel()));
}
根据用户id查找引导列表方法
@Override
public Result findByUser(int userId) {
QueryWrapper<Guide> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("user_id",userId);
List<Guide> result = guideService.list(queryWrapper);
result.sort((t1,t2)->t2.getCreateTime().compareTo(t1.getCreateTime()));//按创建时间排序
return Result.success(result);
}
生成创新方向列表方法
@Override
public Result createDirection(String jsonString) throws JsonProcessingException {
DirectionUtil directionUtil = new DirectionUtil();
Model model = directionUtil.transform(jsonString);
return Result.success(directionUtil.createList(model));
}
四、controller层接口
//更新引导信息
@PostMapping
public Result update(@RequestBody Guide guide) {
return Result.success("创新引导保存成功",guideService.saveOrUpdate(guide));
}
//新增引导信息
@PostMapping("/insert")
public Result insert(@RequestBody Guide guide) {
return guideService.insertGuide(guide);
}
//根据id删除
@DeleteMapping("/{id}")
public Result delete(@PathVariable Integer id) {
return Result.success(guideService.removeById(id));
}
//根据id批量删除
@PostMapping("/del/batch")
public boolean deleteBatch(@RequestBody List<Integer> ids) {
return guideService.removeByIds(ids);
}
//查找所有
@GetMapping
public List<Guide> findAll() {
return guideService.list();
}
//根据id查找
@GetMapping("/{id}")
public Result findOne(@PathVariable Integer id) {
return Result.success(guideService.getById(id));
}
//根据用户id和搜索的引导内容批量查找
@GetMapping("/search")
public List<Guide> findByName(@RequestParam Integer userId,@RequestParam String name) {
return guideService.findByName(userId,name);
}
//获取关系信息
@GetMapping("/getModelLinks")
public Result getModelLinks(@RequestParam Integer id) throws JsonProcessingException {
return guideService.getModelLinks(id);
}
//根据用户id查找
@GetMapping("/user_id/{user_id}")
public Result findByUser(@PathVariable Integer user_id) {
return guideService.findByUser(user_id);
}
//前端传入JSON字符串,生成创新方向列表
@GetMapping("/createDirection")
public Result createDirection(@RequestParam String jsonString) throws JsonProcessingException {
return guideService.createDirection(jsonString);
}