吴学文
2019-07-04 c21650fd560d0f8c84d96a0add6b96cba48b69a8
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
package com.java110.code.web;
 
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.java110.code.BaseGenerator;
 
public class GeneratorManagerComponent extends BaseGenerator {
 
    public void generator(JSONObject data) {
 
        //处理组件
        generatorComponentHtml(data);
        generatorComponentJs(data);
        generatorComponentJava(data);
        genneratorIListSmo(data);
        genneratorListSmoImpl(data);
        genneratorListListener(data);
        genneratorVo(data);
 
        genneratorDataVo(data);
 
 
    }
 
    /**
     * 生成 html js java 类
     *
     * @param data
     */
    private void generatorComponentHtml(JSONObject data) {
 
        StringBuffer sb = readFile(GeneratorStart.class.getResource("/web/manage/manage.html").getFile());
        String fileContext = sb.toString();
 
        fileContext = super.replaceTemplateContext(fileContext, data);
 
        // 处理 th 信息
 
        StringBuffer thSb = new StringBuffer();
        StringBuffer tdSb = new StringBuffer();
        thSb.append("                            <th>" + data.getString("templateKeyName") + "</th>\n");
 
        tdSb.append("                            <td>{{" + data.getString("templateCode") + "." + data.getString("templateKey") + "}}</td>\n");
 
        JSONArray columns = data.getJSONArray("columns");
        for (int columnIndex = 0; columnIndex < columns.size(); columnIndex++) {
            JSONObject column = columns.getJSONObject(columnIndex);
            thSb.append("                            <th>" + column.getString("cnCode") + "</th>\n");
 
            tdSb.append("                            <td>{{" + data.getString("templateCode") + "." + column.getString("code") + "}}</td>\n");
        }
        thSb.append("                            <th class=\"text-right\">操作</th>\n");
        tdSb.append("                            <td><div class=\"btn-group\">\n" +
                "                                    <button class=\"btn-white btn btn-xs\" v-on:click=\"_openEdit"+toUpperCaseFirstOne(data.getString("templateCode"))+"Model("+data.getString("templateCode")+")\">修改</button>\n" +
                "                                </div>\n" +
                "                                <div class=\"btn-group\">\n" +
                "                                    <button class=\"btn-white btn btn-xs\" v-on:click=\"_openDelete"+toUpperCaseFirstOne(data.getString("templateCode"))+"Model("+data.getString("templateCode")+")\">删除</button>\n" +
                "                                </div></td>\n");
 
        fileContext = fileContext.replace("@@columnsCnCode@@", thSb.toString())
                                .replace("@@columnsName@@", tdSb.toString());
 
 
 
        String writePath = this.getClass().getResource("/").getPath()
                +"out/web/component/"+data.getString("templateCode")+"-manage/"+data.getString("templateCode")+"Manage.html";
        System.out.printf("writePath: " + writePath);
        writeFile(writePath,
                fileContext);
 
 
    }
 
    /**
     * 生成 html js java 类
     *
     * @param data
     */
    private void generatorComponentJs(JSONObject data) {
 
        StringBuffer sb = readFile(GeneratorStart.class.getResource("/web/manage/manage.js").getFile());
        String fileContext = sb.toString();
 
        fileContext = super.replaceTemplateContext(fileContext, data);
 
        String writePath = this.getClass().getResource("/").getPath()
                +"out/web/component/"+data.getString("templateCode")+"-manage/"+data.getString("templateCode")+"Manage.js";
        System.out.printf("writePath: " + writePath);
        writeFile(writePath,
                fileContext);
 
 
    }
 
    /**
     * 生成 html js java 类
     *
     * @param data
     */
    private void generatorComponentJava(JSONObject data) {
 
        StringBuffer sb = readFile(GeneratorStart.class.getResource("/web/manage/ManageComponent.java").getFile());
        String fileContext = sb.toString();
 
        fileContext = super.replaceTemplateContext(fileContext, data);
 
        String writePath = this.getClass().getResource("/").getPath()
                +"out/web/component/java/"+data.getString("templateCode")+"/"+toUpperCaseFirstOne(data.getString("templateCode"))+"ManageComponent.java";
        System.out.printf("writePath: " + writePath);
        writeFile(writePath,
                fileContext);
 
 
    }
 
