ExportService.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package edu.nju.service;
  2. import edu.nju.dao.BugDao;
  3. import edu.nju.dao.BugScoreDao;
  4. import edu.nju.dao.ReportDao;
  5. import edu.nju.entities.Bug;
  6. import edu.nju.entities.BugScore;
  7. import edu.nju.entities.Report;
  8. import edu.nju.model.ExportBugDTO;
  9. import edu.nju.model.ExportReportDTO;
  10. import org.json.JSONObject;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.beans.factory.annotation.Value;
  13. import org.springframework.stereotype.Service;
  14. import java.util.ArrayList;
  15. import java.util.Arrays;
  16. import java.util.Comparator;
  17. import java.util.List;
  18. import edu.nju.util.HTTP;
  19. @Service
  20. public class ExportService {
  21. @Autowired
  22. ReportDao reportDao;
  23. @Autowired
  24. BugDao bugDao;
  25. @Autowired
  26. BugScoreDao bugScoreDao;
  27. @Value("${mainsite.url}")
  28. String mainSiteUrl;
  29. public List<ExportReportDTO> getExportReport(String case_take_id){
  30. List<Report> reportList=reportDao.findByCaseTakeId(case_take_id);
  31. List<ExportReportDTO> exportReportDTOList=new ArrayList<>();
  32. for(Report report:reportList){
  33. String workerId=report.getWorker_id();
  34. String result = HTTP.sendGet(mainSiteUrl+"/api/user/" + workerId, "");
  35. String name="";
  36. if(result != null && !result.equals("")) {
  37. JSONObject json = new JSONObject(result);
  38. if(json.has("name")&&!json.isNull("name")) {
  39. name = json.getString("name");
  40. }
  41. }
  42. ExportReportDTO exportReportDTO=new ExportReportDTO();
  43. exportReportDTO.setWorkerId(workerId);
  44. exportReportDTO.setWorkerName(name);
  45. String reportId=report.getId();
  46. List<Bug> bugList=bugDao.findByReport(reportId,case_take_id);
  47. exportReportDTO.setBugCount(String.valueOf(bugList.size()));
  48. List<ExportBugDTO> exportBugDTOList=new ArrayList<>();
  49. //获取bug信息
  50. int totalScore=0;
  51. List<ExportBugDTO> bugDTOList=new ArrayList<>();
  52. for(Bug bug: bugList){
  53. ExportBugDTO exportBugDTO=new ExportBugDTO();
  54. BugScore bugScore=bugScoreDao.findById(bug.getId());
  55. exportBugDTO.setTitle(bug.getTitle());
  56. exportBugDTO.setDescription(bug.getDescription());
  57. if(bugScore!=null) {
  58. exportBugDTO.setScore(String.valueOf(bugScore.getGrade()));
  59. totalScore += bugScore.getGrade();
  60. }else{
  61. exportBugDTO.setScore("0");
  62. }
  63. String page=bug.getBug_page();
  64. String[] pages=page.split("-");
  65. exportBugDTO.setPage1("");
  66. exportBugDTO.setPage2("");
  67. exportBugDTO.setPage3("");
  68. if(pages.length>0) {
  69. exportBugDTO.setPage1(pages[0]);
  70. }
  71. if(pages.length>1) {
  72. exportBugDTO.setPage2(pages[1]);
  73. }
  74. if(pages.length>2) {
  75. exportBugDTO.setPage3(pages[2]);
  76. }
  77. String imgUrl=bug.getImg_url();
  78. String[] imgUrlList=imgUrl.split(",");
  79. exportBugDTO.setImgUrlList(Arrays.asList(imgUrlList));
  80. exportBugDTOList.add(exportBugDTO);
  81. }
  82. exportReportDTO.setExportBugDTOList(exportBugDTOList);
  83. exportReportDTO.setTotalScore(String.valueOf(totalScore));
  84. exportReportDTOList.add(exportReportDTO);
  85. }
  86. totalScoreSort(exportReportDTOList);
  87. return exportReportDTOList;
  88. }
  89. public void totalScoreSort(List<ExportReportDTO> list ){
  90. list.sort(new Comparator<ExportReportDTO>() {
  91. public int compare(ExportReportDTO report1, ExportReportDTO report2) {
  92. return (Integer.parseInt(report2.getTotalScore()) - Integer.parseInt(report1.getTotalScore()));
  93. }
  94. });
  95. }
  96. }