mirror of
https://github.moeyy.xyz/https://github.com/GeWuYou/forgeboot
synced 2025-10-28 05:38:55 +08:00
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:
parent
bf63580581
commit
3821c8b8c7
@ -2,4 +2,5 @@
|
|||||||
dependencies {
|
dependencies {
|
||||||
compileOnly(libs.springBootStarter.jpa)
|
compileOnly(libs.springBootStarter.jpa)
|
||||||
implementation(project(Modules.Webmvc.DTO))
|
implementation(project(Modules.Webmvc.DTO))
|
||||||
|
implementation(project(Modules.Core.EXTENSION))
|
||||||
}
|
}
|
||||||
|
|||||||
@ -74,6 +74,16 @@ interface CrudServiceSpec<Entity: Any, Id: Any, Filter: Any> {
|
|||||||
*/
|
*/
|
||||||
fun deleteByAll(entities: List<Entity>)
|
fun deleteByAll(entities: List<Entity>)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 软删除
|
||||||
|
*
|
||||||
|
* 本函数用于标记实体为删除状态,而不是真正从数据库中移除
|
||||||
|
* 这种方法可以保留历史数据,同时避免数据泄露
|
||||||
|
*
|
||||||
|
* @param id 实体的唯一标识符
|
||||||
|
*/
|
||||||
|
fun softDelete(id: Id)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据ID检查实体是否存在
|
* 根据ID检查实体是否存在
|
||||||
*
|
*
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
package com.gewuyou.forgeboot.webmvc.spec.service.impl
|
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.PageResult
|
||||||
import com.gewuyou.forgeboot.webmvc.dto.extension.map
|
import com.gewuyou.forgeboot.webmvc.dto.extension.map
|
||||||
import com.gewuyou.forgeboot.webmvc.dto.extension.toPageResult
|
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> {
|
override fun saveAll(entities: List<Entity>): List<Entity> {
|
||||||
return repository.saveAll(entities)
|
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("删除失败,找不到该租户")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user