SaveService.java 9.7 KB

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