package com.lovenav.controller; import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONArray; 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.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; 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.ResponseBody; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.*; import java.io.*; @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); countCateContainUrlNumber(email); HashMap 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 urlCateLists = urlCateListService.selectUrListByUserId(Integer.valueOf(userId)); List urlLists = urlListService.selectUrList(); List 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 parentsList = new ArrayList<>(); //声明返回集合 List 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 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 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 result = new HashMap<>(); result.put("code", 200); result.put("msg", "查询成功"); result.put("data",urlList); String jsonString = JSONObject.toJSONString(result); return jsonString; } public String countCateContainUrlNumber(String email){ HashMap result = new HashMap<>(); result.put("code", 200); result.put("msg", "查询成功"); result.put("data",urlCateListService.countCateContainUrlNumber(email)); String jsonString = JSONObject.toJSONString(result); return jsonString; } //处理成JSON // @RequestMapping("/disposeJsonToExcel") // public String disposeJsonToExcel(String userId) // { // // HashMap result = new HashMap<>(); // result.put("code", 200); // result.put("msg", "查询成功"); // result.put("data",urlCateListService.getUrl(userId)); // String jsonString = JSONObject.toJSONString(result); // return jsonString; // } /** * json 转 excel * @param jsonArray * @return * @throws IOException */ public HSSFWorkbook jsonToExcel(JSONArray jsonArray) throws IOException { Set keys = new HashSet<>(); // 创建HSSFWorkbook对象 HSSFWorkbook wb = new HSSFWorkbook(); // 创建HSSFSheet对象 HSSFSheet sheet = wb.createSheet("sheet0"); List jsonObjects = jsonArray.toList(JSONObject.class); // 创建HSSFRow对象 HSSFRow row ; for (int i=0;i result = new HashMap<>(); result.put("code", 500); result.put("msg", "下载失败"); return JSON.toJSONString(result); } } }