forked from opentiny/tiny-engine-backend-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockController.java
More file actions
430 lines (401 loc) · 17.5 KB
/
BlockController.java
File metadata and controls
430 lines (401 loc) · 17.5 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/**
* Copyright (c) 2023 - present TinyEngine Authors.
* Copyright (c) 2023 - present Huawei Cloud Computing Technologies Co., Ltd.
*
* Use of this source code is governed by an MIT-style license.
*
* THE OPEN SOURCE SOFTWARE IN THIS PRODUCT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL,
* BUT WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR
* A PARTICULAR PURPOSE. SEE THE APPLICABLE LICENSES FOR MORE DETAILS.
*
*/
package com.tinyengine.it.controller;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tinyengine.it.common.base.Result;
import com.tinyengine.it.common.enums.Enums;
import com.tinyengine.it.common.log.SystemControllerLog;
import com.tinyengine.it.common.utils.JsonUtils;
import com.tinyengine.it.mapper.BlockMapper;
import com.tinyengine.it.mapper.TenantMapper;
import com.tinyengine.it.model.dto.BlockBuildDto;
import com.tinyengine.it.model.dto.BlockDto;
import com.tinyengine.it.model.dto.BlockParam;
import com.tinyengine.it.model.dto.BlockParamDto;
import com.tinyengine.it.model.dto.NotGroupDto;
import com.tinyengine.it.model.entity.Block;
import com.tinyengine.it.model.entity.Tenant;
import com.tinyengine.it.model.entity.User;
import com.tinyengine.it.service.material.BlockService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* 区块
*
* @author zhangjuncao
* @since 2024-10-30
*/
@Validated
@RestController
@RequestMapping("/material-center/api")
@Tag(name = "区块")
public class BlockController {
/**
* The Block service.
*/
@Autowired
private BlockService blockService;
/**
* The Tenant service.
*/
@Autowired
private TenantMapper tenantMapper;
/**
* The Block mapper.
*/
@Autowired
private BlockMapper blockMapper;
/**
* 获取block列表信息
*
* @param blockParamDto blockParamDto
* @return block列表信息
*/
@Operation(summary = "获取区块列表信息", description = "获取区块列表信息", parameters = {
@Parameter(name = "blockParamDto", description = "入参对象")
}, responses = {
@ApiResponse(responseCode = "200", description = "返回信息",
content = @Content(mediaType = "application/json", schema = @Schema(implementation = Block.class))),
@ApiResponse(responseCode = "400", description = "请求失败")
})
@SystemControllerLog(description = "获取区块列表api")
@GetMapping("/block/list")
public Result<List<Block>> getAllBlocks(@ModelAttribute BlockParamDto blockParamDto) {
IPage<Block> blocksList = blockService.findBlocksByPagetionList(blockParamDto);
List<Block> result = blocksList.getRecords();
return Result.success(result);
}
/**
* 获取区块列表满足查询条件下的条数
*
* @param nameCn name
* @param description description
* @return the integer
*/
@Operation(summary = "获取区块列表满足查询条件下的条数", description = "获取区块列表满足查询条件下的条数",
parameters = {
@Parameter(name = "nameCn", description = "nameCn区块中文名称"),
@Parameter(name = "description", description = "区块描述")
}, responses = {
@ApiResponse(responseCode = "200", description = "返回信息",
content = @Content(mediaType = "application/json", schema = @Schema())),
@ApiResponse(responseCode = "400", description = "请求失败")
})
@SystemControllerLog(description = "获取区块列表满足查询条件下的条数")
@GetMapping("/block/count")
public Result<Integer> getCountByCondition(
@RequestParam(value = "name_cn", required = false) String nameCn,
@RequestParam(value = "description", required = false)
String description) {
// 获取查询条件name_cn和description,若为空查的是全部数据,若不为空按条件查询
List<Block> blocksList = blockMapper.findBlocksByNameCnAndDes(nameCn, description);
return Result.success(blocksList.size());
}
/**
* 根据id查询表block信息
*
* @param id id
* @return BlockDto
*/
@Operation(summary = "查询区块详情", description = "根据id查询表t_block信息并返回", parameters = {
@Parameter(name = "id", description = "区块Id")
}, responses = {
@ApiResponse(responseCode = "200", description = "返回区块信息",
content = @Content(mediaType = "application/json", schema = @Schema(implementation = Block.class))),
@ApiResponse(responseCode = "400", description = "请求失败")
})
@SystemControllerLog(description = "获取区块详情api")
@GetMapping("/block/detail/{id}")
public Result<BlockDto> getBlocksById(@PathVariable Integer id) {
BlockDto block = blockService.queryBlockById(id);
return Result.success(block);
}
/**
* 创建block
*
* @param blockParam the blockParam
* @return BlockDto
*/
@Operation(summary = "创建block", description = "创建block", parameters = {
@Parameter(name = "blockParam", description = "入参对象")
}, responses = {
@ApiResponse(responseCode = "200", description = "返回信息",
content = @Content(mediaType = "application/json", schema = @Schema(implementation = Block.class))),
@ApiResponse(responseCode = "400", description = "请求失败")
})
@SystemControllerLog(description = "区块创建api")
@PostMapping("/block/create")
public Result<BlockDto> createBlocks(@Valid @RequestBody BlockParam blockParam) {
return blockService.createBlock(blockParam);
}
/**
* 删除blocks信息
*
* @param id id
* @return BlockDto
*/
@Operation(summary = "删除blocks信息", description = "删除blocks信息", parameters = {
@Parameter(name = "id", description = "区块id")
}, responses = {
@ApiResponse(responseCode = "200", description = "返回信息",
content = @Content(mediaType = "application/json", schema = @Schema(implementation = Block.class))),
@ApiResponse(responseCode = "400", description = "请求失败")
})
@SystemControllerLog(description = "删除blocks信息")
@GetMapping("/block/delete/{id}")
public Result<BlockDto> deleteBlock(@PathVariable Integer id) {
// 页面返回数据
BlockDto result = blockService.queryBlockById(id);
blockService.deleteBlockById(id);
return Result.success(result);
}
/**
* 生态中心区块列表分页查询
*
* @param blockParamDto blockParamDto
* @return BlockDto
*/
@Operation(summary = "生态中心区块列表分页查询", description = "生态中心区块列表分页查询", parameters = {
@Parameter(name = "blockParamDto", description = "入参对象")
}, responses = {
@ApiResponse(responseCode = "200", description = "返回信息",
content = @Content(mediaType = "application/json", schema = @Schema())),
@ApiResponse(responseCode = "400", description = "请求失败")
})
@SystemControllerLog(description = "生态中心区块列表分页查询api")
@GetMapping("/block")
public Result<List<BlockDto>> find(@ModelAttribute BlockParamDto blockParamDto) {
IPage<Block> blocksIPage = blockService.findBlocksByPagetionList(blockParamDto);
List<Block> blocksList = blocksIPage.getRecords();
List<BlockDto> result = new ArrayList<>();
for (Block blocks : blocksList) {
List<BlockDto> blockDto = blockMapper.findBlockAndHistorByBlockId(blocks.getId());
result.addAll(blockDto);
}
return Result.success(result);
}
/**
* 查找表中所有tags
*
* @return the list
*/
@Operation(summary = "查找表中所有tags", description = "查找表中所有tags", responses = {
@ApiResponse(responseCode = "200", description = "返回信息",
content = @Content(mediaType = "application/json", schema = @Schema())),
@ApiResponse(responseCode = "400", description = "请求失败")
})
@SystemControllerLog(description = "查找表中所有tags")
@GetMapping("/block/tags")
public Result<List<String>> allTags() {
List<String> allTagsList = blockService.allTags();
return Result.success(allTagsList);
}
/**
* 查找不在分组内的区块
*
* @param groupId the groupId
* @return the list
*/
@Operation(summary = "查找不在分组内的区块", description = "查找不在分组内的区块", parameters = {
@Parameter(name = "groupId", description = "分组id")
}, responses = {
@ApiResponse(responseCode = "200", description = "返回信息",
content = @Content(mediaType = "application/json", schema = @Schema(implementation = Block.class))),
@ApiResponse(responseCode = "400", description = "请求失败")
})
@SystemControllerLog(description = "查找不在分组内的区块api")
@GetMapping("/block/notgroup/{groupId}")
public Result<List<BlockDto>> findBlocksNotInGroup(@PathVariable Integer groupId,
@RequestParam(value = "label_contains", required = false) String label,
@RequestParam(value = "name_cn_contains", required = false) String name,
@RequestParam(value = "tags_contains", required = false) String[] tags,
@RequestParam(value = "createdBy", required = false) String createdBy) {
NotGroupDto notGroupDto = new NotGroupDto();
notGroupDto.setGroupId(groupId);
notGroupDto.setLabel(label);
notGroupDto.setName(name);
notGroupDto.setCreatedBy(createdBy);
notGroupDto.setTags(null);
if (tags != null && tags.length > 0) {
notGroupDto.setTags(JsonUtils.encode(tags)); // 将数组转换为有效的 JSON 字符串
}
List<BlockDto> blocksList = blockService.getNotInGroupBlocks(notGroupDto);
return Result.success(blocksList);
}
/**
* 获取区块列表list2
*
* @param request request
* @return the ipage
*/
@Operation(summary = "获取区块列表list2", description = "获取区块列表list2", parameters = {
@Parameter(name = "request", description = "入参对象")
}, responses = {
@ApiResponse(responseCode = "200", description = "返回信息",
content = @Content(mediaType = "application/json", schema = @Schema())),
@ApiResponse(responseCode = "400", description = "请求失败")
})
@SystemControllerLog(description = "获取区块列表api")
@GetMapping("/block/list2")
public Result<IPage<Block>> getBlocks(@RequestBody Map<String, String> request) {
IPage<Block> blocksList = blockService.findBlocksByConditionPagetion(request);
return Result.success(blocksList);
}
/**
* 获取所有租户
*
* @return the list
*/
@Operation(summary = "获取所有租户", description = "获取所有租户", responses = {
@ApiResponse(responseCode = "200", description = "返回信息",
content = @Content(mediaType = "application/json", schema = @Schema(implementation = Tenant.class))),
@ApiResponse(responseCode = "400", description = "请求失败")
})
@SystemControllerLog(description = "获取所有租户api")
@GetMapping("/block/tenants")
public Result<List<Tenant>> allTenant() {
List<Tenant> tenantsList = tenantMapper.queryAllTenant();
return Result.success(tenantsList);
}
/**
* 获取所有用户
*
* @return the list
*/
@Operation(summary = "获取所有用户", description = "获取所有用户", responses = {
@ApiResponse(responseCode = "200", description = "返回信息",
content = @Content(mediaType = "application/json", schema = @Schema(implementation = User.class))),
@ApiResponse(responseCode = "400", description = "请求失败")
})
@SystemControllerLog(description = "获取所有用户api")
@GetMapping("/block/users")
public Result<List<User>> allAuthor() {
List<Block> blocksList = blockMapper.queryAllBlock();
List<User> userList = blockService.getUsers(blocksList);
return Result.success(userList);
}
/**
* 获取区块列表
*
* @param appId appId
* @param groupId groupId
* @return the list
*/
@Operation(summary = "获取区块列表", description = "获取区块列表", parameters = {
@Parameter(name = "appId", description = "app主键id"),
@Parameter(name = "groupId", description = "区块分组id")
}, responses = {
@ApiResponse(responseCode = "200", description = "返回信息",
content = @Content(mediaType = "application/json", schema = @Schema(implementation = Block.class))),
@ApiResponse(responseCode = "400", description = "请求失败")
})
@SystemControllerLog(description = "获取区块列表")
@GetMapping("/blocks")
public Result<List<Block>> getBlockGroups(@Valid @RequestParam(required = false) String appId,
@RequestParam(required = false) String groupId) {
return blockService.listNew(appId, groupId);
}
/**
* 修改block
*
* @param request request
* @param id id
* @return block dto
*/
@Operation(summary = "修改区块", description = "修改区块", parameters = {
@Parameter(name = "blockParam", description = "入参对象"),
@Parameter(name = "id", description = "区块id")
}, responses = {
@ApiResponse(responseCode = "200", description = "返回信息",
content = @Content(mediaType = "application/json", schema = @Schema(implementation = Block.class))),
@ApiResponse(responseCode = "400", description = "请求失败")
})
@SystemControllerLog(description = "区块修改api")
@PostMapping("/block/update/{id}")
public Result<BlockDto> updateBlocks(HttpServletRequest request, @PathVariable Integer id,
@RequestParam(value = "appId", required = false) Integer appId) throws IOException {
// Validate content type
String contentType = request.getContentType();
if (contentType == null || !contentType.contains(Enums.FileType.JSON.getValue())) {
return Result.failed("Content-Type must be application/json");
}
InputStream inputStream = request.getInputStream();
String json = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
BlockParam blockParam = null;
try {
blockParam = JsonUtils.decode(json, BlockParam.class);
} catch (Exception e) {
return Result.failed("Invalid JSON format: " + e.getMessage());
}
blockParam.setId(id);
blockParam.setAppId(appId);
return blockService.updateBlockById(blockParam);
}
/**
* 通过lable和appId查询区块
*
* @param label label
* @param appId appId
* @return the result
*/
@Operation(summary = "根据lable和appId查询区块详情", description = "根据lable和appId查询区块详情", responses = {
@ApiResponse(responseCode = "200", description = "返回信息",
content = @Content(mediaType = "application/json", schema = @Schema(implementation = Block.class))),
@ApiResponse(responseCode = "400", description = "请求失败")
})
@SystemControllerLog(description = "根据lable和appId查询区块详情api")
@GetMapping("/block/label")
public Result<BlockDto> getBlockByLabel(@RequestParam(value = "label") String label,
@RequestParam(value = "appId") Integer appId) {
return blockService.getBlockByLabel(label, appId);
}
/**
* block发布
*
* @param blockBuildDto block
* @return blcok信息
*/
@Operation(summary = "区块发布", description = "区块发布", parameters = {
@Parameter(name = "blockBuildDto", description = "入参对象")
}, responses = {
@ApiResponse(responseCode = "200", description = "返回信息",
content = @Content(mediaType = "application/json", schema = @Schema(implementation = Block.class))),
@ApiResponse(responseCode = "400", description = "请求失败")}
)
@SystemControllerLog(description = "区块发布api")
@PostMapping("/block/deploy")
public Result<BlockDto> build(@Valid @RequestBody BlockBuildDto blockBuildDto) {
return blockService.deploy(blockBuildDto);
}
}