add BaseMapper.deleteAllByExample
This commit is contained in:
@@ -6,6 +6,7 @@ import com.imyeyu.java.bean.timi.TimiException;
|
||||
import com.imyeyu.java.ref.Ref;
|
||||
import com.imyeyu.spring.annotation.table.AutoUUID;
|
||||
import com.imyeyu.spring.annotation.table.Column;
|
||||
import com.imyeyu.spring.annotation.table.DeleteColumn;
|
||||
import com.imyeyu.spring.annotation.table.Id;
|
||||
import com.imyeyu.spring.annotation.table.Table;
|
||||
import com.imyeyu.spring.annotation.table.Transient;
|
||||
@@ -23,6 +24,7 @@ import java.lang.reflect.Field;
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
@@ -232,6 +234,29 @@ public class SQLProvider {
|
||||
return "UPDATE `%s` SET `deleted_at` = %s WHERE `%s` = #{id}".formatted(meta.table, Time.now(), meta.idFieldColumn.columnName);
|
||||
}
|
||||
|
||||
public String deleteAllByExample(Object entity) {
|
||||
EntityMeta meta = getEntityMeta(entity.getClass());
|
||||
TimiException.required(meta.canDelete, "not allow delete for %s".formatted(meta.entityClass));
|
||||
|
||||
FieldColumn deleteColumn = meta.getFieldColumnList().stream().filter(fc -> fc.isDeleteColumn).findFirst().orElse(null);
|
||||
TimiException.required(deleteColumn, "unknown delete column, use com.imyeyu.spring.annotation.table.DeleteColumn annotation on field");
|
||||
assert deleteColumn != null;
|
||||
assert deleteColumn.deleteColumnType != null;
|
||||
|
||||
String delClause = meta.fieldColumnList.stream()
|
||||
.filter(FieldColumn::isNotId)
|
||||
.filter(fc -> fc.isNotNull(entity))
|
||||
.map(fc -> "`%s` = #{%s}".formatted(fc.columnName, fc.fieldName))
|
||||
.collect(Collectors.joining(" AND "));
|
||||
StringBuilder sql = new StringBuilder("UPDATE `%s` SET `%s` = ".formatted(meta.table, deleteColumn.getColumnName()));
|
||||
sql.append("'").append(switch (deleteColumn.deleteColumnType) {
|
||||
case UNIX -> Time.now();
|
||||
case DATE, DATE_TIME -> new Date();
|
||||
}).append("'");
|
||||
sql.append(" WHERE ").append(delClause);
|
||||
return sql.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 硬删除,需要实体实现 {@link Destroyable}
|
||||
*
|
||||
@@ -419,6 +444,10 @@ public class SQLProvider {
|
||||
|
||||
final boolean isAutoUpperUUID;
|
||||
|
||||
final boolean isDeleteColumn;
|
||||
|
||||
final DeleteColumn.Type deleteColumnType;
|
||||
|
||||
public FieldColumn(Field field) {
|
||||
this.field = field;
|
||||
|
||||
@@ -437,6 +466,12 @@ public class SQLProvider {
|
||||
} else {
|
||||
isAutoUpperUUID = false;
|
||||
}
|
||||
isDeleteColumn = field.isAnnotationPresent(DeleteColumn.class);
|
||||
if (isDeleteColumn) {
|
||||
deleteColumnType = field.getAnnotation(DeleteColumn.class).value();
|
||||
} else {
|
||||
deleteColumnType = null;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isNull(Object entity) {
|
||||
|
||||
Reference in New Issue
Block a user