mirror of
https://gitee.com/ssssssss-team/magic-api.git
synced 2026-06-09 10:23:53 +08:00
调整逻辑删除使用方式
This commit is contained in:
@@ -36,6 +36,8 @@ public class NamedTable {
|
||||
|
||||
Object defaultPrimaryValue;
|
||||
|
||||
boolean useLogic = false;
|
||||
|
||||
Where where = new Where(this);
|
||||
|
||||
public NamedTable(String tableName, SQLModule sqlModule, Function<String, String> rowMapColumnMapper) {
|
||||
@@ -46,6 +48,12 @@ public class NamedTable {
|
||||
this.logicDeleteValue = sqlModule.getLogicDeleteValue();
|
||||
}
|
||||
|
||||
@Comment("使用逻辑删除")
|
||||
public NamedTable logic(){
|
||||
this.useLogic = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Comment("设置主键名,update时使用")
|
||||
public NamedTable primary(String primary) {
|
||||
return primary(primary, null);
|
||||
@@ -151,8 +159,13 @@ public class NamedTable {
|
||||
return sqlModule.insert(new BoundSql(builder.toString(), entries.stream().map(Map.Entry::getValue).collect(Collectors.toList()), sqlModule), this.primary);
|
||||
}
|
||||
|
||||
@Comment("执行delete语句(物理删除)")
|
||||
@Comment("执行delete语句")
|
||||
public int delete() {
|
||||
if(useLogic){
|
||||
Map<String, Object> dataMap = new HashMap<>();
|
||||
dataMap.put(logicDeleteColumn, logicDeleteValue);
|
||||
return update(dataMap);
|
||||
}
|
||||
if (where.isEmpty()) {
|
||||
throw new MagicAPIException("delete语句不能没有条件");
|
||||
}
|
||||
@@ -163,20 +176,6 @@ public class NamedTable {
|
||||
return sqlModule.update(new BoundSql(builder.toString(), where.getParams(), sqlModule));
|
||||
}
|
||||
|
||||
@Comment("执行delete语句")
|
||||
public int delete(@Comment("是否逻辑删除") boolean isLogicDelete) {
|
||||
if (where.isEmpty()) {
|
||||
throw new MagicAPIException("delete语句不能没有条件");
|
||||
}
|
||||
if (!isLogicDelete) {
|
||||
return delete();
|
||||
} else {
|
||||
Map<String, Object> dataMap = new HashMap<>();
|
||||
dataMap.put(logicDeleteColumn, logicDeleteValue);
|
||||
return update(dataMap);
|
||||
}
|
||||
}
|
||||
|
||||
@Comment("保存到表中,当主键有值时则修改,否则插入")
|
||||
public Object save() {
|
||||
return this.save(null, false);
|
||||
@@ -227,26 +226,12 @@ public class NamedTable {
|
||||
return sqlModule.select(buildSelect());
|
||||
}
|
||||
|
||||
@Comment("执行`select`查询")
|
||||
public List<Map<String, Object>> select(@Comment("排除无效数据") boolean excludeInvalid) {
|
||||
return sqlModule.select(buildSelect(excludeInvalid));
|
||||
}
|
||||
|
||||
@Comment("执行`selectOne`查询")
|
||||
public Map<String, Object> selectOne() {
|
||||
return sqlModule.selectOne(buildSelect());
|
||||
}
|
||||
|
||||
@Comment("执行`selectOne`查询")
|
||||
public Map<String, Object> selectOne(@Comment("排除无效数据") boolean excludeInvalid) {
|
||||
return sqlModule.selectOne(buildSelect(excludeInvalid));
|
||||
}
|
||||
|
||||
private BoundSql buildSelect() {
|
||||
return buildSelect(false);
|
||||
}
|
||||
|
||||
private BoundSql buildSelect(boolean excludeInvalid) {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.append("select ");
|
||||
if (this.fields.isEmpty()) {
|
||||
@@ -256,7 +241,7 @@ public class NamedTable {
|
||||
}
|
||||
builder.append(" from ").append(tableName);
|
||||
List<Object> params = new ArrayList<>();
|
||||
where.and(excludeInvalid, it -> where.ne(logicDeleteColumn, logicDeleteValue));
|
||||
where.and(useLogic, it -> where.ne(logicDeleteColumn, logicDeleteValue));
|
||||
if (!where.isEmpty()) {
|
||||
where.and();
|
||||
builder.append(where.getSql());
|
||||
@@ -278,11 +263,6 @@ public class NamedTable {
|
||||
return sqlModule.page(buildSelect());
|
||||
}
|
||||
|
||||
@Comment("执行分页查询")
|
||||
public Object page(@Comment("排除无效数据") boolean excludeInvalid) {
|
||||
return sqlModule.page(buildSelect(excludeInvalid));
|
||||
}
|
||||
|
||||
@Comment("执行update语句")
|
||||
public int update() {
|
||||
return update(null);
|
||||
|
||||
@@ -385,14 +385,11 @@ public class Where {
|
||||
return namedTable.delete();
|
||||
}
|
||||
|
||||
@Comment("执行delete语句")
|
||||
public int delete(@Comment("是否逻辑删除")boolean isLogicDelete) {
|
||||
return namedTable.delete(isLogicDelete);
|
||||
}
|
||||
@Comment("执行update语句")
|
||||
public int update(@Comment("各项列和值") Map<String, Object> data) {
|
||||
return namedTable.update(data);
|
||||
}
|
||||
|
||||
@Comment("执行update语句")
|
||||
public int update(@Comment("各项列和值") Map<String, Object> data,@Comment("是否更新空值字段") boolean isUpdateBlank) {
|
||||
return namedTable.update(data,isUpdateBlank);
|
||||
@@ -401,24 +398,14 @@ public class Where {
|
||||
public Object page() {
|
||||
return namedTable.page();
|
||||
}
|
||||
@Comment("执行分页查询")
|
||||
public Object page(@Comment("排除无效数据") boolean excludeInvalid) {
|
||||
return namedTable.page();
|
||||
}
|
||||
|
||||
@Comment("执行select查询")
|
||||
public List<Map<String, Object>> select() {
|
||||
return namedTable.select();
|
||||
}
|
||||
@Comment("执行select查询")
|
||||
public List<Map<String, Object>> select(@Comment("排除无效数据") boolean excludeInvalid) {
|
||||
return namedTable.select(excludeInvalid);
|
||||
}
|
||||
|
||||
@Comment("执行selectOne查询")
|
||||
public Map<String, Object> selectOne() {
|
||||
return namedTable.selectOne();
|
||||
}
|
||||
@Comment("执行selectOne查询")
|
||||
public Map<String, Object> selectOne(@Comment("排除无效数据") boolean excludeInvalid) {
|
||||
return namedTable.selectOne(excludeInvalid);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user