Bladeren bron

add Record Imp

XuShengTao 6 jaren geleden
bovenliggende
commit
4745ee3914

+ 4 - 1
pom.xml

@@ -63,7 +63,10 @@
             <artifactId>lombok</artifactId>
             <optional>true</optional>
         </dependency>
-
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-data-jpa</artifactId>
+        </dependency>
         <!--<dependency>-->
             <!--<groupId>com.graphql-java</groupId>-->
             <!--<artifactId>graphql-spring-boot-starter</artifactId>-->

+ 22 - 0
src/main/java/com/seec/bok/back/entity/Record.java

@@ -0,0 +1,22 @@
+package com.seec.bok.back.entity;
+
+import lombok.Builder;
+import lombok.Data;
+
+import javax.persistence.Entity;
+import java.time.LocalDateTime;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+@Data
+@Entity
+public class Record {
+    @Id
+    @GeneratedValue
+    Long id;
+    String userId;
+    String questionId;
+    String studentAnswer;
+    Boolean pass;
+    LocalDateTime startAt;
+    LocalDateTime finishAt;
+}

+ 10 - 0
src/main/java/com/seec/bok/back/repository/RecordRepository.java

@@ -0,0 +1,10 @@
+package com.seec.bok.back.repository;
+
+import com.seec.bok.back.entity.Record;
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
+import org.springframework.data.repository.query.QueryByExampleExecutor;
+
+public interface RecordRepository extends JpaRepository<Record, Long> , JpaSpecificationExecutor<Record>,QueryByExampleExecutor<Record> {
+
+}

+ 76 - 0
src/main/java/com/seec/bok/service/RecordService.java

@@ -0,0 +1,76 @@
+package com.seec.bok.service;
+
+import com.seec.bok.back.entity.Record;
+import com.seec.bok.back.repository.RecordRepository;
+import com.seec.bok.web.dto.RecordDTO;
+import com.seec.bok.web.vo.RecordVO;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.data.domain.ExampleMatcher;
+import org.springframework.data.jpa.domain.Specification;
+import org.springframework.stereotype.Service;
+
+import javax.persistence.criteria.*;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+import java.util.stream.StreamSupport;
+
+@Service
+public class RecordService {
+    @Autowired
+    private RecordRepository recordRepository;
+
+
+    public Record save(RecordDTO dto) {
+        return recordRepository.save(dto.toEntity());
+    }
+
+    public List<Record> saveAll(Iterable<RecordDTO> iterable) {
+        final List<Record> collect = StreamSupport
+                .stream(iterable.spliterator(), true)
+                .map(RecordDTO::toEntity)
+                .collect(Collectors.toList());
+        return recordRepository.saveAll(collect);
+    }
+
+    public List<Record> findRecordByUidAndQidAndRidIn(List<String> userIdList, List<String> questionIdList, List<String> recordIdList) {
+
+        if(isEmpty(userIdList)&&isEmpty(questionIdList)&&isEmpty(recordIdList)){
+            return recordRepository.findAll();
+        }else{
+            Specification<Record> spec = new Specification<Record>() {
+                @Override
+                public Predicate toPredicate(Root<Record> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
+                    List<Predicate> predicateList =new ArrayList<>();
+                    if(!isEmpty(userIdList)){
+                        Path<Object> userId = root.get("userId");
+                        Predicate p = userId.in(userIdList);
+                        predicateList.add(p);
+                    }
+                    if(!isEmpty(questionIdList)){
+                        Path<Object> questionId = root.get("questionId");
+                        Predicate p = questionId.in(questionIdList);
+                        predicateList.add(p);
+                    }
+                    if(!isEmpty(recordIdList)){
+                        Path<Object> id = root.get("id");//客户名
+                        Predicate p = id.in(recordIdList);
+                        predicateList.add(p);
+                    }
+                    final Predicate[] predicates = predicateList.toArray(new Predicate[0]);
+                    Predicate and = cb.and(predicates);
+                    return and;
+                }
+            };
+            List<Record> all = recordRepository.findAll(spec);
+            return all;
+        }
+
+    }
+    private boolean isEmpty(List list){
+        return list==null||list.size()==0;
+    }
+}
+

+ 0 - 1
src/main/java/com/seec/bok/web/controller/KnowledgeController.java

@@ -27,7 +27,6 @@ public class KnowledgeController {
             @RequestParam(value = "name", required = false)
             @Parameter(description = "知识点name包含关键词")
                     String name) throws JsonProcessingException {
-        System.out.println("ss:"+name);
         return new ArrayList<>(knowledgeGraphService.getKnowledgeNode(name));
     }
 }

+ 8 - 1
src/main/java/com/seec/bok/web/controller/QuestionQueryController.java

@@ -1,10 +1,14 @@
 package com.seec.bok.web.controller;
 
 
+import com.seec.bok.service.QuestionService;
 import com.seec.bok.web.util.FakeQuestionData;
 import com.seec.bok.web.vo.QuestionVO;
 import io.swagger.v3.oas.annotations.Operation;
 import io.swagger.v3.oas.annotations.Parameter;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.domain.PageRequest;
