package com.travel.controller; import com.travel.model.Travel; import com.travel.model.TravelComments; import com.travel.service.TravelCommentsService; import com.travel.util.BeanMap; import com.travel.util.ParsingToken; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletRequest; import java.util.*; /** * 游记评论 */ @RestController @RequestMapping("comments") public class TravelCommentsController { @Autowired private TravelCommentsService travelCommentsService; /** * 新增游记评论 * @param comments 参数 * @param request token * @return 返回结果 */ @PostMapping("addComments") public Map addComments(@RequestBody TravelComments comments, HttpServletRequest request){ Map map = new HashMap<>(); try{ //获取token Map userMap = ParsingToken.tokenParsing(request); comments.setCode((String) userMap.get("code")); //生成uuid String id = UUID.randomUUID().toString().replace("-", ""); comments.setUuid(id); travelCommentsService.addComments(comments); map.put("msg", "200"); }catch (Exception e){ e.printStackTrace(); map.put("msg", "500"); map.put("errMsg", "服务器请求异常,请稍后再试"); } return map; } /** * 删除评论 * @param comments 参数 * @param request token * @return 返回结果 */ @DeleteMapping("delComments") public Map delComments(TravelComments comments, HttpServletRequest request){ Map map = new HashMap<>(); try { //获取token Map userMap = ParsingToken.tokenParsing(request); comments.setCode((String) userMap.get("code")); //查询要删除的评论下的所有评论 并删除 comments.setParentUuid(comments.getUuid()); Map dataMap = getComments(comments, request); delCommentData((List>) dataMap.get("data"));//删除所有子集 travelCommentsService.delComments(comments); map.put("msg", "200"); }catch (Exception e){ e.printStackTrace(); map.put("msg", "500"); map.put("errMsg", "服务器请求异常,请稍后再试"); } return map; } /** * 查询指定游记的所有评论 * @param comments 参数 * @param request token * @return 返回结果 */ @GetMapping("getComments") public Map getComments(TravelComments comments, HttpServletRequest request){ Map map = new HashMap<>(); try { //获取token Map userMap = ParsingToken.tokenParsing(request); comments.setCode((String) userMap.get("code")); map.put("data", recursiveComments(comments)); map.put("msg", "200"); }catch (Exception e){ e.printStackTrace(); map.put("msg", "500"); map.put("errMsg", "服务器请求异常,请稍后再试"); } return map; } /** * 递归查询评论 * @param comments 参数 * @return 返回结果 */ public List recursiveComments(TravelComments comments){ List listData = travelCommentsService.getComments(comments); // List> list = new ArrayList<>(); // for(TravelComments comments1 : listData){ // List list1 = new ArrayList<>(); // list1.add(comments1); // List listData1 = travelCommentsService.getComments(comments1); // for(TravelComments comments2 : listData1){ // list1.add(comments2); // } // list.add(list1); // } return listData; } /** * 递归删除所有制定父级下的所有子集 * @param list 参数 */ public void delCommentData(List> list){ try { for(int i = 0; i < list.size(); i++){ TravelComments comments = new TravelComments().setCode((String) list.get(i).get("code")).setUuid((String) list.get(i).get("uuid")).setTravelUuid((String) list.get(i).get("travelUuid")); List> listChildren = (List>) list.get(i).get("children"); if(listChildren.size() != 0){ delCommentData(listChildren); } travelCommentsService.delComments(comments); } }catch (Exception e){ e.printStackTrace(); } } /** * 查询用户评论 * @param request 参数 * @param travelComments 参数 * @return 返回结果 */ @GetMapping("getCommentsUser") public Map getCommentsUser(TravelComments travelComments, HttpServletRequest request){ Map map = new HashMap<>(); try{ if(travelComments.getCode() == null){ //获取token Map userMap = ParsingToken.tokenParsing(request); travelComments.setCode((String) userMap.get("code")); } List> list = travelCommentsService.getCommentsUser(travelComments); Map mapVal = new HashMap<>(); mapVal.put("list", list); mapVal.put("count", travelCommentsService.getCommentsUserCount(travelComments)); mapVal.put("page", travelComments.getPage()); map.put("data", mapVal); map.put("msg", "200"); }catch (Exception e){ e.printStackTrace(); map.put("msg", "500"); map.put("errMsg", "服务器请求异常,请稍后再试"); } return map; } /** * 查询指定用户的互动消息 * @param request token 参数 * @return 返回结果 */ @GetMapping("getInteractiveMessage") public Map getInteractiveMessage(HttpServletRequest request){ String code = (String)ParsingToken.tokenParsing(request).get("code"); return travelCommentsService.getInteractiveMessage(code); } }