123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- 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 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;
- import java.util.List;
- @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;
- @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(finalReportDTO.getSourceId());
- obj.fluentPut("bugId",bug.getId());
- obj.fluentPut("bugName",bug.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) {
- 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());
- }
- }
|