use of com.github.yiuman.citrus.support.crud.query.builder.SimpleQueryBuilder in project citrus by Yiuman.
the class DefaultQueryParamHandler method handle.
@Override
public void handle(QueryParamMeta paramMeta, Object object, Query query) throws Exception {
Field field = paramMeta.getField();
field.setAccessible(true);
Object value = field.get(object);
if (Objects.isNull(value)) {
return;
}
SimpleQueryBuilder simpleQueryBuilder = QueryBuilders.wrapper(query);
Method conditionMethod = simpleQueryBuilder.getClass().getMethod(paramMeta.getOperator(), String.class, getParameterClass(field));
conditionMethod.setAccessible(true);
String fieldName = ObjectUtil.isNotEmpty(paramMeta.getMapping()) ? paramMeta.getMapping() : field.getName();
conditionMethod.invoke(simpleQueryBuilder, fieldName, field.get(object));
}
use of com.github.yiuman.citrus.support.crud.query.builder.SimpleQueryBuilder in project citrus by Yiuman.
the class BaseQueryRestful method handleSortQuery.
/**
* 处理排序
*
* @param query 查询构造
* @param request 当前的请求
* @throws Exception 绑定数据时发生的异常
*/
protected void handleSortQuery(Query query, HttpServletRequest request) throws Exception {
SimpleQueryBuilder wrapper = QueryBuilders.wrapper(query);
final Consumer<SortBy> sortItemHandler = wrapper::orderBy;
// 拼接排序条件
SortBy sortBy = WebUtils.requestDataBind(SortBy.class, request);
if (Objects.nonNull(sortBy) && org.springframework.util.StringUtils.hasText(sortBy.getSortBy())) {
sortItemHandler.accept(sortBy);
sortByList.stream().filter(sortByItem -> !sortByItem.getSortBy().equals(sortBy.getSortBy())).forEach(sortItemHandler);
} else {
// 构造默认的排序
sortByList.forEach(sortItemHandler);
}
}
use of com.github.yiuman.citrus.support.crud.query.builder.SimpleQueryBuilder in project citrus by Yiuman.
the class MdaServiceImpl method save.
@Override
public String save(Map<String, Object> entity) throws Exception {
if (!this.beforeSave(entity)) {
return null;
}
Table tableEntity = getTableEntity(getModelId(WebUtils.getRequest()));
TableMeta tableMeta = entity2Meta(tableEntity);
PrimaryKeyConstraint primaryKeyConstraint = tableMeta.getPrimaryKey();
final Map<ColumnMeta, Object> keyMap = new HashMap<>(tableMeta.getColumns().size());
if (Objects.nonNull(primaryKeyConstraint)) {
// 如果是组合主键必须主动赋值,否则抛异常
if (isCombinePrimaryKeys(primaryKeyConstraint)) {
primaryKeyConstraint.getColumns().forEach(primaryKeyColumn -> {
Object key = entity.get(primaryKeyColumn.getColumnName());
if (Objects.isNull(key)) {
// 当前模型的主键为组合模式,不能为空
throw new MdaException(String.format("The primary key of the current model is in combination mode and cannot be empty." + " The corresponding columns are [%s]", primaryKeyConstraint.getColumns().stream().map(ColumnMeta::getColumnName).collect(Collectors.joining(","))));
}
keyMap.put(primaryKeyColumn, key);
});
} else {
primaryKeyConstraint.getColumns().stream().findFirst().ifPresent(primaryKeyColumn -> {
if (Objects.isNull(entity.get(primaryKeyColumn.getColumnName()))) {
final IdentifierGenerator identifierGenerator = GlobalConfigUtils.getGlobalConfig(getDmlProcessor().getSqlSessionFactory(tableEntity.getNamespace()).getConfiguration()).getIdentifierGenerator();
IdType idType = tableEntity.getIdType();
Object key;
if (idType == IdType.ASSIGN_ID) {
Number number = identifierGenerator.nextId(entity);
key = primaryKeyColumn.getJdbcType().equals(JDBCType.VARCHAR) ? number.toString() : number;
} else {
key = identifierGenerator.nextUUID(entity);
}
keyMap.put(primaryKeyColumn, key);
entity.put(primaryKeyColumn.getColumnName(), key);
} else {
keyMap.put(primaryKeyColumn, entity.get(primaryKeyColumn.getColumnName()));
}
});
}
}
SaveMeta saveMeta = SaveMeta.builder().namespace(tableMeta.getNamespace()).tableName(tableMeta.getTableName()).entity(entity).build();
SimpleQueryBuilder simpleQueryBuilder = QueryBuilders.create();
keyMap.forEach((key, value) -> simpleQueryBuilder.eq(key.getColumnName(), value));
if (Objects.isNull(get(simpleQueryBuilder.toQuery()))) {
getDmlProcessor().insert(saveMeta);
} else {
UpdateWrapper<?> updateWrapper = Wrappers.update();
keyMap.forEach((key, value) -> {
updateWrapper.eq(key.getColumnName(), value);
entity.remove(key.getColumnName());
});
getDmlProcessor().update(saveMeta, updateWrapper);
}
afterSave(entity);
return keyMap.values().stream().map(Object::toString).collect(Collectors.joining("-"));
}
Aggregations