瀏覽代碼

添加echart接口 仅支持柱状图 折线图

wanghs 2 年之前
父節點
當前提交
456d556b26

+ 44 - 0
src/main/java/com/gyee/table/controller/EchartController.java

@@ -0,0 +1,44 @@
+package com.gyee.table.controller;
+
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson2.JSONObject;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.gyee.table.mapper.ObjectAllMapper;
+import com.gyee.table.service.IObjectService;
+import com.gyee.table.utils.ResponseWrapper;
+import com.gyee.table.vo.LineData;
+import com.gyee.table.vo.Point;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.ArrayList;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * @description:
+ * @auther: Wanghs
+ * @date: 2023-02-07
+ */
+@Slf4j
+@RestController
+@RequestMapping("/echart")
+public class EchartController {
+
+    @Autowired
+    private IObjectService objectService;
+
+    @PostMapping("/line")
+    public JSONObject getLineData(@RequestBody Map map) {
+        String sql = (String) map.get("sql");
+
+        JSONObject jsonObject = objectService.selectBySqlToEchart(map);
+
+        return jsonObject;
+
+    }
+
+
+}

+ 2 - 1
src/main/java/com/gyee/table/controller/TableHeaderController.java

@@ -13,6 +13,7 @@ import org.apache.commons.lang3.StringUtils;
 import org.springframework.web.bind.annotation.*;
 
 import javax.annotation.Resource;
+import javax.script.ScriptException;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -54,7 +55,7 @@ public class TableHeaderController {
         return Result.successData(ResultCode.SUCCESS,header);
     }
     @PostMapping("/data")
