diff --git a/.github/workflows/checkstyle.yml b/.github/workflows/checkstyle.yml index e1e7ad25..c9650c31 100644 --- a/.github/workflows/checkstyle.yml +++ b/.github/workflows/checkstyle.yml @@ -22,24 +22,16 @@ jobs: uses: actions/setup-java@v4 with: java-version: '8.*' + distribution: 'temurin' - # 安装 Checkstyle(如果你是用 Maven 或 Gradle) - - name: Install dependencies + # 安装依赖并运行 Checkstyle(如果是 Maven 项目) + - name: Install dependencies and run Checkstyle run: | - ./mvnw install # 如果是 Maven 项目 - # 或者 - # ./gradlew build # 如果是 Gradle 项目 - - # 运行 Checkstyle - - name: Run Checkstyle - run: | - ./mvnw checkstyle:check # 如果是 Maven 项目 - # 或者 - # ./gradlew check # 如果是 Gradle 项目 + mvn checkstyle:check # 查看 Checkstyle 检查报告 - name: Upload Checkstyle report uses: actions/upload-artifact@v4 with: name: checkstyle-report - path: target/checkstyle-result.xml # 如果是 Maven 项目 \ No newline at end of file + path: target/checkstyle-result.xml # 这个路径应该是 Maven 生成的检查报告路径 diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml deleted file mode 100644 index 383e426d..00000000 --- a/.github/workflows/maven.yml +++ /dev/null @@ -1,35 +0,0 @@ -# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time -# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-maven - -# This workflow uses actions that are not certified by GitHub. -# They are provided by a third-party and are governed by -# separate terms of service, privacy policy, and support -# documentation. - -name: Java CI with Maven - -on: - push: - branches: [ "develop" ] - pull_request: - branches: [ "develop" ] - -jobs: - build: - - runs-on: ubuntu-24.04 - - steps: - - uses: actions/checkout@v4 - - name: Set up JDK 8.* - uses: actions/setup-java@v4 - with: - java-version: '8.*' - distribution: 'temurin' - cache: maven - - name: Build with Maven - run: mvn -B package --file pom.xml - - # Optional: Uploads the full dependency graph to GitHub to improve the quality of Dependabot alerts this repository can receive - - name: Update dependency graph - uses: advanced-security/maven-dependency-submission-action@571e99aab1055c2e71a1e2309b9691de18d6b7d6 diff --git a/app/src/main/resources/sql/mysql/create_all_tables_ddl_v1.mysql.sql b/app/src/main/resources/sql/mysql/create_all_tables_ddl_v1.mysql.sql index 80dfe976..e94f5395 100644 --- a/app/src/main/resources/sql/mysql/create_all_tables_ddl_v1.mysql.sql +++ b/app/src/main/resources/sql/mysql/create_all_tables_ddl_v1.mysql.sql @@ -198,7 +198,6 @@ create table `t_block` `platform_id` int not null comment '设计器id', `app_id` int not null comment '创建区块时所在appid', `content_blocks` longtext comment '设计预留字段', - `block_group_id` int comment '区块分组id', `tenant_id` varchar(60) not null comment '租户id', `renter_id` varchar(60) comment '业务租户id', `site_id` varchar(60) comment '站点id,设计预留字段', diff --git a/base/src/main/java/com/tinyengine/it/controller/BlockController.java b/base/src/main/java/com/tinyengine/it/controller/BlockController.java index 54c15b1f..a2cd10f0 100644 --- a/base/src/main/java/com/tinyengine/it/controller/BlockController.java +++ b/base/src/main/java/com/tinyengine/it/controller/BlockController.java @@ -19,6 +19,7 @@ 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; @@ -150,13 +151,13 @@ public Result getBlocksById(@PathVariable Integer id) { /** * 创建block * - * @param blockDto the block dto + * @param blockParam the blockParam * @return BlockDto */ @Operation(summary = "创建block", description = "创建block", parameters = { - @Parameter(name = "blockDto", description = "入参对象") + @Parameter(name = "blockParam", description = "入参对象") }, responses = { @ApiResponse(responseCode = "200", description = "返回信息", @@ -166,8 +167,8 @@ public Result getBlocksById(@PathVariable Integer id) { ) @SystemControllerLog(description = "区块创建api") @PostMapping("/block/create") - public Result createBlocks(@Valid @RequestBody BlockDto blockDto) { - return blockService.createBlock(blockDto); + public Result createBlocks(@Valid @RequestBody BlockParam blockParam) { + return blockService.createBlock(blockParam); } @@ -268,7 +269,7 @@ public Result> allTags() { @GetMapping("/block/notgroup/{groupId}") public Result> findBlocksNotInGroup(@PathVariable Integer groupId, @RequestParam(value = "label_contains", required = false) String label, - @RequestParam(value = "tags_contains", required = false) String [] tags, + @RequestParam(value = "tags_contains", required = false) String[] tags, @RequestParam(value = "createdBy", required = false) String createdBy) { NotGroupDto notGroupDto = new NotGroupDto(); notGroupDto.setGroupId(groupId); @@ -378,14 +379,14 @@ public Result> getBlockGroups( /** * 修改block * - * @param blockDto blockDto + * @param blockParam blockParam * @param id id * @return block dto */ @Operation(summary = "修改区块", description = "修改区块", parameters = { - @Parameter(name = "blockDto", description = "入参对象"), + @Parameter(name = "blockParam", description = "入参对象"), @Parameter(name = "id", description = "区块id") }, responses = { @@ -396,10 +397,10 @@ public Result> getBlockGroups( ) @SystemControllerLog(description = "区块修改api") @PostMapping("/block/update/{id}") - public Result updateBlocks(@Valid @RequestBody BlockDto blockDto, @PathVariable Integer id, + public Result updateBlocks(@Valid @RequestBody BlockParam blockParam, @PathVariable Integer id, @RequestParam(value = "appId", required = false) Integer appId) { - blockDto.setId(id); - return blockService.updateBlockById(blockDto, appId); + blockParam.setId(id); + return blockService.updateBlockById(blockParam, appId); } /** diff --git a/base/src/main/java/com/tinyengine/it/model/dto/BlockDto.java b/base/src/main/java/com/tinyengine/it/model/dto/BlockDto.java index b0c0fc6e..b60fa9d1 100644 --- a/base/src/main/java/com/tinyengine/it/model/dto/BlockDto.java +++ b/base/src/main/java/com/tinyengine/it/model/dto/BlockDto.java @@ -18,6 +18,7 @@ import com.tinyengine.it.common.base.BaseEntity; import com.tinyengine.it.common.handler.ListTypeHandler; import com.tinyengine.it.common.handler.MapTypeHandler; +import com.tinyengine.it.model.entity.BlockGroup; import com.tinyengine.it.model.entity.BlockHistory; import com.tinyengine.it.model.entity.User; @@ -133,7 +134,7 @@ public class BlockDto extends BaseEntity { @TableField(exist = false) @Schema(name = "groups", type = " List", description = "区块分组") - private List groups = new ArrayList<>(); + private List groups = new ArrayList<>(); @TableField(exist = false) @Schema(name = "histories", type = " List", description = "区块历史") diff --git a/base/src/main/java/com/tinyengine/it/model/dto/BlockParam.java b/base/src/main/java/com/tinyengine/it/model/dto/BlockParam.java new file mode 100644 index 00000000..93b4f168 --- /dev/null +++ b/base/src/main/java/com/tinyengine/it/model/dto/BlockParam.java @@ -0,0 +1,164 @@ +/** + * 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.model.dto; + +import com.baomidou.mybatisplus.annotation.TableField; +import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.tinyengine.it.common.base.BaseEntity; +import com.tinyengine.it.common.handler.ListTypeHandler; +import com.tinyengine.it.common.handler.MapTypeHandler; +import com.tinyengine.it.model.entity.BlockHistory; +import com.tinyengine.it.model.entity.User; +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Map; + +/** + *

