This commit is contained in:
jingna 2025-05-21 18:29:41 +08:00
commit dfb0490c04
11 changed files with 436 additions and 18 deletions

3
core/core-backend/.env Normal file
View File

@ -0,0 +1,3 @@
{
"JAVA_HOME": "C:\\Users\\13910\\.jdks\\corretto-21.0.5"
}

View File

@ -93,6 +93,11 @@
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.38</version>
</dependency>
<dependency>
<groupId>io.gisbi</groupId>
<artifactId>api-permissions</artifactId>
@ -184,6 +189,15 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>21</source>
<target>21</target>
</configuration>
</plugin>
</plugins>
</build>
</profile>

View File

@ -201,7 +201,7 @@ public class RoleController {
boolean isOk = true;
String[] temp = userids.split(",");
for (String userid : temp) {
isOk = isOk && userService.addUserRoles(roleid, userid);
isOk = isOk && userService.addUserRole(roleid, userid);
}
if (isOk) {
return ResponseResult.success();

View File

@ -86,5 +86,5 @@ public interface IUserService extends IService<User> {
* userids 用户id组
* 返回值说明: 是否新增成功
***********************************/
boolean addUserRoles(String roleid, String userid);
boolean addUserRole(String roleid, String userid);
}

View File

@ -59,8 +59,6 @@ public class OrganizationServiceImpl extends ServiceImpl<OrganizationMapper, Org
return new ArrayList<>();
}
List<Map<String, Object>> result = new ArrayList<>();
for (Map<String, Object> item : listMap) {
Object idObj = item.get("id");
// 避免空指针
@ -69,11 +67,11 @@ public class OrganizationServiceImpl extends ServiceImpl<OrganizationMapper, Org
}
List<Map<String, Object>> childList = child(idObj.toString(), appId, orgname);
item.put("childList", childList); // 添加新列 子集
if (childList != null && !childList.isEmpty()) {
result.add(item); // 仅保留有子节点的数据
}
// if (childList != null && !childList.isEmpty()) {
// listMap.add(item); // 仅保留有子节点的数据
// }
}
return result;
return listMap;
}

View File

