DiagnoseFaultController.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. if (conf){
  46. List<Casefaultalg> list = casefaultalgService.getAll(station, wtId, id, faultid, model, faultcode, st, et, algcode, false);
  47. return JsonResult.successData(ResultCode.SUCCESS, list);
  48. }else{
  49. List<Casefaultalg> array = new ArrayList<>();
  50. List<Casefaultalg> list = casefaultalgService.getAll(station, wtId, id, faultid, model, faultcode, st, et, algcode, false);
  51. List<FaultInfo> faults = remoteServiceBuilder.sharding().getFaultRecent(et);
  52. List<FaultInfo> collect = faults.stream()
  53. .filter(f -> f.getCategory1().equals("FJ") && f.getCategory2().equals("GZ"))
  54. .filter(a -> StringUtils.isEmpty(station) || a.getStationId().equals(station))
  55. .filter(a -> StringUtils.isEmpty(wtId) || a.getWindturbineId().equals(wtId))
  56. .collect(Collectors.toList());
  57. for (int i = 0; i < collect.size(); i++){
  58. FaultInfo info = collect.get(i);
  59. if (list == null || list.size() == 0){
  60. array.add(format(info));
  61. }else{
  62. List<Casefaultalg> ls = list.stream().filter(a -> a.getFaultid().equals(info.getId())).collect(Collectors.toList());
  63. if (ls.size() == 0)
  64. array.add(format(info));
  65. }
  66. }
  67. return JsonResult.successData(ResultCode.SUCCESS, array);
  68. }
  69. }
  70. /**
  71. * 离线诊断
  72. * @return
  73. */
  74. @PostMapping("/offlinediagnose")
  75. public JSONObject offlineDiagnose(@RequestBody JSONObject json){
  76. if (json == null)
  77. return JsonResult.error(ResultCode.PARAM_IS_BLANK);
  78. String model = json.getString("model");
  79. List<Casefaultalg> faults = JSONObject.parseArray(json.getJSONArray("faults").toString(), Casefaultalg.class);
  80. faults.stream().forEach(obj -> {
  81. FaultInfo info = new FaultInfo();
  82. info.setStationId(obj.getStationen());
  83. info.setModelId(obj.getModel());
  84. info.setId(Long.valueOf(obj.getFaultid()));
  85. autoCmdService.exec(info);
  86. });
  87. return JsonResult.success(ResultCode.SUCCESS);
  88. }
  89. /**
  90. * 诊断准确率统计
  91. * @return
  92. */
  93. @GetMapping("/statistic")
  94. public JSONObject statistic(String[] station, String st, String et){
  95. if (StringUtils.isEmpty(st) || StringUtils.isEmpty(et)){
  96. Calendar cal = Calendar.getInstance();
  97. int year = cal.get(Calendar.YEAR);
  98. st = year + "-01-01 00:00:00";
  99. et = year + "-12-30 23:59:59";
  100. }
  101. List<Object> list = new ArrayList<>();
  102. for (String pt : station){
  103. List<Casefaultalg> collect = casefaultalgService.getAll(pt, null, null, null, null, null, st, et, null, null);
  104. int size = collect.stream().filter(a -> a.getConfirm()).collect(Collectors.toList()).size();
  105. int count = collect.stream().filter(a -> a.getAlgcode().equals(a.getFaultcode())).collect(Collectors.toList()).size();
  106. Map<String, Object> map = new HashMap<>();
  107. int accuracy = size > 0 ? count / size : -1;
  108. map.put("station", station);
  109. map.put("accuracy", accuracy);
  110. list.add(map);
  111. }
  112. return JsonResult.successData(ResultCode.SUCCESS, list);
  113. }
  114. private Casefaultalg format(FaultInfo info){
  115. Casefaultalg fault = new Casefaultalg();
  116. fault.setStationcn(info.getStationName());
  117. fault.setStationen(info.getStationId());
  118. fault.setTag(0);
  119. fault.setFaultid(info.getId());
  120. fault.setWindturbineid(info.getWindturbineId());
  121. fault.setDiagnosetype(info.getAlertText());
  122. fault.setStarttime(info.getFaultTime());
  123. fault.setModel(info.getModelId());
  124. fault.setCategory("0");
  125. return fault;
  126. }
  127. }