-    private JSONObject getData(@RequestBody Map map){
+    private JSONObject getData(@RequestBody Map map) throws ScriptException {
         return objectService.selectBySql(map);
     }
 

+ 2 - 0
src/main/java/com/gyee/table/entity/TableColumnHeader.java

@@ -46,6 +46,8 @@ public class TableColumnHeader implements Serializable {
 
     private Boolean sortable;
 
+    private boolean fixed;
+
     @TableField(typeHandler = FastjsonTypeHandler.class)
     private String[] operations;
 

+ 5 - 1
src/main/java/com/gyee/table/service/IObjectService.java

@@ -2,6 +2,7 @@ package com.gyee.table.service;
 
 import com.alibaba.fastjson2.JSONObject;
 
+import javax.script.ScriptException;
 import java.util.Map;
 
 /**
@@ -14,5 +15,8 @@ import java.util.Map;
  */
 public interface IObjectService {
 
-    JSONObject selectBySql(Map map);
+    JSONObject selectBySql(Map map) throws ScriptException;
+    JSONObject selectBySqlToEchart(Map map);
+
+
 }

+ 31 - 2
src/main/java/com/gyee/table/service/impl/ObjectServiceImpl.java

@@ -1,5 +1,7 @@
 package com.gyee.table.service.impl;
 
+import com.alibaba.fastjson2.JSON;
+import com.alibaba.fastjson2.JSONArray;
 import com.alibaba.fastjson2.JSONObject;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
@@ -83,8 +85,14 @@ public class ObjectServiceImpl implements IObjectService {
         table.setTableHeader(tableColumnHeader);
         table.setTableData(lms);
         //截取sql
-        if (sql.contains("limit")){
-            String newSql = sql.split("limit")[0];
+        if (sql.contains("limit")||sql.contains("LIMIT")){
+
+            String newSql = "";
+            if(sql.contains("LIMIT"))
+                newSql=  sql.split("LIMIT")[0];
+            else if (sql.contains("limit")){
+                newSql=  sql.split("limit")[0];
+            }
             List<LinkedHashMap<String, Object>> linkedHashMaps = objectMapper.selectAll(newSql);
             table.setCount(linkedHashMaps.size());
         }else {
@@ -93,6 +101,23 @@ public class ObjectServiceImpl implements IObjectService {
         return Result.successData(ResultCode.SUCCESS,table);
     }
 
+    @Override
+    public JSONObject selectBySqlToEchart(Map map) {
+        String sql = (String)map.get("sql");
+        if (StringUtils.isBlank(sql)) {
+            return Result.error(ResultCode.PARAM_IS_BLANK);
+        } else {
+            Matcher matcher = this.compile.matcher(sql);
+            if (matcher.find()) {
+                String group = matcher.group(0);
+                return Result.error(ResultCode.ERROR_UNSUPPORTED_SQL, "查看字符:" + group);
+            } else {
+                List<LinkedHashMap<String, Object>> lms = this.objectMapper.selectAll(sql);
+                return Result.successData(ResultCode.SUCCESS, lms);
+            }
+        }
+    }
+
     /**
      * 根据表头和数据生成默认表头
      * @param tablename
@@ -102,8 +127,11 @@ public class ObjectServiceImpl implements IObjectService {
     private List<TableColumnHeader> createTableColumnHeader(String tablename, LinkedHashMap<String, Object> solhm) {
         List<TableColumnHeader> tableHeaderColumns = new ArrayList<>();
         long tableId = tableColumnHeaderMapper.selectCount(Wrappers.emptyWrapper()) + 1;
+        int id=0;
         for (String s : solhm.keySet()) {
             TableColumnHeader tch = new TableColumnHeader();
+            id=id+1;
+            tch.setId(String.valueOf(id));
             tch.setTid(tableId);
             tch.setName(tablename);
             tch.setType("");
@@ -121,6 +149,7 @@ public class ObjectServiceImpl implements IObjectService {
             tch.setFilters(emptyArray);
             tch.setFormattype("");
             tch.setFormat(emptyArray);
+            tch.setFixed(false);
 
             tableHeaderColumns.add(tch);
         }

+ 91 - 0
src/main/java/com/gyee/table/utils/ResponseWrapper.java

@@ -0,0 +1,91 @@
+package com.gyee.table.utils;
+import lombok.Data;
+
+import java.io.Serializable;
+import java.util.Collection;
+
+
+@Data
+public class ResponseWrapper<T> implements Serializable {
+    private static final long serialVersionUID = 1L;
+    private Boolean success;
+    private Integer status;
+    private Integer count;
+    private String msg;
+    private T data;
+    private static final int STATUS_SUCCESS = 20000;
+    private static final int STATUS_ERROR = 50000;
+    public static final int STATUS_ERROR_SYSTEM = 50100;
+
+    private String token;
+
+    public ResponseWrapper() {
+    }
+
+    private ResponseWrapper(Boolean success, Integer status, Integer count, String msg, Class entityType, T data) {
+        this.success = success;
+        this.status = status;
+        this.count = count;
+        this.msg = msg;
+        this.data = data;
+    }
+
+    private ResponseWrapper(Boolean success, Integer status, Integer count, String msg, T data) {
+        this.success = success;
+        this.status = status;
+        this.count = count;
+        this.msg = msg;
+        this.data = data;
+    }
+
+    public static ResponseWrapper success(int count, String msg, Object data) {
+        if (data != null) {
+            return data instanceof Collection ? new ResponseWrapper(true, 20000, ((Collection)data).size(), msg, data) : new ResponseWrapper(true, 20000, count, msg, data);
+        } else {
+            return count == -1 ? new ResponseWrapper(true, 20000, -1, msg, msg) : new ResponseWrapper(true, 20000, count, msg, count);
+        }
+    }
+
+    public static ResponseWrapper success(String msg, int count, Object data) {
+        return new ResponseWrapper(true, 20000, count, msg, data);
+    }
+
+    public static ResponseWrapper success(int count, String msg) {
+        return success(count, msg, (Object)null);
+    }
+
+    public static ResponseWrapper success(int count) {
+        return success(count, "count");
+    }
+
+    public static ResponseWrapper success(String msg) {
+        return success(-1, msg);
+    }
+
+    public ResponseWrapper<T> success(T data) {
+        this.data = data;
+        return this;
+    }
+
+    public static ResponseWrapper success(String msg, Object data) {
+        return success(1, msg, data);
+    }
+
+    public static ResponseWrapper error(String msg, Object data, int status) {
+        return new ResponseWrapper(false, status, 0, msg, data.getClass(), data);
+    }
+
+    public static ResponseWrapper error(String msg, Object data) {
+        return new ResponseWrapper(false, 50000, 0, msg, data.getClass(), data);
+    }
+
+    public static ResponseWrapper error(String msg, int status) {
+        return new ResponseWrapper(false, status, -1, msg, String.class, msg);
+    }
+
+    public static ResponseWrapper error(String msg) {
+        return error(msg, 50000);
+    }
+
+
+}

+ 17 - 0
src/main/java/com/gyee/table/vo/LineData.java

@@ -0,0 +1,17 @@
+package com.gyee.table.vo;
+
+import lombok.Data;
+
+import java.util.List;
+
+/**
+ * @description:
+ * @auther: Wanghs
+ * @date: 2023-02-07
+ */
+
+@Data
+public class LineData {
+
+    private  List<List<Point>>  dataList;
+}

+ 20 - 0
src/main/java/com/gyee/table/vo/Point.java

@@ -0,0 +1,20 @@
+package com.gyee.table.vo;
+
+import lombok.Data;
+import lombok.experimental.Accessors;
+
+import java.io.Serializable;
+
+/**
+ * @description:
+ * @auther: Wanghs
+ * @date: 2023-02-07
+ */
+@Data
+@Accessors(chain = true)
+public class Point  implements Serializable {
+
+    private Object x;
+
+    private double y;
+}

+ 1 - 1
src/main/resources/application.yml

@@ -1,5 +1,5 @@
 server:
-  port: 8078
+  port: 8091
 spring:
   mvc:
     pathmatch: