吴学文
2019-07-29 1992e00e12df39dbc0acf1ef037082cbab88d87c
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
package com.java110.code.relationship;
 
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.java110.code.BaseGenerator;
import com.java110.code.web.GeneratorStart;
import org.springframework.util.StringUtils;
 
public class GeneratorBindingComponent extends BaseGenerator {
 
    public void generator(JSONObject data) {
 
        //处理组件
        generatorComponentHtml(data);
        generatorComponentJs(data);
        generatorComponentJava(data);
        genneratorIListSmo(data);
        genneratorListSmoImpl(data);
        //genneratorListListener(data);
 
        //genneratorServiceCodeConstant(data);
 
 
    }
 
    /**
     * 生成常量类
     *
     * @param data
     */
    private void genneratorServiceCodeConstant(JSONObject data) {
        StringBuffer sb = readFile(GeneratorStart.class.getResource("/web/constant/ServiceCodeConstant.java").getFile());
        String fileContext = sb.toString();
 
        fileContext = super.replaceTemplateContext(fileContext, data);
 
        String writePath = this.getClass().getResource("/").getPath()
                + "out/web/constant/" + data.getString("templateCode") + "/ServiceCode" + toUpperCaseFirstOne(data.getString("templateCode")) + "Constant.java";
        System.out.printf("writePath: " + writePath);
        writeFile(writePath,
                fileContext);
 
    }
 
    /**
     * 生成 html js java 类
     *
     * @param data
     */
    private void generatorComponentHtml(JSONObject data) {
 
        StringBuffer sb = readFile(GeneratorStart.class.getResource("/relationship/binding/binding.html").getFile());
        String fileContext = sb.toString();
 
        fileContext = super.replaceBindingTemplateContext(fileContext, data);
 
        //  @@allStep@@
 
        StringBuffer allStep = new StringBuffer();
 
        JSONArray flows = data.getJSONArray("flows");
        for (int flowIndex = 0; flowIndex < flows.size(); flowIndex++) {
            JSONObject flow = flows.getJSONObject(flowIndex);
            String showAffirmPage = data.getBoolean("needAffirm") ? " || " + data.getString("templateCode") + "Info.index == " + flows.size() : "";
            allStep.append("<div v-if=\"" + data.getString("templateCode") + "Info.index == " + flowIndex + showAffirmPage + "\">\n" +
                    "        <vc:create name=\"" + flow.getString("vcName") + "\"\n" +
                    "                   callBackListener=\"" + data.getString("templateCode") + "\"\n" +
                    "                   callBackFunction=\"notify\"\n" +
                    "        ></vc:create>\n" +
                    "    </div>\n");
 
        }
 
        int stepLastIndex = data.getBoolean("needAffirm") ? flows.size() : flows.size() - 1;
        fileContext = fileContext.replace(" @@allStep@@", allStep.toString())
                .replace("@@stepLastIndex@@", stepLastIndex + "");
 
 
        String writePath = this.getClass().getResource("/").getPath()
                + "out/relationship/component/" + data.getString("package") + "/" + data.getString("templateCode") + "/" + data.getString("templateCode") + ".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("/relationship/binding/binding.js").getFile());
        String fileContext = sb.toString();
 
        fileContext = super.replaceBindingTemplateContext(fileContext, data);
 
        //替换 变量@@templateCodeColumns@@
        JSONArray columns = data.getJSONArray("columns");
 
        StringBuffer variable = new StringBuffer();
        String defaultValue = "";
 
        StringBuffer validateInfo = new StringBuffer();
        StringBuffer allStep = new StringBuffer("[");
        JSONArray flows = data.getJSONArray("flows");
        for (int flowIndex = 0; flowIndex < flows.size(); flowIndex++) {
            JSONObject flow = flows.getJSONObject(flowIndex);
            allStep.append("\"" + flow.getString("cnCode") + "\"");
            if (flowIndex < flows.size() - 1) {
                allStep.append(",");
            }
 
 
            validateInfo.append("vc.emit('" + flow.getString("vcName") + "', 'onIndex', vc.component.serviceBindingInfo.index);\n");
 
        }
        String showAffirmPage = data.getBoolean("needAffirm") ? "确认信息" : "";
        allStep.append(showAffirmPage);
        allStep.append("]");
        fileContext = fileContext.replace("@@stepTitle@@", allStep.toString());
        fileContext = fileContext.replace("@@notifyOnIndex@@", validateInfo.toString());
        fileContext = fileContext.replace("@@jumpUrl@@", data.getString("successUrl"));
 
        // 替换 数据校验部分代码
 
 
        String writePath = this.getClass().getResource("/").getPath()
                + "out/relationship/component/" + data.getString("package") + "/" + data.getString("templateCode") + "/" + data.getString("templateCode") + ".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("/relationship/binding/BindingComponent.java").getFile());
        String fileContext = sb.toString();
 
        fileContext = super.replaceBindingTemplateContext(fileContext, data);
 
        String writePath = this.getClass().getResource("/").getPath()
                + "out/relationship/component/java/" + data.getString("templateCode") + "/" + toUpperCaseFirstOne(data.getString("templateCode")) + "BindingComponent.java";
        System.out.printf("writePath: " + writePath);
        writeFile(writePath,
                fileContext);
 
 
    }
 
