use of com.diboot.core.binding.parser.PropInfo in project diboot by dibo-software.
the class Pagination method toPage.
/**
* 转换为IPage
*
* @param <T>
* @return
*/
public <T> Page<T> toPage() {
List<OrderItem> orderItemList = null;
// 解析排序
if (V.notEmpty(this.orderBy)) {
orderItemList = new ArrayList<>();
// orderBy=shortName:DESC,age:ASC,birthdate
String[] orderByFields = S.split(this.orderBy);
for (String field : orderByFields) {
V.securityCheck(field);
if (field.contains(":")) {
String[] fieldAndOrder = S.split(field, ":");
String fieldName = fieldAndOrder[0];
String columnName = S.toSnakeCase(fieldName);
PropInfo propInfo = getEntityPropInfo();
if (propInfo != null) {
// 前参数为字段名
if (propInfo.getFieldToColumnMap().containsKey(fieldName)) {
columnName = propInfo.getFieldToColumnMap().get(fieldName);
} else // 前参数为列名
if (propInfo.getColumnToFieldMap().containsKey(fieldName)) {
columnName = fieldName;
}
}
if (Cons.ORDER_DESC.equalsIgnoreCase(fieldAndOrder[1])) {
orderItemList.add(OrderItem.desc(columnName));
} else {
orderItemList.add(OrderItem.asc(columnName));
}
} else {
orderItemList.add(OrderItem.asc(S.toSnakeCase(field)));
}
}
}
Page<T> page = new Page<T>().setCurrent(getPageIndex()).setSize(getPageSize()).setTotal(getTotalCount() > 0 ? -1 : getTotalCount());
if (orderItemList != null) {
page.addOrder(orderItemList);
}
return page;
}
use of com.diboot.core.binding.parser.PropInfo in project diboot by dibo-software.
the class BaseController method attachMoreRelatedData.
/**
* 通用的attachMore获取数据
*
* @param attachMore 相应的attachMore
* @param parentValue 父级值
* @return labelValue集合
*/
protected List<LabelValue> attachMoreRelatedData(AttachMoreDTO attachMore, String parentValue) {
String entityClassName = S.capFirst(S.toLowerCaseCamel(attachMore.getTarget()));
Class<?> entityClass = BindingCacheManager.getEntityClassBySimpleName(entityClassName);
if (V.isEmpty(entityClass)) {
throw new BusinessException("attachMore: " + attachMore.getTarget() + " 不存在");
}
BaseService<?> baseService = ContextHelper.getBaseServiceByEntity(entityClass);
if (baseService == null) {
throw new BusinessException("attachMore: " + attachMore.getTarget() + " 的Service不存在 ");
}
PropInfo propInfoCache = BindingCacheManager.getPropInfoByClass(entityClass);
Function<String, String> field2column = field -> {
if (V.notEmpty(field)) {
String column = propInfoCache.getColumnByField(field);
if (V.notEmpty(column)) {
return column;
} else {
throw new BusinessException("attachMore: " + attachMore.getTarget() + " 无 `" + field + "` 属性");
}
}
return null;
};
String label = field2column.apply(S.defaultIfBlank(attachMore.getLabel(), "label"));
String value = S.defaultString(field2column.apply(attachMore.getValue()), propInfoCache.getIdColumn());
String ext = field2column.apply(attachMore.getExt());
// 构建查询条件
QueryWrapper<?> queryWrapper = Wrappers.query().select(V.isEmpty(ext) ? new String[] { label, value } : new String[] { label, value, ext }).like(V.notEmpty(attachMore.getKeyword()), label, attachMore.getKeyword());
// 解析排序
if (V.notEmpty(attachMore.getOrderBy())) {
String[] orderByFields = S.split(attachMore.getOrderBy(), ",");
for (String field : orderByFields) {
V.securityCheck(field);
String[] fieldAndOrder = field.split(":");
String columnName = field2column.apply(fieldAndOrder[0]);
if (fieldAndOrder.length > 1 && Cons.ORDER_DESC.equalsIgnoreCase(fieldAndOrder[1])) {
queryWrapper.orderByDesc(columnName);
} else {
queryWrapper.orderByAsc(columnName);
}
}
}
// 父级限制
String parentColumn = field2column.apply(S.defaultIfBlank(attachMore.getParent(), attachMore.isTree() ? "parentId" : null));
if (V.notEmpty(parentColumn)) {
if (V.notEmpty(parentValue)) {
queryWrapper.eq(parentColumn, parentValue);
} else {
queryWrapper.and(e -> e.isNull(parentColumn).or().eq(parentColumn, 0));
}
}
// 构建附加条件
buildAttachMoreCondition(attachMore, queryWrapper, field2column);
// 获取数据并做相应填充
List<LabelValue> labelValueList = baseService.getLabelValueList(queryWrapper);
if (V.notEmpty(parentColumn) || attachMore.getNext() != null) {
Boolean leaf = !attachMore.isTree() && attachMore.getNext() == null ? true : null;
// 第一层tree与最后一层无需返回type
String type = attachMore.isTree() || Boolean.TRUE.equals(leaf) ? null : attachMore.getTarget();
labelValueList.forEach(item -> {
item.setType(type);
item.setLeaf(leaf);
// 非异步加载
if (!attachMore.isLazy()) {
List<LabelValue> children = attachMoreRelatedData(attachMore, S.valueOf(item.getValue()), type);
item.setChildren(children.isEmpty() ? null : children);
item.setDisabled(children.isEmpty() && attachMore.getNext() != null ? true : null);
}
});
}
return labelValueList;
}
use of com.diboot.core.binding.parser.PropInfo in project diboot by dibo-software.
the class BindingCacheManager method initPropInfoCache.
/**
* 初始化bean的属性缓存
* @param beanClazz
* @return
*/
private static PropInfo initPropInfoCache(Class<?> beanClazz) {
PropInfo propInfoCache = new PropInfo(beanClazz);
getCacheManager().putCacheObj(CACHE_NAME_CLASS_PROP, beanClazz.getName(), propInfoCache);
return propInfoCache;
}
Aggregations