Browse Source

修改最终报告导入到区块链的结构

insomniaLee 5 năm trước cách đây
mục cha
commit
9d64bd1fe3

+ 11 - 0
src/main/java/com/mooctest/controller/FinalReportController.java

@@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSONObject;
 import com.mooctest.data.BugDTO;
 import com.mooctest.data.FinalReportDTO;
 import com.mooctest.data.TaskDTO;
+import com.mooctest.service.BlockchainService;
 import com.mooctest.service.BugReportService;
 import com.mooctest.service.FinalReportService;
 import com.mooctest.service.TaskService;
@@ -27,6 +28,9 @@ public class FinalReportController {
     @Autowired
     BugReportService bugReportService;
 
+    @Autowired
+    BlockchainService blockchainService;
+
     @PostMapping("final_report")
     @ResponseBody
     public FinalReportDTO createReport(@RequestBody FinalReportDTO dto) {
@@ -105,7 +109,14 @@ public class FinalReportController {
         return finalReportService.getReportExportExcelAttr(task);
     }
 
+    @GetMapping("final_report/export_block")
+    @ResponseBody
+    public String exportBlockChain(@RequestParam("examId") long examId,
+                              @RequestParam("caseId") long caseId) {
 
+        blockchainService.addFinalReportList2BlockChain(examId, caseId);
+        return "success";
+    }
 
 
     @RequestMapping(value = "/appendBugToFinalReport")

+ 33 - 1
src/main/java/com/mooctest/service/BlockchainService.java

@@ -23,6 +23,7 @@ import org.springframework.util.MultiValueMap;
 import org.springframework.web.client.RestTemplate;
 
 import java.nio.charset.StandardCharsets;
+import java.util.List;
 
 
 @Component
@@ -36,9 +37,40 @@ public class BlockchainService {
     BugReportService bugReportService;
     @Autowired
     TaskService taskService;
+    @Autowired
+    FinalReportService finalReportService;
+
+    public void addFinalReportList2BlockChain(long examId,long caseId) {
+        List<FinalReportDTO> data  = finalReportService.getByExamIdAndCaseId(examId,caseId); // 所有的finalReport信息
+        TaskDTO task = taskService.getByExamIdAndCaseId(examId, caseId);
+        JSONArray array = new JSONArray();
+        for(FinalReportDTO finalReportDTO : data){
+            JSONObject obj = new JSONObject();
+            //Bug bug = bugReportService.getSourceBugById(finalReport.getSourceId());
+            obj.fluentPut("bugId",finalReportDTO.getId());
+            obj.fluentPut("bugName",finalReportDTO.getDescription());
+            array.add(obj);
+        }
+        JSONObject wordObj = new JSONObject();
+        wordObj.fluentPut("reportHash",task.getExamId()+task.getCaseId()+"");
+        wordObj.fluentPut("reportMixer","admin");
+        wordObj.fluentPut("taskId",task.getCaseId()+"-"+task.getExamId());
+        wordObj.fluentPut("taskName",task.getName());
+        wordObj.fluentPut("type",0);
+        wordObj.fluentPut("updateTime",System.currentTimeMillis());
+        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);
+    }
 
     @Async
-    public void addFinalToBlockChain(FinalReport finalReport) throws InterruptedException{
+    public void addFinalToBlockChain(FinalReport finalReport) {
         if(finalReport==null){
             return ;
         }

+ 5 - 5
src/main/java/com/mooctest/service/FinalReportService.java

@@ -47,11 +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();
-        }
+//        try {
+//            blockchainService.addFinalToBlockChain(finalReport);
+//        } catch (InterruptedException e) {
+//            e.printStackTrace();
+//        }
         return dto;
     }
 

+ 20 - 0
src/main/resources/templates/final_report_list.html

@@ -112,6 +112,12 @@
                 <span>导出报告(Excel)</span>
             </button>
         </div>
+        <div class="assign-div pull-right">
+            <button id="block-chain-btn" class="btn btn-sm btn-assign pull-right">
+                <i class="fa fa-coffee" style="margin-right: 3px;"></i>
+                <span>将数据导入区块链</span>
+            </button>
+        </div>
         <table id="report-list" class="table table-striped table-bordered" cellpadding="0" width="100%">
             <thead>
             <tr>
@@ -260,6 +266,20 @@
                 }
             });
         });
+
+        $('#block-chain-btn').click(function(){
+            // 修改assign-btn的文字以及让其处于不可用状态
+            // $(this).attr('disabled', true);
+            var urlParams = new URLSearchParams(window.location.search);
+            var examId = urlParams.get('examId');
+            var caseId = urlParams.get('caseId');
+            $.get('/final_report/export_block', {examId: examId, caseId: caseId}, function (result) {
+                // var url = JSON.parse(result).downloadUrl;
+                console.log(result)
+                alert("已提交导入区块链")
+                $('#block-chain-btn').attr('disabled', false);
+            });
+        });
     });