Skip to content

Commit 8cc8ca5

Browse files
committed
fix: Modify code format
1 parent 9c1225a commit 8cc8ca5

File tree

5 files changed

+124
-68
lines changed

5 files changed

+124
-68
lines changed

.github/workflows/checkstyle.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ jobs:
2727
# 安装依赖并运行 Checkstyle(如果是 Maven 项目)
2828
- name: Install dependencies and run Checkstyle
2929
run: |
30-
mvn clean install
30+
mvn checkstyle:check
3131
3232
# 查看 Checkstyle 检查报告
3333
- name: Upload Checkstyle report

base/src/main/java/com/tinyengine/it/model/dto/BlockParam.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,18 @@
2424
import lombok.Data;
2525

2626
import java.util.ArrayList;
27+
import java.util.Arrays;
2728
import java.util.List;
2829
import java.util.Map;
2930

3031
/**
3132
* <p>
3233
* 区块param
3334
* </p>
34-
*
35+
* This class serves as a parameter object for block creation and updates,
36+
* working in conjunction with {@link BlockDto}. While BlockDto represents
37+
* the full block data model, BlockParam is specifically designed for
38+
* handling input parameters during block operations.
3539
* @author lu-yg
3640
* @since 2024-01-27
3741
*/
@@ -151,6 +155,9 @@ public class BlockParam extends BaseEntity {
151155
private String currentVersion;
152156
@JsonProperty("public")
153157
public Integer getPublic() {
158+
if (this.publicStatus != null && !Arrays.asList(0, 1, 2).contains(this.publicStatus)) {
159+
throw new IllegalStateException("Invalid public status value: " + this.publicStatus);
160+
}
154161
return this.publicStatus;
155162
}
156163
}

base/src/main/java/com/tinyengine/it/service/material/impl/BlockServiceImpl.java