    /**
     * 生成接口类
     * @param data
     */
    private void genneratorIListSmo(JSONObject data){
        StringBuffer sb = readFile(GeneratorStart.class.getResource("/web/manage/IListSMO.java").getFile());
        String fileContext = sb.toString();
 
        fileContext = super.replaceTemplateContext(fileContext, data);
 
        String writePath = this.getClass().getResource("/").getPath()
                +"out/web/smo/"+data.getString("templateCode")+"/IList"+toUpperCaseFirstOne(data.getString("templateCode"))+"sSMO.java";
        System.out.printf("writePath: " + writePath);
        writeFile(writePath,
                fileContext);
    }
 
    /**
     * 生成接口类
     * @param data
     */
    private void genneratorListSmoImpl(JSONObject data){
        StringBuffer sb = readFile(GeneratorStart.class.getResource("/web/manage/ListSMOImpl.java").getFile());
        String fileContext = sb.toString();
 
        fileContext = super.replaceTemplateContext(fileContext, data);
 
        String writePath = this.getClass().getResource("/").getPath()
                +"out/web/smo/"+data.getString("templateCode")+"/impl/List"+toUpperCaseFirstOne(data.getString("templateCode"))+"sSMOImpl.java";
        System.out.printf("writePath: " + writePath);
        writeFile(writePath,
                fileContext);
    }
 
    /**
     * 生成API 侦听处理类
     * @param data
     */
    private void genneratorListListener(JSONObject data){
        StringBuffer sb = readFile(GeneratorStart.class.getResource("/web/manage/ListListener.java").getFile());
        String fileContext = sb.toString();
 
        fileContext = super.replaceTemplateContext(fileContext, data);
 
        String writePath = this.getClass().getResource("/").getPath()
                +"out/api/listener/"+data.getString("templateCode")+"/List"+toUpperCaseFirstOne(data.getString("templateCode"))+"sListener.java";
        System.out.printf("writePath: " + writePath);
        writeFile(writePath,
                fileContext);
    }
 
    private void genneratorVo(JSONObject data){
        StringBuffer sb = readFile(GeneratorStart.class.getResource("/web/manage/ApiVo.java").getFile());
        String fileContext = sb.toString();
 
        fileContext = super.replaceTemplateContext(fileContext, data);
 
        String writePath = this.getClass().getResource("/").getPath()
                +"out/api/vo/"+data.getString("templateCode")+"/Api"+toUpperCaseFirstOne(data.getString("templateCode"))+"Vo.java";
        System.out.printf("writePath: " + writePath);
        writeFile(writePath,
                fileContext);
    }
 
    private void genneratorDataVo(JSONObject data){
        StringBuffer sb = readFile(GeneratorStart.class.getResource("/web/manage/ApiDataVo.java").getFile());
        String fileContext = sb.toString();
 
        fileContext = super.replaceTemplateContext(fileContext, data);
        JSONArray columns = data.getJSONArray("columns");
        String variable = "";
        String variableGetSet = "";
 
        variable += "private String " + data.getString("templateKey") + ";\n";
 
        variableGetSet += "public String get" + toUpperCaseFirstOne(data.getString("templateKey")) + "() {\n"
                + "        return " + data.getString("templateKey") + ";\n"
                + "    }\n";
        variableGetSet += "public void set" + toUpperCaseFirstOne(data.getString("templateKey")) + "(String " + data.getString("templateKey") + ") {\n"
                + "        this." + data.getString("templateKey") + " = " + data.getString("templateKey") + ";\n"
                + "    }\n";
 
        for (int columnIndex = 0; columnIndex < columns.size(); columnIndex++) {
            JSONObject column = columns.getJSONObject(columnIndex);
            String key = column.getString("code");
            variable += "private String " + key + ";\n";
 
            variableGetSet += "public String get" + toUpperCaseFirstOne(key) + "() {\n"
                    + "        return " + key + ";\n"
                    + "    }\n";
            variableGetSet += "public void set" + toUpperCaseFirstOne(key) + "(String " + key + ") {\n"
                    + "        this." + key + " = " + key + ";\n"
                    + "    }\n";
        }
 
        fileContext = fileContext.replace("@@templateColumns@@", variable + variableGetSet);
 
        String writePath = this.getClass().getResource("/").getPath()
                +"out/api/vo/"+data.getString("templateCode")+"/Api"+toUpperCaseFirstOne(data.getString("templateCode"))+"DataVo.java";
        System.out.printf("writePath: " + writePath);
        writeFile(writePath,
                fileContext);
    }
 
}