Bläddra i källkod

修改获取问题的算法

wuweihao 4 år sedan
förälder
incheckning
7e6c70becb

+ 1 - 0
gis_domain/src/main/java/com/gis/domain/po/QuestionEntity.java

@@ -3,6 +3,7 @@ package com.gis.domain.po;
 import io.swagger.annotations.ApiModelProperty;
 import lombok.Data;
 
+import javax.persistence.OneToMany;
 import javax.persistence.Table;
 import javax.persistence.Transient;
 import java.io.Serializable;

+ 2 - 0
gis_mapper/src/main/java/com/gis/mapper/AnswerMapper.java

@@ -15,4 +15,6 @@ public interface AnswerMapper extends IBaseMapper<AnswerEntity, Long> {
 
     @Select("select * from tb_answer where question_id = #{questionId}")
     List<AnswerEntity> findByQuestionId(Long questionId);
+
+
 }

+ 4 - 2
gis_mapper/src/main/java/com/gis/mapper/QuestionMapper.java

@@ -13,6 +13,8 @@ import java.util.List;
 @Mapper
 public interface QuestionMapper extends IBaseMapper<QuestionEntity, Long> {
 
-    @Select("select id from tb_question where is_delete = 0 ")
-    List<Integer> getIds();
+
+
+    @Select("select id from tb_question where is_delete = 0 AND topic_type = #{type} ORDER BY RAND() LIMIT #{pcs} ")
+    List<Integer> getRandomIds(Integer type, Integer pcs);
 }

+ 3 - 1
gis_service/src/main/java/com/gis/service/AnswerService.java

@@ -8,8 +8,10 @@ import java.util.List;
 /**
  * Created by owen on 2020/3/11 0011 16:14
  */
