BlockchainService.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package com.mooctest.service;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.mooctest.data.BugDTO;
  6. import com.mooctest.data.FinalReportDTO;
  7. import com.mooctest.data.TaskDTO;
  8. import com.mooctest.model.Bug;
  9. import com.mooctest.model.FinalReport;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.beans.factory.annotation.Value;
  12. import org.springframework.http.HttpEntity;
  13. import org.springframework.http.HttpHeaders;
  14. import org.springframework.http.MediaType;
  15. import org.springframework.http.ResponseEntity;
  16. import org.springframework.http.converter.StringHttpMessageConverter;
  17. import org.springframework.scheduling.annotation.Async;
  18. import org.springframework.stereotype.Component;
  19. import org.springframework.util.LinkedMultiValueMap;
  20. import org.springframework.util.MultiValueMap;
  21. import org.springframework.web.client.RestTemplate;
  22. import java.nio.charset.StandardCharsets;
  23. import java.util.List;
  24. @Component
  25. public class BlockchainService {
  26. // @Value("${blockchain.host}")
  27. String blockChainHost = "111.231.68.200:8082";
  28. private final String HTTP = "http://";
  29. @Autowired
  30. BugReportService bugReportService;
  31. @Autowired
  32. TaskService taskService;
  33. @Autowired
  34. FinalReportService finalReportService;
  35. public void addFinalReportList2BlockChain(long examId,long caseId) {
  36. List<FinalReportDTO> data = finalReportService.getByExamIdAndCaseId(examId,caseId); // 所有的finalReport信息
  37. TaskDTO task = taskService.getByExamIdAndCaseId(examId, caseId);
  38. JSONArray array = new JSONArray();
  39. for(FinalReportDTO finalReportDTO : data){
  40. JSONObject obj = new JSONObject();
  41. Bug bug = bugReportService.getSourceBugById(finalReportDTO.getSourceId());
  42. obj.fluentPut("bugId",bug.getId());
  43. obj.fluentPut("bugName",bug.getDescription());
  44. array.add(obj);
  45. }
  46. JSONObject wordObj = new JSONObject();
  47. wordObj.fluentPut("reportHash",task.getExamId()+task.getCaseId()+"");
  48. wordObj.fluentPut("reportMixer","admin");
  49. wordObj.fluentPut("taskId",task.getCaseId()+"-"+task.getExamId());
  50. wordObj.fluentPut("taskName",task.getName());
  51. wordObj.fluentPut("type",0);
  52. wordObj.fluentPut("updateTime",System.currentTimeMillis());
  53. wordObj.fluentPut("bugReportList",array);
  54. RestTemplate template = new RestTemplate();
  55. String url = HTTP+blockChainHost+"/finalReport"; // 上传最终的报告
  56. template.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
  57. HttpHeaders headers = new HttpHeaders();
  58. MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
  59. headers.setContentType(type);
  60. HttpEntity<String> entity = new HttpEntity<String>(wordObj.toJSONString(),headers);
  61. template.postForEntity(url, entity, String.class);
  62. }
  63. @Async
  64. public void addFinalToBlockChain(FinalReport finalReport) {
  65. if(finalReport==null){
  66. return ;
  67. }
  68. TaskDTO task = taskService.getByExamIdAndCaseId(finalReport.getExamId(), finalReport.getCaseId());
  69. JSONObject wordObj = new JSONObject();
  70. wordObj.fluentPut("reportHash",finalReport.getId()+"");
  71. wordObj.fluentPut("reportMixer",finalReport.getWriterId()+"");
  72. wordObj.fluentPut("taskId",task.getCaseId()+"-"+task.getExamId());
  73. wordObj.fluentPut("taskName",task.getName());
  74. wordObj.fluentPut("type",0);
  75. wordObj.fluentPut("updateTime",System.currentTimeMillis());
  76. JSONArray array = new JSONArray();
  77. JSONObject obj = new JSONObject();
  78. Bug bug = bugReportService.getSourceBugById(finalReport.getSourceId());
  79. obj.fluentPut("bugId",bug.getId());
  80. obj.fluentPut("bugName",bug.getTitle());
  81. array.add(obj);
  82. wordObj.fluentPut("bugReportList",array);
  83. RestTemplate template = new RestTemplate();
  84. String url = HTTP+blockChainHost+"/finalReport"; // 上传最终的报告
  85. template.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
  86. HttpHeaders headers = new HttpHeaders();
  87. MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
  88. headers.setContentType(type);
  89. HttpEntity<String> entity = new HttpEntity<String>(wordObj.toJSONString(),headers);
  90. template.postForEntity(url, entity, String.class);
  91. // JSONObject tasksJson = JSON.parseObject(response2.getBody());
  92. }
  93. }