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("
\n"); } if (condIndex % 3 == 0 || condIndex % 3 == 1) { conditionInput.append("
2) { conditionInput.append("v-if=\"" + data.getString("templateCode") + "ManageInfo.moreCondition == true\">\n"); } else { conditionInput.append(">\n"); } if ("choose".equals(tmpCond.getString("inputType"))) { conditionInput.append("
\n" + " \n" + "
\n" + " \n" + "
\n" + "
\n"); if (tmpCond.containsKey("componentName")) { vcCreate.append("\n"); } } else if ("input".equals(tmpCond.getString("inputType"))) { conditionInput.append("
\n" + " \n" + "
"); } else if ("select".equals(tmpCond.getString("inputType"))){ String[] selectValues = tmpCond.getString("selectValue").split(","); String[] selectValueNames = tmpCond.getString("selectValueName").split(","); String option = ""; for (int valueIndex = 0; valueIndex < selectValues.length; valueIndex++) { String value = selectValues[valueIndex]; option += "\n"; } conditionInput.append("" ); } conditionInput.append("
"); if (condIndex == 2) { conditionInput.append("
\n" + " \n" + "
"); } else if (condIndex % 3 == 2) { conditionInput.append("
\n" + "
"); } if (condIndex % 3 == 2 || condIndex == tmpConditions.size() - 1) { conditionInput.append("
\n"); } } fileContext = fileContext.replace("@@conditionInput@@", conditionInput.toString()); fileContext = fileContext.replace("@@vcCreate@@", vcCreate.toString()); if (tmpConditions.size() > 3) { fileContext = fileContext.replace("@@moreCondition@@", ""); } else { fileContext = fileContext.replace("@@moreCondition@@", ""); } // 处理 th 信息 StringBuffer thSb = new StringBuffer(); StringBuffer tdSb = new StringBuffer(); thSb.append(" " + data.getString("templateKeyName") + "\n"); tdSb.append(" {{" + data.getString("templateCode") + "." + data.getString("templateKey") + "}}\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(" " + column.getString("cnCode") + "\n"); tdSb.append(" {{" + data.getString("templateCode") + "." + column.getString("code") + "}}\n"); } } thSb.append(" 操作\n"); tdSb.append("
\n" + " \n" + "
\n" + "
\n" + " \n" + "
\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); } }