    /**
     * 生成接口类
     *
     * @param data
     */
    private void genneratorIListSmo(JSONObject data) {
        StringBuffer sb = readFile(GeneratorStart.class.getResource("/relationship/binding/IBindingSMO.java").getFile());
        String fileContext = sb.toString();
 
        fileContext = super.replaceBindingTemplateContext(fileContext, data);
 
        String writePath = this.getClass().getResource("/").getPath()
                + "out/relationship/smo/" + data.getString("templateCode") + "/I" + toUpperCaseFirstOne(data.getString("templateCode")) + "BindingSMO.java";
        System.out.printf("writePath: " + writePath);
        writeFile(writePath,
                fileContext);
    }
 
    /**
     * 生成接口类
     *
     * @param data
     */
    private void genneratorListSmoImpl(JSONObject data) {
        StringBuffer sb = readFile(GeneratorStart.class.getResource("/relationship/binding/BindingSMOImpl.java").getFile());
        String fileContext = sb.toString();
 
        fileContext = super.replaceBindingTemplateContext(fileContext, data);
 
//        //替换校验部分代码 @@validateTemplateColumns@@
//        JSONArray columns = data.getJSONArray("columns");
//        StringBuffer validateStr = new StringBuffer();
//        for (int columnIndex = 0; columnIndex < columns.size(); columnIndex++) {
//            JSONObject column = columns.getJSONObject(columnIndex);
//            if (column.getBoolean("required")) {
//                validateStr.append("Assert.hasKeyAndValue(paramIn, \"" + column.getString("code") + "\", \"" + column.getString("desc") + "\");\n");
//            }
//        }
//
//        fileContext = fileContext.replace("@@validateTemplateColumns@@", validateStr.toString());
 
 
        String writePath = this.getClass().getResource("/").getPath()
                + "out/relationship/smo/" + data.getString("templateCode") + "/impl/" + toUpperCaseFirstOne(data.getString("templateCode")) + "BindingSMOImpl.java";
        System.out.printf("writePath: " + writePath);
        writeFile(writePath,
                fileContext);
    }
 
    /**
     * 生成API 侦听处理类
     *
     * @param data
     */
    private void genneratorListListener(JSONObject data) {
        StringBuffer sb = readFile(GeneratorStart.class.getResource("/web/add/SaveListener.java").getFile());
        String fileContext = sb.toString();
 
        fileContext = super.replaceTemplateContext(fileContext, data);
 
        //替换校验部分代码 @@validateTemplateColumns@@
        JSONArray columns = data.getJSONArray("columns");
        StringBuffer validateStr = new StringBuffer();
        for (int columnIndex = 0; columnIndex < columns.size(); columnIndex++) {
            JSONObject column = columns.getJSONObject(columnIndex);
            if (column.getBoolean("required")) {
                validateStr.append("Assert.hasKeyAndValue(reqJson, \"" + column.getString("code") + "\", \"" + column.getString("desc") + "\");\n");
            }
        }
 
        fileContext = fileContext.replace("@@validateTemplateColumns@@", validateStr.toString());
 
 
        String writePath = this.getClass().getResource("/").getPath()
                + "out/api/listener/" + data.getString("templateCode") + "/Save" + toUpperCaseFirstOne(data.getString("templateCode")) + "Listener.java";
        System.out.printf("writePath: " + writePath);
        writeFile(writePath,
                fileContext);
    }
 
 
}