forked from opentiny/tiny-engine-backend-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicService.java
More file actions
243 lines (214 loc) · 7.34 KB
/
DynamicService.java
File metadata and controls
243 lines (214 loc) · 7.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package com.tinyengine.it.dynamic.service;
import com.alibaba.fastjson.JSONObject;
import com.tinyengine.it.common.context.LoginUserContext;
import com.tinyengine.it.dynamic.dao.ModelDataDao;
import com.tinyengine.it.dynamic.dto.*;
import com.tinyengine.it.model.entity.Model;
import com.tinyengine.it.service.material.ModelService;
import jakarta.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigInteger;
import java.util.*;
@Service
public class DynamicService {
@Autowired
private ModelDataDao dynamicDao;
@Autowired
private ModelService modelService;
@Autowired
private LoginUserContext loginUserContext;
// 操作类型常量
private static final String OPERATION_SELECT = "SELECT";
private static final String OPERATION_INSERT = "INSERT";
private static final String OPERATION_UPDATE = "UPDATE";
private static final String OPERATION_DELETE = "DELETE";
/**
* 查询数据
* @param dto
* @return list
*/
public List<JSONObject> query(DynamicQuery dto) {
String tableName = getTableName(dto.getNameEn());
Map<String, Object> params = new HashMap<>();
params.put("tableName", tableName);
params.put("fields", dto.getFields());
params.put("conditions", dto.getParams());
params.put("pageNum", dto.getCurrentPage());
params.put("pageSize", dto.getPageSize());
params.put("orderBy", dto.getOrderBy());
params.put("orderType", dto.getOrderType());
return dynamicDao.select(params);
}
/**
* 统计数量
* @param tableName
* @param conditions
* @return long
*/
public Long count(String tableName, Map<String, Object> conditions) {
Map<String, Object> params = new HashMap<>();
params.put("tableName", tableName);
params.put("fields", Arrays.asList("COUNT(*) as count"));
params.put("conditions", conditions);
List<JSONObject> result = dynamicDao.select(params);
return Long.parseLong(result.get(0).get("count").toString());
}
/**
* 分页查询
* @param dto
* @return map
*/
public Map<String, Object> queryWithPage(DynamicQuery dto) {
if( dto.getNameEn() == null || dto.getNameEn().trim().isEmpty()) {
throw new IllegalArgumentException("查询操作必须指定模型名称");
}
if( dto.getCurrentPage() == null || dto.getCurrentPage() <= 0) {
dto.setCurrentPage(1);
}
if( dto.getPageSize() == null || dto.getPageSize() <= 0) {
dto.setPageSize(10);
}
validateTableExists(dto.getNameEn());
validateTableAndData(dto.getNameEn(), dto.getParams());
List<JSONObject> list = query(dto);
String tableName = getTableName(dto.getNameEn());
Long total = count(tableName, dto.getParams());
Map<String, Object> result = new HashMap<>();
result.put("list", list);
result.put("total", total);
result.put("pageNum", dto.getCurrentPage());
result.put("pageSize", dto.getPageSize());
result.put("pages", (int) Math.ceil((double) total / dto.getPageSize()));
return result;
}
/**
* 插入数据
* @param dto
* @return map
*/
@Transactional
public Map<String, Object> insert(DynamicInsert dto) {
if( dto.getNameEn() == null || dto.getNameEn().trim().isEmpty()) {
throw new IllegalArgumentException("插入操作必须指定模型名称");
}
if( dto.getParams() == null || dto.getParams().isEmpty()) {
throw new IllegalArgumentException("插入数据不能为空");
}
validateTableExists(dto.getNameEn());
validateTableAndData(dto.getNameEn(), dto.getParams());
String tableName = getTableName(dto.getNameEn());
Map<String, Object> params = new HashMap<>();
params.put("tableName", tableName);
params.put("data", dto.getParams());
String userId = loginUserContext.getLoginUserId();
if( userId == null || userId.trim().isEmpty()) {
List<Model> modelList = modelService.getModelByEnName(dto.getNameEn());
if( modelList.isEmpty()) {
throw new IllegalArgumentException("模型不存在: " + dto.getNameEn());
}else {
userId=modelList.get(0).getCreatedBy();
}
}
// 添加系统字段
dto.getParams().put("created_by", userId);
dto.getParams().put("updated_by", userId);
Map<String, Object> result = new HashMap<>();
Long insertRow = dynamicDao.insert(params);
BigInteger id = (BigInteger) params.get("id");
result.put("insert", insertRow);
result.put("id", id.longValue());
return result;
}
/**
* 更新数据
* @param dto
* @return
*/
@Transactional
public Map<String, Object> update(DynamicUpdate dto) {
if( dto.getNameEn() == null || dto.getNameEn().trim().isEmpty()) {
throw new IllegalArgumentException("更新操作必须指定模型名称");
}
if (dto.getParams() == null || dto.getParams().isEmpty()) {
throw new IllegalArgumentException("更新操作必须指定条件");
}
if( dto.getData() == null || dto.getData().isEmpty()) {
throw new IllegalArgumentException("更新数据不能为空");
}
validateTableExists(dto.getNameEn());
validateTableAndData(dto.getNameEn(), dto.getData());
String tableName = getTableName(dto.getNameEn());
Map<String, Object> params = new HashMap<>();
params.put("tableName", tableName);
params.put("data", dto.getData());
params.put("conditions", dto.getParams());
Map<String, Object> result = new HashMap<>();
Integer update = dynamicDao.update(params);
result.put("update", update);
return result;
}
/**
* 删除数据
*/
@Transactional
public Map<String, Object> delete(DynamicDelete dto) {
if( dto.getNameEn() == null || dto.getNameEn().trim().isEmpty()) {
throw new IllegalArgumentException("删除操作必须指定模型名称");
}
if (dto.getId() == null ) {
throw new IllegalArgumentException("删除操作必须指定id");
}
validateTableExists(dto.getNameEn());
String tableName = getTableName(dto.getNameEn());
Map<String, Object> params = new HashMap<>();
Map<Object, Object> conditions = new HashMap<>();
conditions.put("id", dto.getId());
params.put("tableName", tableName);
params.put("conditions",conditions);
Map<String, Object> result = new HashMap<>();
Integer delete = dynamicDao.delete(params);
result.put("delete", delete);
return result;
}
/**
* 获取表结构
*/
public List<Map<String, Object>> getTableStructure(String tableName) {
validateTableExists(tableName);
return dynamicDao.getTableStructure(tableName);
}
/**
* 验证表和数据
*/
private void validateTableAndData(String tableName, Map<String, Object> data) {
if (tableName == null || tableName.trim().isEmpty()) {
throw new IllegalArgumentException("表名不能为空");
}
// 防止SQL注入,验证表名格式
if (!tableName.matches("^[a-zA-Z_][a-zA-Z0-9_]*$")) {
throw new IllegalArgumentException("表名格式不正确");
}
if (data == null || data.isEmpty()) {
throw new IllegalArgumentException("数据不能为空");
}
// 验证字段名格式
for (String field : data.keySet()) {
if (!field.matches("^[a-zA-Z_][a-zA-Z0-9_]*$")) {
throw new IllegalArgumentException("字段名格式不正确: " + field);
}
}
}
/**
* 验证表是否存在
*/
public void validateTableExists(String tableName) {
List<String> tables = modelService.getAllModelName();
if (!tables.contains(tableName)) {
throw new IllegalArgumentException("模型不存在: " + tableName);
}
}
private String getTableName(String modelId) {
return "dynamic_" + modelId.toLowerCase(Locale.ROOT);
}
}