SaveService.java 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. package edu.nju.service;
  2. import java.text.SimpleDateFormat;
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.HashSet;
  6. import java.util.List;
  7. import java.util.Set;
  8. import edu.nju.util.HTTP;
  9. import org.json.JSONObject;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.beans.factory.annotation.Value;
  12. import org.springframework.stereotype.Service;
  13. import edu.nju.dao.BugDao;
  14. import edu.nju.dao.BugHistoryDao;
  15. import edu.nju.dao.BugMirrorDao;
  16. import edu.nju.dao.BugPageDao;
  17. import edu.nju.dao.KWDao;
  18. import edu.nju.dao.StuInfoDao;
  19. import edu.nju.dao.ThumsUpDao;
  20. import edu.nju.entities.Bug;
  21. import edu.nju.entities.BugHistory;
  22. import edu.nju.entities.BugMirror;
  23. import edu.nju.entities.BugPage;
  24. import edu.nju.entities.KeyWords;
  25. import edu.nju.entities.ThumsUp;
  26. import edu.nju.util.StringMatch;
  27. @Service
  28. public class SaveService {
  29. @Autowired
  30. BugMirrorDao mirrordao;
  31. @Autowired
  32. BugDao bugdao;
  33. @Autowired
  34. BugHistoryDao historydao;
  35. @Autowired
  36. BugPageDao pagedao;
  37. @Autowired
  38. HistoryService hisservice;
  39. @Autowired
  40. DotService dotservice;
  41. @Autowired
  42. KWDao kwdao;
  43. @Autowired
  44. ThumsUpDao tdao;
  45. @Autowired
  46. StuInfoDao sdao;
  47. @Autowired
  48. AnalyzeService aservice;
  49. @Value("${mainsite.url}")
  50. String mainSiteUrl;
  51. public String save(String case_take_id, String bug_category, String description, String img_url, String severity, String recurrent, String title, String report_id, String parent, String page, String useCase, String case_id) {
  52. try {
  53. StringMatch match = new StringMatch();
  54. // System.out.println("report"+report_id);
  55. String id = bugdao.save(new Bug(case_take_id, Long.toString(System.currentTimeMillis()), bug_category, description, img_url, severityTranse(severity), recurrentTranse(recurrent), title, report_id, page, case_id));
  56. mirrordao.save(new BugMirror(id, case_take_id, bug_category, severityTranse(severity), recurrentTranse(recurrent), title, img_url, new HashSet<String>(), new HashSet<String>(), report_id, useCase, true));
  57. kwdao.save(new KeyWords(id, match.Ansj(title), match.Ansj(description)));
  58. if(!parent.equals("null")) {
  59. String[] sp = parent.split("-");
  60. dotservice.saveType1(id, sp[1]);
  61. historydao.addChild(sp[0], id);
  62. String p = hisservice.parents(sp[0]).get(0);
  63. historydao.save(new BugHistory(id, sp[0], new ArrayList<String>(), p));
  64. } else {
  65. historydao.save(new BugHistory(id, parent, new ArrayList<String>(), id));
  66. }
  67. if(!page.equals("")) {
  68. savePage(id, case_take_id, page);
  69. }
  70. return id;
  71. } catch(Exception e) {
  72. e.printStackTrace();
  73. return "";
  74. }
  75. }
  76. // public boolean update(String id, String case_take_id, String bug_category, String description, String img_url, int severity, int recurrent, String title, String report_id, String parent,String page, String useCase, String case_id) {
  77. // try {
  78. // bugdao.save(new Bug(id, case_take_id, Long.toString(System.currentTimeMillis()), bug_category, description, img_url, severityTranse(severity), recurrentTranse(recurrent), title, report_id, page, case_id));
  79. // mirrordao.save(new BugMirror(id, case_take_id, bug_category, severityTranse(severity), recurrentTranse(recurrent), title, img_url, new HashSet<String>(), new HashSet<String>(), report_id, useCase, true));
  80. // if(!page.equals("")) {
  81. // savePage(id, case_take_id, page);
  82. // }
  83. // return true;
  84. // } catch(Exception e) {
  85. // e.printStackTrace();
  86. // return false;
  87. // }
  88. // }
  89. public boolean saveStu(String report_id, String worker_id) {
  90. try {
  91. String name = sdao.findById(report_id);
  92. if(name.equals("null") || name.equals("")) {
  93. String result = HTTP.sendGet(mainSiteUrl+"/api/user/" + worker_id, "");
  94. if(result != null && !result.equals("")) {
  95. JSONObject json = new JSONObject(result);
  96. sdao.save(report_id, worker_id, json.getString("name"));
  97. }
  98. }
  99. return true;
  100. } catch (Exception e) {
  101. e.printStackTrace();
  102. return false;
  103. }
  104. }
  105. public boolean repair(String id, String page, String case_take_id, String bug_category, String description, String img_url, String severity, String recurrent, String report_id, String case_id) {
  106. try {
  107. if(id == null || id.equals("undefined")) { return false; }
  108. KeyWords keywords = kwdao.findById(id);
  109. String title = "";
  110. if(keywords != null) { title = keywords.getTitle().replaceAll(",", ""); }
  111. Bug bug = new Bug(case_take_id, Long.toString(System.currentTimeMillis()), bug_category, description, img_url, severityTranse(severity), recurrentTranse(recurrent), title, report_id, page, case_id);
  112. bug.setId(id);
  113. bugdao.save(bug);
  114. mirrordao.save(new BugMirror(id, case_take_id, bug_category, severityTranse(severity), recurrentTranse(recurrent), title, img_url, new HashSet<String>(), new HashSet<String>(), report_id, "null", true)); //null为useCase
  115. return true;
  116. } catch(Exception e) {
  117. e.printStackTrace();
  118. return false;
  119. }
  120. }
  121. public boolean repairThums(String case_take_id) {
  122. try {
  123. List<Bug> bugs = bugdao.findByCaseid(case_take_id);
  124. Set<String> reports = new HashSet<String>();
  125. for(Bug bug: bugs) {
  126. String report_id = bug.getReport_id();
  127. if(!reports.contains(report_id)) {
  128. reports.add(report_id);
  129. ThumsUp thumsup = tdao.findByReport(report_id);
  130. if(thumsup == null) { continue; }
  131. Set<String> thums = thumsup.getThums();
  132. Set<String> diss = thumsup.getDiss();
  133. if(thums != null && !thums.isEmpty()) {
  134. for(String id : thums) {
  135. BugMirror mirror = mirrordao.findById(id);
  136. if(mirror == null) { continue; }
  137. mirror.getGood().add(report_id);
  138. mirrordao.save(mirror);
  139. }
  140. }
  141. if(diss != null && !diss.isEmpty()) {
  142. for(String id : diss) {
  143. BugMirror mirror = mirrordao.findById(id);
  144. if(mirror == null) { continue; }
  145. mirror.getBad().add(report_id);
  146. mirrordao.save(mirror);
  147. }
  148. }
  149. }
  150. }
  151. return true;
  152. } catch (Exception e) {
  153. e.printStackTrace();
  154. return false;
  155. }
  156. }
  157. public boolean repairTime(String case_take_id) {
  158. try {
  159. SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  160. // long start = dateformat.parse("2018-11-16 08:05:17").getCreate_time();
  161. // long middle = dateformat.parse("2018-11-16 09:15:17").getCreate_time();
  162. // long end = dateformat.parse("2018-11-16 11:55:17").getCreate_time();
  163. long start = dateformat.parse("2018-11-16 17:05:17").getTime();
  164. long middle = dateformat.parse("2018-11-16 18:15:17").getTime();
  165. long end = dateformat.parse("2018-11-16 19:55:17").getTime();
  166. List<String> ids = aservice.getValid(case_take_id);
  167. Collections.sort(ids);
  168. int size = ids.size();
  169. int middle_size = (int)(size * 0.7);
  170. Long gap1 = (middle - start) / middle_size;
  171. Long gap2 = (end - middle) / (size - middle_size);
  172. for(int i = 0; i < middle_size; i ++) {
  173. long new_time = start + gap1 * i;
  174. Bug bug = bugdao.findByid(ids.get(i));
  175. bug.setCreate_time_millis(Long.toString(new_time));
  176. bugdao.save(bug);
  177. }
  178. for(int i = middle_size; i < size; i ++) {
  179. long new_time = middle + gap2 * (i - middle_size);
  180. Bug bug = bugdao.findByid(ids.get(i));
  181. bug.setCreate_time_millis(Long.toString(new_time));
  182. bugdao.save(bug);
  183. }
  184. return true;
  185. } catch (Exception e) {
  186. e.printStackTrace();
  187. return false;
  188. }
  189. }
  190. public boolean saveTitle(String id, String title) {
  191. try {
  192. Bug bug = bugdao.findByid(id);
  193. if(bug != null) {
  194. bug.setTitle(title);
  195. bugdao.save(bug);
  196. }
  197. BugMirror mirror = mirrordao.findById(id);
  198. if(mirror != null) {
  199. mirror.setTitle(title);
  200. mirrordao.save(mirror);
  201. }
  202. return true;
  203. } catch (Exception e) {
  204. e.printStackTrace();
  205. return false;
  206. }
  207. }
  208. public boolean confirm(String id, String report_id) {
  209. try {
  210. if(mirrordao.haveJudged(id, report_id)) {
  211. mirrordao.good(id, report_id);
  212. tdao.saveGood(id, report_id);
  213. return true;
  214. }
  215. return false;
  216. } catch(Exception e) {
  217. return false;
  218. }
  219. }
  220. public boolean cancelGood(String id, String report_id) {
  221. try {
  222. mirrordao.cancelGood(id, report_id);
  223. tdao.cancelGood(id, report_id);
  224. return true;
  225. } catch(Exception e) {
  226. return false;
  227. }
  228. }
  229. public boolean diss(String id, String report_id) {
  230. try {
  231. if(mirrordao.haveJudged(id, report_id)) {
  232. mirrordao.bad(id, report_id);
  233. tdao.saveDiss(id, report_id);
  234. return true;
  235. }
  236. return false;
  237. } catch(Exception e) {
  238. return false;
  239. }
  240. }
  241. public boolean cancelDiss(String id, String report_id) {
  242. try {
  243. mirrordao.canelBad(id, report_id);
  244. tdao.cancelDiss(id, report_id);
  245. return true;
  246. } catch(Exception e) {
  247. return false;
  248. }
  249. }
  250. private void savePage(String id, String case_take_id, String page) {
  251. String[] pages = page.split("-");
  252. int length = pages.length;
  253. String page2 = "";
  254. String page3 = "";
  255. if(length == 2) {
  256. page2 = pages[1];
  257. } else if(length == 3) {
  258. page2 = pages[1];
  259. page3 = pages[2];
  260. }
  261. //如果在第三级页面出现了-
  262. else{
  263. page2=pages[1];
  264. for(int i=2;i<length-1;i++){
  265. page3+=(pages[i]+"-");
  266. }
  267. page3+=pages[pages.length-1];
  268. }
  269. BugPage save = new BugPage(id, pages[0], page2, page3, case_take_id);
  270. pagedao.save(save);
  271. }
  272. private int severityTranse(String str) {
  273. if(str.equals("待定")) {
  274. return 1;
  275. }
  276. if(str.equals("较轻")) {
  277. return 2;
  278. }
  279. if(str.equals("一般")) {
  280. return 3;
  281. }
  282. if(str.equals("严重")) {
  283. return 4;
  284. }
  285. if(str.equals("紧急")) {
  286. return 5;
  287. }
  288. return 0;
  289. }
  290. private int recurrentTranse(String str) {
  291. if(str.equals("其他")) {
  292. return 1;
  293. }
  294. if(str.equals("无规律复现")) {
  295. return 2;
  296. }
  297. if(str.equals("小概率复现")) {
  298. return 3;
  299. }
  300. if(str.equals("大概率复现")) {
  301. return 4;
  302. }
  303. if(str.equals("必现")) {
  304. return 5;
  305. }
  306. return 0;
  307. }
  308. }