@ -313,7 +313,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IU
* 返回值说明: 是否新增成功
***********************************/
@Override
public boolean addUserRoles(String roleid, String userid) {
public boolean addUserRole(String roleid, String userid) {
boolean isOk = true;
if (StringUtils.isEmpty(roleid) || StringUtils.isEmpty(userid)) {
return false;

View File

@ -1,8 +1,14 @@
package io.gisbi.dataset.dao.auto.mapper;
import io.gisbi.application.system.domain.Role;
import io.gisbi.dataset.dao.auto.entity.CoreDatasetTable;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.util.List;
import java.util.Map;
/**
* <p>
@ -14,5 +20,6 @@ import org.apache.ibatis.annotations.Mapper;
*/
@Mapper
public interface CoreDatasetTableMapper extends BaseMapper<CoreDatasetTable> {
@Select("select b.name group_name,a.table_name table_name,a.id table_id from core_dataset_table a join core_dataset_group b on (a.dataset_group_id=b.id) where b.app_id=#{appid} and a.type='db' order by a.name")
List<Map<String,Object>> getTablesByAppId(String appid);
}

View File

@ -1,5 +1,7 @@
package io.gisbi.dataset.manage;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.gisbi.api.chart.dto.DeSortField;
import io.gisbi.api.dataset.dto.*;
import io.gisbi.api.dataset.union.DatasetGroupInfoDTO;
@ -7,6 +9,10 @@ import io.gisbi.api.dataset.union.DatasetTableInfoDTO;
import io.gisbi.chart.utils.ChartDataBuild;
import io.gisbi.commons.utils.SqlparserUtils;
import io.gisbi.dataset.constant.DatasetTableType;
import io.gisbi.dataset.dao.auto.entity.CoreDatasetTable;
import io.gisbi.dataset.dao.auto.entity.CoreDatasetTableField;
import io.gisbi.dataset.dao.auto.mapper.CoreDatasetTableFieldMapper;
import io.gisbi.dataset.dao.auto.mapper.CoreDatasetTableMapper;
import io.gisbi.dataset.utils.DatasetUtils;
import io.gisbi.dataset.utils.FieldUtils;
import io.gisbi.dataset.utils.TableUtils;
@ -71,7 +77,10 @@ public class DatasetDataManage {
private DatasetTableSqlLogManage datasetTableSqlLogManage;
@Autowired(required = false)
private PluginManageApi pluginManage;
@Resource
private CoreDatasetTableFieldMapper coreDatasetTableFieldMapper;
@Resource
private CoreDatasetTableMapper coreDatasetTableMapper;
@Resource
private DataSourceManage dataSourceManage;
@ -1139,4 +1148,346 @@ public class DatasetDataManage {
return nodes;
}
//-----------------------下面代码为扩展代码实现了数据表的增删改查功能--zhengsl at 2025-05-20----------------------------------------------------------------------------------------//
public List<Map<String,Object>> getTablesByAppId(String appid) throws Exception {
List<Map<String,Object>> tables = coreDatasetTableMapper.getTablesByAppId(appid);
return tables;
}
public List<Map<String,Object>> getFieldsByTableId(Long id) throws Exception {
QueryWrapper<CoreDatasetTableField> wrapper = new QueryWrapper<>();
wrapper.select("id","origin_name","name","type","size");
wrapper.eq("dataset_table_id", id);
wrapper.eq("checked", true);
wrapper.isNull("chart_id");
List<Map<String,Object>> fields = coreDatasetTableFieldMapper.selectMaps(wrapper);
return fields;
}
public boolean addTableData(Long datasourceId, String tableData) throws Exception {
// 根据数据源 id 查询数据源信息调用通用数据源执行器
CoreDatasource coreDatasource = coreDatasourceMapper.selectById(datasourceId);
if (coreDatasource == null) {
DEException.throwException("数据源不存在");
}
// 解析 tableData JSON 字符串 "{ \"tableName\": \"user\", \"data\": [ { \"fieldName\": \"id\", \"fieldType\": \"varchar\", \"IsPrimaryKey\": true, \"fieldValue\": \"0001\" }, { \"fieldName\": \"name\", \"fieldType\": \"varchar\", \"fieldValue\": \"张三\" } ] }";
Map<String, Object> dataMap = JsonUtil.parseObject(tableData, Map.class);
String tableName = (String) dataMap.get("tableName");
List<Map<String, Object>> fieldList = (List<Map<String, Object>>) dataMap.get("data");
if (fieldList == null || fieldList.isEmpty()) {
DEException.throwException("没有可插入的数据字段");
}
// 构建插入语句
StringBuilder columns = new StringBuilder();
StringBuilder values = new StringBuilder();
for (int i = 0; i < fieldList.size(); i++) {
Map<String, Object> field = fieldList.get(i);
String fieldName = (String) field.get("fieldName");
Object fieldValue = field.get("fieldValue");
if (i > 0) {
columns.append(", ");
values.append(", ");
}
columns.append(fieldName);
if (fieldValue instanceof String) {
values.append("'").append(fieldValue).append("'");
} else {
values.append(fieldValue);
}
}
String sql = String.format("INSERT INTO %s (%s) VALUES (%s)", tableName, columns.toString(), values.toString());
// 调用执行器向数据表中插入 tableData 数据
DatasourceSchemaDTO datasourceSchemaDTO = new DatasourceSchemaDTO();
BeanUtils.copyBean(datasourceSchemaDTO, coreDatasource);
Provider provider = ProviderFactory.getProvider(coreDatasource.getType());
DatasourceRequest datasourceRequest = new DatasourceRequest();
datasourceRequest.setQuery(sql);
datasourceRequest.setDsList(Map.of(datasourceSchemaDTO.getId(), datasourceSchemaDTO));
logger.debug("执行插入数据的SQL: {}", sql);
// 执行插入操作
int result= provider.executeUpdate(datasourceRequest);
if (result==1) {
return true;
// process result set
} else {
return false;
}
}
public boolean updateTableData(Long datasourceId, String tableData) throws Exception {
// 获取数据源信息
CoreDatasource coreDatasource = coreDatasourceMapper.selectById(datasourceId);
if (coreDatasource == null) {
DEException.throwException("数据源不存在");
}
//String tableDataJson = "{ \"tableName\": \"user\", \"primaryKeyField\": \"id\", \"primaryKeyValue\": \"0001\", \"data\": [ { \"fieldName\": \"name\", \"fieldType\": \"varchar\", \"fieldValue\": \"李四\" } ] }";
// 解析 JSON 数据
Map<String, Object> dataMap = JsonUtil.parseObject(tableData, Map.class);
String tableName = (String) dataMap.get("tableName");
String primaryKeyField = (String) dataMap.get("primaryKeyField");
Object primaryKeyValue = dataMap.get("primaryKeyValue");
List<Map<String, Object>> fieldList = (List<Map<String, Object>>) dataMap.get("data");
if (fieldList == null || fieldList.isEmpty()) {
DEException.throwException("没有可更新的数据字段");
}
if (StringUtils.isBlank(tableName)) {
DEException.throwException("表名不能为空");
}
if (StringUtils.isBlank(primaryKeyField) || primaryKeyValue == null) {
DEException.throwException("主键字段或值不能为空");
}
// 构建 UPDATE 语句
StringBuilder setClause = new StringBuilder();
for (int i = 0; i < fieldList.size(); i++) {
Map<String, Object> field = fieldList.get(i);
String fieldName = (String) field.get("fieldName");
Object fieldValue = field.get("fieldValue");
if (i > 0) {
setClause.append(", ");
}
if (fieldValue instanceof String) {
setClause.append(String.format("%s = '%s'", fieldName, fieldValue));
} else {
setClause.append(String.format("%s = %s", fieldName, fieldValue));
}
}
String whereClause = String.format("%s = ", primaryKeyField);
if (primaryKeyValue instanceof String) {
whereClause += String.format("'%s'", primaryKeyValue);
} else {
whereClause += primaryKeyValue;
}
String sql = String.format("UPDATE %s SET %s WHERE %s", tableName, setClause.toString(), whereClause);
// 调用执行器执行 SQL
DatasourceSchemaDTO datasourceSchemaDTO = new DatasourceSchemaDTO();
BeanUtils.copyBean(datasourceSchemaDTO, coreDatasource);
Provider provider = ProviderFactory.getProvider(coreDatasource.getType());
DatasourceRequest datasourceRequest = new DatasourceRequest();
datasourceRequest.setQuery(sql);
datasourceRequest.setDsList(Map.of(datasourceSchemaDTO.getId(), datasourceSchemaDTO));
logger.debug("执行更新数据的SQL: {}", sql);
// 执行更新操作
int result= provider.executeUpdate(datasourceRequest);
if (result==1) {
return true;
// process result set
} else {
return false;
}
}
public boolean deleteTableData(Long datasourceId, String whereJson) throws Exception {
// 获取数据源信息
CoreDatasource coreDatasource = coreDatasourceMapper.selectById(datasourceId);
if (coreDatasource == null) {
DEException.throwException("数据源不存在");
}
// 解析 JSON 数据
//String tableDataJson = "{ \"tableName\": \"user\", \"primaryKeyField\": \"id\", \"primaryKeyValue\": \"0001\" }";
Map<String, Object> dataMap = JsonUtil.parseObject(whereJson, Map.class);
String tableName = (String) dataMap.get("tableName");
String primaryKeyField = (String) dataMap.get("primaryKeyField");
Object primaryKeyValue = dataMap.get("primaryKeyValue");
if (StringUtils.isBlank(tableName)) {
DEException.throwException("表名不能为空");
}
if (StringUtils.isBlank(primaryKeyField) || primaryKeyValue == null) {
DEException.throwException("主键字段或值不能为空");
}
// 构建 DELETE 语句
String whereClause = String.format("%s = ", primaryKeyField);
if (primaryKeyValue instanceof String) {
whereClause += String.format("'%s'", primaryKeyValue);
} else {
whereClause += primaryKeyValue;
}
String sql = String.format("DELETE FROM %s WHERE %s", tableName, whereClause);
// 调用执行器执行 SQL
DatasourceSchemaDTO datasourceSchemaDTO = new DatasourceSchemaDTO();
BeanUtils.copyBean(datasourceSchemaDTO, coreDatasource);
Provider provider = ProviderFactory.getProvider(coreDatasource.getType());
DatasourceRequest datasourceRequest = new DatasourceRequest();
datasourceRequest.setQuery(sql);
datasourceRequest.setDsList(Map.of(datasourceSchemaDTO.getId(), datasourceSchemaDTO));
logger.debug("执行删除数据的SQL: {}", sql);
// 执行删除操作
int result= provider.executeUpdate(datasourceRequest);
if (result==1) {
return true;
// process result set
} else {
return false;
}
}
public Page<Map<String, Object>> queryTableDataPaged(Long datasourceId, String queryJson) throws Exception {
// 获取数据源信息
CoreDatasource coreDatasource = coreDatasourceMapper.selectById(datasourceId);
if (coreDatasource == null) {
DEException.throwException("数据源不存在");
}
//String queryJson = "{ \"tableName\": \"user\", \"conditions\": [ { \"field\": \"name\", \"operator\": \"like\", \"value\": \"\" }, { \"field\": \"id\", \"operator\": \"in\", \"value\": [1, 2, 3] } ], \"pageNum\": 1, \"pageSize\": 10 }";
// 解析 JSON 查询参数
Map<String, Object> dataMap = JsonUtil.parseObject(queryJson, Map.class);
String tableName = (String) dataMap.get("tableName");
List<Map<String, Object>> conditionList = (List<Map<String, Object>>) dataMap.get("conditions");
Integer pageNum = (Integer) dataMap.getOrDefault("pageNum", 1);
Integer pageSize = (Integer) dataMap.getOrDefault("pageSize", 10);
if (StringUtils.isBlank(tableName)) {
DEException.throwException("表名不能为空");
}
// 构建 WHERE 条件子句
StringBuilder whereClause = new StringBuilder();
if (conditionList != null && !conditionList.isEmpty()) {
whereClause.append(" WHERE ");
for (int i = 0; i < conditionList.size(); i++) {
Map<String, Object> condition = conditionList.get(i);
String field = (String) condition.get("field");
String operator = ((String) condition.get("operator")).toLowerCase();
Object value = condition.get("value");
if (i > 0) {
whereClause.append(" AND ");
}
// 处理不同类型的条件
switch (operator) {
case "like":
whereClause.append(String.format("%s LIKE '%%%s%%'", field, value));
break;
case "=":
appendValue(whereClause, field, value, "=");
break;
case "<":
appendValue(whereClause, field, value, "<");
break;
case ">":
appendValue(whereClause, field, value, ">");
break;
case "<=":
appendValue(whereClause, field, value, "<=");
break;
case ">=":
appendValue(whereClause, field, value, ">=");
break;
case "!=":
case "<>":
appendValue(whereClause, field, value, "<>");
break;
case "in":
if (!(value instanceof List<?>)) {
DEException.throwException("IN 操作符要求值为列表类型");
}
List<?> values = (List<?>) value;
String inValues = values.stream()
.map(v -> v instanceof String ? "'" + v + "'" : v.toString())
.collect(Collectors.joining(", "));
whereClause.append(String.format("%s IN (%s)", field, inValues));
break;
default:
DEException.throwException("不支持的操作符: " + operator);
}
}
}
// 构建基础查询语句
String baseSql = String.format("SELECT * FROM %s%s", tableName, whereClause);
// 根据数据库类型生成分页语句
String dbType = coreDatasource.getType().toLowerCase();
String pagedSql = buildPagedSQL(baseSql, dbType, pageNum, pageSize);
// 构建 COUNT 查询语句用于分页计算
String countSql = String.format("SELECT COUNT(*) FROM %s%s", tableName, whereClause);
// 执行查询
DatasourceSchemaDTO schemaDTO = new DatasourceSchemaDTO();
BeanUtils.copyBean(schemaDTO, coreDatasource);
Provider provider = ProviderFactory.getProvider(coreDatasource.getType());
DatasourceRequest request = new DatasourceRequest();
request.setDsList(Map.of(schemaDTO.getId(), schemaDTO));
// 1. 查询分页数据
request.setQuery(pagedSql);
Map<String, Object> result = provider.fetchResultField(request);
List<Map<String, Object>> dataList = (List<Map<String, Object>>) result.get("data");
// 2. 查询总记录数
request.setQuery(countSql);
Map<String, Object> countResult = provider.fetchResultField(request);
long total = Long.parseLong(((List<String[]>) countResult.get("data")).get(0)[0]);
// 封装分页结果
Page<Map<String, Object>> page = new Page<>();
page.setCurrent(pageNum);
page.setSize(pageSize);
page.setTotal(total);
page.setRecords(dataList);
return page;
}
private void appendValue(StringBuilder sb, String field, Object value, String op) {
if (value instanceof String) {
sb.append(String.format("%s %s '%s'", field, op, value));
} else {
sb.append(String.format("%s %s %s", field, op, value));
}
}
private String buildPagedSQL(String baseSql, String dbType, int pageNum, int pageSize) {
int offset = (pageNum - 1) * pageSize;
switch (dbType) {
case "mysql":
case "mariadb":
return String.format("%s LIMIT %d OFFSET %d", baseSql, pageSize, offset);
case "postgresql":
return String.format("%s LIMIT %d OFFSET %d", baseSql, pageSize, offset);
case "oracle":
int start = offset + 1;
int end = offset + pageSize;
return String.format("SELECT * FROM (SELECT ROWNUM rn, t.* FROM (%s) t WHERE ROWNUM <= %d) WHERE rn >= %d", baseSql, end, start);
case "sqlserver":
return String.format("%s OFFSET %d ROWS FETCH NEXT %d ROWS ONLY", baseSql, offset, pageSize);
default:
// 默认使用 MySQL 方式
return String.format("%s LIMIT %d OFFSET %d", baseSql, pageSize, offset);
}
}
}

View File

@ -1,5 +1,6 @@
package io.gisbi.dataset.server;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.gisbi.api.dataset.DatasetDataApi;
import io.gisbi.api.dataset.dto.BaseTreeNodeDTO;
import io.gisbi.api.dataset.dto.EnumValueRequest;
@ -7,13 +8,14 @@ import io.gisbi.api.dataset.dto.MultFieldValuesRequest;
import io.gisbi.api.dataset.dto.PreviewSqlDTO;
import io.gisbi.api.dataset.union.DatasetGroupInfoDTO;
import io.gisbi.api.dataset.dto.EnumObj;
import io.gisbi.dataset.dao.auto.entity.CoreDatasetTable;
import io.gisbi.dataset.dao.auto.entity.CoreDatasetTableField;
import io.gisbi.dataset.manage.DatasetDataManage;
import io.gisbi.extensions.datasource.dto.DatasetTableDTO;
import io.gisbi.extensions.datasource.dto.DatasetTableFieldDTO;
import io.gisbi.utils.LogUtil;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
@ -32,6 +34,7 @@ public class DatasetDataServer implements DatasetDataApi {
return datasetDataManage.previewDataWithLimit(datasetGroupInfoDTO, 0, 100, false);
}
@Override
public List<DatasetTableFieldDTO> tableField(DatasetTableDTO datasetTableDTO) throws Exception {
return datasetDataManage.getTableFields(datasetTableDTO);
@ -100,4 +103,47 @@ public class DatasetDataServer implements DatasetDataApi {
return null;
}
}
@GetMapping("getTablesByAppId")
public List<Map<String,Object>> getTablesByAppId(String appid) throws Exception {
List<Map<String,Object>> result = datasetDataManage.getTablesByAppId(appid);
return result;
}
@GetMapping("getFieldsByTableId")
public List<Map<String,Object>> getFieldsByTableId(Long id) throws Exception {
List<Map<String,Object>> result = datasetDataManage.getFieldsByTableId(id);
return result;
}
@PostMapping("addTableData")
public boolean addTableData(Long datasourceId, @RequestBody String tableData) throws Exception {
boolean result = datasetDataManage.addTableData(
datasourceId,
tableData
);
return result;
}
@PostMapping("updateTableData")
public boolean updateTableData(Long datasourceId, @RequestBody String tableData) throws Exception {
boolean result = datasetDataManage.updateTableData(
datasourceId,
tableData
);
return result;
}
@PostMapping("deleteTableData")
public boolean deleteTableData(Long datasourceId, @RequestBody String whereJson) throws Exception {
boolean result = datasetDataManage.deleteTableData(
datasourceId,
whereJson
);
return result;
}
@PostMapping("queryTableDataPaged")
public Page<Map<String, Object>> queryTableDataPaged(Long datasourceId, @RequestBody String queryJson) throws Exception {
Page<Map<String, Object>> result = datasetDataManage.queryTableDataPaged(
datasourceId,
queryJson
);
return result;
}
}

View File

@ -4,6 +4,5 @@
// Generated by unplugin-auto-import
export {}
declare global {
const ElMessage: typeof import('element-plus-secondary/es')['ElMessage']
const ElMessageBox: typeof import('element-plus-secondary/es')['ElMessageBox']
}

View File

@ -80,9 +80,9 @@ public class TokenFilter implements Filter {
filterChain.doFilter(servletRequest, servletResponse);
return;
}
String token = ServletUtils.getToken();
TokenUserBO userBO = TokenUtils.validate(token);
UserUtils.setUserInfo(userBO);
// String token = ServletUtils.getToken();
// TokenUserBO userBO = TokenUtils.validate(token);
// UserUtils.setUserInfo(userBO);
filterChain.doFilter(servletRequest, servletResponse);
} catch (Exception e) {
throw e;