+import org.springframework.data.domain.Pageable;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RequestParam;
@@ -16,11 +20,14 @@ import java.util.List;
 @RestController
 @RequestMapping("/api/question-query")
 public class QuestionQueryController {
+    @Autowired
+    private QuestionService questionService;
 
     @GetMapping("/id")
     @Operation(description = "根据id批量获取题目")
     public List<QuestionVO> getQuestionListById(
             @RequestParam("id") @Parameter(description = "批量获取") List<String> id) {
-        return FakeQuestionData.buildList();
+        Pageable pageable = PageRequest.of(0, Integer.MAX_VALUE);
+        return questionService.findByIdIn(id,pageable).getQuestions();
     }
 }

+ 10 - 3
src/main/java/com/seec/bok/web/controller/RecordController.java

@@ -1,14 +1,18 @@
 package com.seec.bok.web.controller;
 
 
+import com.seec.bok.back.entity.Record;
+import com.seec.bok.service.RecordService;
 import com.seec.bok.web.dto.RecordDTO;
 import com.seec.bok.web.util.FakeRecordData;
 import com.seec.bok.web.vo.RecordVO;
 import io.swagger.v3.oas.annotations.Operation;
 import io.swagger.v3.oas.annotations.Parameter;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 
 import java.util.List;
+import java.util.stream.Collectors;
 
 /**
  * 用户的做题记录
@@ -18,11 +22,13 @@ import java.util.List;
 @RestController
 @RequestMapping("/api/record")
 public class RecordController {
-
+    @Autowired
+    private RecordService recordService;
     @PostMapping("")
     @Operation(description = "新增用户的做题记录")
     public List<RecordVO> createUserRecord(@RequestBody List<RecordDTO> records) {
-        return FakeRecordData.buildList();
+        final List<Record> records1 = recordService.saveAll(records);
+        return records1.stream().map(RecordVO::fromEntity).collect(Collectors.toList());
     }
 
     @GetMapping("")
@@ -34,6 +40,7 @@ public class RecordController {
                     List<String> questionIdList,
             @RequestParam(value = "recordId", required = false) @Parameter(description = "记录id")
                     List<String> recordIdList) {
-        return FakeRecordData.buildList();
+        final List<Record> records1 =recordService.findRecordByUidAndQidAndRidIn(userIdList,questionIdList,recordIdList);
+        return records1.stream().map(RecordVO::fromEntity).collect(Collectors.toList());
     }
 }

+ 10 - 0
src/main/java/com/seec/bok/web/dto/RecordDTO.java

@@ -1,6 +1,8 @@
 package com.seec.bok.web.dto;
 
+import com.seec.bok.back.entity.Record;
 import lombok.Data;
+import org.springframework.beans.BeanUtils;
 
 import java.time.LocalDateTime;
 
@@ -13,4 +15,12 @@ public class RecordDTO {
     Boolean pass;
     LocalDateTime startAt;
     LocalDateTime finishAt;
+
+    public Record toEntity(){
+        Record record=new Record();
+        BeanUtils.copyProperties(this,record);
+        record.setUserId(studentId);
+        record.setStudentAnswer(studentAnswer.toString());
+        return record;
+    }
 }

+ 14 - 0
src/main/java/com/seec/bok/web/vo/RecordVO.java

@@ -1,13 +1,20 @@
 package com.seec.bok.web.vo;
 
+import com.seec.bok.back.entity.Record;
+import lombok.AllArgsConstructor;
 import lombok.Builder;
 import lombok.Data;
+import lombok.NoArgsConstructor;
+import org.springframework.beans.BeanUtils;
 
+import javax.persistence.criteria.From;
 import java.time.LocalDateTime;
 
 /** @author Kunduin */
 @Data
 @Builder
+@NoArgsConstructor
+@AllArgsConstructor
 public class RecordVO {
     String id;
     String userId;
@@ -16,4 +23,11 @@ public class RecordVO {
     Boolean pass;
     LocalDateTime startAt;
     LocalDateTime finishAt;
+
+    public static RecordVO fromEntity(Record record){
+        RecordVO recordVO=new RecordVO();
+        BeanUtils.copyProperties(record,recordVO);
+        recordVO.setId(record.getId().toString());
+        return recordVO;
+    }
 }

+ 1 - 1
src/main/resources/application.properties

@@ -15,4 +15,4 @@ bok.es.cluster.name=se4-es
 bok.es.port=9300
 bok.es.address=192.168.68.76
 proxy.neo=http://bok-neo-backend.seecoder.cn
-proxy.sklearn=http://localhost:5000
+proxy.sklearn=http://localhost:5000

+ 20 - 0
src/main/resources/application.yml

@@ -0,0 +1,20 @@
+spring:
+  h2:
+    console:
+      enabled: true
+      settings:
+        web-allow-others: true
+
+  jpa:
+    hibernate:
+      ddl-auto: update
+    show-sql: false
+
+  datasource:
+    url: jdbc:h2:file:~/.h2/bok_h2
+    username: seecbok
+    password: seecbok
+
+  servlet:
+    multipart:
+      max-file-size: 5MB