From 997109693f5723b063b7e2dd7ef3f5b32eccb063 Mon Sep 17 00:00:00 2001 From: wangjiadong <1654135867@qq.com> Date: Fri, 8 Nov 2024 19:33:15 +0800 Subject: [PATCH] =?UTF-8?q?cms=E9=A6=96=E9=A1=B5=E8=A6=81=E9=97=AE?= =?UTF-8?q?=E9=80=9F=E6=8A=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cms/controller/CmsHomePageController.java | 66 +++++++------------ 1 file changed, 24 insertions(+), 42 deletions(-) diff --git a/jeecg-boot/jeecg-module-demo/src/main/java/org/jeecg/modules/demo/cms/controller/CmsHomePageController.java b/jeecg-boot/jeecg-module-demo/src/main/java/org/jeecg/modules/demo/cms/controller/CmsHomePageController.java index 98755f4..a7e3f8f 100644 --- a/jeecg-boot/jeecg-module-demo/src/main/java/org/jeecg/modules/demo/cms/controller/CmsHomePageController.java +++ b/jeecg-boot/jeecg-module-demo/src/main/java/org/jeecg/modules/demo/cms/controller/CmsHomePageController.java @@ -20,7 +20,7 @@ import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest; -import java.util.List; +import java.util.*; @Api(tags = "cms首页") @RestController @@ -69,51 +69,33 @@ public class CmsHomePageController /*extends JeecgController getindexnew(CmsArticle cmsArticle, - @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, - @RequestParam(name = "pageSize", defaultValue = "6") Integer pageSize, - HttpServletRequest req) { - QueryWrapper queryWrapper = QueryGenerator.initQueryWrapper(cmsArticle, req.getParameterMap()); - queryWrapper.eq("status", "1"); - queryWrapper.eq("column_id", "1717099205205401602"); - queryWrapper.last("order by publish_time desc"); - Page page = new Page(pageNo, pageSize); - IPage pageList = cmsArticleService.page(page, queryWrapper); - return Result.OK(pageList); - } - - @ApiOperation(value = "用于首页的竞赛导航", notes = "竞赛导航") - @GetMapping(value = "/getindexcomp") - public Result getindexcomp(CmsArticle cmsArticle, - @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, - @RequestParam(name = "pageSize", defaultValue = "8") Integer pageSize, - HttpServletRequest req) { + @ApiOperation(value = "用于首页的要问速报", notes = "用于首页的要问速报") + @GetMapping(value = "/indexywsb") + public Result getindexaward(CmsArticle cmsArticle, + @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, + @RequestParam(name = "pageSize", defaultValue = "3") Integer pageSize, + HttpServletRequest req) { QueryWrapper queryWrapper = QueryGenerator.initQueryWrapper(cmsArticle, req.getParameterMap()); queryWrapper.eq("status", "1"); - queryWrapper.eq("column_id", "1712301393339650049"); - queryWrapper.last("order by publish_time desc"); - Page page = new Page(pageNo, pageSize); - IPage pageList = cmsArticleService.page(page, queryWrapper); - return Result.OK(pageList); - } + List allArticles = cmsArticleService.list(queryWrapper); + if (allArticles.size() <= 3) { + return Result.OK(allArticles); + } else { + // 随机选择3条记录 + Random random = new Random(); + List randomArticles = new ArrayList<>(3); + Set selectedIndices = new HashSet<>(3); // 使用Set来确保索引的唯一性 - @ApiOperation(value = "用于首页的研究成果", notes = "研究成果") - @GetMapping(value = "/getindexaward") - public Result getindexaward(CmsArticle cmsArticle, - @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, - @RequestParam(name = "pageSize", defaultValue = "3") Integer pageSize, - HttpServletRequest req) { - QueryWrapper queryWrapper = QueryGenerator.initQueryWrapper(cmsArticle, req.getParameterMap()); - queryWrapper.eq("status", "1"); - queryWrapper.eq("column_id", "1717099355382456322"); - queryWrapper.last("order by publish_time desc"); - Page page = new Page(pageNo, pageSize); - IPage pageList = cmsArticleService.page(page, queryWrapper); - return Result.OK(pageList); + while (selectedIndices.size() < 3) { + int index = random.nextInt(allArticles.size()); + selectedIndices.add(index); + } + for (Integer index : selectedIndices) { + randomArticles.add(allArticles.get(index)); + } + return Result.OK(randomArticles); + } } - }