Service实现类
根据关键词获取已发布方案相关数据:findAllBySearch
根据传入的页号、页码以及关键词获取当前页的解决方案列表并根据浏览量、收藏量、时间排序。
@Override //根据关键词查找社区中所有已发布方案,按时间、收藏、浏览排序
public HashMap<String, Page> findAllBySearch(Integer pageNum,Integer pageSize,String words) {
QueryWrapper<Solution> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("statement","已发布");
List<Solution> solutionList = solutionService.list(queryWrapper);
solutionList.sort(Comparator.comparing(Solution::getCreateTime).reversed());
List<SolutionAllDTO> solutionAllDTOS = new ArrayList<>();
HashMap<String,Page> hashMap = new HashMap<>();
for (Solution solution : solutionList) {
SolutionAllDTO solutionAllDTO = new SolutionAllDTO();
UserDTO userDTO = userService.getUserDTO(solution.getUserId());
solutionAllDTO.setId(solution.getId());
solutionAllDTO.setName(solution.getName());
solutionAllDTO.setView(solution.getView());
solutionAllDTO.setUserDTO(userDTO);
solutionAllDTO.setScore(scoreService.getScore(solution.getId()));
String content = Md2Text.convert(solution.getDetail());
if (content.length() <= 150){
solutionAllDTO.setDetail(content);
}
else {
solutionAllDTO.setDetail(content.substring(0,150) + "……");
}
solutionAllDTO.setCreateTime(solution.getCreateTime().toLocalDate());
solutionAllDTO.setRelativeTime(TimeUtil.createRelative(solution.getCreateTime()));
solutionAllDTO.setComments(commentService.getCount(solution.getId()));
solutionAllDTO.setCollections(collectionService.getCount(solution.getId()));
solutionAllDTOS.add(solutionAllDTO);
}
//关键词匹配
List<SolutionAllDTO> timeList = new ArrayList<>();
List<SolutionAllDTO> viewList = new ArrayList<>();
List<SolutionAllDTO> collectionList = new ArrayList<>();
if (StrUtil.isBlank(words)) {
timeList = new ArrayList<>(solutionAllDTOS);
viewList = new ArrayList<>(solutionAllDTOS);
collectionList = new ArrayList<>(solutionAllDTOS);
}
else {
Pattern pattern = Pattern.compile(words,Pattern.CASE_INSENSITIVE);
for (SolutionAllDTO solutionAllDTO : solutionAllDTOS) {
Matcher matcher1 = pattern.matcher(solutionAllDTO.getName());
Matcher matcher2 = pattern.matcher(solutionAllDTO.getUserDTO().getNickname());
if (matcher1.find() || matcher2.find()) {
timeList.add(solutionAllDTO);
}
}
if (timeList.isEmpty()) {
return null;
}
viewList = new ArrayList<>(timeList);
collectionList = new ArrayList<>(timeList);
}
viewList.sort((t1,t2)->t2.getView().compareTo(t1.getView()));
collectionList.sort((t1,t2)->t2.getCollections().compareTo(t1.getCollections()));
Page page = new Page();
hashMap.put("time",page.startPage(timeList,pageNum,pageSize));
hashMap.put("view",page.startPage(viewList,pageNum,pageSize));
hashMap.put("collection",page.startPage(collectionList,pageNum,pageSize));
return hashMap;
}
根据解决方案id对方案进行浏览量的更新:updateView
@Override //更新访问量
public Integer updateView(Integer id) {
Solution solution = solutionService.getById(id);
solution.setView(solution.getView() + 1);
solutionService.saveOrUpdate(solution);
return solution.getView();
}
获取当前用户个人主页的发布方案列表:getSolutionHomes
@Override //个人主页发布的方案
public List<SolutionAllDTO> getSolutionHomes(Integer userId) {
QueryWrapper<Solution> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("statement","已发布");
queryWrapper.eq("user_id",userId);
List<Solution> solutionList = solutionService.list(queryWrapper);
List<SolutionAllDTO> solutionAllDTOS = new ArrayList<>();
for (Solution solution : solutionList) {
SolutionAllDTO solutionAllDTO = new SolutionAllDTO();
solutionAllDTO.setId(solution.getId());
solutionAllDTO.setName(solution.getName());
String content = Md2Text.convert(solution.getDetail());
if (content.length() <= 150){
solutionAllDTO.setDetail(content);
}
else {
solutionAllDTO.setDetail(content.substring(0,150) + "……");
}
solutionAllDTO.setScore(scoreService.getScore(solution.getId()));
solutionAllDTO.setView(solution.getView());
solutionAllDTO.setCreateTime(solution.getCreateTime().toLocalDate());
solutionAllDTO.setComments(commentService.getCount(solution.getId()));
solutionAllDTO.setCollections(collectionService.getCount(solution.getId()));
solutionAllDTO.setRelativeTime(TimeUtil.createRelative(solution.getCreateTime()));
solutionAllDTOS.add(solutionAllDTO);
}
solutionAllDTOS.sort(Comparator.comparing(SolutionAllDTO::getCreateTime).reversed());
return solutionAllDTOS;
}
根据id删除方案及其收藏、评论、打分数据:removeSolutionInfo
根据id判断解决方案状态为回收后将相关数据删除。
@Override //根据方案id删除方案及其评论、收藏、打分
public boolean removeSolutionInfo(Integer id) {
Solution one = getOneById(id);
if (one.getStatement().equals("回收")) {
boolean result = solutionService.removeById(id);
collectionService.deleteBySolution(id);
commentService.deleteBySolution(id);
scoreService.deleteBySolution(id);
return result;
}
else
throw new ServiceException(Constants.CODE_600,"方案状态不符合");
}
管理员通过待审核方案:toPublish
只有查找到的方案状态为待审核才能将其发布
@Override //管理员审核通过方案
public Result toPublish(Integer id) {
Solution find = solutionService.getById(id);
Solution solution = new Solution();
solution.setId(id);
if (find == null)
return Result.error(Constants.CODE_500,"未找到该方案");
if (!find.getStatement().equals("待审核"))
return Result.error(Constants.CODE_500,"方案状态不为待审核");
solution.setStatement("已发布");
solution.setCreateTime(LocalDateTime.now());
if (solutionService.saveOrUpdate(solution))
return Result.success("发布成功");
else
return Result.error(Constants.CODE_500,"发布失败");
}
管理员打回待审核或已发布方案:toUnPublish
只有查找到的方案状态为待审核或已发布才能将其打回,打回已发布方案后会将浏览量、评论和打分清空。
@Override //管理员打回方案
public Result toUnPublish(Integer id) {
Solution find = solutionService.getById(id);
Solution solution = new Solution();
solution.setId(id);
if (find == null)
return Result.error(Constants.CODE_500,"未找到该方案");
if (!find.getStatement().equals("已发布") && !find.getStatement().equals("待审核"))
return Result.error(Constants.CODE_500,"方案状态不为已发布和待审核");
else {
if (find.getStatement().equals("已发布")) {
solution.setStatement("未发布");
solution.setView(0);
if (solutionService.saveOrUpdate(solution)) {
commentService.deleteBySolution(id);
scoreService.deleteBySolution(id);
return Result.success("修改成功");
}
else
return Result.error(Constants.CODE_500,"修改失败");
}
else {
solution.setStatement("未发布");
if (solutionService.saveOrUpdate(solution)) {
return Result.success("修改成功");
}
else
return Result.error(Constants.CODE_500,"修改失败");
}
}
}
管理员获取已发布解决方案列表:adminGetPublished
管理员获取已发布解决方案列表,可以根据用户id和方案名称进行搜索。
@Override //管理员获取已发布方案列表
public Result adminGetPublished(int PageNum, int PageSize, Integer userId, String name, String sortWay) {
QueryWrapper<Solution> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("statement","已发布");
if (userId != null)
queryWrapper.eq("user_id",userId);
queryWrapper.like("name",name);
List<Solution> solutionList = solutionService.list(queryWrapper);
Page page = new Page();
if (sortWay.equals("new"))
solutionList.sort(Comparator.comparing(Solution::getCreateTime).reversed());
else
solutionList.sort(Comparator.comparing(Solution::getCreateTime));
return Result.success(page.startPage(solutionList,PageNum,PageSize));
}
管理员获取待审核解决方案列表:adminGetToExamine
管理员获取待审核解决方案列表,可以根据用户id和方案名称进行搜索。
@Override //管理员获取待审核方案列表
public Result adminGetToExamine(int PageNum, int PageSize, Integer userId, String name) {
QueryWrapper<Solution> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("statement","待审核");
if (userId != null)
queryWrapper.eq("user_id",userId);
queryWrapper.like("name",name);
Page page = new Page();
return Result.success(page.startPage(solutionService.list(queryWrapper),PageNum,PageSize));
}
用户管理方案仓库:manageSolution
用户可以对解决方案仓库中不同状态的解决方案进行不同的操作,可以对已发布方案进行设为私密、编辑方案、删除方案;对未发布方案进行发布方案、编辑方案、删除方案;对回收方案进行恢复方案和彻底删除操作。其中会判断方案是否存在以及状态是否符合。
@Override //用户管理方案仓库
public Result manageSolution(Solution solution) {
Solution one = getOneById(solution.getId());
if (one == null || !one.getUserId().equals(solution.getUserId()))
return Result.error(Constants.CODE_600,"方案不存在");
switch (solution.getStatement()) {
//方案设为私密
case "未发布":
if (one.getStatement().equals("回收") || one.getStatement().equals("未发布"))
return Result.error(Constants.CODE_600, "方案状态不符合");
else if (one.getStatement().equals("已发布")) {
solution.setView(0);
if (solutionService.saveOrUpdate(solution)) {
commentService.deleteBySolution(solution.getId());
scoreService.deleteBySolution(solution.getId());
return Result.success("修改成功");
} else
return Result.error(Constants.CODE_500, "修改失败");
}
break;
//用户发布方案
case "待审核":
if (one.getStatement().equals("回收") || one.getStatement().equals("待审核") || one.getStatement().equals("已发布"))
return Result.error(Constants.CODE_600, "方案状态不符合");
break;
//用户将方案加入回收站
case "回收":
if (one.getStatement().equals("回收"))
return Result.error(Constants.CODE_600, "方案状态不符合");
else if (one.getStatement().equals("已发布")) {
solution.setView(0);
if (solutionService.saveOrUpdate(solution)) {
commentService.deleteBySolution(solution.getId());
scoreService.deleteBySolution(solution.getId());
return Result.success("修改成功");
} else
return Result.error(Constants.CODE_500, "修改失败");
}
break;
}
if (saveOrUpdate(solution))
return Result.success("操作成功");
return Result.error(Constants.CODE_500,"修改失败");
}
用户从回收站恢复方案:recoverySolution
@Override //用户从回收站恢复方案
public Result recoverySolution(Solution solution) {
Solution one = getOneById(solution.getId());
if (one == null || !one.getUserId().equals(solution.getUserId()))
return Result.error(Constants.CODE_600,"方案不存在");
if (solution.getStatement().equals("未发布") && one.getStatement().equals("回收")) {
if (saveOrUpdate(solution))
return Result.success("恢复成功");
}
return Result.error(Constants.CODE_600,"方案状态不符合");
}