zm
2020-05-18 a18bfacbf56b401f6e0fdae8710fbca4df8cff77
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package cn.autoform.web.controller.datasource;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
 
import cn.autoform.db.entity.DBConnectionManagerEntity;
import cn.autoform.db.entity.FieldPropertyEntity;
import cn.autoform.db.entity.FormdatsourceEntity;
import cn.autoform.fw.exception.RestResult;
import cn.autoform.fw.service.DatabaseMetaDateService;
import cn.autoform.fw.utility.RestResultGenerator;
import cn.autoform.web.service.datasource.DatasourceService;
 
/**
 * 数据源信息管理
 * @author liumy
 *
 * @param <T>
 */
@RestController
public class DatasourceController<T> {
 
    @Autowired
    private DatasourceService datasourceService = null;
 
    @Autowired
    private DatabaseMetaDateService databaseMetaDateService = null;
 
    /**
     * 初期化
     * 
     * @param tenantID
     *            租户ID
     * @param formid
     *            表单ID
     * @return 表单数据源
     */
    @RequestMapping(value = "${requset.datasource01}", method = RequestMethod.GET)
    public RestResult<List<FormdatsourceEntity>> getFormdatsourceListService(String tenantID, String formID) {
 
        List<FormdatsourceEntity> rsList = datasourceService.getFormdatsourceList(tenantID, formID);
        return RestResultGenerator.genSuccessResult(rsList);
    }
 
    /**
     * 新增一条数据源
     * 
     * @param tenantID
     * @param formID
     * @param dataSourceName
     * @param databaseName
     * @param datasource
     * @param createUser
     * @return
     */
    @RequestMapping(value = "${requset.datasource02}", method = RequestMethod.POST)
    public RestResult<String> createFormdatsourceService(@RequestBody FormdatsourceEntity formdatsource) {
 
        int nRs = datasourceService.createFormdatsource(formdatsource);
        return RestResultGenerator.genSuccessResult(String.valueOf(nRs));
    }
 
    /**
     * 删除一条数据源
     * 
     * @param tenantID
     * @param formID
     * @param dataSourceName
     * @return
     */
    @RequestMapping(value = "${requset.datasource03}", method = RequestMethod.GET)
    public RestResult<String> deleteFormdatsourceService(String tenantID, String formID, String dataSourceName) {
        int nRs = datasourceService.deleteFormdatsource(tenantID, formID, dataSourceName);
        return RestResultGenerator.genSuccessResult(String.valueOf(nRs));
    }
 
    /**
     * 数据连接名取得
     * 
     * @param tenantID
     * @return
     */
    @RequestMapping(value = "${requset.datasource04}", method = RequestMethod.GET)
    public RestResult<List<String>> getDataConnectionNameService(String tenantID) {
        List<String> rsList = datasourceService.getDataConnectionName(tenantID);
        return RestResultGenerator.genSuccessResult(rsList);
    }
 
    /**
     * 数据源详细取得
     * 
     * @param tenantID
     * @param dataConnectionName
     * @return
     * @throws Exception 
     */
    @RequestMapping(value = "${requset.datasource05}", method = RequestMethod.GET)
    public RestResult<List<String>> getDatasourceListService(String tenantID, String dataConnectionName) {
        System.out.println(tenantID);
        List<String> rsList = databaseMetaDateService.getDatabaseTableName(tenantID, dataConnectionName);
 
        return RestResultGenerator.genSuccessResult(rsList);
    }
 
