From d3089f54a262ef95f6c8963fd9cdc1e550c3ec4c Mon Sep 17 00:00:00 2001 From: hubin Date: Mon, 27 May 2019 09:21:06 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E8=8E=B7=E5=8F=96=E6=96=87=E7=AB=A0?= =?UTF-8?q?=E7=9A=84=E4=B8=8A=E4=B8=8B=E5=86=85=E5=AE=B9=E9=9B=86=E5=90=88?= =?UTF-8?q?=E5=A4=A9=E6=89=8D=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- java/PreAfterList.java | 45 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 java/PreAfterList.java diff --git a/java/PreAfterList.java b/java/PreAfterList.java new file mode 100644 index 0000000..279e049 --- /dev/null +++ b/java/PreAfterList.java @@ -0,0 +1,45 @@ +package com.gitee.bullshit.code; + +import com.baomidou.mybatisplus.core.toolkit.Wrappers; + +public class PreAfterList { + + // 需求:根据 ID 获取指定 ID 文章的包括当前 ID 及上一条和下一条内容集合 + // 原样贴出天才同事的操作如下,看完肺都气炸了!! 看官们你觉得正确做法是什么?? + public ArticleVO getArticleVO(Long id) { + ArticleVO vo = new ArticleVO(getById(id)); + List
list = articleMapper.selectList(baseMapper.selectList(Wrappers.
lambdaQuery().orderByDesc(Article::getCreateTime))); + if (list.size() == 1) { + return vo; + } else if (list.size() == 2) { + if (list.get(0).getId().equals(vo.getId())) { + // first is itself + vo.setAfter(list.get(1)); + } else { + vo.setPre(list.get(0)); + } + } else { + // > 3 + Article pre = list.get(0); + if (pre.getId().equals(vo.getId())) { + vo.setPre(null); + vo.setAfter(list.get(1)); + return vo; + } + for (Article article : list) { + if (article.getId().equals(vo.getId())) { + vo.setPre(pre); + int preIdx = list.indexOf(pre); + if (preIdx + 2 >= list.size()) { + break; + } + vo.setAfter(list.get(preIdx + 2)); + break; + } else { + pre = article; + } + } + } + return vo; + } +} -- Gitee From 7efd016049373d6f9b34e15beff91901e88ca49b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=9D=92=E8=8B=97?= <243194995@qq.com> Date: Mon, 27 May 2019 09:27:17 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20PreAfterList.java?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- java/PreAfterList.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/java/PreAfterList.java b/java/PreAfterList.java index 279e049..f73552c 100644 --- a/java/PreAfterList.java +++ b/java/PreAfterList.java @@ -4,11 +4,11 @@ import com.baomidou.mybatisplus.core.toolkit.Wrappers; public class PreAfterList { - // 需求:根据 ID 获取指定 ID 文章的包括当前 ID 及上一条和下一条内容集合 + // 需求:根据 ID 获取指定 ID 文章,包括当前 ID 及上一条和下一条的内容集合 // 原样贴出天才同事的操作如下,看完肺都气炸了!! 看官们你觉得正确做法是什么?? public ArticleVO getArticleVO(Long id) { ArticleVO vo = new ArticleVO(getById(id)); - List
list = articleMapper.selectList(baseMapper.selectList(Wrappers.
lambdaQuery().orderByDesc(Article::getCreateTime))); + List
list = articleMapper.selectList(Wrappers.
lambdaQuery().orderByDesc(Article::getCreateTime)); if (list.size() == 1) { return vo; } else if (list.size() == 2) { -- Gitee