Lines changed: 90 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
import java.time.LocalDateTime;
5858
import java.time.format.DateTimeFormatter;
5959
import java.util.ArrayList;
60-
import java.util.Arrays;
6160
import java.util.Collections;
6261
import java.util.HashMap;
6362
import java.util.HashSet;
@@ -94,6 +93,8 @@ public class BlockServiceImpl implements BlockService {
9493
@Autowired
9594
private BlockGroupBlockMapper blockGroupBlockMapper;
9695

96+
private static final int DEFAULT_PLATFORM_ID = 1;
97+
private static final String DEFAULT_USER_ID = "1";
9798
/**
9899
* 查询表t_block所有数据
99100
*
@@ -120,8 +121,7 @@ public BlockDto queryBlockById(@Param("id") Integer id) {
120121
&& blockDto.getLastBuildInfo().get("result") instanceof Boolean
121122
? (Boolean) blockDto.getLastBuildInfo().get("result") : Boolean.FALSE;
122123
blockDto.setIsPublished(isPublished);
123-
String createdBy = "1";
124-
List<BlockGroup> groups = blockGroupMapper.findBlockGroupByBlockId(blockDto.getId(), createdBy);
124+
List<BlockGroup> groups = blockGroupMapper.findBlockGroupByBlockId(blockDto.getId(), DEFAULT_USER_ID);
125125
blockDto.setGroups(groups);
126126
return blockDto;
127127
}
@@ -156,15 +156,20 @@ public Integer deleteBlockById(@Param("id") Integer id) {
156156
*/
157157
@Override
158158
public Result<BlockDto> updateBlockById(BlockParam blockParam, Integer appId) {
159+
if (blockParam == null || blockParam.getId() == null) {
160+
return Result.failed(ExceptionEnum.CM002);
161+
}
159162
Block blockResult = blockMapper.queryBlockById(blockParam.getId());
160-
163+
if (blockResult == null) {
164+
return Result.failed(ExceptionEnum.CM001);
165+
}
161166
if (!Objects.equals(blockResult.getAppId(), appId)) {
162167
return Result.failed(ExceptionEnum.CM007);
163168
}
164169
// 把前端传参赋值给实体
165170
Block blocks = new Block();
166171
BeanUtils.copyProperties(blockParam, blocks);
167-
blocks.setOccupierBy(String.valueOf(1));
172+
blocks.setOccupierBy(DEFAULT_USER_ID);
168173
if (blockParam.getLatestHistoryId() != null) {
169174
blocks.setLatestHistoryId(blockParam.getLatestHistoryId().getId());
170175
}
@@ -180,10 +185,8 @@ public Result<BlockDto> updateBlockById(BlockParam blockParam, Integer appId) {
180185
return Result.success(blockDtoResult);
181186
}
182187

183-
// 对接登录后获取用户id
184-
String createdBy = "1";
185188
// 根据区块id获取区块所在分组
186-
List<BlockGroup> blockGroups = blockGroupMapper.findBlockGroupByBlockId(blocks.getId(), createdBy);
189+
List<BlockGroup> blockGroups = blockGroupMapper.findBlockGroupByBlockId(blocks.getId(), DEFAULT_USER_ID);
187190
if (!blockGroups.isEmpty()) {
188191
List<Integer> blockGroupIds = blockGroups.stream().map(BlockGroup::getId).collect(Collectors.toList());
189192
for (Integer id : blockGroupIds) {
@@ -212,6 +215,9 @@ public Result<BlockDto> updateBlockById(BlockParam blockParam, Integer appId) {
212215
*/
213216
@Override
214217
public Result<BlockDto> createBlock(BlockParam blockParam) {
218+
if (blockParam == null || blockParam.getLabel() == null) {
219+
return Result.failed(ExceptionEnum.CM002);
220+
}
215221
// 对接收到的参数occupier为对应的一个对象,进行特殊处理并重新赋值
216222
Block blocks = new Block();
217223
if (blockParam.getOccupier() != null) {
@@ -220,7 +226,7 @@ public Result<BlockDto> createBlock(BlockParam blockParam) {
220226
BeanUtils.copyProperties(blockParam, blocks);
221227
blocks.setIsDefault(false);
222228
blocks.setIsOfficial(false);
223-
blocks.setPlatformId(1); // 新建区块给默认值
229+
blocks.setPlatformId(DEFAULT_PLATFORM_ID); // 新建区块给默认值
224230

225231
int result = blockMapper.createBlock(blocks);
226232
if (result < 1) {
@@ -229,12 +235,17 @@ public Result<BlockDto> createBlock(BlockParam blockParam) {
229235
int id = blocks.getId();
230236
BlockDto blocksResult = queryBlockById(id);
231237
List<Integer> groups = blockParam.getGroups();
232-
if (groups != null && !groups.isEmpty()) {
233-
Integer groupId = groups.get(0); // 强制类型转换
234-
BlockGroupBlock blockGroupBlock = new BlockGroupBlock();
235-
blockGroupBlock.setBlockGroupId(groupId);
236-
blockGroupBlock.setBlockId(id);
237-
blockGroupBlockMapper.createBlockGroupBlock(blockGroupBlock);
238+
if (groups == null || groups.isEmpty()) {
239+
return Result.success(blocksResult);
240+
}
241+
242+
Integer groupId = groups.get(0); // 强制类型转换
243+
BlockGroupBlock blockGroupBlock = new BlockGroupBlock();
244+
blockGroupBlock.setBlockGroupId(groupId);
245+
blockGroupBlock.setBlockId(id);
246+
int groupResult = blockGroupBlockMapper.createBlockGroupBlock(blockGroupBlock);
247+
if (groupResult < 1) {
248+
return Result.failed(ExceptionEnum.CM001);
238249
}
239250
return Result.success(blocksResult);
240251
}
@@ -428,16 +439,13 @@ public List<String> allTags() {
428439
@SystemServiceLog(description = "getNotInGroupBlocks 获取不在分组内的区块 实现类")
429440
@Override
430441
public List<BlockDto> getNotInGroupBlocks(NotGroupDto notGroupDto) {
431-
// 获取缓存中的登录用户
432-
int userId = 1;
433-
User user = userMapper.queryUserById(userId);
434442
List<BlockDto> blocksList = blockMapper.findBlocksReturn(notGroupDto);
435443
if (blocksList == null || blocksList.isEmpty()) {
436444
return blocksList;
437445
}
438446

439447
for (BlockDto blockDto : blocksList) {
440-
List<BlockGroup> blockGroups = blockGroupMapper.findBlockGroupByBlockId(blockDto.getId(), String.valueOf(userId));
448+
List<BlockGroup> blockGroups = blockGroupMapper.findBlockGroupByBlockId(blockDto.getId(), DEFAULT_USER_ID);
441449
blockDto.setGroups(blockGroups);
442450
}
443451
return blocksList.stream()
@@ -449,15 +457,15 @@ public List<BlockDto> getNotInGroupBlocks(NotGroupDto notGroupDto) {
449457
}
450458
// 组过滤
451459
if (item.getGroups() != null && item.getGroups().stream()
452-
.anyMatch(group -> group instanceof BlockGroup
453-
&& ((BlockGroup) group).getId().equals(notGroupDto.getGroupId()))) {
460+
.anyMatch(group -> group != null
461+
&& group.getId().equals(notGroupDto.getGroupId()))) {
454462
return false;
455463
}
456464
// 公开范围过滤
457465
if (item.getPublicStatus() == Enums.Scope.FULL_PUBLIC.getValue()) {
458466
return true;
459467
}
460-
return user != null && item.getPublicStatus() == Enums.Scope.PUBLIC_IN_TENANTS.getValue();
468+
return item.getPublicStatus() == Enums.Scope.PUBLIC_IN_TENANTS.getValue();
461469
})
462470
.collect(Collectors.toList());
463471
}
@@ -491,6 +499,9 @@ public Result<BlockDto> getBlockByLabel(String label, Integer appId) {
491499
*/
492500
@Override
493501
public Result<BlockDto> deploy(BlockBuildDto blockBuildDto) {
502+
if (blockBuildDto == null || blockBuildDto.getBlock() == null) {
503+
return Result.failed(ExceptionEnum.CM002);
504+
}
494505
Map<String, Object> content = blockBuildDto.getBlock().getContent();
495506
if (content.isEmpty()) {
496507
return Result.failed(ExceptionEnum.CM204);
@@ -502,45 +513,43 @@ public Result<BlockDto> deploy(BlockBuildDto blockBuildDto) {
502513
if (isHistory) {
503514
return Result.failed(ExceptionEnum.CM205);
504515
}
505-
BlockParam blockParam = blockBuildDto.getBlock();
506-
List<I18nEntryDto> i18nList = i18nEntryMapper.findI18nEntriesByHostandHostType(id, "block");
507-
// 序列化国际化词条
508-
SchemaI18n appEntries = i18nEntryService.formatEntriesList(i18nList);
509-
BlockHistory blockHistory = new BlockHistory();
510-
blockParam.setCreatedTime(null);
511-
blockParam.setLastUpdatedTime(null);
512-
blockParam.setTenantId(null);
513-
Map<String, Map<String, String>> i18n = new HashMap<>();
514-
i18n.put("zh_CN", appEntries.getZhCn());
515-
i18n.put("en_US", appEntries.getEnUs());
516-
517-
blockParam.setI18n(i18n);
518-
blockHistory.setIsPublic(blockParam.getPublic());
519-
BeanUtil.copyProperties(blockParam, blockHistory);
520-
blockHistory.setRefId(id);
521-
blockHistory.setVersion(blockBuildDto.getVersion());
522-
blockHistory.setMessage(blockBuildDto.getDeployInfo());
523-
Map<String, Object> buildInfo = new HashMap<>();
524-
buildInfo.put("result", true);
525-
buildInfo.put("versions", Arrays.asList(blockBuildDto.getVersion()));
526-
// 获取当前时间
527-
LocalDateTime now = LocalDateTime.now();
516+
try {
517+
BlockParam blockParam = blockBuildDto.getBlock();
518+
List<I18nEntryDto> i18nList = i18nEntryMapper.findI18nEntriesByHostandHostType(id, "block");
519+
// 序列化国际化词条
520+
SchemaI18n appEntries = i18nEntryService.formatEntriesList(i18nList);
521+
BlockHistory blockHistory = new BlockHistory();
522+
blockParam.setCreatedTime(null);
523+
blockParam.setLastUpdatedTime(null);
524+
blockParam.setTenantId(null);
525+
Map<String, Map<String, String>> i18n = new HashMap<>();
526+
i18n.put("zh_CN", appEntries.getZhCn());
527+
i18n.put("en_US", appEntries.getEnUs());
528+
529+
blockParam.setI18n(i18n);
530+
blockHistory.setIsPublic(blockParam.getPublic());
531+
BeanUtil.copyProperties(blockParam, blockHistory);
532+
blockHistory.setRefId(id);
533+
blockHistory.setVersion(blockBuildDto.getVersion());
534+
blockHistory.setMessage(blockBuildDto.getDeployInfo());
535+
536+
// 获取当前时间
537+
LocalDateTime now = LocalDateTime.now();
538+
Map<String, Object> buildInfo = createBuildInfo(blockBuildDto.getVersion(), now);
539+
blockHistory.setBuildInfo(buildInfo);
540+
blockHistory.setId(null);
541+
int blockHistoryResult = blockHistoryMapper.createBlockHistory(blockHistory);
542+
if (blockHistoryResult < 1) {
543+
return Result.failed(ExceptionEnum.CM008);
544+
}
545+
blockParam.setLastBuildInfo(buildInfo);
546+
blockParam.setLatestHistoryId(blockHistory);
547+
blockParam.setLatestVersion(blockHistory.getVersion());
528548

529-
// 使用自定义格式化输出
530-
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
531-
String formattedDate = now.format(formatter);
532-
buildInfo.put("endTime", formattedDate);
533-
blockHistory.setBuildInfo(buildInfo);
534-
blockHistory.setId(null);
535-
int blockHistoryResult = blockHistoryMapper.createBlockHistory(blockHistory);
536-
if (blockHistoryResult < 1) {
537-
return Result.failed(ExceptionEnum.CM008);
538-
}
539-
blockParam.setLastBuildInfo(buildInfo);
540-
blockParam.setLatestHistoryId(blockHistory);
541-
blockParam.setLatestVersion(blockHistory.getVersion());
542-
543-
return updateBlockById(blockParam, blockParam.getAppId());
549+
return updateBlockById(blockParam, blockParam.getAppId());
550+
} catch (Exception e) {
551+
return Result.failed(ExceptionEnum.CM001);
552+
}
544553
}
545554

546555
/**
@@ -619,10 +628,9 @@ public Result<List<Block>> listNew(String appId, String groupId) {
619628
}
620629
}
621630
List<Block> blocksList = new ArrayList<>();
622-
String createdBy = "1"; // 获取用户登录id
623631
// 如果有 groupId, 只查group下的block,以及自己创建的区块
624632
if (groupIdTemp != 0) {
625-
blocksList = blockMapper.findBlockByBlockGroupId(groupIdTemp, createdBy);
633+
blocksList = blockMapper.findBlockByBlockGroupId(groupIdTemp, DEFAULT_USER_ID);
626634
return Result.success(blocksList);
627635
}
628636
// 如果没有 groupId
@@ -634,7 +642,7 @@ public Result<List<Block>> listNew(String appId, String groupId) {
634642
List<Block> appBlocks = blocksList;
635643
// 通过createBy查询区块表数据
636644
Block blocks = new Block();
637-
blocks.setCreatedBy(createdBy);
645+
blocks.setCreatedBy(DEFAULT_USER_ID);
638646
List<Block> personalBlocks = queryBlockByCondition(blocks);
639647
List<Block> retBlocks = new ArrayList<>();
640648
// 合并 personalBlocks 和 appBlocks 数组
@@ -670,15 +678,16 @@ public Result<List<Block>> listNew(String appId, String groupId) {
670678
* @return the id
671679
*/
672680
public int ensureBlockId(BlockParam blockParam) {
681+
log.debug("Ensuring block ID for label: {}", blockParam.getLabel());
673682
if (blockParam.getId() != null) {
683+
log.debug("Block ID already exists: {}", blockParam.getId());
674684
return blockParam.getId();
675685
}
676686
// 查询当前用户信息
677-
int userId = 86;
678687
Block queryBlock = new Block();
679688
queryBlock.setLabel(blockParam.getLabel());
680689
queryBlock.setFramework(blockParam.getFramework());
681-
queryBlock.setCreatedBy(String.valueOf(userId));
690+
queryBlock.setCreatedBy(DEFAULT_USER_ID);
682691
List<Block> blockList = blockMapper.queryBlockByCondition(queryBlock);
683692
if (blockList.isEmpty()) {
684693
createBlock(blockParam);
@@ -705,4 +714,20 @@ public boolean isHistoryExisted(Integer id, String version) {
705714
}
706715
return true;
707716
}
717+
718+
/**
719+
* 创建构建信息
720+
*
721+
* @param version the id
722+
* @param buildTime the buildTime
723+
* @return buildInfo the buildInfo
724+
*/
725+
private Map<String, Object> createBuildInfo(String version, LocalDateTime buildTime) {
726+
Map<String, Object> buildInfo = new HashMap<>();
727+
buildInfo.put("result", true);
728+
buildInfo.put("versions", Collections.singletonList(version));
729+
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
730+
buildInfo.put("endTime", buildTime.format(formatter));
731+
return buildInfo;
732+
}
708733
}

base/src/test/java/com/tinyengine/it/controller/BlockControllerTest.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@
1212

1313
package com.tinyengine.it.controller;
1414

15+
import static org.mockito.ArgumentMatchers.argThat;
16+
import static org.mockito.ArgumentMatchers.eq;
1517
import static org.mockito.Mockito.any;
1618
import static org.mockito.Mockito.anyInt;
1719
import static org.mockito.Mockito.anyString;
20+
import static org.mockito.Mockito.verify;
1821
import static org.mockito.Mockito.when;
1922

2023
import com.baomidou.mybatisplus.core.metadata.IPage;
@@ -99,11 +102,18 @@ void testGetBlocksById() {
99102
@Test
100103
void testCreateBlocks() {
101104
BlockParam mockParam = new BlockParam();
105+
mockParam.setName("Test Block");
106+
mockParam.setLabel("test-block");
102107
BlockDto mockData = new BlockDto();
108+
mockData.setName("Test Block");
109+
mockData.setLabel("test-block");
103110
when(blockService.createBlock(any(BlockParam.class))).thenReturn(Result.success(mockData));
104111

105112
Result<BlockDto> result = blockController.createBlocks(mockParam);
106113
Assertions.assertEquals(mockData, result.getData());
114+
verify(blockService).createBlock(argThat(param ->
115+
param.getName().equals("Test Block") && param.getLabel().equals("test-block")
116+
));
107117
}
108118

109119
@Test
@@ -179,15 +189,21 @@ void testGetAllBlockCategories() {
179189
Assertions.assertEquals(mockData, result.getData());
180190
}
181191

192+
182193
@Test
183194
void testUpdateBlocks() {
184195
BlockParam blockParam = new BlockParam();
196+
blockParam.setName("Updated Block");
185197
BlockDto returnData = new BlockDto();
198+
returnData.setName("Updated Block");
186199
when(blockService.updateBlockById(any(BlockParam.class), anyInt())).thenReturn(Result.success(returnData));
187-
188200
when(blockService.queryBlockById(anyInt())).thenReturn(returnData);
189201

190202
Result<BlockDto> result = blockController.updateBlocks(blockParam, Integer.valueOf(0), Integer.valueOf(1));
191203
Assertions.assertEquals(returnData, result.getData());
204+
verify(blockService).updateBlockById(argThat(param ->
205+
param.getName().equals("Updated Block") &&
206+
param.getId().equals(0)
207+
), eq(1));
192208
}
193209
}

0 commit comments

Comments
 (0)