增加 根据条件删除的方法

beetlsql3-dev
Mlxa0324 3 years ago
parent a8cc2e40bc
commit 769025fefe

@ -1,9 +1,8 @@
package com.ibeetl.admin.core.service; package com.ibeetl.admin.core.service;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.exceptions.UtilException; import cn.hutool.core.exceptions.UtilException;
import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.*;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import com.ibeetl.admin.core.annotation.Dict; import com.ibeetl.admin.core.annotation.Dict;
import com.ibeetl.admin.core.annotation.DictDeep; import com.ibeetl.admin.core.annotation.DictDeep;
import com.ibeetl.admin.core.annotation.DictEnum; import com.ibeetl.admin.core.annotation.DictEnum;
@ -12,6 +11,9 @@ import com.ibeetl.admin.core.util.PlatformException;
import com.ibeetl.admin.core.util.enums.DelFlagEnum; import com.ibeetl.admin.core.util.enums.DelFlagEnum;
import org.beetl.sql.core.SQLManager; import org.beetl.sql.core.SQLManager;
import org.beetl.sql.core.TailBean; import org.beetl.sql.core.TailBean;
import org.beetl.sql.core.query.LambdaQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.lang.reflect.Field; import java.lang.reflect.Field;
@ -29,6 +31,8 @@ import java.util.Map;
*/ */
public class CoreBaseService<T> { public class CoreBaseService<T> {
private final Logger log = LoggerFactory.getLogger(this.getClass());
@Resource @Resource
protected CoreDictService dictUtil; protected CoreDictService dictUtil;
@Resource @Resource
@ -43,7 +47,7 @@ public class CoreBaseService<T> {
*/ */
public T queryById(Object id) { public T queryById(Object id) {
T t = sqlManager.single(getCurrentEntityClassz(), id); T t = sqlManager.single(getCurrentEntityClassz(), id);
queryEntityAfter((Object) t); queryEntityAfter(t);
return t; return t;
} }
@ -85,7 +89,7 @@ public class CoreBaseService<T> {
if (!(paras instanceof TailBean)) { if (!(paras instanceof TailBean)) {
throw new PlatformException("指定的pojo" + paras.getClass() + " 不能获取数据字典需要继承TailBean"); throw new PlatformException("指定的pojo" + paras.getClass() + " 不能获取数据字典需要继承TailBean");
} }
List<T> list = (List<T>) sqlManager.template(paras); List<T> list = sqlManager.template(paras);
T t = null; T t = null;
if (list.size() > 0) { if (list.size() > 0) {
t = list.get(0); t = list.get(0);
@ -113,7 +117,7 @@ public class CoreBaseService<T> {
*/ */
public T queryById(Class<T> classz, Object id) { public T queryById(Class<T> classz, Object id) {
T t = sqlManager.unique(classz, id); T t = sqlManager.unique(classz, id);
queryEntityAfter((Object) t); queryEntityAfter(t);
return t; return t;
} }
@ -158,16 +162,6 @@ public class CoreBaseService<T> {
* @return * @return
*/ */
public boolean insertBatch(List<T> list) { public boolean insertBatch(List<T> list) {
return insertBatch(list, true);
}
/**
* ID
*
* @param list
* @return
*/
public boolean insertBatch(List<T> list, Boolean autoDbAssignKey) {
if (list.size() > 0) { if (list.size() > 0) {
return sqlManager.insertBatch(list.get(0).getClass(), list).length > 0; return sqlManager.insertBatch(list.get(0).getClass(), list).length > 0;
} else { } else {
@ -188,7 +182,7 @@ public class CoreBaseService<T> {
List<Object> list = new ArrayList<>(); List<Object> list = new ArrayList<>();
for (Long id : ids) { for (Long id : ids) {
Map map = new HashMap(); Map map = new HashMap(2);
map.put("id", id); map.put("id", id);
map.put("delFlag", DelFlagEnum.DELETED.getValue()); map.put("delFlag", DelFlagEnum.DELETED.getValue());
list.add(map); list.add(map);
@ -203,7 +197,7 @@ public class CoreBaseService<T> {
public boolean deleteById(Integer id) { public boolean deleteById(Integer id) {
Map map = new HashMap(); Map map = new HashMap(2);
map.put("id", id); map.put("id", id);
map.put("delFlag", DelFlagEnum.DELETED.getValue()); map.put("delFlag", DelFlagEnum.DELETED.getValue());
int ret = sqlManager.updateTemplateById(getCurrentEntityClassz(), map); int ret = sqlManager.updateTemplateById(getCurrentEntityClassz(), map);
@ -220,6 +214,56 @@ public class CoreBaseService<T> {
return ret == 1; return ret == 1;
} }
/**
* ID
* @param condition
* @return
*/
public boolean deleteByCondition(T condition) {
/**
* ,
*/
String[] ignores = new String[]{
"org_id", "user_id", "_add_time", "create_time", "_create_time", "update_time",
"_update_time"
};
/**
*
* | $
* ArrayUtil.map(ignores, ReUtil::escape)
* [org_id|user_id|_add_time|]$
*/
String regex = String.format("[%s]$", ArrayUtil.join(ArrayUtil.map(ignores, ReUtil::escape), "|"));
log.debug("生成的正则:{}", regex);
// 构建LambdaQuery
LambdaQuery<T> tLambdaQuery = sqlManager.lambdaQuery(getCurrentEntityClassz());
// 实体类属性转Map
Map<String, Object> stringObjectMap = BeanUtil.beanToMap(condition);
// 移除空值或者忽略字段
for (String k : stringObjectMap.keySet()) {
/**
*
* {@link cn.hutool.core.util.ObjectUtil#isEmpty}
*/
if (ObjectUtil.isEmpty(stringObjectMap.get(k)) || ReUtil.isMatch(regex, k)) {
stringObjectMap.remove(k);
}
}
// 目前只支持 等于操作将条件追加到Query中
stringObjectMap.forEach(tLambdaQuery::andEq);
log.debug("生成的SQL预览 {}", tLambdaQuery.getSql());
// 执行删除
return tLambdaQuery.delete() == 1;
}
/** /**
* id * id
* *

Loading…
Cancel
Save