浏览代码

对接区块链

insomniaLee 5 年之前
父节点
当前提交
44601c1780

+ 70 - 0
src/main/java/com/mooctest/service/BlockchainService.java

@@ -0,0 +1,70 @@
+package com.mooctest.service;
+
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
+import com.mooctest.data.BugDTO;
+import com.mooctest.data.FinalReportDTO;
+import com.mooctest.data.TaskDTO;
+import com.mooctest.model.Bug;
+import com.mooctest.model.FinalReport;
+import com.sun.javafx.tk.Toolkit;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.http.HttpEntity;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.MediaType;
+import org.springframework.http.ResponseEntity;
+import org.springframework.http.converter.StringHttpMessageConverter;
+import org.springframework.scheduling.annotation.Async;
+import org.springframework.stereotype.Component;
+import org.springframework.util.LinkedMultiValueMap;
+import org.springframework.util.MultiValueMap;
+import org.springframework.web.client.RestTemplate;
+
+import java.nio.charset.StandardCharsets;
+
+
+@Component
+public class BlockchainService {
+
+//    @Value("${blockchain.host}")
+    String blockChainHost  = "111.231.68.200:8082";
+    private final String HTTP = "http://";
+
+    @Autowired
+    BugReportService bugReportService;
+    @Autowired
+    TaskService taskService;
+
+    @Async
+    public void addFinalToBlockChain(FinalReport finalReport) throws InterruptedException{
+        if(finalReport==null){
+            return ;
+        }
+        TaskDTO task = taskService.getByExamIdAndCaseId(finalReport.getExamId(), finalReport.getCaseId());
+        JSONObject wordObj = new JSONObject();
+        wordObj.fluentPut("reportHash",finalReport.getId()+"");
+        wordObj.fluentPut("reportMixer",finalReport.getWriterId()+"");
+        wordObj.fluentPut("taskId",task.getCaseId()+"-"+task.getExamId());
+        wordObj.fluentPut("taskName",task.getName());
+        wordObj.fluentPut("type",0);
+        wordObj.fluentPut("updateTime",System.currentTimeMillis());
+        JSONArray array = new JSONArray();
+        JSONObject obj = new JSONObject();
+        Bug bug = bugReportService.getSourceBugById(finalReport.getSourceId());
+        obj.fluentPut("bugId",bug.getId());
+        obj.fluentPut("bugName",bug.getTitle());
+        array.add(obj);
+        wordObj.fluentPut("bugReportList",array);
+        RestTemplate template = new RestTemplate();
+        String url = HTTP+blockChainHost+"/finalReport"; // 上传最终的报告
+        template.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
+        HttpHeaders headers = new HttpHeaders();
+        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
+        headers.setContentType(type);
+        HttpEntity<String> entity = new HttpEntity<String>(wordObj.toJSONString(),headers);
+        template.postForEntity(url, entity, String.class);
+//        JSONObject tasksJson = JSON.parseObject(response2.getBody());
+    }
+}

+ 2 - 0
src/main/java/com/mooctest/service/BugReportService.java

@@ -2,6 +2,7 @@ package com.mooctest.service;
 
 import com.mooctest.data.BugDTO;
 import com.mooctest.data.ReportDTO;
+import com.mooctest.model.Bug;
 
 import java.util.List;
 import java.util.Map;
@@ -13,5 +14,6 @@ public interface BugReportService {
     Map<String, BugDTO> getAllBugsMap(long examId, long caseId);
     BugDTO getBugById(String bugId);
     BugDTO getBugById(String bugId, long examId, long caseId);
+    Bug    getSourceBugById(String bugId);
 //    Map<String, List<BugDTO>> getMasterBugMap(Map<String, List<String>> masterBugIdsMap, Map<String, BugDTO> bugsMap);
 }

+ 8 - 0
src/main/java/com/mooctest/service/FinalReportService.java

@@ -36,6 +36,9 @@ public class FinalReportService {
     @Value("${report.export.excel.addr}")
     String reportExportExcelAttr;
 
+    @Autowired
+    BlockchainService blockchainService;
+
     public FinalReportDTO save(FinalReportDTO dto) {
 
         FinalReport finalReport = new FinalReport();
@@ -44,6 +47,11 @@ public class FinalReportService {
         finalReport.setCreateTime(new Date());
         finalReport = finalReportDao.save(finalReport);
         BeanUtils.copyProperties(finalReport, dto);
+        try {
+            blockchainService.addFinalToBlockChain(finalReport);
+        } catch (InterruptedException e) {
+            e.printStackTrace();
+        }
         return dto;
     }
 

+ 7 - 0
src/main/java/com/mooctest/service/impl/BugReportServiceImpl.java

@@ -114,11 +114,18 @@ public class BugReportServiceImpl implements BugReportService {
     }
 
 
+
+
     public BugDTO getBugById(String bugId, long examId, long caseId) {
         Bug bug = bugDao.findById(bugId).orElse(null);
         return wrap(bug);
     }
 
+    @Override
+    public Bug getSourceBugById(String bugId) {
+        return bugDao.findById(bugId).orElse(null);
+    }
+
     public Map<String, List<BugDTO>> getMasterBugMap(Map<String, List<String>> masterBugIdsMap, Map<String, BugDTO> bugsMap) {
 
         Map<String, List<BugDTO>> masterBugMap = new HashMap<>();

+ 2 - 2
src/main/java/com/mooctest/util/Doc2VecUtil.java

@@ -15,8 +15,8 @@ public class Doc2VecUtil {
             synchronized (Doc2VecUtil.class) {
                 if (docVectorModel == null) {
                     try {
-//                        docVectorModel = new DocVectorModel(new WordVectorModel("/project/sgns.wiki.word"));
-                        docVectorModel = new DocVectorModel(new  WordVectorModel("/Users/insomnialee/Desktop/sgns.wiki.word"));
+                        docVectorModel = new DocVectorModel(new WordVectorModel("/project/sgns.wiki.word"));
+//                        docVectorModel = new DocVectorModel(new  WordVectorModel("/Users/insomnialee/Desktop/sgns.wiki.word"));
                     } catch (IOException e) {
                         e.printStackTrace();
                     }