Sfoglia il codice sorgente

增加移动端首页根据浏览量查询农技接口

jiuling 3 mesi fa
parent
commit
d05825f6ab

+ 25 - 15
ruoyi-modules/ruoyi-uniapp/src/main/java/com/ruoyi/uniapp/controller/KnowledgeController.java

@@ -30,20 +30,20 @@ public class KnowledgeController extends BaseController {
     public AjaxResult list(Integer page, Integer pageSize, String category)
     {
         startPage();
-        
+
         KnowledgeContent knowledgeContent = new KnowledgeContent();
         if (category != null) {
             knowledgeContent.setContentCategory(category);
         }
-        
+
         List<KnowledgeContent> list = knowledgeContentService.selectKnowledgeContentList(knowledgeContent);
         TableDataInfo dataTable = getDataTable(list);
-        
+
         // 构造与前端一致的数据格式
         Map<String, Object> data = new HashMap<>();
         data.put("list", dataTable.getRows());
         data.put("total", dataTable.getTotal());
-        
+
         return AjaxResult.success(data);
     }
 
@@ -55,7 +55,7 @@ public class KnowledgeController extends BaseController {
     {
         return AjaxResult.success(knowledgeContentService.selectKnowledgeContentById(id));
     }
-    
+
     /**
      * 更新阅读量
      */
@@ -66,11 +66,11 @@ public class KnowledgeController extends BaseController {
         if (id == null) {
             return AjaxResult.error("参数错误");
         }
-        
+
         knowledgeContentService.incrementViewCount(id);
         return AjaxResult.success();
     }
-    
+
     /**
      * 点赞内容
      */
@@ -81,11 +81,11 @@ public class KnowledgeController extends BaseController {
         if (id == null) {
             return AjaxResult.error("参数错误");
         }
-        
+
         // 实际业务中可能需要记录用户的点赞状态
         return AjaxResult.success();
     }
-    
+
     /**
      * 取消点赞
      */
@@ -96,11 +96,11 @@ public class KnowledgeController extends BaseController {
         if (id == null) {
             return AjaxResult.error("参数错误");
         }
-        
+
         // 实际业务中可能需要删除用户的点赞记录
         return AjaxResult.success();
     }
-    
+
     /**
      * 收藏内容
      */
@@ -111,11 +111,11 @@ public class KnowledgeController extends BaseController {
         if (id == null) {
             return AjaxResult.error("参数错误");
         }
-        
+
         // 实际业务中可能需要记录用户的收藏状态
         return AjaxResult.success();
     }
-    
+
     /**
      * 取消收藏
      */
@@ -126,7 +126,6 @@ public class KnowledgeController extends BaseController {
         if (id == null) {
             return AjaxResult.error("参数错误");
         }
-        
         // 实际业务中可能需要删除用户的收藏记录
         return AjaxResult.success();
     }
@@ -163,4 +162,15 @@ public class KnowledgeController extends BaseController {
     {
         return toAjax(knowledgeContentService.deleteKnowledgeContentByIds(ids));
     }
-} 
+
+    /**
+     * 根据浏览量查询
+     */
+    @GetMapping("/top-articles")
+    public List<KnowledgeContent> topArticles(
+            @RequestParam(defaultValue = "4") int limit,
+            @RequestParam(required = false) String category) {
+        return knowledgeContentService.getTopArticles(limit, category);
+    }
+
+}

+ 4 - 0
ruoyi-modules/ruoyi-uniapp/src/main/java/com/ruoyi/uniapp/mapper/KnowledgeContentMapper.java

@@ -2,6 +2,8 @@ package com.ruoyi.uniapp.mapper;
 
 import java.util.List;
 import com.ruoyi.uniapp.domain.KnowledgeContent;
+import org.apache.ibatis.annotations.Param;
+import org.apache.ibatis.annotations.Select;
 
 /**
  * 知识内容Mapper接口
@@ -65,4 +67,6 @@ public interface KnowledgeContentMapper
      * @return 结果
      */
     public int incrementViewCount(Integer id);
+
+    List<KnowledgeContent> getTopArticles(@Param("limit") int limit,@Param("category") String category);
 }

+ 5 - 0
ruoyi-modules/ruoyi-uniapp/src/main/java/com/ruoyi/uniapp/service/impl/KnowledgeContentServiceImpl.java

@@ -101,4 +101,9 @@ public class KnowledgeContentServiceImpl implements IKnowledgeContentService
     {
         return knowledgeContentMapper.incrementViewCount(id);
     }
+
+    @Override
+    public List<KnowledgeContent> getTopArticles(int limit, String category) {
+        return  knowledgeContentMapper.getTopArticles(limit, category);
+    }
 } 

+ 21 - 1
ruoyi-modules/ruoyi-uniapp/src/main/resources/mapper/KnowledgeContentMapper.xml

@@ -46,7 +46,27 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
         <include refid="selectKnowledgeContentVo"/>
         where id = #{id}
     </select>
-    
+    <select id="getTopArticles" resultMap="KnowledgeContentResult">
+        SELECT
+        id,
+        title,
+        description,
+        image_url,
+        view_count,
+        publish_date,
+        author
+        FROM knowledge_content
+        WHERE status = 1
+        AND content_type = 'article'
+
+        <if test="category != null and category != ''">
+            AND content_category = #{category}
+        </if>
+
+        ORDER BY view_count DESC
+        LIMIT #{limit}
+    </select>
+
     <insert id="insertKnowledgeContent" parameterType="com.ruoyi.uniapp.domain.KnowledgeContent" useGeneratedKeys="true" keyProperty="id">
         insert into knowledge_content
         <trim prefix="(" suffix=")" suffixOverrides=",">