java110
2021-11-14 411b3c869bfa74cf23e7c38e5054670dfc11b71b
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
package com.java110.code.web;
 
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.java110.code.back.BaseGenerator;
 
public class GeneratorViewComponent extends BaseGenerator {
 
    public void generator(JSONObject data) {
 
        //处理组件
        generatorComponentHtml(data);
        generatorComponentJs(data);
 
    }
 
 
    /**
     * 生成 html js java 类
     *
     * @param data
     */
    private void generatorComponentHtml(JSONObject data) {
 
        StringBuffer sb = readFile(GeneratorStart.class.getResource("/web/view/viewInfo.html").getFile());
        String fileContext = sb.toString();
 
        fileContext = super.replaceTemplateContext(fileContext, data);
 
        // 处理 th 信息
 
        StringBuffer thSb = new StringBuffer();
 
        JSONArray columns = data.getJSONArray("columns");
 
        JSONArray cols = new JSONArray();
        JSONObject col = new JSONObject();
        col.put("cnCode", data.getString("templateKeyName"));
        col.put("code", data.getString("templateKey"));
        cols.addAll(columns);
        for (int columnIndex = 0; columnIndex < cols.size(); columnIndex++) {
            JSONObject column = cols.getJSONObject(columnIndex);
            if (columnIndex % 3 == 0) {
                thSb.append("<div class=\"row\">\n");
            }
 
            thSb.append("<div class=\"col-sm-4\">\n" +
                    "                        <div class=\"form-group\">\n" +
                    "                            <label class=\"col-form-label\" >" + column.getString("cnCode") + ":</label>\n" +
                    "                            <label class=\"\">{{view" + toUpperCaseFirstOne(data.getString("templateCode")) + "Info." + column.getString("code") + "}}</label>\n" +
                    "                        </div>\n" +
                    "</div>\n");
 
            if (columnIndex % 3 == 2 || columnIndex == columns.size() - 1) {
                thSb.append("</div>\n");
            }
 
        }
 
        fileContext = fileContext.replace("@@viewInfo@@", thSb.toString());
 
 
        String writePath = this.getClass().getResource("/").getPath()
                + "out/web/components/" + data.getString("directories") + "/view" + toUpperCaseFirstOne(data.getString("templateCode")) + "Info/view" + toUpperCaseFirstOne(data.getString("templateCode")) + "Info.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/view/viewInfo.js").getFile());
        String fileContext = sb.toString();
 
        fileContext = super.replaceTemplateContext(fileContext, data);
 
        //替换 变量@@templateCodeColumns@@
        JSONArray columns = data.getJSONArray("columns");
 
        StringBuffer variable = new StringBuffer();
        String defaultValue = "";
 
        for (int columnIndex = 0; columnIndex < columns.size(); columnIndex++) {
            JSONObject column = columns.getJSONObject(columnIndex);
            variable.append(column.getString("code") + ":'',\n");
 
        }
        fileContext = fileContext.replace("@@templateCodeColumns@@", variable.toString());
 
        // 替换 数据校验部分代码
 
 
        String writePath = this.getClass().getResource("/").getPath()
                + "out/web/components/" + data.getString("directories") + "/view"
                + toUpperCaseFirstOne(data.getString("templateCode")) + "Info/view" + toUpperCaseFirstOne(data.getString("templateCode")) + "Info.js";
        System.out.printf("writePath: " + writePath);
        writeFile(writePath,
                fileContext);
 
 
    }
 
 
}