-public interface AnswerService {
+public interface AnswerService extends IBaseService<AnswerEntity, Long> {
 
 
     List<AnswerEntity> findByQuestionId(Long id);
+
+    List<AnswerEntity> findByQuestionIds(List questionIds);
 }

+ 2 - 2
gis_service/src/main/java/com/gis/service/QuestionService.java

@@ -9,8 +9,8 @@ import java.util.List;
 /**
  * Created by owen on 2020/3/11 0011 16:14
  */
-public interface QuestionService {
+public interface QuestionService extends IBaseService<QuestionEntity, Long> {
 
 
-    Result<List<QuestionEntity>> getQuestion();
+    Result<List<QuestionEntity>> getQuestion(Integer type, Integer pcs);
 }

+ 13 - 0
gis_service/src/main/java/com/gis/service/impl/AnswerServiceImpl.java

@@ -7,6 +7,7 @@ import com.gis.service.AnswerService;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
+import tk.mybatis.mapper.entity.Condition;
 
 import java.util.List;
 
@@ -32,4 +33,16 @@ public class AnswerServiceImpl extends IBaseServiceImpl<AnswerEntity, Long> impl
     public List<AnswerEntity> findByQuestionId(Long questionId) {
         return entityMapper.findByQuestionId(questionId);
     }
+
+    @Override
+    public List<AnswerEntity> findByQuestionIds(List questionIds) {
+        // in 查询不能直接在mapper里写 in #{xxx}
+        Iterable<Integer> in = questionIds;
+        Condition condition = new Condition(AnswerEntity.class);
+
+        condition.and().andIn("questionId", in);
+        List<AnswerEntity> answers =  entityMapper.selectByCondition(condition);
+
+        return answers;
+    }
 }

+ 63 - 25
gis_service/src/main/java/com/gis/service/impl/QuestionServiceImpl.java

@@ -3,17 +3,18 @@ package com.gis.service.impl;
 import com.gis.common.util.Result;
 import com.gis.domain.po.AnswerEntity;
 import com.gis.domain.po.QuestionEntity;
+import com.gis.mapper.AnswerMapper;
 import com.gis.mapper.IBaseMapper;
 import com.gis.mapper.QuestionMapper;
 import com.gis.service.AnswerService;
 import com.gis.service.QuestionService;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
 import java.util.ArrayList;
 import java.util.List;
-import java.util.Random;
 
 
 /**
@@ -30,45 +31,82 @@ public class QuestionServiceImpl extends IBaseServiceImpl<QuestionEntity, Long>
     @Autowired
     AnswerService answerService;
 
+    @Autowired
+    AnswerMapper answerMapper;
+
     @Override
     public IBaseMapper<QuestionEntity, Long> getBaseMapper() {
         return this.entityMapper;
     }
 
 
-    @Override
-    public Result<List<QuestionEntity>> getQuestion() {
+//    @Override
+//    public Result<List<QuestionEntity>> getQuestion() {
+//
+//        // 获取所有题目id
+//        List<Integer> allIds = entityMapper.getIds();
+//
+//        //要随机取的元素个数
+//        Random random = new Random();
+//        int size = 10;
+//
+//        // 定义一个跟all一样长的数字,默认为false, 用来防重的
+//        boolean r[]=new boolean[allIds.size()];
+//
+//        int n = 0;
+//
+//        List<QuestionEntity> result = new ArrayList<>();
+//
+//        while (true) {
+//            int temp = random.nextInt(allIds.size());
+//            if(!r[temp]){
+//                if (n == size) {
+//                    break;
+//                }
+//                QuestionEntity entity = this.findById(Long.valueOf(allIds.get(temp)));
+//                List<AnswerEntity> answers = answerService.findByQuestionId(entity.getId());
+//                entity.setAnswers(answers);
+//                result.add(entity);
+////                    System.out.println("得到的第" + n +"个随du机数为:" + temp + " rtemp:"+r[temp] );
+//                n ++;
+//                // 修改下标值
+//                r[temp] = true;
+//            }
+//        }
+//        log.info("返回问题数量: {}", result.size());
+//        return Result.success(result);
+//
+//
+//    }
 
-        // 获取所有题目id
-        List<Integer> allIds = entityMapper.getIds();
+    @Override
+    public Result<List<QuestionEntity>> getQuestion(Integer type, Integer pcs) {
 
-        //要随机取的元素个数
-        Random random = new Random();
-        int size = 10;
+        // 随机获取10条问题
+        List<Integer> randomIds = entityMapper.getRandomIds(type, pcs);
 
-        // 定义一个跟all一样长的数字,默认为false, 用来防重的
-        boolean r[]=new boolean[allIds.size()];
+        // list to sting 以逗号分隔
+        String join = StringUtils.join(randomIds.toArray(),",");
 
-        int n = 0;
+        // 获取问题、答案
+        List<QuestionEntity> questions = this.findByIds(join);
+        List<AnswerEntity>  answers = answerService.findByQuestionIds(randomIds);
 
+        // 整合问题、答案
         List<QuestionEntity> result = new ArrayList<>();
-
-        while (true) {
-            int temp = random.nextInt(allIds.size());
-            if(!r[temp]){
-                if (n == size) {
-                    break;
+        for (QuestionEntity question : questions) {
+            List<AnswerEntity> ans = new ArrayList<>();
+            for (AnswerEntity answer : answers) {
+                // 答案分组
+                if (question.getId() == answer.getQuestionId()) {
+                    ans.add(answer);
                 }
-                QuestionEntity entity = this.findById(Long.valueOf(allIds.get(temp)));
-                List<AnswerEntity> answers = answerService.findByQuestionId(entity.getId());
-                entity.setAnswers(answers);
-                result.add(entity);
-//                    System.out.println("得到的第" + n +"个随du机数为:" + temp + " rtemp:"+r[temp] );
-                n ++;
-                // 修改下标值
-                r[temp] = true;
             }
+            question.setAnswers(ans);
+            result.add(question);
         }
+
+
         log.info("返回问题数量: {}", result.size());
         return Result.success(result);
 

+ 9 - 3
gis_web/src/main/java/com/gis/web/controller/WebController.java

@@ -2,12 +2,14 @@ package com.gis.web.controller;
 
 import com.gis.common.util.Result;
 import com.gis.domain.po.QuestionEntity;
+import com.gis.service.AnswerService;
 import com.gis.service.QuestionService;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PathVariable;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
 
@@ -33,13 +35,17 @@ public class WebController extends BaseController {
      * @return
      */
     @ApiOperation("获取答题")
-    @GetMapping("list")
-    public Result<List<QuestionEntity>> getQuestion() {
-        return questionService.getQuestion();
+    @GetMapping("list/{type}/{pcs}")
+    public Result<List<QuestionEntity>> getQuestion(@PathVariable Integer type, @PathVariable Integer pcs) {
+
+        return questionService.getQuestion(type, pcs);
     }
 
 
 
 
 
+
+
+
 }