Search in sources :

Example 1 with SimpleQueryBuilder

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));
}
Also used : Field(java.lang.reflect.Field) SimpleQueryBuilder(com.github.yiuman.citrus.support.crud.query.builder.SimpleQueryBuilder) Method(java.lang.reflect.Method)

Example 2 with SimpleQueryBuilder

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);
    }
}
Also used : ValidateUtils(com.github.yiuman.citrus.support.utils.ValidateUtils) ObjectUtil(cn.hutool.core.util.ObjectUtil) QueryHelper(com.github.yiuman.citrus.support.crud.query.QueryHelper) Page(com.github.yiuman.citrus.support.model.Page) ValidateException(cn.hutool.core.exceptions.ValidateException) ArrayList(java.util.ArrayList) HttpServletRequest(javax.servlet.http.HttpServletRequest) InjectAnnotationParserHolder(com.github.yiuman.citrus.support.inject.InjectAnnotationParserHolder) Fn(com.github.yiuman.citrus.support.crud.query.Fn) SpringUtils(com.github.yiuman.citrus.support.utils.SpringUtils) HttpServletResponse(javax.servlet.http.HttpServletResponse) Serializable(java.io.Serializable) Objects(java.util.Objects) Consumer(java.util.function.Consumer) LambdaUtils(com.github.yiuman.citrus.support.utils.LambdaUtils) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) Query(com.github.yiuman.citrus.support.crud.query.Query) QueryBuilders(com.github.yiuman.citrus.support.crud.query.builder.QueryBuilders) SimpleQueryBuilder(com.github.yiuman.citrus.support.crud.query.builder.SimpleQueryBuilder) Modifier(java.lang.reflect.Modifier) Optional(java.util.Optional) ViewHelper(com.github.yiuman.citrus.support.crud.view.ViewHelper) WebUtils(com.github.yiuman.citrus.support.utils.WebUtils) SortBy(com.github.yiuman.citrus.support.model.SortBy) BeanUtils(org.springframework.beans.BeanUtils) SortBy(com.github.yiuman.citrus.support.model.SortBy) SimpleQueryBuilder(com.github.yiuman.citrus.support.crud.query.builder.SimpleQueryBuilder)

Example 3 with SimpleQueryBuilder

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("-"));
}
Also used : Table(com.github.yiuman.citrus.mda.entity.Table) SimpleQueryBuilder(com.github.yiuman.citrus.support.crud.query.builder.SimpleQueryBuilder) IdType(com.baomidou.mybatisplus.annotation.IdType) MdaException(com.github.yiuman.citrus.mda.exception.MdaException) IdentifierGenerator(com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator)

Aggregations

SimpleQueryBuilder (com.github.yiuman.citrus.support.crud.query.builder.SimpleQueryBuilder)3 ValidateException (cn.hutool.core.exceptions.ValidateException)1 ObjectUtil (cn.hutool.core.util.ObjectUtil)1 IdType (com.baomidou.mybatisplus.annotation.IdType)1 IdentifierGenerator (com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator)1 Table (com.github.yiuman.citrus.mda.entity.Table)1 MdaException (com.github.yiuman.citrus.mda.exception.MdaException)1 Fn (com.github.yiuman.citrus.support.crud.query.Fn)1 Query (com.github.yiuman.citrus.support.crud.query.Query)1 QueryHelper (com.github.yiuman.citrus.support.crud.query.QueryHelper)1 QueryBuilders (com.github.yiuman.citrus.support.crud.query.builder.QueryBuilders)1 ViewHelper (com.github.yiuman.citrus.support.crud.view.ViewHelper)1 InjectAnnotationParserHolder (com.github.yiuman.citrus.support.inject.InjectAnnotationParserHolder)1 Page (com.github.yiuman.citrus.support.model.Page)1 SortBy (com.github.yiuman.citrus.support.model.SortBy)1 LambdaUtils (com.github.yiuman.citrus.support.utils.LambdaUtils)1 SpringUtils (com.github.yiuman.citrus.support.utils.SpringUtils)1 ValidateUtils (com.github.yiuman.citrus.support.utils.ValidateUtils)1 WebUtils (com.github.yiuman.citrus.support.utils.WebUtils)1 Serializable (java.io.Serializable)1