|
|
@ -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删除数据
|
|
|
|
*
|
|
|
|
*
|
|
|
|