1 Star 0 Fork 462

誓言/bullshit-codes

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
PreAfterList.java 1.58 KB
一键复制 编辑 原始数据 按行查看 历史
青苗 提交于 2019-05-27 09:27 +08:00 . 更新 PreAfterList.java
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<Article> list = articleMapper.selectList(Wrappers.<Article>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;
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/java521/bullshit-codes.git
git@gitee.com:java521/bullshit-codes.git
java521
bullshit-codes
bullshit-codes
master

搜索帮助