xyc
2024-09-28 05a4c18628bba5010c0ce423408cc5f40f864ee0
modules/mainPart/src/main/java/com/zt/life/modules/mainPart/taskReliability/controller/SimulatAssessController.java
@@ -12,17 +12,17 @@
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.dto.*;
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.service.SimulatAssessService;
import com.zt.life.modules.mainPart.taskReliability.service.TaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@@ -34,10 +34,7 @@
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.*;
/**
@@ -51,8 +48,6 @@
@Api(tags = "simulat_assess")
public class SimulatAssessController {
    private static final Logger logger = LoggerFactory.getLogger(SimulatAssessController.class);
    private static final String taskTypeProgress = "calcprog";
    private List<Map<String, Double>> dialgram = new ArrayList<>();
    @Autowired
    private RedisTemplate redisTemplate;
@@ -63,28 +58,9 @@
    @Autowired
    private TaskService taskService;
    @Value("${data.reliaSimLib.resultHome}")
    private String path;
    @Autowired
    private SimulatAssessService simulatAssessService;
    private JSONObject xmlJSONObj;
    private JSONObject dialgramJson;
   /* @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("信息")
@@ -96,8 +72,8 @@
    @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);
    }
@@ -106,7 +82,6 @@
    @ApiOperation("信息")
    public Result<SimulatAssess> getSimulatParams(Long id, Long taskModelId) {
        SimulatAssess data = simulatAssessService.getParams(id, taskModelId);
        return Result.ok(data);
    }
@@ -121,141 +96,69 @@
    @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;
        try {
            InputStream in = new FileInputStream(filePath);
            xml = IOUtils.toString(in);
        } 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 = simulatAssess.getSamplPeriod().doubleValue();
        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())) {
                    JSONArray jsonArray2 = jsonArray.getJSONObject(i).getJSONArray("phase");
                    List<Double> doubleArray = new ArrayList<>();
                    String availability = null;
                    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");
                        }
                    }
                    String[] arr = availability.split(" ");
                    // 遍历子字符串数组,将每个元素转换为double并存储到double数组中
                    for (int a = 0; a < arr.length; a++) {
                        doubleArray.add(Double.parseDouble(arr[a]));
                    }
                    param.setYData(doubleArray);
                    data.setMtbf((Double) jsonArray.getJSONObject(i).get("mttf"));
                    data.setMttr((Double) jsonArray.getJSONObject(i).get("mttr"));
                }
                data.setCurveParam(param);
            }
        }
        return data;
    }
    @GetMapping("getStatus")
    public Result<List<ProductStatusDto>> getStatus(Long productId, Long taskId, Long fzId) {
        String filePath = path + "/" + fzId + "/" + "status.json";
        ObjectMapper mapper = new ObjectMapper();
        String jsonStr = null;
        try {
            // 使用 ObjectMapper 的 readValue 方法,将文件中的 JSON 数据转换为一个 Java 对象
            // 这里使用 Object 类作为泛型参数,表示任意类型的对象
            Object obj = mapper.readValue(new File(filePath), Object.class);
            // 使用 ObjectMapper 的 writeValueAsString 方法,将 Java 对象转换为 JSON 字符串
            jsonStr = mapper.writeValueAsString(obj);
        } catch (IOException e) {
            // 处理异常
            e.printStackTrace();
        }
        dialgramJson = new JSONObject(jsonStr);
        List<ProductStatusDto> childList = this.getStatusData(productId, taskId);
        return Result.ok(childList);
    public Result<String> getTimeDiagram(Long productId, Long taskId, Long fzId) {
        String data = simulatAssessService.getTimeDiagram(productId, taskId, fzId);
        return Result.ok(data);
    }
    public List<ProductStatusDto> getStatusData(Long productId, Long taskId) {
        List<ProductStatusDto> childList = simulatAssessService.getChildren(productId, taskId);
        for (ProductStatusDto item : childList) {
            if (dialgram.size() > 0) {
                dialgram.clear();
            }
            JSONArray jsonArray = dialgramJson.getJSONArray(String.valueOf(item.getId()));
            if (jsonArray != null) {
                this.processJSONArray(jsonArray, item);
            }
        }
        return childList;
    @GetMapping("getDiagram")
    @ApiOperation("查询流程图")
    @LogOperation("查询流程图")
    public Result<TimeDiagram> getDiagram(String projectId, String diagramId, String showType, String isShow, String digramParams, String majorId, Integer winWidth, Integer winHeight) throws Exception {
        if (StringUtils.isBlank(diagramId))
            diagramId = projectId;
        TimeDiagram diagram = simulatAssessService.getDiagram(projectId, diagramId, showType, isShow, digramParams, majorId, winWidth, winHeight);
        return Result.ok(diagram);
    }
    public void processJSONArray(JSONArray jsonArray, ProductStatusDto productStatusDto) {
        String status = null;
        Double times = null;
        for (int i = 0; i < jsonArray.size(); i++) {
            Object item = jsonArray.get(i);
            if (item instanceof JSONArray) {
                processJSONArray((JSONArray) item, productStatusDto); // 递归调用
            } else {
                if (item instanceof Double) {
                    times = (Double) item;
                } else {
                    status = String.valueOf(item);
                }
            }
        }
        if (status != null || times != null) {
            StatusDto statusDto =new StatusDto();
            statusDto.setTimes(times);
            statusDto.setStatus(status);
            productStatusDto.getStatusList().add(statusDto);
        }
    @PutMapping("saveDiagram")
    @ApiOperation("查询流程图")
    @LogOperation("查询流程图")
    public Result saveDiagram(@RequestBody TimeDiagram diagram) {
        // 效验数据
        //ValidatorUtils.validateEntity(project.getProject(), UpdateGroup.class, DefaultGroup.class);
        if (diagram.getConfigChange() == null)
            diagram.setConfigChange(0);
        diagram.setSaveStatus(2);
        simulatAssessService.updateDiagram(diagram);
        //simulatAssessService.combineDiagram(diagram);
        return Result.ok();
    }
    @GetMapping("getCalcProgress")
    public Result getCalcProgress(Long taskId) {
        String key = taskId.toString() + taskTypeProgress;
        String key = taskId.toString() + simulatAssessService.RELIA_SIM_TASK_TYPE_PROGRESS;
        String progress = (String) redisTemplate.opsForValue().get(key);
        if (progress == null) progress = "0";
        if (Objects.equals(progress, "100")) {
            simulatAssessService.deleteSimInfoInRedis(taskId);
        }
        return Result.ok(progress);
    }
    @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<SchemeComparDto> SchemeCompar(@RequestParam("taskList[]") String[] taskList,Long showProductId) {
       SchemeComparDto data = simulatAssessService.SchemeCompar(taskList,showProductId);
        return Result.ok(data);
    }
    @PutMapping
    @ApiOperation("修改")
@@ -278,7 +181,6 @@
        return Result.ok();
    }
}