B模板 用的多

发布于:2024-07-11 ⋅ 阅读:(46) ⋅ 点赞:(0)
 /*   series、dataset、legend*/

    ("/v2/importData/demo")
    (rollbackFor = Exception.class) // 添加事务管理注解,捕获所有异常回滚事务
    public AjaxResult cornerLoopPhoto( ChartDateTto chartDateTto) throws Exception {
        String chartValue = chartDateTto.getChartValue();
        List<List<String>> lists = extractDataFromList(chartDateTto.getDataList());

        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode chartValueJson = objectMapper.readTree(chartValue);

        // 获取dataset节点并替换source节点
        ArrayNode dataset = (ArrayNode) chartValueJson.path("dataset");
        for (JsonNode node : dataset) {
            if (node.has("source")) {
                ((com.fasterxml.jackson.databind.node.ObjectNode) node).set("source", objectMapper.valueToTree(lists));
                break;
            }
        }

        // 移除集合中第一个对象的第一个元素
        if (!lists.isEmpty() && !lists.get(0).isEmpty()) {
            lists.get(0).remove(0);
        }


        // 获取series字段并删除其余对象
        ArrayNode seriesArray = (ArrayNode) chartValueJson.path("series");
        if (seriesArray.size() > 1) {
            while (seriesArray.size() > 1) {
                seriesArray.remove(1);
            }
        }

        // 计算第一个对象剩余元素数量
        int firstObjectRemainingElements = lists.isEmpty() ? 0 : lists.get(0).size();

        // 复制series的第一个对象
        JsonNode firstSeriesObject = seriesArray.get(0);
        for (int i = 1; i < firstObjectRemainingElements; i++) {
            seriesArray.add(firstSeriesObject.deepCopy());
        }

        // 更新series中每个对象的name字段
        for (int i = 0; i < firstObjectRemainingElements; i++) {
            ((ObjectNode) seriesArray.get(i)).put("name", lists.get(0).get(i));
        }

        // 获取legend节点并更新data
        JsonNode legend = chartValueJson.path("legend");
        ChartDataUtils.updateLegendData(legend, lists);

        String updatedChartValue = chartValueJson.toString();
        ArrayList<ChartSmartVo> newList = new ArrayList<>();
        // 获取ChartConfigs对象
        ChartConfigs configs = chartConfigsService.getById(chartDateTto.getConfigId());
        // 创建新的ChartSmartVo对象并设置属性
        ChartSmartVo chartSmartVo = new ChartSmartVo();
        chartSmartVo.setConfigId(configs.getId()); // 假设ChartConfigs有getId方法
        chartSmartVo.setConfigName(configs.getConfigName()); // 假设ChartConfigs有getName方法
        chartSmartVo.setConfigValue(updatedChartValue);
        chartSmartVo.setChartTypeId(configs.getChartTypeId());

        newList.add(chartSmartVo);
        System.out.println(updatedChartValue);
        return AjaxResult.success(newList);

    }
      String chartValue = chartDateTto.getChartValue();
        List<List<String>> lists = extractDataFromList(chartDateTto.getDataList());

        ObjectMapper objectMapper = new ObjectMapper();
        JsonNode chartValueJson = objectMapper.readTree(chartValue);

        // 获取dataset节点并替换source节点
        ArrayNode dataset = (ArrayNode) chartValueJson.path("dataset");
        for (JsonNode node : dataset) {
            if (node.has("source")) {
                ((com.fasterxml.jackson.databind.node.ObjectNode) node).set("source", objectMapper.valueToTree(lists));
                break;
            }
        }

        // 移除集合中第一个对象的第一个元素
        if (!lists.isEmpty() && !lists.get(0).isEmpty()) {
            lists.get(0).remove(0);
        }


        // 获取series字段并删除其余对象
        ArrayNode seriesArray = (ArrayNode) chartValueJson.path("series");
        if (seriesArray.size() > 1) {
            while (seriesArray.size() > 1) {
                seriesArray.remove(1);
            }
        }

        // 计算第一个对象剩余元素数量
        int firstObjectRemainingElements = lists.isEmpty() ? 0 : lists.get(0).size();

        // 复制series的第一个对象
        JsonNode firstSeriesObject = seriesArray.get(0);
        for (int i = 1; i < firstObjectRemainingElements; i++) {
            seriesArray.add(firstSeriesObject.deepCopy());
        }

        // 更新series中每个对象的name字段
        for (int i = 0; i < firstObjectRemainingElements; i++) {
            ((ObjectNode) seriesArray.get(i)).put("name", lists.get(0).get(i));
        }

        // 获取legend节点并更新data
        JsonNode legend = chartValueJson.path("legend");
        ChartDataUtils.updateLegendData(legend, lists);

        String updatedChartValue = chartValueJson.toString();
        ArrayList<ChartSmartVo> newList = new ArrayList<>();
        // 获取ChartConfigs对象
        ChartConfigs configs = chartConfigsService.getById(chartDateTto.getConfigId());
        // 创建新的ChartSmartVo对象并设置属性
        ChartSmartVo chartSmartVo = new ChartSmartVo();
        chartSmartVo.setConfigId(configs.getId()); // 假设ChartConfigs有getId方法
        chartSmartVo.setConfigName(configs.getConfigName()); // 假设ChartConfigs有getName方法
        chartSmartVo.setConfigValue(updatedChartValue);
        chartSmartVo.setChartTypeId(configs.getChartTypeId());

        newList.add(chartSmartVo);
        System.out.println(updatedChartValue);
        return AjaxResult.success(newList);