一系列接口
This commit is contained in:
188
src/main/java/com/lovenav/controller/UrlAndCateController.java
Normal file
188
src/main/java/com/lovenav/controller/UrlAndCateController.java
Normal file
@@ -0,0 +1,188 @@
|
||||
package com.lovenav.controller;
|
||||
|
||||
import com.alibaba.fastjson2.JSONObject;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import com.lovenav.entity.UrlCateList;
|
||||
import com.lovenav.entity.UrlList;
|
||||
import com.lovenav.service.UrlCateListService;
|
||||
import com.lovenav.service.UrlListService;
|
||||
import com.lovenav.vo.CateAndUrl;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/UrlAndCate")
|
||||
public class UrlAndCateController {
|
||||
|
||||
@Autowired
|
||||
ObjectMapper objectMapper;
|
||||
|
||||
@Autowired
|
||||
UrlCateListService urlCateListService;
|
||||
|
||||
@Autowired
|
||||
UrlListService urlListService;
|
||||
|
||||
|
||||
//处理JSON
|
||||
@RequestMapping("/disposeJson")
|
||||
public String disposeJson(@RequestBody String data2 ,String email)
|
||||
{
|
||||
String jsonString;
|
||||
//先转换成ObjectMapper类型
|
||||
ObjectNode objectNode = objectMapper.createObjectNode();
|
||||
try {
|
||||
JsonNode rootNode = objectMapper.readTree(data2);
|
||||
disposeBookmarkFunction1(rootNode,"top",email);
|
||||
HashMap<String, Object> result = new HashMap<>();
|
||||
result.put("code", 200);
|
||||
result.put("msg", "查询成功");
|
||||
jsonString = JSONObject.toJSONString(result);
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return jsonString;
|
||||
}
|
||||
public void disposeBookmarkFunction1(JsonNode rootNode,String parent,String email)
|
||||
{
|
||||
|
||||
for(int i=0;i<rootNode.size();i++)
|
||||
{
|
||||
JsonNode sonNode = rootNode.get(i);
|
||||
if(String.valueOf(sonNode.get("folder")).equals("true")){
|
||||
urlCateListService.selectAndInsertUrlCate(email,String.valueOf(sonNode.get("name")),parent);
|
||||
JsonNode children = sonNode.get("children");
|
||||
disposeBookmarkFunction1(children, String.valueOf(sonNode.get("name")),email);
|
||||
|
||||
}else{
|
||||
String name = String.valueOf(sonNode.get("name"));
|
||||
String url = String.valueOf(sonNode.get("url"));
|
||||
String adddata = String.valueOf(sonNode.get("adddata"));
|
||||
urlListService.selectCateAndInsertUrl(parent,name,"123",url,email);
|
||||
System.out.println(name +"---" + url + "---" + adddata + "---" + parent );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return ;
|
||||
}
|
||||
|
||||
|
||||
|
||||
//处理成JSON
|
||||
@RequestMapping("/disposeBookmarkToJson")
|
||||
public String disposeBookmarkToJson(Integer userId)
|
||||
{
|
||||
List<UrlCateList> urlCateLists = urlCateListService.selectUrListByUserId(userId);
|
||||
List<UrlList> urlLists = urlListService.selectUrList();
|
||||
|
||||
List<CateAndUrl> cateAndUrlList = new ArrayList<>();
|
||||
|
||||
|
||||
|
||||
|
||||
//预处理为CateAndUrl对象
|
||||
for( int i = 0 ; i < urlCateLists.size() ; i++)
|
||||
{
|
||||
CateAndUrl cateAndUrl = new CateAndUrl();
|
||||
cateAndUrl.setFloder("true");
|
||||
cateAndUrl.setName(urlCateLists.get(i).getName());
|
||||
cateAndUrl.setParentId(urlCateLists.get(i).getRootCateId());
|
||||
cateAndUrl.setCateId(urlCateLists.get(i).getId());
|
||||
cateAndUrlList.add(cateAndUrl);
|
||||
|
||||
}
|
||||
for( int i = 0 ; i < urlLists.size() ; i++)
|
||||
{
|
||||
CateAndUrl cateAndUrl = new CateAndUrl();
|
||||
cateAndUrl.setFloder("false");
|
||||
cateAndUrl.setName(urlLists.get(i).getName());
|
||||
cateAndUrl.setParentId(urlLists.get(i).getCateId());
|
||||
cateAndUrl.setUrlId(urlLists.get(i).getId());
|
||||
cateAndUrlList.add(cateAndUrl);
|
||||
|
||||
}
|
||||
|
||||
List<CateAndUrl> parentsList = new ArrayList<>();
|
||||
//声明返回集合
|
||||
List<CateAndUrl> resultList = new ArrayList<>();
|
||||
//对全部数据进行遍历
|
||||
for (CateAndUrl disease : cateAndUrlList) {
|
||||
//判断是否是第一梯队,或者说是树的根节点,如果是根节点就加入到父类集合与返回集合
|
||||
if (disease.getParentId() == 0) {
|
||||
parentsList.add(disease);
|
||||
resultList.add(disease);
|
||||
} else {
|
||||
//对于不是第一梯队的数据进行遍历
|
||||
for (CateAndUrl parent : parentsList) {
|
||||
//对数据的pid与父类集合中的父节点进行配对,如果配对成功,就把数据加入到父节点中的子节点集合
|
||||
|
||||
if (parent.getFloder().equals("true") ) {
|
||||
if( parent.getCateId() == disease.getParentId())
|
||||
{
|
||||
parent.getChildUC().add(disease);
|
||||
//当前数据有可能是别的数据的父节点,加到父类容器
|
||||
parentsList.add(disease);
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
if (parent.getFloder().equals("false") ) {
|
||||
if(parent.getUrlId() == disease.getParentId())
|
||||
{
|
||||
parent.getChildUC().add(disease);
|
||||
//当前数据有可能是别的数据的父节点,加到父类容器
|
||||
parentsList.add(disease);
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
String Json;
|
||||
HashMap<String, Object> result = new HashMap<>();
|
||||
// java对象转换为json字符换
|
||||
try {
|
||||
Json = mapper.writeValueAsString(resultList);
|
||||
result.put("code", 200);
|
||||
result.put("msg", "处理成功");
|
||||
result.put("data", resultList);
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return JSONObject.toJSONString(result);
|
||||
}
|
||||
public String setResult(Integer code, String msg) {
|
||||
HashMap<String, Object> result = new HashMap<>();
|
||||
result.put("code", code);
|
||||
result.put("msg", msg);
|
||||
String jsonString = JSONObject.toJSONString(result);
|
||||
return jsonString;
|
||||
}
|
||||
|
||||
//详情页面
|
||||
@RequestMapping("/clickUrl")
|
||||
public String clickUrl(String urlId)
|
||||
{
|
||||
|
||||
UrlList urlList = urlListService.selectUrlListByUrlId(Long.valueOf(urlId));
|
||||
HashMap<String, Object> result = new HashMap<>();
|
||||
result.put("code", 200);
|
||||
result.put("msg", "查询成功");
|
||||
result.put("data",urlList);
|
||||
String jsonString = JSONObject.toJSONString(result);
|
||||
return jsonString;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user