feat(spec): Add solid soft delete function

- Add softDelete method to the CrudServiceSpec interface to soft delete entities
- Implement softDelete method in the CrudServiceImplSpec class, including finding entities, marking delete status, and updating entities
- Added setDeleted abstract method, and implements specific tag deletion logic from subclasses
- Add log records, output error message when the corresponding entity cannot be found
- Introduce log extension functions and Core. EXTENSION module
This commit is contained in:
gewuyou 2025-06-03 15:14:05 +08:00
parent bf63580581
commit 3821c8b8c7
3 changed files with 39 additions and 0 deletions

View File

@ -2,4 +2,5 @@
dependencies {
compileOnly(libs.springBootStarter.jpa)
implementation(project(Modules.Webmvc.DTO))
implementation(project(Modules.Core.EXTENSION))
}

View File

@ -74,6 +74,16 @@ interface CrudServiceSpec<Entity: Any, Id: Any, Filter: Any> {
*/
fun deleteByAll(entities: List<Entity>)
/**
* 软删除
*
* 本函数用于标记实体为删除状态而不是真正从数据库中移除
* 这种方法可以保留历史数据同时避免数据泄露
*
* @param id 实体的唯一标识符
*/
fun softDelete(id: Id)
/**
* 根据ID检查实体是否存在
*

View File

@ -1,5 +1,6 @@
package com.gewuyou.forgeboot.webmvc.spec.service.impl
import com.gewuyou.forgeboot.core.extension.log
import com.gewuyou.forgeboot.webmvc.dto.PageResult
import com.gewuyou.forgeboot.webmvc.dto.extension.map
import com.gewuyou.forgeboot.webmvc.dto.extension.toPageResult
@ -201,4 +202,31 @@ abstract class CrudServiceImplSpec<Entity : Any, Id : Any, Filter : Any>(
override fun saveAll(entities: List<Entity>): List<Entity> {
return repository.saveAll(entities)
}
/**
* 标记实体为软删除状态
*
* 此方法应在子类中实现用于定义如何将实体标记为已删除例如设置一个 deleted 字段
* 软删除不会从数据库中物理移除记录而是将其标记为已删除状态以便保留历史数据
*
* @param entity 实体对象表示要标记为删除状态的对象
*/
protected abstract fun setDeleted(entity: Entity)
/**
* 执行软删除操作
*
* 该方法会根据提供的 ID 查找实体如果找到该实体则调用 [setDeleted] 方法将其标记为删除状态
* 然后通过 [update] 方法保存更改如果没有找到实体则记录一条错误日志
*
* 软删除机制可以保留历史数据同时避免敏感信息的直接删除确保数据可追溯且安全
*
* @param id 实体的唯一标识符用于查找需要删除的实体
*/
override fun softDelete(id: Id) {
val exist: Entity? = findById(id)
exist?.let {
setDeleted(it)
update(it)
} ?: log.error("删除失败,找不到该租户")
}
}