组件逻辑删除

This commit is contained in:
lxp
2026-10-12 13:09:12 +08:00
parent 9e3bc846e8
commit b2ff01e5e8
7 changed files with 76 additions and 10 deletions
@@ -54,7 +54,7 @@ public class ComponentController {
}
@AuditOperation(module = "COMPONENT", action = "DELETE", targetType = "COMPONENT")
@ApiRequestLog("删除组件")
@ApiRequestLog("逻辑删除组件")
@DeleteMapping("/{id:[0-9]+}")
public ApiResponse<SuccessResponse> delete(@PathVariable @Min(1) Long id) {
componentService.deleteComponent(operatorProvider.userId(), id);
@@ -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<String, Object> 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<Component>()
.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)
@@ -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
)