|  |  | 
 |  |  | import cn.hutool.json.XML; | 
 |  |  | import com.fasterxml.jackson.databind.ObjectMapper; | 
 |  |  | import com.zt.common.annotation.LogOperation; | 
 |  |  | import com.zt.common.annotation.QueryParam; | 
 |  |  | import com.zt.common.constant.Constant; | 
 |  |  | import com.zt.common.db.query.QueryFilter; | 
 |  |  | import com.zt.common.exception.RenException; | 
 |  |  | import com.zt.common.servlet.PageResult; | 
 |  |  | import com.zt.common.servlet.Result; | 
 |  |  | import com.zt.common.validator.AssertUtils; | 
 |  |  | import com.zt.common.validator.ValidatorUtils; | 
 |  |  | import com.zt.common.validator.group.DefaultGroup; | 
 |  |  | import com.zt.common.validator.group.UpdateGroup; | 
 |  |  | import com.zt.life.modules.mainPart.async.PythonLib; | 
 |  |  | import com.zt.life.modules.mainPart.taskReliability.dto.SimulaDataDto; | 
 |  |  | import com.zt.life.modules.mainPart.taskReliability.dto.ProductStatusDto; | 
 |  |  | import com.zt.life.modules.mainPart.taskReliability.dto.StatusDto; | 
 |  |  | import com.zt.life.modules.mainPart.taskReliability.model.CurveParam; | 
 |  |  | import com.zt.life.modules.mainPart.taskReliability.model.SimulatAssess; | 
 |  |  | import com.zt.life.modules.mainPart.taskReliability.model.Task; | 
 |  |  | import com.zt.life.modules.mainPart.taskReliability.model.TimeDiagram; | 
 |  |  | import com.zt.life.modules.mainPart.taskReliability.dto.*; | 
 |  |  | import com.zt.life.modules.mainPart.taskReliability.model.*; | 
 |  |  | import com.zt.life.modules.mainPart.taskReliability.service.SimulatAssessService; | 
 |  |  | import com.zt.life.modules.mainPart.taskReliability.service.TaskService; | 
 |  |  | import com.zt.life.modules.mainPart.utils.OSUtils.ProcessUtils; | 
 |  |  | import io.swagger.annotations.Api; | 
 |  |  | import io.swagger.annotations.ApiImplicitParam; | 
 |  |  | import io.swagger.annotations.ApiImplicitParams; | 
 |  |  | import io.swagger.annotations.ApiOperation; | 
 |  |  | import org.apache.commons.io.IOUtils; | 
 |  |  | import org.apache.commons.lang3.StringUtils; | 
 |  |  | 
 |  |  | import org.springframework.beans.factory.annotation.Value; | 
 |  |  | import org.springframework.data.redis.core.RedisTemplate; | 
 |  |  | import org.springframework.web.bind.annotation.*; | 
 |  |  | import springfox.documentation.annotations.ApiIgnore; | 
 |  |  |  | 
 |  |  | import java.io.File; | 
 |  |  | import java.io.FileInputStream; | 
 |  |  | import java.io.IOException; | 
 |  |  | import java.io.InputStream; | 
 |  |  | import java.util.ArrayList; | 
 |  |  | import java.util.HashMap; | 
 |  |  | import java.util.List; | 
 |  |  | import java.util.Map; | 
 |  |  | import java.util.*; | 
 |  |  |  | 
 |  |  |  | 
 |  |  | /** | 
 |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private RedisTemplate redisTemplate; | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private PythonLib pythonLib; | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private TaskService taskService; | 
 |  |  |     @Value("${data.reliaSimLib.resultHome}") | 
 |  |  |     private String path; | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private SimulatAssessService simulatAssessService; | 
 |  |  |  | 
 |  |  |     private JSONObject xmlJSONObj; | 
 |  |  |  | 
 |  |  |  | 
 |  |  |    /* @GetMapping("page") | 
 |  |  |     @ApiOperation("分页") | 
 |  |  |     @ApiImplicitParams({ | 
 |  |  |             @ApiImplicitParam(name = Constant.Q.PAGE, value = Constant.QV.PAGE, required = true, dataType = Constant.QT.INT), | 
 |  |  |             @ApiImplicitParam(name = Constant.Q.LIMIT, value = Constant.QV.LIMIT, required = true, dataType = Constant.QT.INT), | 
 |  |  |             @ApiImplicitParam(name = Constant.Q.ORDER_FIELD, value = Constant.QV.ORDER_FIELD, dataType = Constant.QT.STRING), | 
 |  |  |             @ApiImplicitParam(name = Constant.Q.ORDER, value = Constant.QV.ORDER, dataType = Constant.QT.STRING), | 
 |  |  |     }) | 
 |  |  |     public PageResult<SimulatAssess> page(@ApiIgnore @QueryParam QueryFilter queryFilter) { | 
 |  |  |  | 
 |  |  |         return PageResult.ok(simulatAssessService.page(queryFilter)); | 
 |  |  |     }*/ | 
 |  |  |  | 
 |  |  |     @GetMapping("{id}") | 
 |  |  |     @ApiOperation("信息") | 
 |  |  | 
 |  |  |  | 
 |  |  |     @GetMapping("getSimulatList") | 
 |  |  |     @ApiOperation("信息") | 
 |  |  |     public Result<List<SimulatAssess>> getSimulatList(Long productId, Long taskModelId) { | 
 |  |  |         List<SimulatAssess> data = simulatAssessService.getList(productId, taskModelId); | 
 |  |  |     public Result<List<SimulatAssess>> getSimulatList(Long taskModelId) { | 
 |  |  |         List<SimulatAssess> data = simulatAssessService.getList(taskModelId); | 
 |  |  |  | 
 |  |  |         return Result.ok(data); | 
 |  |  |     } | 
 |  |  | 
 |  |  |     @ApiOperation("信息") | 
 |  |  |     public Result<SimulatAssess> getSimulatParams(Long id, Long taskModelId) { | 
 |  |  |         SimulatAssess data = simulatAssessService.getParams(id, taskModelId); | 
 |  |  |  | 
 |  |  |         return Result.ok(data); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @GetMapping("verify") | 
 |  |  |     public Result<String> verify(Long taskId) { | 
 |  |  |         String result = simulatAssessService.verify(taskId); | 
 |  |  |         result = result.replaceAll("\n", "<br>"); | 
 |  |  |         return Result.ok(result); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @PostMapping("analyze") | 
 |  |  |     public Result<Long> analyze(@RequestBody SimulatAssess simulatAssess) { | 
 |  |  |     public Result<Object> analyze(@RequestBody SimulatAssess simulatAssess) { | 
 |  |  |         //效验数据 | 
 |  |  | //        Result result = pythonLib.callPython(simulatAssess); | 
 |  |  |         Result result = simulatAssessService.simulate(simulatAssess); | 
 |  |  |  | 
 |  |  |         return result; | 
 |  |  |         List<TaskModelCheckResultDto> result = simulatAssessService.simulateChecK(simulatAssess); | 
 |  |  |         if (result != null) { | 
 |  |  |             return Result.ok(result); | 
 |  |  |         } | 
 |  |  |         Boolean result2 = simulatAssessService.simulate(simulatAssess); | 
 |  |  |         if (result2) | 
 |  |  |             return Result.ok(); | 
 |  |  |         else | 
 |  |  |             return Result.error("算法库计算失败!"); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @GetMapping("process") | 
 |  |  |     @ApiOperation("获取算法库进程信息") | 
 |  |  |     @ApiImplicitParams({ | 
 |  |  |             @ApiImplicitParam(name = Constant.Q.PAGE, value = Constant.QV.PAGE, required = true, dataType = Constant.QT.INT), | 
 |  |  |             @ApiImplicitParam(name = Constant.Q.LIMIT, value = Constant.QV.LIMIT, required = true, dataType = Constant.QT.INT), | 
 |  |  |             @ApiImplicitParam(name = "isAlive", value = "存活", dataType = Constant.QT.Boolean), | 
 |  |  |     }) | 
 |  |  |     public PageResult<SimulatAssess> process(@ApiIgnore @QueryParam QueryFilter queryFilter) { | 
 |  |  |         List<SimulatAssess> page = simulatAssessService.process(queryFilter); | 
 |  |  |         return PageResult.ok(page); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     @PostMapping("getResultXML") | 
 |  |  |     public Result<SimulaDataDto> getResultXML(@RequestBody SimulatAssess simulatAssess) { | 
 |  |  |         if (simulatAssess.getDataType().equals("fz")) { | 
 |  |  |             Integer num = simulatAssessService.getNumById(simulatAssess.getProductId(), simulatAssess.getTaskModelId()); | 
 |  |  |             simulatAssess.setName("仿真记录" + (num + 1)); | 
 |  |  |             simulatAssessService.insert(simulatAssess); | 
 |  |  |         } | 
 |  |  |         String filePath = path + "/" + simulatAssess.getId() + "/" + "result.xml"; | 
 |  |  |         String xml = null; | 
 |  |  |         InputStream in =null; | 
 |  |  |         try { | 
 |  |  |             in = new FileInputStream(filePath); | 
 |  |  |             xml = IOUtils.toString(in); | 
 |  |  |         } catch (IOException e) { | 
 |  |  |             e.printStackTrace(); | 
 |  |  |         }finally { | 
 |  |  |             try { | 
 |  |  |                 in.close(); | 
 |  |  |             } catch (IOException e) { | 
 |  |  |                 e.printStackTrace(); | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |         xmlJSONObj = XML.toJSONObject(xml); | 
 |  |  |         simulatAssess.setShowProductId(simulatAssess.getProductId()); | 
 |  |  |         SimulaDataDto data = this.getResultData(simulatAssess); | 
 |  |  |         SimulaDataDto data = simulatAssessService.getResultXML(simulatAssess); | 
 |  |  |         return Result.ok(data); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @PostMapping("getResultData") | 
 |  |  |     public Result<SimulaDataDto> getData(@RequestBody SimulatAssess simulatAssess) { | 
 |  |  |         SimulaDataDto data = this.getResultData(simulatAssess); | 
 |  |  |         SimulaDataDto data = simulatAssessService.getResultData(simulatAssess); | 
 |  |  |         return Result.ok(data); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     public SimulaDataDto getResultData(SimulatAssess simulatAssess) { | 
 |  |  |         SimulaDataDto data = new SimulaDataDto(); | 
 |  |  |         CurveParam param = new CurveParam(); | 
 |  |  |         Double samplPeriod = Double.valueOf(simulatAssess.getSamplPeriod()); | 
 |  |  |         Task task = taskService.get(simulatAssess.getTaskModelId()); | 
 |  |  |         samplPeriod = samplPeriod / 60; | 
 |  |  |         List<Double> xList = new ArrayList<>(); | 
 |  |  |         for (Double i = 0.0; i + samplPeriod <= task.getTaskDuration(); i += samplPeriod) { | 
 |  |  |             xList.add(i); | 
 |  |  |         } | 
 |  |  |         param.setXData(xList); | 
 |  |  |         JSONArray jsonArray = xmlJSONObj.getJSONObject("ResultNodes").getJSONArray("ResultNode"); | 
 |  |  |         for (int i = 0; i < jsonArray.size(); i++) { | 
 |  |  |             Long productId = null; | 
 |  |  |             Object nameValue = jsonArray.getJSONObject(i).get("name"); | 
 |  |  |             if (nameValue instanceof Long) { | 
 |  |  |                 productId = (Long) nameValue; | 
 |  |  |                 if (productId.equals(simulatAssess.getShowProductId())) { | 
 |  |  |                     Object object = jsonArray.getJSONObject(i).get("phase"); | 
 |  |  |                     List<Double> doubleArray = new ArrayList<>(); | 
 |  |  |                     String availability = null; | 
 |  |  |                     if (object instanceof JSONArray) { | 
 |  |  |                         JSONArray jsonArray2 = jsonArray.getJSONObject(i).getJSONArray("phase"); | 
 |  |  |                         for (int j = 0; j < jsonArray2.size(); j++) { | 
 |  |  |                             if (availability != null) { | 
 |  |  |                                 availability = availability + " " + jsonArray2.getJSONObject(j).get("availability"); | 
 |  |  |                             } else { | 
 |  |  |                                 availability = (String) jsonArray2.getJSONObject(j).get("availability"); | 
 |  |  |                             } | 
 |  |  |                         } | 
 |  |  |                     } else { | 
 |  |  |                         JSONObject jsonObject2 = jsonArray.getJSONObject(i).getJSONObject("phase"); | 
 |  |  |                         availability = (String) jsonObject2.get("availability"); | 
 |  |  |                     } | 
 |  |  |  | 
 |  |  |                     String[] arr = availability.split(" "); | 
 |  |  |                     // 遍历子字符串数组,将每个元素转换为double并存储到double数组中 | 
 |  |  |                     for (int a = 0; a < arr.length; a++) { | 
 |  |  |                         doubleArray.add(Double.parseDouble(arr[a])); | 
 |  |  |                     } | 
 |  |  |                     param.setYData(doubleArray); | 
 |  |  |                     Double mtbf = (Double) jsonArray.getJSONObject(i).get("mttf"); | 
 |  |  |                     Double mttr = (Double) jsonArray.getJSONObject(i).get("mttr"); | 
 |  |  |                     Double msr = (Double) jsonArray.getJSONObject(i).get("msr"); | 
 |  |  |                     data.setMtbf(mtbf); | 
 |  |  |                     data.setMttr(mttr); | 
 |  |  |                     data.setMsr(msr); | 
 |  |  |                 } | 
 |  |  |                 data.setCurveParam(param); | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         return data; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @GetMapping("getStatus") | 
 |  |  |     public Result<String> getTimeDiagram(Long productId, Long taskId, Long fzId) { | 
 |  |  |         String data = simulatAssessService.getTimeDiagram(productId, taskId, fzId); | 
 |  |  |     public Result<TimeDiagramDto> getTimeDiagram(Long productId, Long taskId, Long fzId, double smallWidth, double minPointNum) { | 
 |  |  |         TimeDiagramDto data = simulatAssessService.getTimeDiagram(productId, taskId, fzId, smallWidth, minPointNum); | 
 |  |  |         return Result.ok(data); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @GetMapping("getCalcProgress") | 
 |  |  |     public Result getCalcProgress(Long taskId) { | 
 |  |  |         String key = taskId.toString() + simulatAssessService.RELIA_SIM_TASK_TYPE_PROGRESS; | 
 |  |  |         String progress = (String) redisTemplate.opsForValue().get(key); | 
 |  |  |         if (progress == null) progress = "0"; | 
 |  |  |         return Result.ok(progress); | 
 |  |  |     public Result getCalcProgress(@RequestParam("taskList[]") Long[] taskList) { | 
 |  |  |         Integer progress = 0; | 
 |  |  |         String progress2 = ""; | 
 |  |  |         String key = null; | 
 |  |  |         if (taskList.length == 1) { | 
 |  |  |             Long taskId = taskList[0]; | 
 |  |  |             SimulatAssess simulatAssess = simulatAssessService.get(taskId); | 
 |  |  |             boolean isProcessAlive = ProcessUtils.isProcessAlive(simulatAssess.getPid()); | 
 |  |  |             key = taskId.toString() + simulatAssessService.RELIA_SIM_TASK_TYPE_PROGRESS; | 
 |  |  |             progress2 = (String) redisTemplate.opsForValue().get(key); | 
 |  |  |             if (progress2 == null) progress2 = "0"; | 
 |  |  |             progress = Integer.parseInt(progress2); | 
 |  |  |             if (progress == null) progress = 0; | 
 |  |  |             updateSimulateAssess(simulatAssess, isProcessAlive, progress); | 
 |  |  |             if (progress < 100 && !isProcessAlive) { | 
 |  |  |                 logger.error("算法库进程在计算途中异常终止了:taskId="+taskId+" pid="+simulatAssess.getPid()+" progress="+progress); | 
 |  |  |                 simulatAssessService.deleteSimInfoInRedis(taskId); | 
 |  |  |                 return Result.error("算法库进程在计算途中异常终止了!"); | 
 |  |  |             } | 
 |  |  |             if (progress == 100) { | 
 |  |  |                 try { | 
 |  |  |                     Thread.sleep(1000); | 
 |  |  |                 } catch (Exception e) { | 
 |  |  |                     e.printStackTrace(); | 
 |  |  |                 } | 
 |  |  |                 isProcessAlive = ProcessUtils.isProcessAlive(simulatAssess.getPid()); | 
 |  |  |                 simulatAssess.setProcessIsAlive(isProcessAlive); | 
 |  |  |                 simulatAssessService.update(simulatAssess); | 
 |  |  | //                simulatAssessService.deleteSimInfoInRedis(taskId); | 
 |  |  |             } | 
 |  |  |         } else { | 
 |  |  |             for (Long taskId : taskList) { | 
 |  |  |                 int progTmp = 0; | 
 |  |  |                 SimulatAssess simulatAssess = simulatAssessService.get(taskId); | 
 |  |  |                 boolean isProcessAlive = ProcessUtils.isProcessAlive(simulatAssess.getPid()); | 
 |  |  |                 key = taskId.toString() + simulatAssessService.RELIA_SIM_TASK_TYPE_PROGRESS; | 
 |  |  |                 progress2 = (String) redisTemplate.opsForValue().get(key); | 
 |  |  |                 if (progress2 == null) progress2 = "0"; | 
 |  |  |                 progTmp = Integer.parseInt(progress2); | 
 |  |  |                 updateSimulateAssess(simulatAssess, isProcessAlive, progTmp); | 
 |  |  |                 if (progTmp < 100 && !isProcessAlive) { | 
 |  |  |                     logger.error("算法库进程在计算途中异常终止了:taskId="+taskId+" pid="+simulatAssess.getPid()+" progress="+progTmp); | 
 |  |  | //                    simulatAssessService.deleteSimInfoInRedis(taskId); | 
 |  |  |                     return Result.error("算法库进程在计算途中异常终止了!"); | 
 |  |  |                 } | 
 |  |  |                 if (progTmp == 100) { | 
 |  |  |                     try { | 
 |  |  |                         Thread.sleep(500); | 
 |  |  |                     } catch (Exception e) { | 
 |  |  |                         e.printStackTrace(); | 
 |  |  |                     } | 
 |  |  |                     isProcessAlive = ProcessUtils.isProcessAlive(simulatAssess.getPid()); | 
 |  |  |                     simulatAssess.setProcessIsAlive(isProcessAlive); | 
 |  |  |                     simulatAssessService.update(simulatAssess); | 
 |  |  | //                    simulatAssessService.deleteSimInfoInRedis(taskId); | 
 |  |  |                 } | 
 |  |  |                 progress = progress + progTmp; | 
 |  |  |             } | 
 |  |  |             progress = progress / taskList.length; | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         return Result.ok(progress.toString()); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     private void updateSimulateAssess(SimulatAssess simulatAssess, boolean isProcessAlive, int progress) { | 
 |  |  |         if (!isProcessAlive) { | 
 |  |  |             simulatAssess.setProcessIsAlive(false); | 
 |  |  |             simulatAssess.setProgress(progress); | 
 |  |  |             simulatAssess.setProcessEndTime(new Date()); | 
 |  |  |             simulatAssessService.update(simulatAssess); | 
 |  |  |         } else { | 
 |  |  |             if (simulatAssess.getProgress() != progress) { | 
 |  |  |                 simulatAssess.setProgress(progress); | 
 |  |  |                 simulatAssessService.update(simulatAssess); | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @GetMapping("ReliabilityWeakness") | 
 |  |  |     public Result<List<WeakDto>> getReliabilityWeakness(Long fzId, Long taskId, Long productId) { | 
 |  |  |         List<WeakDto> data = simulatAssessService.getReliabilityWeakness(fzId, taskId, productId); | 
 |  |  |         return Result.ok(data); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @GetMapping("SchemeCompar") | 
 |  |  |     public Result<SimulatResult> SchemeCompar(@RequestParam("taskList[]") String[] taskList, Long showProductId, Integer samplPeriod, Integer simulatFrequency) { | 
 |  |  |         SimulatResult data = simulatAssessService.SchemeCompar(taskList, showProductId, samplPeriod, simulatFrequency); | 
 |  |  |         return Result.ok(data); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @GetMapping("getEcharts") | 
 |  |  |     public Result<SchemeComparDto> getEcharts(@RequestParam("taskList[]") String[] taskList, Long showProductId) { | 
 |  |  |         SchemeComparDto data = simulatAssessService.getEcharts(taskList, showProductId); | 
 |  |  |         return Result.ok(data); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @GetMapping("killProcess") | 
 |  |  |     public Result<SchemeComparDto> killProcess(@RequestParam("taskId") Long taskId) { | 
 |  |  |         SimulatAssess simulatAssess = simulatAssessService.get(taskId); | 
 |  |  |         ProcessUtils.killProcess(simulatAssess.getPid()); | 
 |  |  |         boolean isProcessAlive = ProcessUtils.isProcessAlive(simulatAssess.getPid()); | 
 |  |  |         simulatAssess.setProcessIsAlive(isProcessAlive); | 
 |  |  |         simulatAssessService.update(simulatAssess); | 
 |  |  |         return Result.ok(); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @PutMapping |