+ * 区块param + *

+ * This class serves as a parameter object for block creation and updates, + * working in conjunction with {@link BlockDto}. While BlockDto represents + * the full block data model, BlockParam is specifically designed for + * handling input parameters during block operations. + * @author lu-yg + * @since 2024-01-27 + */ +@Data +public class BlockParam extends BaseEntity { + private static final long serialVersionUID = 1L; + + @Schema(name = "label", description = "区块显示名称,严格大小写格式") + private String label; + + @Schema(name = "name", description = "区块名称") + @JsonProperty("name_cn") + private String name; + + @Schema(name = "framework", description = "技术栈") + private String framework; + + @TableField(typeHandler = JacksonTypeHandler.class) + @Schema(name = "assets", description = "构建资源") + private Map assets; + + @JsonProperty("last_build_info") + @TableField(typeHandler = JacksonTypeHandler.class) + @Schema(name = "lastBuildInfo", description = "最新一次构建信息") + private Map lastBuildInfo; + + @TableField(typeHandler = JacksonTypeHandler.class) + @Schema(name = "content", description = "区块内容") + private Map content; + + @Schema(name = "description", description = "描述") + private String description; + + @Schema(name = "tags", description = "标签") + @TableField(typeHandler = ListTypeHandler.class) + private List tags; + + @Schema(name = "latestHistoryId", description = "当前历史记录表ID") + @JsonProperty("current_history") + private BlockHistory latestHistoryId; + + @Schema(name = "screenshot", description = "截屏") + private String screenshot; + + @Schema(name = "path", description = "区块路径") + private String path; + + @Schema(name = "latestVersion", description = "当前历史记录表最新版本") + @JsonProperty("version") + private String latestVersion; + + @Schema(name = "occupierId", description = "当前锁定人id") + private String occupierId; + + @Schema(name = "isOfficial", description = "是否是官方") + @JsonProperty("is_official") + private Boolean isOfficial; + + @Schema(name = "isDefault", description = "是否是默认") + @JsonProperty("is_default") + private Boolean isDefault; + + @Schema(name = "tinyReserved", description = "是否是tiny专有") + @JsonProperty("tiny_reserved") + private Boolean isTinyReserved; + + @Schema(name = "npmName", description = "npm包名") + @JsonProperty("npm_name") + private String npmName; + + @Schema(name = "public", description = "公开状态:0,1,2") + @JsonProperty("public") + private Integer publicStatus; + + @Schema(name = "i18n", description = "国际化") + @TableField(typeHandler = MapTypeHandler.class) + private Map> i18n; + + @Schema(name = "appId", description = "创建区块时所在appId") + @JsonProperty("created_app") + private Integer appId; + + @Schema(name = "contentBlocks", description = "*设计预留字段用途*") + @JsonProperty("content_blocks") + private String contentBlocks; + + @Schema(name = "platformId", description = "设计器ID") + @JsonProperty("platform_id") + private Integer platformId; + + @JsonProperty("occupier") + @Schema(name = "occupierBy", description = "当前锁定人") + private User occupier; + + @TableField(exist = false) + @JsonProperty("public_scope_tenants") + private List publicScopeTenants = new ArrayList<>(); + + @TableField(exist = false) + @Schema(name = "groups", type = " List", description = "区块分组") + private List groups = new ArrayList<>(); + + @TableField(exist = false) + @Schema(name = "histories", type = " List", description = "区块历史") + private List histories = new ArrayList<>(); + + @TableField(exist = false) + @JsonProperty("histories_length") + private Integer historiesLength = 0; + + @TableField(exist = false) + @JsonProperty("is_published") + private Boolean isPublished; + + @TableField(exist = false) + @JsonProperty("current_version") + private String currentVersion; + @JsonProperty("public") + public Integer getPublic() { + if (this.publicStatus != null && !Arrays.asList(0, 1, 2).contains(this.publicStatus)) { + throw new IllegalStateException("Invalid public status value: " + this.publicStatus); + } + return this.publicStatus; + } +} + diff --git a/base/src/main/java/com/tinyengine/it/service/material/BlockService.java b/base/src/main/java/com/tinyengine/it/service/material/BlockService.java index 32cb4df7..7d1e9e7c 100644 --- a/base/src/main/java/com/tinyengine/it/service/material/BlockService.java +++ b/base/src/main/java/com/tinyengine/it/service/material/BlockService.java @@ -16,6 +16,7 @@ import com.tinyengine.it.common.base.Result; 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; @@ -66,18 +67,18 @@ public interface BlockService { /** * 根据主键id更新表t_block信息 * - * @param blockDto the block dto + * @param blockParam the block param * @return the BlockDto */ - Result updateBlockById(BlockDto blockDto, Integer appId); + Result updateBlockById(BlockParam blockParam, Integer appId); /** * 新增表t_block数据 * - * @param blockDto the block dto + * @param blockParam the blockParam * @return the result */ - Result createBlock(BlockDto blockDto); + Result createBlock(BlockParam blockParam); /** * 区块分页查询 diff --git a/base/src/main/java/com/tinyengine/it/service/material/impl/BlockServiceImpl.java b/base/src/main/java/com/tinyengine/it/service/material/impl/BlockServiceImpl.java index d2eb05b8..1a2d9603 100644 --- a/base/src/main/java/com/tinyengine/it/service/material/impl/BlockServiceImpl.java +++ b/base/src/main/java/com/tinyengine/it/service/material/impl/BlockServiceImpl.java @@ -32,6 +32,7 @@ import com.tinyengine.it.mapper.UserMapper; 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.I18nEntryDto; import com.tinyengine.it.model.dto.NotGroupDto; @@ -56,7 +57,6 @@ import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; @@ -93,6 +93,8 @@ public class BlockServiceImpl implements BlockService { @Autowired private BlockGroupBlockMapper blockGroupBlockMapper; + private static final int DEFAULT_PLATFORM_ID = 1; + private static final String DEFAULT_USER_ID = "1"; /** * 查询表t_block所有数据 * @@ -119,6 +121,8 @@ public BlockDto queryBlockById(@Param("id") Integer id) { && blockDto.getLastBuildInfo().get("result") instanceof Boolean ? (Boolean) blockDto.getLastBuildInfo().get("result") : Boolean.FALSE; blockDto.setIsPublished(isPublished); + List groups = blockGroupMapper.findBlockGroupByBlockId(blockDto.getId(), DEFAULT_USER_ID); + blockDto.setGroups(groups); return blockDto; } @@ -147,85 +151,89 @@ public Integer deleteBlockById(@Param("id") Integer id) { /** * 根据主键id更新表t_block数据 * - * @param blockDto blockDto + * @param blockParam blockParam * @return blockDto */ @Override - public Result updateBlockById(BlockDto blockDto, Integer appId) { - Block blockResult = blockMapper.queryBlockById(blockDto.getId()); - + public Result updateBlockById(BlockParam blockParam, Integer appId) { + if (blockParam == null || blockParam.getId() == null) { + return Result.failed(ExceptionEnum.CM002); + } + Block blockResult = blockMapper.queryBlockById(blockParam.getId()); + if (blockResult == null) { + return Result.failed(ExceptionEnum.CM001); + } if (!Objects.equals(blockResult.getAppId(), appId)) { return Result.failed(ExceptionEnum.CM007); } // 把前端传参赋值给实体 Block blocks = new Block(); - BeanUtils.copyProperties(blockDto, blocks); - blocks.setOccupierBy(String.valueOf(1)); - if (blockDto.getLatestHistoryId() != null) { - blocks.setLatestHistoryId(blockDto.getLatestHistoryId().getId()); + BeanUtils.copyProperties(blockParam, blocks); + blocks.setOccupierBy(DEFAULT_USER_ID); + if (blockParam.getLatestHistoryId() != null) { + blocks.setLatestHistoryId(blockParam.getLatestHistoryId().getId()); } // 处理区块截图 - if (blockDto.getScreenshot() != null && blockDto.getLabel() != null) { + if (blockParam.getScreenshot() != null && blockParam.getLabel() != null) { // 图片上传,此处给默认值空字符 blocks.setScreenshot(""); } - Integer result; - if (blockDto.getGroups() == null || blockDto.getGroups().isEmpty()) { + + if (blockParam.getGroups() == null) { blockMapper.updateBlockById(blocks); BlockDto blockDtoResult = queryBlockById(blocks.getId()); return Result.success(blockDtoResult); } - // 过滤出 Integer 类型的对象 - // 转换为 Integer 类型 - // 收集为 List; - List groups = blockDto.getGroups().stream() - .filter(obj -> obj instanceof Integer) - .map(obj -> (Integer) obj) - .collect(Collectors.toList()); - - // 对接登录后获取用户id - String createdBy = "1"; // 根据区块id获取区块所在分组 - List blockGroups = blockGroupMapper.findBlockGroupByBlockId(blocks.getId(), createdBy); - if (!blockGroups.isEmpty()) { + List blockGroups = blockGroupMapper.findBlockGroupByBlockId(blocks.getId(), DEFAULT_USER_ID); + // 删除区块与分组关系 + if(blockGroups != null && !blockGroups.isEmpty()){ List blockGroupIds = blockGroups.stream().map(BlockGroup::getId).collect(Collectors.toList()); for (Integer id : blockGroupIds) { blockGroupBlockMapper.deleteByGroupIdAndBlockId(id, blocks.getId()); } } + // 更新区块 + blockMapper.updateBlockById(blocks); + BlockDto blockDtoResult = new BlockDto(); + // 参数存在区块分组且无值 + if (blockParam.getGroups().isEmpty()) { + blockDtoResult = queryBlockById(blocks.getId()); + return Result.success(blockDtoResult); + } - for (Integer groupId : groups) { + for (Integer groupId : blockParam.getGroups()) { BlockGroupBlock blockGroupBlock = new BlockGroupBlock(); - blockGroupBlock.setBlockId(blockDto.getId()); + blockGroupBlock.setBlockId(blockParam.getId()); blockGroupBlock.setBlockGroupId(groupId); blockGroupBlockMapper.createBlockGroupBlock(blockGroupBlock); } - - blockMapper.updateBlockById(blocks); - - BlockDto blockDtoResult = queryBlockById(blocks.getId()); + blockDtoResult = queryBlockById(blocks.getId()); return Result.success(blockDtoResult); } /** * 新增表t_block数据 * - * @param blockDto the block dto + * @param blockParam the blockParam * @return execute success the result */ @Override - public Result createBlock(BlockDto blockDto) { + public Result createBlock(BlockParam blockParam) { + if (blockParam == null || blockParam.getLabel() == null) { + return Result.failed(ExceptionEnum.CM002); + } // 对接收到的参数occupier为对应的一个对象,进行特殊处理并重新赋值 Block blocks = new Block(); - if (blockDto.getOccupier() != null) { - blocks.setOccupierBy(String.valueOf(blockDto.getOccupier().getId())); + if (blockParam.getOccupier() != null) { + blocks.setOccupierBy(String.valueOf(blockParam.getOccupier().getId())); } - BeanUtils.copyProperties(blockDto, blocks); + BeanUtils.copyProperties(blockParam, blocks); blocks.setIsDefault(false); blocks.setIsOfficial(false); - blocks.setPlatformId(1); // 新建区块给默认值 + blocks.setPlatformId(DEFAULT_PLATFORM_ID); // 新建区块给默认值 int result = blockMapper.createBlock(blocks); if (result < 1) { @@ -233,13 +241,18 @@ public Result createBlock(BlockDto blockDto) { } int id = blocks.getId(); BlockDto blocksResult = queryBlockById(id); - List groups = blockDto.getGroups(); - if (!groups.isEmpty() && groups.get(0) instanceof Integer) { - Integer groupId = (Integer) groups.get(0); // 强制类型转换 - BlockGroupBlock blockGroupBlock = new BlockGroupBlock(); - blockGroupBlock.setBlockGroupId(groupId); - blockGroupBlock.setBlockId(id); - blockGroupBlockMapper.createBlockGroupBlock(blockGroupBlock); + List groups = blockParam.getGroups(); + if (groups == null || groups.isEmpty()) { + return Result.success(blocksResult); + } + + Integer groupId = groups.get(0); // 强制类型转换 + BlockGroupBlock blockGroupBlock = new BlockGroupBlock(); + blockGroupBlock.setBlockGroupId(groupId); + blockGroupBlock.setBlockId(id); + int groupResult = blockGroupBlockMapper.createBlockGroupBlock(blockGroupBlock); + if (groupResult < 1) { + return Result.failed(ExceptionEnum.CM001); } return Result.success(blocksResult); } @@ -433,18 +446,14 @@ public List allTags() { @SystemServiceLog(description = "getNotInGroupBlocks 获取不在分组内的区块 实现类") @Override public List getNotInGroupBlocks(NotGroupDto notGroupDto) { - // 获取缓存中的登录用户 - int userId = 1; - User user = userMapper.queryUserById(userId); List blocksList = blockMapper.findBlocksReturn(notGroupDto); if (blocksList == null || blocksList.isEmpty()) { return blocksList; } for (BlockDto blockDto : blocksList) { - List blockGroups = blockGroupMapper.findBlockGroupByBlockId(blockDto.getId(), String.valueOf(userId)); - List objectGroups = new ArrayList<>(blockGroups); - blockDto.setGroups(objectGroups); + List blockGroups = blockGroupMapper.findBlockGroupByBlockId(blockDto.getId(), DEFAULT_USER_ID); + blockDto.setGroups(blockGroups); } return blocksList.stream() .filter(item -> @@ -455,15 +464,15 @@ public List getNotInGroupBlocks(NotGroupDto notGroupDto) { } // 组过滤 if (item.getGroups() != null && item.getGroups().stream() - .anyMatch(group -> group instanceof BlockGroup - && ((BlockGroup) group).getId().equals(notGroupDto.getGroupId()))) { + .anyMatch(group -> group != null + && group.getId().equals(notGroupDto.getGroupId()))) { return false; } // 公开范围过滤 if (item.getPublicStatus() == Enums.Scope.FULL_PUBLIC.getValue()) { return true; } - return user != null && item.getPublicStatus() == Enums.Scope.PUBLIC_IN_TENANTS.getValue(); + return item.getPublicStatus() == Enums.Scope.PUBLIC_IN_TENANTS.getValue(); }) .collect(Collectors.toList()); } @@ -497,6 +506,9 @@ public Result getBlockByLabel(String label, Integer appId) { */ @Override public Result deploy(BlockBuildDto blockBuildDto) { + if (blockBuildDto == null || blockBuildDto.getBlock() == null) { + return Result.failed(ExceptionEnum.CM002); + } Map content = blockBuildDto.getBlock().getContent(); if (content.isEmpty()) { return Result.failed(ExceptionEnum.CM204); @@ -508,44 +520,46 @@ public Result deploy(BlockBuildDto blockBuildDto) { if (isHistory) { return Result.failed(ExceptionEnum.CM205); } - BlockDto blockDto = blockBuildDto.getBlock(); - List i18nList = i18nEntryMapper.findI18nEntriesByHostandHostType(id, "block"); - // 序列化国际化词条 - SchemaI18n appEntries = i18nEntryService.formatEntriesList(i18nList); - BlockHistory blockHistory = new BlockHistory(); - blockDto.setCreatedTime(null); - blockDto.setLastUpdatedTime(null); - blockDto.setTenantId(null); - Map> i18n = new HashMap<>(); - i18n.put("zh_CN", appEntries.getZhCn()); - i18n.put("en_US", appEntries.getEnUs()); - - blockDto.setI18n(i18n); - blockHistory.setIsPublic(blockDto.getPublic()); - BeanUtil.copyProperties(blockDto, blockHistory); - blockHistory.setRefId(id); - blockHistory.setVersion(blockBuildDto.getVersion()); - blockHistory.setMessage(blockBuildDto.getDeployInfo()); - Map buildInfo = new HashMap<>(); - buildInfo.put("result", true); - buildInfo.put("versions", Arrays.asList(blockBuildDto.getVersion())); - // 获取当前时间 - LocalDateTime now = LocalDateTime.now(); - // 使用自定义格式化输出 - DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); - String formattedDate = now.format(formatter); - buildInfo.put("endTime", formattedDate); - blockHistory.setBuildInfo(buildInfo); - blockHistory.setId(null); - int blockHistoryResult = blockHistoryMapper.createBlockHistory(blockHistory); - if (blockHistoryResult < 1) { - return Result.failed(ExceptionEnum.CM008); - } - blockDto.setLastBuildInfo(buildInfo); - blockDto.setLatestHistoryId(blockHistory); - - return updateBlockById(blockDto, blockDto.getAppId()); + try { + BlockDto blockDto = blockBuildDto.getBlock(); + List i18nList = i18nEntryMapper.findI18nEntriesByHostandHostType(id, "block"); + // 序列化国际化词条 + SchemaI18n appEntries = i18nEntryService.formatEntriesList(i18nList); + BlockHistory blockHistory = new BlockHistory(); + blockDto.setCreatedTime(null); + blockDto.setLastUpdatedTime(null); + blockDto.setTenantId(null); + Map> i18n = new HashMap<>(); + i18n.put("zh_CN", appEntries.getZhCn()); + i18n.put("en_US", appEntries.getEnUs()); + + blockDto.setI18n(i18n); + blockHistory.setIsPublic(blockDto.getPublic()); + BeanUtil.copyProperties(blockDto, blockHistory); + blockHistory.setRefId(id); + blockHistory.setVersion(blockBuildDto.getVersion()); + blockHistory.setMessage(blockBuildDto.getDeployInfo()); + + // 获取当前时间 + LocalDateTime now = LocalDateTime.now(); + Map buildInfo = createBuildInfo(blockBuildDto.getVersion(), now); + blockHistory.setBuildInfo(buildInfo); + blockHistory.setId(null); + int blockHistoryResult = blockHistoryMapper.createBlockHistory(blockHistory); + if (blockHistoryResult < 1) { + return Result.failed(ExceptionEnum.CM008); + } + BlockParam blockParam = new BlockParam(); + blockParam.setLastBuildInfo(buildInfo); + blockParam.setLatestHistoryId(blockHistory); + blockParam.setLatestVersion(blockHistory.getVersion()); + blockParam.setId(blockDto.getId()); + + return updateBlockById(blockParam, blockDto.getAppId()); + } catch (Exception e) { + return Result.failed(ExceptionEnum.CM001); + } } /** @@ -624,10 +638,9 @@ public Result> listNew(String appId, String groupId) { } } List blocksList = new ArrayList<>(); - String createdBy = "1"; // 获取用户登录id // 如果有 groupId, 只查group下的block,以及自己创建的区块 if (groupIdTemp != 0) { - blocksList = blockMapper.findBlockByBlockGroupId(groupIdTemp, createdBy); + blocksList = blockMapper.findBlockByBlockGroupId(groupIdTemp, DEFAULT_USER_ID); return Result.success(blocksList); } // 如果没有 groupId @@ -639,7 +652,7 @@ public Result> listNew(String appId, String groupId) { List appBlocks = blocksList; // 通过createBy查询区块表数据 Block blocks = new Block(); - blocks.setCreatedBy(createdBy); + blocks.setCreatedBy(DEFAULT_USER_ID); List personalBlocks = queryBlockByCondition(blocks); List retBlocks = new ArrayList<>(); // 合并 personalBlocks 和 appBlocks 数组 @@ -675,19 +688,26 @@ public Result> listNew(String appId, String groupId) { * @return the id */ public int ensureBlockId(BlockDto blockDto) { + log.debug("Ensuring block ID for label: {}", blockDto.getLabel()); if (blockDto.getId() != null) { + log.debug("Block ID already exists: {}", blockDto.getId()); return blockDto.getId(); } // 查询当前用户信息 - int userId = 86; Block queryBlock = new Block(); queryBlock.setLabel(blockDto.getLabel()); queryBlock.setFramework(blockDto.getFramework()); - queryBlock.setCreatedBy(String.valueOf(userId)); + queryBlock.setCreatedBy(DEFAULT_USER_ID); List blockList = blockMapper.queryBlockByCondition(queryBlock); + List groups = blockDto.getGroups().stream().map(BlockGroup::getId).collect(Collectors.toList()); + ; + blockDto.setGroups(null); + BlockParam blockParam = new BlockParam(); + BeanUtil.copyProperties(blockDto, blockParam); + blockParam.setGroups(groups); if (blockList.isEmpty()) { - createBlock(blockDto); - return blockDto.getId(); + createBlock(blockParam); + return blockParam.getId(); } return blockList.get(0).getId(); @@ -710,4 +730,20 @@ public boolean isHistoryExisted(Integer id, String version) { } return true; } + + /** + * 创建构建信息 + * + * @param version the id + * @param buildTime the buildTime + * @return buildInfo the buildInfo + */ + private Map createBuildInfo(String version, LocalDateTime buildTime) { + Map buildInfo = new HashMap<>(); + buildInfo.put("result", true); + buildInfo.put("versions", Collections.singletonList(version)); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); + buildInfo.put("endTime", buildTime.format(formatter)); + return buildInfo; + } } diff --git a/base/src/test/java/com/tinyengine/it/controller/BlockControllerTest.java b/base/src/test/java/com/tinyengine/it/controller/BlockControllerTest.java index ba0bb146..1dc58ef6 100644 --- a/base/src/test/java/com/tinyengine/it/controller/BlockControllerTest.java +++ b/base/src/test/java/com/tinyengine/it/controller/BlockControllerTest.java @@ -12,9 +12,12 @@ package com.tinyengine.it.controller; +import static org.mockito.ArgumentMatchers.argThat; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.any; import static org.mockito.Mockito.anyInt; import static org.mockito.Mockito.anyString; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import com.baomidou.mybatisplus.core.metadata.IPage; @@ -23,6 +26,7 @@ import com.tinyengine.it.mapper.BlockMapper; import com.tinyengine.it.mapper.TenantMapper; 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.entity.Block; import com.tinyengine.it.model.entity.Tenant; @@ -97,11 +101,19 @@ void testGetBlocksById() { @Test void testCreateBlocks() { + BlockParam mockParam = new BlockParam(); + mockParam.setName("Test Block"); + mockParam.setLabel("test-block"); BlockDto mockData = new BlockDto(); - when(blockService.createBlock(any(BlockDto.class))).thenReturn(Result.success(mockData)); + mockData.setName("Test Block"); + mockData.setLabel("test-block"); + when(blockService.createBlock(any(BlockParam.class))).thenReturn(Result.success(mockData)); - Result result = blockController.createBlocks(new BlockDto()); + Result result = blockController.createBlocks(mockParam); Assertions.assertEquals(mockData, result.getData()); + verify(blockService).createBlock(argThat(param -> + param.getName().equals("Test Block") && param.getLabel().equals("test-block") + )); } @Test @@ -177,14 +189,21 @@ void testGetAllBlockCategories() { Assertions.assertEquals(mockData, result.getData()); } + @Test void testUpdateBlocks() { + BlockParam blockParam = new BlockParam(); + blockParam.setName("Updated Block"); BlockDto returnData = new BlockDto(); - when(blockService.updateBlockById(any(BlockDto.class), anyInt())).thenReturn(Result.success(returnData)); - + returnData.setName("Updated Block"); + when(blockService.updateBlockById(any(BlockParam.class), anyInt())).thenReturn(Result.success(returnData)); when(blockService.queryBlockById(anyInt())).thenReturn(returnData); - Result result = blockController.updateBlocks(returnData, Integer.valueOf(0), Integer.valueOf(1)); + Result result = blockController.updateBlocks(blockParam, Integer.valueOf(0), Integer.valueOf(1)); Assertions.assertEquals(returnData, result.getData()); + verify(blockService).updateBlockById(argThat(param -> + param.getName().equals("Updated Block") && + param.getId().equals(0) + ), eq(1)); } } diff --git a/base/src/test/java/com/tinyengine/it/service/material/impl/BlockServiceImplTest.java b/base/src/test/java/com/tinyengine/it/service/material/impl/BlockServiceImplTest.java index 741e9c76..f4469344 100644 --- a/base/src/test/java/com/tinyengine/it/service/material/impl/BlockServiceImplTest.java +++ b/base/src/test/java/com/tinyengine/it/service/material/impl/BlockServiceImplTest.java @@ -28,6 +28,7 @@ import com.tinyengine.it.mapper.BlockMapper; import com.tinyengine.it.mapper.UserMapper; 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.App; @@ -114,28 +115,30 @@ void testCreateBlock() { BlockDto t = new BlockDto(); t.setName("test"); when(blockMapper.findBlockAndGroupAndHistoByBlockId(any())).thenReturn(t); - BlockDto blockDto = new BlockDto(); - blockDto.setId(1); - blockDto.setScreenshot("aa"); - blockDto.setLabel("bb"); - blockDto.setFramework("cc"); - blockDto.setPlatformId(1); - blockDto.setAppId(1); - blockDto.setName("testBlock"); - Result result = blockServiceImpl.createBlock(blockDto); + BlockParam blockParam = new BlockParam(); + blockParam.setId(0); + // Add test cases for required fields + blockParam.setName("test"); + blockParam.setLabel("test-label"); + blockParam.setFramework("vue"); + Result result = blockServiceImpl.createBlock(blockParam); Assertions.assertEquals("test", result.getData().getName()); + // Test validation failure + BlockParam invalidParam = new BlockParam(); + Result invalidResult = blockServiceImpl.createBlock(invalidParam); + Assertions.assertFalse(invalidResult.isSuccess()); } @Test void testUpdateBlockById() { - BlockDto blockDto = new BlockDto(); + BlockParam blockParam = new BlockParam(); when(blockMapper.updateBlockById(any())).thenReturn(1); when(blockMapper.findBlockAndGroupAndHistoByBlockId(anyInt())).thenReturn(new BlockDto()); Block block = new Block(); block.setAppId(1); - when(blockMapper.queryBlockById(blockDto.getId())).thenReturn(block); + when(blockMapper.queryBlockById(blockParam.getId())).thenReturn(block); - Result result = blockServiceImpl.updateBlockById(blockDto, 1); + Result result = blockServiceImpl.updateBlockById(blockParam, 1); Assertions.assertEquals(null, result.getData()); }