Helius
2021-02-26 de04085526eda992155716eda98af621ad681e4e
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
package com.matrix.codeGeneration.ext;
 
import com.matrix.codeGeneration.convert.DefaultTypeHandle;
import com.matrix.codeGeneration.model.OutDataSource;
import com.matrix.codeGeneration.model.PropertyColumn;
import com.matrix.codeGeneration.model.TableClassModel;
import com.matrix.codeGeneration.plugin.ExcelImport;
import org.apache.commons.lang.StringUtils;
 
import java.io.File;
import java.io.IOException;
import java.util.*;
 
/**
 * excel数据源
 * 
 * @author jiangyouyao
 *
 */
public class ExcelDataSource extends OutDataSource {
    // excel文件地址
    private String sourcePath;
    private String author;
 
    private String dbType;
 
    public ExcelDataSource() {
    }
 
    @Override
    public List<TableClassModel> convertToTableModel() {
 
        Map<String, List<List<Object>>> analysisExcel = new HashMap<>();
        // 源文件路径
        File sourceDir = new File(sourcePath);
        if (!sourceDir.isDirectory()) {
            new Exception("目标目录不存在");
        }
        File[] files = sourceDir.listFiles();
        if (files != null) {
            for (File file : files) {
                if (file.canRead()) {// 是否为一个可读文件
                    String fileName = file.getName();
                    // 检查扩展名称
                    String extension = fileName.lastIndexOf(".") == -1 ? ""
                            : fileName.substring(fileName.lastIndexOf(".") + 1);
                    if (extension.equals("xls") || extension.equals("xlsx")) {
                        System.out.println(fileName);
                        // 读取excel文件的内容
                        try {
                            analysisExcel.putAll(ExcelImport.read2007ExcelAllShell(file, null, 6));
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                } else {
                    new RuntimeException(file.getName() + "文件不可读");
                }
            }
        }
        List<TableClassModel> models = analysisExcel(analysisExcel);
        this.setTableClassModels(models);
        return this.getTableClassModels();
    }
 
    /**
     * 解析excel文件中数据表信息
     * 
     * @param analysisExcel
     * @return
     */
    private List<TableClassModel> analysisExcel(Map<String, List<List<Object>>> analysisExcel) {
        List<TableClassModel> models = new ArrayList<>();
        Set<String> keys = analysisExcel.keySet();
        // 除了第一行,其他每一行代表一个字段,一个key是一个表
        for (String tableName : keys) {
            TableClassModel model = new TableClassModel();
            List<List<Object>> data = analysisExcel.get(tableName);
            // 设置数据表基本信息
            // 构建表名
 
            int nameIndex = tableName.indexOf("(");
            model.setTableMemo(tableName.substring(nameIndex+1, tableName.length()-1));
            model.setTableName(tableName.substring(0, nameIndex));
            // 设置类名和变量名称
            model.setClassName(getNameConvert().tableNameToClassName(model.getTableName()));
            model.setClassVariableName(getNameConvert().classNameToVariableName(model.getClassName()));
             
            // 该表需要展示到页面的字段个数
            int showCount=0;
            // 遍历设置字段信息
            for (int i = 1; i < data.size(); i++) {
                List<Object> row = data.get(i);
                PropertyColumn p = new PropertyColumn();
                // 列名
                String columnName = (String) row.get(0);
                p.setColumn(columnName);
                // 属性名称
                p.setProperty(getNameConvert().columnToProperty(columnName));
 
                // 字段类型、长度
                String type = (String) row.get(2);
                p.setFullJdbcType(type);
 
                // jdbc类型设置
                int index = type.indexOf("(") > 0 ? type.indexOf("(") : type.length();
 
                p.setJdbcType(type.substring(0, index).trim().replaceAll("2", ""));
                System.out.println("jdbctype=" + p.getJdbcType());
                // 基本类型
                p.setClassType(DefaultTypeHandle.TypeMapping.get(dbType).get(p.getJdbcType()));
 
                String isAllowNull = (String) row.get(3);
 
                if (isAllowNull != null && isAllowNull.equals("N")) {
                    isAllowNull = " NOT NULL ";
                } else {
                    isAllowNull = "";
                }
 
                p.setIsAllowNull(isAllowNull);
 
                // 备注中 [表示界面显示字段] [表示必填字段*]
                String memo = (String) row.get(1);
                int begin = memo.indexOf("[");
                int end = memo.indexOf("]");
 
                if (begin > -1 && end - begin > 1) {
                    showCount++;
                    // 显示到页面上的字段
                    String subString1 = memo.substring(begin, end);
                    boolean isNecessary = subString1.indexOf("*") > 0;
                    if (isNecessary) {
                        p.setShowName(memo.substring(begin + 1, end - 1));
                    } else {
                        p.setShowName(memo.substring(begin + 1, end));
                    }
                    p.setIsNecessary(isNecessary);
                    p.setIsVisible(true);
                    // 截取[符号前面的字段作为数据库的备注
                    p.setMemo(memo.substring(0, begin));
                } else {
                    p.setIsNecessary(false);
                    p.setIsVisible(false);
                    p.setMemo(memo);
                }
                // get,set方法名称
                p.setMethodName(getNameConvert().propertyToMethod(p.getProperty()));
 
                // 设置主键
                if (p.getMemo() != null && p.getMemo().equals("主键")) {
                    p.setIsPrimaryKey(true);
                    model.setPrimaryKey(p);
                } else {
                    p.setIsPrimaryKey(false);
                }
 
                // 索引
                String indexName = (String) row.get(5);
                if(null == indexName || "".equals(indexName)){
                    indexName = null;
                }
                p.setIndexName(indexName);
 
                // 设置jsp编辑页面字段长度限制
                String columnLengthStr = row.get(6) == null ? null : (String) row.get(6);
                if (columnLengthStr!=null && columnLengthStr.trim().length()>0) {
                    // 从excel中读取的数字默认是double类型的,所以做如下处理去掉小数点
                    Double columnLength = Double.parseDouble(columnLengthStr);
                    p.setColumnLength(columnLength.intValue()+"");
                }
 
                model.getMapping().add(p);
            }
            // 需要加上该表有多少个显示到list列表的字段个数
            model.setShowCount(showCount);
            models.add(model);
        }
        System.out.println(analysisExcel);
        return models;
    }
 
    public String getSourcePath() {
        return sourcePath;
    }
 
    public void setDbType(String dbType) {
        this.dbType = dbType;
    }
 
    public void setSourcePath(String sourcePath) {
        this.sourcePath = sourcePath;
    }
 
    public void setAuthor(String author) {
        this.author = author;
    }
 
    public String getAuthor() {
        return author;
    }
 
    public String getDbType() {
        return dbType;
    }
 
}