diff --git a/README.md b/README.md index 014ff90..3e4ef51 100644 --- a/README.md +++ b/README.md @@ -19,11 +19,13 @@ Java 21 / Spring Boot 3.4.4 / MyBatis-Plus 后台多模块项目。后台只保 运行代码只访问 `component`、`sys_user`、`sys_operation_log` 三张业务表。使用已重建的组件表和通用操作日志表;`sys_user` 结构和用户管理行为保持不变。 -组件存储基本信息、零件参数、两组自定义字段、STEP 对象键、URDF 目录及实际文件清单、`version` 和维护时间。通用日志存储模块、动作、目标快照、操作人、请求元信息和结果,不依赖组件专属列。 +组件存储基本信息、零件参数、两组自定义字段、STEP 对象键、URDF 目录及实际文件清单、`version`、`deleted` 删除标记和维护时间。通用日志存储模块、动作、目标快照、操作人、请求元信息和结果,不依赖组件专属列。 应用启动不建表、不迁移旧数据。本次未修改 `.sql` 文件;仓库中历史 SQL 不代表当前组件和日志结构,请使用已经建好的表。MySQL 需支持现有建表语句中的 JSON / TEXT 表达式默认值。 -组件名称允许重复,编码唯一且不可修改。组件物理删除,用户逻辑删除;已删除用户的手机号仍被占用。组件替换资源或删除记录均不自动删除 OSS 文件。 +组件名称允许重复,编码唯一且不可修改。组件和用户均逻辑删除;已删除组件的编码和已删除用户的手机号仍被占用。组件替换资源或逻辑删除均不自动删除 OSS 文件。 + +部署前需为 `component` 表添加 `deleted TINYINT UNSIGNED NOT NULL DEFAULT 0` 字段,中文注释为“删除标记:0-未删除,1-已删除”。已有记录默认未删除;运行代码通过 MyBatis-Plus 自动过滤已删除组件,列表、详情、修改和编辑上传签名均只处理未删除记录。 ## 配置与启动 @@ -77,7 +79,7 @@ Spring Boot 不自动读取 `.env`,需要通过 shell 导入。默认 profile | --- | --- | | /admin-api/storage/upload-signature | POST,新增签名、编辑签名、续签 | | /admin-api/components | GET 分页列表,POST 新增 | -| /admin-api/components/{id} | GET 详情,PUT 全量表单修改,DELETE 删除 | +| /admin-api/components/{id} | GET 详情,PUT 全量表单修改,DELETE 逻辑删除 | | /admin-api/component-assets/{objectKey} | GET 下载资源 | | /admin-api/users | GET 分页列表,POST 新增 | | /admin-api/users/{id} | GET 详情,PUT 修改,DELETE 逻辑删除 | @@ -105,7 +107,7 @@ mvn -pl cadworld-adminapi -am clean test mvn -pl cadworld-adminapi -am -DskipTests package ``` -自动测试通过 Java 在 H2 创建临时表,使用内存 OSS,不连接业务 MySQL、不执行业务数据库 DDL。覆盖签名与续签、资源校验及替换、默认值、自定义字段、版本冲突、审计、删除快照、旧路由 404 和用户管理回归,并校验 OSS V4 签名策略。 +自动测试通过 Java 在 H2 创建临时表,使用内存 OSS,不连接业务 MySQL、不执行业务数据库 DDL。覆盖签名与续签、资源校验及替换、默认值、自定义字段、版本冲突、审计、逻辑删除及目标快照、旧路由 404 和用户管理回归,并校验 OSS V4 签名策略。 真实 OSS 的独立测试需显式配置凭证并启用 `oss-integration` profile: diff --git a/cadworld-adminapi/docs/admin-api.md b/cadworld-adminapi/docs/admin-api.md index b3b02eb..d0fef4e 100644 --- a/cadworld-adminapi/docs/admin-api.md +++ b/cadworld-adminapi/docs/admin-api.md @@ -31,7 +31,7 @@ JSON 请求设置 `Content-Type: application/json`。当前没有登录认证, | POST | /admin-api/components | 新增组件,校验上传文件后入库 | | GET | /admin-api/components/{id} | 组件完整详情 | | PUT | /admin-api/components/{id} | 全量更新表单,可替换资源 | -| DELETE | /admin-api/components/{id} | 删除组件记录 | +| DELETE | /admin-api/components/{id} | 逻辑删除组件 | | GET | /admin-api/component-assets/{objectKey} | 下载资源文件 | | GET | /admin-api/users | 用户分页查询 | | POST | /admin-api/users | 创建用户 | @@ -197,7 +197,7 @@ PUT 是全量表单更新,前端应提交完整表单。省略可选字段会 } ``` -成功返回完整组件详情,初始 `version=1`。重复使用已保存编码新增返回 409。 +成功返回完整组件详情,初始 `version=1`。重复使用已保存编码新增返回 409,包含已逻辑删除组件的编码。 ### 5.2 详情及保存响应 @@ -283,7 +283,9 @@ PUT 是全量表单更新,前端应提交完整表单。省略可选字段会 ### 5.5 删除和下载 -`DELETE /admin-api/components/123` 成功返回 `data={"success":true}`。物理删除组件记录,保留删除前的日志目标快照和 OSS 文件。找不到组件返回 404。 +`DELETE /admin-api/components/123` 成功返回 `data={"success":true}`。将组件的 `deleted` 标记从 `0` 改为 `1`,保留数据库记录、删除前的日志目标快照和 OSS 文件。找不到组件或组件已删除时返回 404。 + +已删除组件不出现在列表和详情中,不能修改、申请编辑上传签名或续签绑定该组件的上传会话。组件编码仍保持唯一,不能复用原新增会话重新创建同编码组件。删除标记仅用于数据库内部,不增加到接口响应中。 使用详情中的 `stepUrl`、`urdfUrl` 或依赖文件 URL 直接 GET 下载。下载成功返回原文件流,不包 JSON;对象不存在返回 404。对象键区分大小写且必须属于当前环境根目录。 diff --git a/cadworld-adminapi/src/main/java/com/linkerbot/cadworld/controller/ComponentController.java b/cadworld-adminapi/src/main/java/com/linkerbot/cadworld/controller/ComponentController.java index 15b6a74..3af16f1 100644 --- a/cadworld-adminapi/src/main/java/com/linkerbot/cadworld/controller/ComponentController.java +++ b/cadworld-adminapi/src/main/java/com/linkerbot/cadworld/controller/ComponentController.java @@ -54,7 +54,7 @@ public class ComponentController { } @AuditOperation(module = "COMPONENT", action = "DELETE", targetType = "COMPONENT") - @ApiRequestLog("删除组件") + @ApiRequestLog("逻辑删除组件") @DeleteMapping("/{id:[0-9]+}") public ApiResponse delete(@PathVariable @Min(1) Long id) { componentService.deleteComponent(operatorProvider.userId(), id); diff --git a/cadworld-adminapi/src/test/java/com/linkerbot/cadworld/AdminApiTest.java b/cadworld-adminapi/src/test/java/com/linkerbot/cadworld/AdminApiTest.java index 8603d88..a5bcfc9 100644 --- a/cadworld-adminapi/src/test/java/com/linkerbot/cadworld/AdminApiTest.java +++ b/cadworld-adminapi/src/test/java/com/linkerbot/cadworld/AdminApiTest.java @@ -3,11 +3,14 @@ package com.linkerbot.cadworld; import ch.qos.logback.classic.Logger; import ch.qos.logback.classic.spi.ILoggingEvent; import ch.qos.logback.core.read.ListAppender; +import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.linkerbot.cadworld.common.api.OperationTargetHolder; +import com.linkerbot.cadworld.entity.Component; import com.linkerbot.cadworld.entity.OperationLog; import com.linkerbot.cadworld.logging.ApiRequestLogCapture; +import com.linkerbot.cadworld.mapper.ComponentMapper; import com.linkerbot.cadworld.service.OperationLogService; import com.linkerbot.cadworld.service.support.ComponentUploadSessionTokenProvider; import com.linkerbot.cadworld.service.support.ComponentUploadSessionTokenProvider.UploadSessionClaims; @@ -49,6 +52,7 @@ class AdminApiTest { @Autowired InMemoryObjectStorageConfiguration.InMemoryObjectStorageClient storage; @Autowired ComponentUploadSessionTokenProvider tokens; @Autowired OperationLogService logs; + @Autowired ComponentMapper componentMapper; @Autowired @org.springframework.beans.factory.annotation.Qualifier("requestMappingHandlerMapping") org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping mappings; @@ -395,6 +399,15 @@ class AdminApiTest { String code = created.path("componentCode").asText(); mvc.perform(delete("/admin-api/components/{id}",id).header("X-Request-Id","delete-snapshot")) .andExpect(status().isOk()); + assertThat(count("component")).isEqualTo(1); + assertThat(jdbc.queryForObject("SELECT deleted FROM component WHERE id=?", Integer.class, id)).isEqualTo(1); + assertThat(jdbc.queryForObject("SELECT component_code FROM component WHERE id=?", String.class, id)).isEqualTo(code); + assertThat(jdbc.queryForObject("SELECT component_name FROM component WHERE id=?", String.class, id)).isEqualTo("测试组件"); + mvc.perform(get("/admin-api/components")) + .andExpect(jsonPath("$.data.total").value(0)) + .andExpect(jsonPath("$.data.items").isEmpty()); + assertThat(storage.objectExists(created.path("stepObjectKey").asText())).isTrue(); + assertThat(storage.objectExists(created.path("urdfMainObjectKey").asText())).isTrue(); mvc.perform(get("/admin-api/components/{id}",id)).andExpect(status().isNotFound()); mvc.perform(delete("/admin-api/components/{id}",id)).andExpect(status().isNotFound()); assertThat(count("sys_operation_log")).isEqualTo(3); // 成功新增、成功删除、失败删除各一条。 @@ -420,6 +433,45 @@ class AdminApiTest { assertThat(OperationTargetHolder.get()).isNull(); } + @Test + void logicalDeletionRejectsEditsUploadSignaturesAndCodeReuseAndKeepsOtherComponentsVisible() throws Exception { + Map originalRequest = uploaded(); + JsonNode created = create(originalRequest); + long id = created.path("id").asLong(); + JsonNode editSession = sign(Map.of("componentId", id)); + JsonNode remaining = create(uploaded()); + assertThat(jdbc.queryForObject("SELECT deleted FROM component WHERE id=?", Integer.class, id)).isZero(); + + mvc.perform(delete("/admin-api/components/{id}", id)).andExpect(status().isOk()); + + mvc.perform(get("/admin-api/components")) + .andExpect(jsonPath("$.data.total").value(1)) + .andExpect(jsonPath("$.data.items[0].id").value(remaining.path("id").asLong())); + mvc.perform(get("/admin-api/components").param("keyword", created.path("componentCode").asText())) + .andExpect(jsonPath("$.data.total").value(0)); + expectUpdateError(id, update(1), 404, "COMPONENT_NOT_FOUND"); + mvc.perform(post("/admin-api/storage/upload-signature").contentType(MediaType.APPLICATION_JSON) + .content(json.writeValueAsString(Map.of("componentId", id)))) + .andExpect(status().isNotFound()) + .andExpect(jsonPath("$.code").value("COMPONENT_NOT_FOUND")); + mvc.perform(post("/admin-api/storage/upload-signature").contentType(MediaType.APPLICATION_JSON) + .content(json.writeValueAsString(Map.of("uploadSessionToken", editSession.path("uploadSessionToken").asText())))) + .andExpect(status().isNotFound()) + .andExpect(jsonPath("$.code").value("COMPONENT_NOT_FOUND")); + expectCreateError(originalRequest, 409, "COMPONENT_CODE_DUPLICATE"); + + // 模拟修改请求已读取版本后,组件被另一个请求逻辑删除;原子更新必须拒绝已删除记录。 + Component staleUpdate = new Component(); + staleUpdate.setComponentName("删除后不能覆盖"); + staleUpdate.setVersion(2L); + assertThat(componentMapper.update(staleUpdate, new LambdaUpdateWrapper() + .eq(Component::getId, id).eq(Component::getVersion, 1L))).isZero(); + assertThat(jdbc.queryForObject("SELECT component_name FROM component WHERE id=?", String.class, id)).isEqualTo("测试组件"); + assertThat(jdbc.queryForObject("SELECT version FROM component WHERE id=?", Long.class, id)).isEqualTo(1L); + assertThat(jdbc.queryForObject("SELECT deleted FROM component WHERE id=?", Integer.class, id)).isEqualTo(1); + assertThat(count("component")).isEqualTo(2); + } + @Test void userCrudAndLogicalDeletionRemainUnaudited() throws Exception { JsonNode user = data(mvc.perform(post("/admin-api/users").contentType(MediaType.APPLICATION_JSON) diff --git a/cadworld-adminapi/src/test/java/com/linkerbot/cadworld/TestSchemaSupport.java b/cadworld-adminapi/src/test/java/com/linkerbot/cadworld/TestSchemaSupport.java index 5d63a4c..7dd555f 100644 --- a/cadworld-adminapi/src/test/java/com/linkerbot/cadworld/TestSchemaSupport.java +++ b/cadworld-adminapi/src/test/java/com/linkerbot/cadworld/TestSchemaSupport.java @@ -30,6 +30,7 @@ final class TestSchemaSupport { urdf_main_object_key VARCHAR(512) NOT NULL DEFAULT '', urdf_files CLOB NOT NULL DEFAULT '[]', version BIGINT NOT NULL DEFAULT 1, + deleted TINYINT NOT NULL DEFAULT 0 COMMENT '删除标记:0-未删除,1-已删除', created_at TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP ) diff --git a/cadworld-bean/src/main/java/com/linkerbot/cadworld/entity/Component.java b/cadworld-bean/src/main/java/com/linkerbot/cadworld/entity/Component.java index bcd0e88..1345e1e 100644 --- a/cadworld-bean/src/main/java/com/linkerbot/cadworld/entity/Component.java +++ b/cadworld-bean/src/main/java/com/linkerbot/cadworld/entity/Component.java @@ -1,6 +1,10 @@ package com.linkerbot.cadworld.entity; -import com.baomidou.mybatisplus.annotation.*; +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableField; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableLogic; +import com.baomidou.mybatisplus.annotation.TableName; import com.fasterxml.jackson.databind.JsonNode; import com.linkerbot.cadworld.common.mybatis.JsonNodeTypeHandler; import java.math.BigDecimal; @@ -35,4 +39,8 @@ public class Component extends BaseEntity { @TableField(typeHandler = JsonNodeTypeHandler.class) private JsonNode urdfFiles; private Long version; + + /** 删除标记:0-未删除,1-已删除。 */ + @TableLogic(value = "0", delval = "1") + private Integer deleted; } diff --git a/cadworld-service/src/main/java/com/linkerbot/cadworld/service/impl/ComponentServiceImpl.java b/cadworld-service/src/main/java/com/linkerbot/cadworld/service/impl/ComponentServiceImpl.java index 0820763..851fb4e 100644 --- a/cadworld-service/src/main/java/com/linkerbot/cadworld/service/impl/ComponentServiceImpl.java +++ b/cadworld-service/src/main/java/com/linkerbot/cadworld/service/impl/ComponentServiceImpl.java @@ -87,7 +87,7 @@ public class ComponentServiceImpl implements ComponentService { throw notFound(); } }); - log.info("组件删除成功 componentId={} userId={} requestId={}", id, userId, RequestIdHolder.get()); + log.info("组件逻辑删除成功 componentId={} userId={} requestId={}", id, userId, RequestIdHolder.get()); } @Override @@ -124,6 +124,7 @@ public class ComponentServiceImpl implements ComponentService { component.setStepObjectKey(uploads.verifyStep(session, request.stepObjectKey())); converter.applyUrdf(component, uploads.verifyUrdf(session, request.urdfObjectKeys())); component.setVersion(1L); + component.setDeleted(0); log.info("组件上传资源校验完成 componentCode={} urdfFileCount={} requestId={}", component.getComponentCode(), request.urdfObjectKeys().size(), RequestIdHolder.get()); return component;