site.sorghum.anno.db.service.DbServiceWood Maven / Gradle / Ivy
The newest version!
package site.sorghum.anno.db.service;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import jakarta.inject.Inject;
import jakarta.inject.Named;
import lombok.SneakyThrows;
import org.noear.wood.DbTableQuery;
import org.noear.wood.IPage;
import site.sorghum.anno._common.AnnoBeanUtils;
import site.sorghum.anno._common.exception.BizException;
import site.sorghum.anno._metadata.AnEntity;
import site.sorghum.anno._metadata.AnField;
import site.sorghum.anno._metadata.MetadataManager;
import site.sorghum.anno.anno.annotation.field.AnnoEditImpl;
import site.sorghum.anno.anno.entity.common.AnnoPage;
import site.sorghum.anno.anno.proxy.field.EmptyFieldBaseSupplier;
import site.sorghum.anno.anno.proxy.field.FieldBaseSupplier;
import site.sorghum.anno.anno.util.AnnoFieldCache;
import site.sorghum.anno.db.*;
import site.sorghum.anno.db.exception.AnnoDbException;
import site.sorghum.anno.db.service.context.AnnoDbContext;
import site.sorghum.anno.i18n.I18nUtil;
import site.sorghum.plugin.join.util.InvokeUtil;
import java.lang.reflect.Field;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* 数据库服务
*
* @author sorghum
* @since 2023/07/07
*/
@Named("dbServiceWood")
public class DbServiceWood implements DbService {
@Inject
DbTableContext dbTableContext;
@Inject
MetadataManager metadataManager;
@Override
public AnnoPage page(DbCriteria criteria) {
TableParam tableParam = dbTableContext.getTableParam(criteria.getEntityName());
return AnnoDbContext.dynamicDbContext(
tableParam.getDbName(), () -> {
if (criteria.getPage() == null) {
criteria.page(1, 10);
}
DbTableQuery dbTableQuery = buildCommonDbTableQuery(criteria);
IPage page = null;
try {
page = dbTableQuery.selectPage(tableParam.getColumnStr(), tableParam.getClazz());
} catch (SQLException e) {
throw new BizException(e);
}
return new AnnoPage<>(true, page.getList(), page.getTotal(), criteria.getPage().getPageSize(), criteria.getPage().getPage());
}
);
}
@SneakyThrows
@Override
public List list(DbCriteria criteria) {
TableParam tableParam = dbTableContext.getTableParam(criteria.getEntityName());
return AnnoDbContext.dynamicDbContext(tableParam.getDbName(),
() -> {
DbTableQuery dbTableQuery = buildCommonDbTableQuery(criteria);
try {
return dbTableQuery.selectList(tableParam.getColumnStr(), tableParam.getClazz());
} catch (SQLException e) {
throw new BizException(e);
}
});
}
@Override
public T queryOne(DbCriteria criteria) {
List ts = list(criteria);
if (ts.size() > 1) {
throw new AnnoDbException(I18nUtil.getMessage("exception.db.out-one"));
}
if (ts.isEmpty()) {
return null;
}
return ts.get(0);
}
@SneakyThrows
@Override
public int update(T t, DbCriteria criteria) {
TableParam tableParam = dbTableContext.getTableParam(criteria.getEntityName());
AnEntity entity = metadataManager.getEntity(tableParam.getClazz());
return AnnoDbContext.dynamicDbContext(entity.getDbName(),() -> {
DbTableQuery dbTableQuery = buildCommonDbTableQuery(criteria);
// 执行值
preProcess(t, entity, false);
dbTableQuery.setEntityIf(t, (k, v) -> filterField(entity, tableParam, k, v));
try {
return dbTableQuery.update();
} catch (SQLException e) {
throw new BizException(e);
}
});
}
@SneakyThrows
@Override
public long insert(T t) {
TableParam tableParam = dbTableContext.getTableParam(t.getClass());
DbRemove dbRemove = tableParam.getDbRemove();
AnEntity entity = metadataManager.getEntity(tableParam.getClazz());
return AnnoDbContext.dynamicDbContext(entity.getDbName(),() -> {
preProcess(t, entity, true);
DbTableQuery dbTableQuery = AnnoDbContext.dbContext().table(tableParam.getTableName())
.setEntityIf(t, (k, v) -> filterField(entity, tableParam, k, v));
if (dbRemove.getLogic()) {
// 查询当前字段的类型
Field field = ReflectUtil.getField(tableParam.getClazz(), AnnoFieldCache.getFieldNameBySqlColumn(tableParam.getClazz(), dbRemove.getRemoveColumn()));
if (field == null) {
throw new AnnoDbException("未找在实体中找到对应的逻辑删除字段,请检查:%s".formatted(dbRemove.getRemoveColumn()));
}
Object converted = Convert.convert(field.getType(), dbRemove.getNotRemoveValue());
dbTableQuery.set(dbRemove.getRemoveColumn(), converted);
}
try {
return dbTableQuery.insert();
} catch (SQLException e) {
throw new BizException(e);
}
});
}
@SneakyThrows
@Override
public int delete(DbCriteria criteria) {
TableParam tableParam = dbTableContext.getTableParam(criteria.getEntityName());
return AnnoDbContext.dynamicDbContext(tableParam.getDbName(),() -> {
DbTableQuery dbTableQuery = buildCommonDbTableQuery(criteria);
DbRemove dbRemove = tableParam.getDbRemove();
if (dbRemove.getLogic()) {
// 查询当前字段的类型
Field field = ReflectUtil.getField(tableParam.getClazz(), AnnoFieldCache.getFieldNameBySqlColumn(tableParam.getClazz(), dbRemove.getRemoveColumn()));
if (field == null) {
throw new AnnoDbException("未找在实体中找到对应的逻辑删除字段,请检查:%s".formatted(dbRemove.getRemoveColumn()));
}
Object converted = Convert.convert(field.getType(), dbRemove.getRemoveValue());
try {
return dbTableQuery.set(dbRemove.getRemoveColumn(), converted).update();
} catch (SQLException e) {
throw new BizException(e);
}
} else {
try {
return dbTableQuery.delete();
} catch (SQLException e) {
throw new BizException(e);
}
}
});
}
@Override
public List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy