DiagnoseFaultController.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package com.gyee.impala.controller.diagnose;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.gyee.impala.common.feign.RemoteServiceBuilder;
  4. import com.gyee.impala.common.result.JsonResult;
  5. import com.gyee.impala.common.result.ResultCode;
  6. import com.gyee.impala.common.util.DateUtil;
  7. import com.gyee.impala.model.custom.diagnose.FaultInfo;
  8. import com.gyee.impala.model.master.Casefaultalg;
  9. import com.gyee.impala.service.custom.diagnose.AutoCmdService;
  10. import com.gyee.impala.service.master.CasefaultalgService;
  11. import org.apache.commons.lang3.StringUtils;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.web.bind.annotation.*;
  14. import java.util.*;
  15. import java.util.stream.Collectors;
  16. /**
  17. * 离线故障诊断
  18. */
  19. @CrossOrigin
  20. @RestController
  21. @RequestMapping("/api/diagnosefault")
  22. public class DiagnoseFaultController {
  23. @Autowired
  24. private AutoCmdService autoCmdService;
  25. @Autowired
  26. private CasefaultalgService casefaultalgService;
  27. @Autowired
  28. private RemoteServiceBuilder remoteServiceBuilder;
  29. /**
  30. * 离线列表数据
  31. * @return
  32. */
  33. @GetMapping("/offline")
  34. public JSONObject offline(@RequestParam(value = "station", required = false) String station,
  35. @RequestParam(value = "windturbineid", required = false) String wtId,
  36. @RequestParam(value = "id", required = false) String[] id,
  37. @RequestParam(value = "faultid", required = false) Long[] faultid,
  38. @RequestParam(value = "model", required = false) String[] model,
  39. @RequestParam(value = "faultcode", required = false) String[] faultcode,
  40. @RequestParam(value = "st", required = false) String st,
  41. @RequestParam(value = "et", required = false) String et,
  42. @RequestParam(value = "algcode", required = false) String algcode,
  43. @RequestParam(value = "confirm", required = false) Optional<Boolean> confirm){
  44. boolean conf = confirm.isPresent() ? confirm.get() : false;
  45. List<Casefaultalg> list = casefaultalgService.getAll(station, wtId, id, faultid, model, faultcode, st, et, algcode, conf);
  46. if (conf){
  47. return JsonResult.successData(ResultCode.SUCCESS, list);
  48. }else{
  49. List<Casefaultalg> array = new ArrayList<>();
  50. Map<Long, List<Casefaultalg>> map = list.stream().collect(Collectors.groupingBy(Casefaultalg::getFaultid));
  51. List<FaultInfo> faults = remoteServiceBuilder.sharding().getFaultInfoList("FJ", "GZ", st, et, station, 1);
  52. for (int i = 0; i < faults.size(); i++){
  53. FaultInfo info = faults.get(i);
  54. if (!map.containsKey(info.getId()))
  55. array.add(format(info));
  56. }
  57. return JsonResult.successData(ResultCode.SUCCESS, array);
  58. }
  59. }
  60. /**
  61. * 离线诊断
  62. * @return
  63. */
  64. @PostMapping("/offlinediagnose")
  65. public JSONObject offlineDiagnose(@RequestBody JSONObject json){
  66. if (json == null)
  67. return JsonResult.error(ResultCode.PARAM_IS_BLANK);
  68. String model = json.getString("model");
  69. List<Casefaultalg> faults = JSONObject.parseArray(json.getJSONArray("faults").toString(), Casefaultalg.class);
  70. faults.stream().forEach(obj -> {
  71. FaultInfo info = new FaultInfo();
  72. info.setStationId(obj.getStationen());
  73. info.setModelId(obj.getModel());
  74. info.setId(Long.valueOf(obj.getFaultid()));
  75. autoCmdService.exec(info);
  76. });
  77. return JsonResult.success(ResultCode.SUCCESS);
  78. }
  79. /**
  80. * 诊断准确率统计
  81. * @return
  82. */
  83. @GetMapping("/statistic")
  84. public JSONObject statistic(String[] station, String st, String et){
  85. if (StringUtils.isEmpty(st) || StringUtils.isEmpty(et)){
  86. Calendar cal = Calendar.getInstance();
  87. int year = cal.get(Calendar.YEAR);
  88. st = year + "-01-01 00:00:00";
  89. et = year + "-12-30 23:59:59";
  90. }
  91. List<Object> list = new ArrayList<>();
  92. for (String pt : station){
  93. List<Casefaultalg> faults = casefaultalgService.getAll(pt, null, null, null, null, null, st, et, null, null);
  94. List<Casefaultalg> collect = faults.stream().filter(a -> a.getConfirm()).collect(Collectors.toList());
  95. int size = collect.size();
  96. int count = collect.stream().filter(f -> !StringUtils.isEmpty(f.getDiagnosecode())
  97. && !StringUtils.isEmpty(f.getFaultcode())
  98. && f.getDiagnosecode().equalsIgnoreCase(f.getFaultcode())).collect(Collectors.toList()).size();
  99. double accuracy = size > 0 ? (double)count / (double)size : -1;
  100. Map<String, Object> map = new HashMap<>();
  101. map.put("station", pt);
  102. map.put("accuracy", accuracy);
  103. list.add(map);
  104. }
  105. return JsonResult.successData(ResultCode.SUCCESS, list);
  106. }
  107. private Casefaultalg format(FaultInfo info){
  108. Casefaultalg fault = new Casefaultalg();
  109. fault.setStationcn(info.getStationName());
  110. fault.setStationen(info.getStationId());
  111. fault.setTag(0);
  112. fault.setFaultid(info.getId());
  113. fault.setWindturbineid(info.getWindturbineId());
  114. fault.setDiagnosetype(info.getAlertText());
  115. fault.setStarttime(info.getFaultTime());
  116. fault.setModel(info.getModelId());
  117. fault.setCategory("0");
  118. return fault;
  119. }
  120. }