Search in sources :

Example 1 with Table

use of com.github.yiuman.citrus.mda.entity.Table in project citrus by Yiuman.

the class HistoricConverter method convert.

@Override
public HistoryTable convert(Table table) {
    HistoryTable historyTable = Converter.super.convert(table);
    historyTable.setUuid(null);
    historyTable.setTableUuid(table.getUuid());
    List<Column> columnList = TableRelUtils.getTableRelInfos(table.getUuid(), Column.class);
    if (!CollectionUtils.isEmpty(columnList)) {
        historyTable.setColumns(columnList.parallelStream().map(LambdaUtils.functionWrapper(column -> ConvertUtils.convert(HistoryColumn.class, column))).collect(Collectors.toList()));
    }
    List<Indexes> indexesList = TableRelUtils.getTableRelInfos(table.getUuid(), Indexes.class);
    if (!CollectionUtils.isEmpty(indexesList)) {
        historyTable.setIndexes(indexesList.parallelStream().map(LambdaUtils.functionWrapper(indexes -> ConvertUtils.convert(HistoryIndexes.class, indexes))).collect(Collectors.toList()));
    }
    return historyTable;
}
Also used : HistoryTable(com.github.yiuman.citrus.mda.entity.history.HistoryTable) ConvertUtils(com.github.yiuman.citrus.support.utils.ConvertUtils) Collectors(java.util.stream.Collectors) Indexes(com.github.yiuman.citrus.mda.entity.Indexes) LambdaUtils(com.github.yiuman.citrus.support.utils.LambdaUtils) Table(com.github.yiuman.citrus.mda.entity.Table) HistoryColumn(com.github.yiuman.citrus.mda.entity.history.HistoryColumn) List(java.util.List) HistoryIndexes(com.github.yiuman.citrus.mda.entity.history.HistoryIndexes) CollectionUtils(org.springframework.util.CollectionUtils) Converter(com.github.yiuman.citrus.mda.converter.Converter) Column(com.github.yiuman.citrus.mda.entity.Column) TableRelUtils(com.github.yiuman.citrus.mda.converter.TableRelUtils) HistoryColumn(com.github.yiuman.citrus.mda.entity.history.HistoryColumn) Column(com.github.yiuman.citrus.mda.entity.Column) Indexes(com.github.yiuman.citrus.mda.entity.Indexes) HistoryIndexes(com.github.yiuman.citrus.mda.entity.history.HistoryIndexes) HistoryTable(com.github.yiuman.citrus.mda.entity.history.HistoryTable)

Example 2 with Table

use of com.github.yiuman.citrus.mda.entity.Table in project citrus by Yiuman.

the class TableEntityServiceImpl method beforeSave.

@Override
public boolean beforeSave(Table entity) throws Exception {
    // 实体变更前保存一份历史记录
    if (StringUtils.isBlank(entity.getUuid())) {
        Table table = get(entity.getUuid());
        if (Objects.nonNull(table)) {
            CrudMapper<HistoryTable> historyTableCrudMapper = getHistoryTableCrudMapper();
            HistoryTable historyTable = entity2History(table);
            historyTableCrudMapper.saveEntity(historyTable);
            TableRelUtils.saveTableRelInfos(historyTable.getColumns(), historyTable.getUuid());
            TableRelUtils.saveTableRelInfos(historyTable.getIndexes(), historyTable.getUuid());
        }
    }
    return true;
}
Also used : HistoryTable(com.github.yiuman.citrus.mda.entity.history.HistoryTable) Table(com.github.yiuman.citrus.mda.entity.Table) HistoryTable(com.github.yiuman.citrus.mda.entity.history.HistoryTable)

Example 3 with Table

use of com.github.yiuman.citrus.mda.entity.Table in project citrus by Yiuman.

the class MdaServiceImpl method getKeyColumn.

@Override
public String getKeyColumn() {
    Table tableEntity = getTableEntity(getModelId(WebUtils.getRequest()));
    TableMeta tableMeta = entity2Meta(tableEntity);
    return getKeyColumn(tableMeta);
}
Also used : Table(com.github.yiuman.citrus.mda.entity.Table)

Example 4 with Table

use of com.github.yiuman.citrus.mda.entity.Table 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

Table (com.github.yiuman.citrus.mda.entity.Table)4 HistoryTable (com.github.yiuman.citrus.mda.entity.history.HistoryTable)2 IdType (com.baomidou.mybatisplus.annotation.IdType)1 IdentifierGenerator (com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator)1 Converter (com.github.yiuman.citrus.mda.converter.Converter)1 TableRelUtils (com.github.yiuman.citrus.mda.converter.TableRelUtils)1 Column (com.github.yiuman.citrus.mda.entity.Column)1 Indexes (com.github.yiuman.citrus.mda.entity.Indexes)1 HistoryColumn (com.github.yiuman.citrus.mda.entity.history.HistoryColumn)1 HistoryIndexes (com.github.yiuman.citrus.mda.entity.history.HistoryIndexes)1 MdaException (com.github.yiuman.citrus.mda.exception.MdaException)1 SimpleQueryBuilder (com.github.yiuman.citrus.support.crud.query.builder.SimpleQueryBuilder)1 ConvertUtils (com.github.yiuman.citrus.support.utils.ConvertUtils)1 LambdaUtils (com.github.yiuman.citrus.support.utils.LambdaUtils)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 CollectionUtils (org.springframework.util.CollectionUtils)1