    /**
     * 数据库詳細JSON取得(表单设计用)
     * 
     * @param datasource
     * @param tenantID
     * @param dataConnectionName
     * @return
     * @throws Exception 
     */
    @RequestMapping(value = "${requset.datasource06}", method = RequestMethod.GET)
    public RestResult<List<Map<String,List<String>>>> getDatasourceColumnsNameService(String formID, String tenantID) {
        
        List<Map<String,List<String>>> rsList = new ArrayList<Map<String,List<String>>>();
        // 当前表单下所有数据源取得
        List<FormdatsourceEntity> listDataSource = datasourceService.getFormdatsourceList(tenantID, formID);
        for(FormdatsourceEntity formdatsource : listDataSource) {
            // 数据源名
            String datasourceName = formdatsource.getDataSourceName();
            // 数据库名
            String databaseName = formdatsource.getDatabaseName();
            // 已选择的数据表名
            String tableName = formdatsource.getDatasource();
            // 获取所有列名List
            List<String> listColumns = databaseMetaDateService.getDatabaseColumnsName(tenantID, databaseName, tableName);
            
            Map<String,List<String>> mpTableMap = new HashMap<String,List<String>>();
            
            mpTableMap.put(datasourceName, listColumns);
            
            rsList.add(mpTableMap);
            
        }
        
        return RestResultGenerator.genSuccessResult(rsList);
 
    }
 
    /**
     * 新增一条数据连接
     * 
     * @param dBConnectionManagerEntity
     * @return
     */
    @RequestMapping(value = "${requset.datasource07}", method = RequestMethod.POST)
    public RestResult<String> createDBconnectionService(
            @RequestBody DBConnectionManagerEntity dBConnectionManagerEntity) {
        int nRs = datasourceService.createDBconnection(dBConnectionManagerEntity);
        
        return RestResultGenerator.genSuccessResult(String.valueOf(nRs));
    }
 
//    @RequestMapping(value = "${requset.datasource08}", method = RequestMethod.GET)
//    public RestResult<Map<String,List<String>>> getDatasourceColumnsValueService1(String tenantID, String formID, String dataSourceName, String columnNames) {
//        
//        List<FormdatsourceEntity> listDataSource = datasourceService.getDatasource(tenantID, formID, dataSourceName);
//        Map<String,List<String>> mpColumnInfo = new HashMap<String,List<String>>();
//        
//        FormdatsourceEntity formdatsource = listDataSource.get(0);
//        
//        String[] arrColumns = columnNames.split(",");
//        
//        for(String column : arrColumns) {
////            
////            List<String> listColumnValue = databaseMetaDateService.getColumnsInfo(formdatsource, column);    
////            mpColumnInfo.put(column, listColumnValue);
//            
//        }
//        
//        return RestResultGenerator.genSuccessResult(mpColumnInfo);
//    }
    
    /**
     * 获取列详细值
     * @param tenantID 租户
     * @param databaseName 数据连接名
     * @param datasource 表名
     * @param columnNames 列名
     * @return
     * @throws Exception 
     */
    @RequestMapping(value = "${requset.datasource08}", method = RequestMethod.GET)
    public RestResult<Map<String,List<String>>> getDatasourceColumnsValueService(String tenantID, String databaseName, String datasource, String columnNames) {
        
        String[] arrColumns = columnNames.split(",");
        Map<String,List<String>> mpColumnInfo = new HashMap<String,List<String>>();
        
        for(String column : arrColumns) {
            
            List<String> listColumnValue = databaseMetaDateService.getColumnsInfo(tenantID,databaseName, datasource,column);    
            mpColumnInfo.put(column, listColumnValue);
            
        }
        
        return RestResultGenerator.genSuccessResult(mpColumnInfo);
    }
    
