yangys
2025-09-22 12313d5b8a2b6eaa2c7562291cf08f23644ef1d7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/**
 * BladeX Commercial License Agreement
 * Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
 * <p>
 * Use of this software is governed by the Commercial License Agreement
 * obtained after purchasing a license from BladeX.
 * <p>
 * 1. This software is for development use only under a valid license
 * from BladeX.
 * <p>
 * 2. Redistribution of this software's source code to any third party
 * without a commercial license is strictly prohibited.
 * <p>
 * 3. Licensees may copyright their own code but cannot use segments
 * from this software for such purposes. Copyright of this software
 * remains with BladeX.
 * <p>
 * Using this software signifies agreement to this License, and the software
 * must not be used for illegal purposes.
 * <p>
 * THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
 * not liable for any claims arising from secondary or illegal development.
 * <p>
 * Author: Chill Zhuang (bladejava@qq.com)
 */
package org.springblade.develop.service.impl;
 
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.builder.ConfigBuilder;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import lombok.RequiredArgsConstructor;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.tool.utils.StringPool;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.develop.mapper.ModelPrototypeMapper;
import org.springblade.develop.pojo.entity.Datasource;
import org.springblade.develop.pojo.entity.ModelPrototype;
import org.springblade.develop.service.IDatasourceService;
import org.springblade.develop.service.IModelPrototypeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.Iterator;
import java.util.List;
 
/**
 * 数据原型表 服务实现类
 *
 * @author Chill
 */
@Service
@RequiredArgsConstructor
public class ModelPrototypeServiceImpl extends BaseServiceImpl<ModelPrototypeMapper, ModelPrototype> implements IModelPrototypeService {
 
    private final IDatasourceService datasourceService;
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean submitList(List<ModelPrototype> modelPrototypes) {
        modelPrototypes.forEach(modelPrototype -> {
            if (modelPrototype.getId() == null) {
                this.save(modelPrototype);
            } else {
                this.updateById(modelPrototype);
            }
        });
        return true;
    }
 
    @Override
    public List<ModelPrototype> prototypeList(Long modelId) {
        return this.list(Wrappers.<ModelPrototype>lambdaQuery().eq(ModelPrototype::getModelId, modelId));
    }
 
    @Override
    public TableInfo getTableInfo(String tableName, Long datasourceId) {
        Datasource datasource = datasourceService.getById(datasourceId);
        ConfigBuilder config = getConfigBuilder(datasource, tableName);
        List<TableInfo> tableInfoList = config.getTableInfoList();
        TableInfo tableInfo = null;
        Iterator<TableInfo> iterator = tableInfoList.stream().filter(table -> table.getName().equals(tableName)).toList().iterator();
        if (iterator.hasNext()) {
            tableInfo = iterator.next();
            if (tableName.contains(StringPool.UNDERSCORE)) {
                String entityPrefix = StringUtil.firstCharToUpper(tableName.split(StringPool.UNDERSCORE)[0]);
                String entityName = StringUtil.removePrefix(tableInfo.getEntityName(), entityPrefix);
                tableInfo.setEntityName(entityName);
            } else {
                tableInfo.setEntityName(StringUtil.firstCharToUpper(tableName));
            }
        }
        return tableInfo;
    }
 
    @Override
    public ConfigBuilder getConfigBuilder(Datasource datasource, String tableName) {
        StrategyConfig.Builder builder = new StrategyConfig.Builder();
        //表前缀过滤,目前官方仅支持一个前缀,可自行修改为sys_或tb_或其他业务表前缀
        //builder.likeTable(new LikeTable("blade_", SqlLike.RIGHT));
        if (StringUtil.isNotBlank(tableName)) {
            builder.addInclude(tableName);
        }
        StrategyConfig strategyConfig = builder.entityBuilder()
            .naming(NamingStrategy.underline_to_camel)
            .columnNaming(NamingStrategy.underline_to_camel).build();
        DataSourceConfig datasourceConfig = new DataSourceConfig.Builder(
            datasource.getUrl(), datasource.getUsername(), datasource.getPassword()
        ).build();
        return new ConfigBuilder(null, datasourceConfig, strategyConfig, null, null, null);
    }
 
}