wuxw
2019-08-10 c8d3216d510e0330a7b010dbdbfb58debcf4b328
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
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);
 
        //处理查询条件
        JSONArray tmpConditions = data.getJSONArray("conditions");
 
        StringBuffer conditionInput = new StringBuffer();
        StringBuffer vcCreate = new StringBuffer();
 
        for (int condIndex = 0; condIndex < tmpConditions.size(); condIndex++) {
 
            JSONObject tmpCond = tmpConditions.getJSONObject(condIndex);
 
            if (condIndex % 3 == 0) {
                conditionInput.append("<div class=\"row\">\n");
            }
 
            if (condIndex % 3 == 0 || condIndex % 3 == 1) {
                conditionInput.append("<div class=\"col-sm-4\" ");
            } else if (condIndex % 3 == 2) {
                conditionInput.append("<div class=\"col-sm-3\" ");
            }
 
            if (condIndex > 2) {
                conditionInput.append("v-if=\"" + data.getString("templateCode") + "ManageInfo.moreCondition == true\">\n");
            } else {
                conditionInput.append(">\n");
            }
 
            if ("choose".equals(tmpCond.getString("inputType"))) {
                conditionInput.append("                            <div class=\"form-group input-group\">\n" +
                        "                                <input type=\"text\" placeholder=\"请选择" + tmpCond.getString("name") +
                        "\" v-model=\"" + data.getString("templateCode") + "ManageInfo.conditions." + tmpCond.getString("code") + "\" class=\" form-control\">\n" +
                        "                                <div class=\"input-group-prepend\">\n" +
                        "                                    <button type=\"button\" class=\"btn btn-primary btn-sm\" v-on:click=\"_open" + toUpperCaseFirstOne(tmpCond.getString("componentName")) + "Method()\"><i\n" +
                        "                                            class=\"glyphicon glyphicon-search\"></i> 选择\n" +
                        "                                    </button>\n" +
                        "                                </div>\n" +
                        "                            </div>\n");
 
                if (tmpCond.containsKey("componentName")) {
                    vcCreate.append("<vc:create name=\"" + tmpCond.getString("componentName") + "\"\n" +
                            "               emit" + toUpperCaseFirstOne(tmpCond.getString("componentName")) + "=\"" + data.getString("templateCode") + "Manage\"\n" +
                            "               emitLoadData=\"" + data.getString("templateCode") + "Manage\"\n" +
                            "    ></vc:create>\n");
 
                }
 
            } else if ("input".equals(tmpCond.getString("inputType"))) {
                conditionInput.append("<div class=\"form-group\">\n" +
                        "                                <input type=\"text\" placeholder=\"请输入" + tmpCond.getString("name") + "\" v-model=\"" + data.getString("templateCode") + "ManageInfo.conditions." + tmpCond.getString("code") + "\" class=\" form-control\">\n" +
                        "                            </div>");
            }
 
            conditionInput.append("                        </div>");
            if (condIndex == 2) {
                conditionInput.append("<div class=\"col-sm-1\">\n" +
                        "                            <button type=\"button\" class=\"btn btn-primary btn-sm\" v-on:click=\"_query" + toUpperCaseFirstOne(data.getString("templateCode")) + "Method()\">\n" +
                        "                                    <i class=\"glyphicon glyphicon-search\"></i> 查询\n" +
                        "                            </button>\n" +
                        "                        </div>");
            } else if (condIndex % 3 == 2) {
                conditionInput.append("<div class=\"col-sm-1\">\n" +
                        "                        </div>");
 
            }
 
 
            if (condIndex % 3 == 2 || condIndex == tmpConditions.size() - 1) {
                conditionInput.append("</div>\n");
            }
        }
 
        fileContext = fileContext.replace("@@conditionInput@@", conditionInput.toString());
        fileContext = fileContext.replace("@@vcCreate@@", vcCreate.toString());
 
        if (tmpConditions.size() > 3) {
            fileContext = fileContext.replace("@@moreCondition@@", "<button type=\"button\"  class=\"btn btn-link btn-sm\" style=\"margin-right:10px;\"  v-on:click=\"_moreCondition()\">更多</button>");
        } else {
            fileContext = fileContext.replace("@@moreCondition@@", "");
 
        }
 
        // 处理 th 信息
 
        StringBuffer thSb = new StringBuffer();
        StringBuffer tdSb = new StringBuffer();
        thSb.append("                            <th class=\"text-center\">" + data.getString("templateKeyName") + "</th>\n");
 
        tdSb.append("                            <td class=\"text-center\">{{" + 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);
            if (column.getBoolean("show")) {
                thSb.append("                            <th class=\"text-center\">" + column.getString("cnCode") + "</th>\n");
                tdSb.append("                            <td class=\"text-center\">{{" + data.getString("templateCode") + "." + column.getString("code") + "}}</td>\n");
            }
        }
        thSb.append("                            <th class=\"text-center\">操作</th>\n");
        tdSb.append("                            <td class=\"text-center\"><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") + "Package/" + 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);
 
        //处理查询条件
        JSONArray tmpConditions = data.getJSONArray("conditions");
 
        StringBuffer conditionInput = new StringBuffer();
 
        StringBuffer conditionMethod = new StringBuffer();
 
        StringBuffer conditionEvent = new StringBuffer();
 
        for (int condIndex = 0; condIndex < tmpConditions.size(); condIndex++) {
 
            JSONObject tmpCond = tmpConditions.getJSONObject(condIndex);
            conditionInput.append(tmpCond.getString("code") + ":'',\n");
            if(tmpCond.containsKey("key")){
                conditionInput.append(tmpCond.getString("key") + ":'',\n");
            }
 
            if (tmpCond.containsKey("componentName")) {
                conditionMethod.append(",\n _open" + toUpperCaseFirstOne(tmpCond.getString("componentName")) + "Method:function(){\n" +
                        "                vc.emit('" + tmpCond.getString("componentName") + "','open" + toUpperCaseFirstOne(tmpCond.getString("componentName")) + "Model',{});\n" +
                        "\n" +
                        "            }"
                );
 
                conditionEvent.append("vc.on('" + data.getString("templateCode") + "Manage','" + tmpCond.getString("componentName") + "',function(_param){\n" +
                        "              vc.copyObject(_param,vc.component." + data.getString("templateCode") + "ManageInfo.conditions);\n" +
                        "            });\n");
            }
 
        }
 
        fileContext = fileContext.replace("@@conditions@@", conditionInput.toString());
        fileContext = fileContext.replace("@@extendMethods@@", conditionMethod.toString());
        fileContext = fileContext.replace("@@extendEvent@@", conditionEvent.toString());
 
        String writePath = this.getClass().getResource("/").getPath()
                + "out/web/component/" + data.getString("templateCode") + "Package/" + 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);
    }
 
}