    /**
     * 获取当前表单数据源详细信息
     * @param tenantID
     * @param formID
     * @return
     * @throws Exception 
     */
    @RequestMapping(value = "${requset.datasource09}", method = RequestMethod.GET)
    public RestResult<List<Map<String, List<Map<String,List<Map<String,List<String>>>>>>>> getDatasourceInfoService(String tenantID, String formID){
        // 当前表单下所有数据源取得
        List<FormdatsourceEntity> listDataSource = datasourceService.getFormdatsourceList(tenantID, formID);
        
        List<Map<String, List<Map<String,List<Map<String,List<String>>>>>>> rsList = new ArrayList<Map<String, List<Map<String,List<Map<String,List<String>>>>>>>();
        
        for(FormdatsourceEntity formdatsource : listDataSource) {
            // 数据源名
            String datasourceName = formdatsource.getDataSourceName();
            // 数据库名
            String databaseName = formdatsource.getDatabaseName();
            // 已选择的数据表名
            String tableName = formdatsource.getDatasource();
            
            Map<String,List<String>> mpColumn = databaseMetaDateService.getDatabaseColumnsInfo(tenantID, databaseName, tableName);
            
            List<Map<String,List<String>>> listColumnInfo = new ArrayList<Map<String,List<String>>>();
            
            listColumnInfo.add(mpColumn);
            
            Map<String,List<Map<String,List<String>>>> mpTable = new HashMap<String,List<Map<String,List<String>>>>();
            
            mpTable.put(tableName, listColumnInfo);
            
            List<Map<String,List<Map<String,List<String>>>>> listTables = new ArrayList<Map<String,List<Map<String,List<String>>>>>();
            
            listTables.add(mpTable);
            
            Map<String, List<Map<String,List<Map<String,List<String>>>>>> mpDatabase = new HashMap<String, List<Map<String,List<Map<String,List<String>>>>>>();
            
            mpDatabase.put(datasourceName, listTables);
            rsList.add(mpDatabase);
        }
        
        
        return RestResultGenerator.genSuccessResult(rsList);
    }
    
    /**
     * 字段数据源取得服务(发布用)
     * @param tenantID
     * @param formID
     * @return
     */
    @RequestMapping(value = "${requset.datasource10}", method = RequestMethod.GET)
    public RestResult<List<Map<String,Map<String,String>>>> getFieldDatasourceInfoService(String tenantID, String formID ) {
        
        List<Map<String,Map<String,String>>> rsList = new ArrayList<Map<String,Map<String,String>>>();
        // 取得当前表单字段属性datasourcetype为dataSource的所有字段信息  datasource textcolumn valuecolumn namecolumn
        // 设定数据源的字段column取得
        List<FieldPropertyEntity> listFieldProper = datasourceService.getColumnNum(tenantID, formID);
        for(FieldPropertyEntity fieldProperty : listFieldProper) {
 
            //  
            int columnOrderNum = fieldProperty.getColumnOrderNum();
            
            // 别名取得
            String fieldKey = datasourceService.getFieldkeyList(tenantID, formID, columnOrderNum).get(0).getFieldKey();
            
            // 数据源名
            String datasource = datasourceService.getFieldPropertyList(tenantID, formID, columnOrderNum, "datasource").get(0).getValue();
            // 列1名
            String textcolumn = datasourceService.getFieldPropertyList(tenantID, formID, columnOrderNum, "textcolumn").get(0).getValue();
            // 列2名
            String valuecolumn = datasourceService.getFieldPropertyList(tenantID, formID, columnOrderNum, "valuecolumn").get(0).getValue();
            // 列3
            String namecolumn = "";
            if(!datasourceService.getFieldPropertyList(tenantID, formID, columnOrderNum, "namecolumn").isEmpty()) {
                namecolumn = datasourceService.getFieldPropertyList(tenantID, formID, columnOrderNum, "namecolumn").get(0).getValue();
            }
            
            Map<String,String> mpFieldInfo = new HashMap<String,String>();
            
            mpFieldInfo.put("datasource", datasource);
            
            mpFieldInfo.put("textcolumn", textcolumn);
            
            mpFieldInfo.put("valuecolumn", valuecolumn);
            
            mpFieldInfo.put("namecolumn", namecolumn);
            
            Map<String,Map<String,String>> mpField = new HashMap<String,Map<String,String>>();
            
            mpField.put(fieldKey, mpFieldInfo);
            
            rsList.add(mpField);
        }
        
        return RestResultGenerator.genSuccessResult(rsList);
    }
}