TableToJsonWeb.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package com.java110.code;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.java110.utils.util.StringUtil;
  5. public class TableToJsonWeb {
  6. //show create table c_orders 用这个语句获取
  7. public static final String createTableSql = "CREATE TABLE `meter_type` (\n" +
  8. " `type_id` varchar(30) NOT NULL COMMENT '类型ID',\n" +
  9. " `type_name` varchar(12) NOT NULL COMMENT '名称',\n" +
  10. " `community_id` varchar(30) NOT NULL COMMENT '小区ID',\n" +
  11. " `remark` varchar(200) DEFAULT NULL COMMENT '说明',\n" +
  12. " `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',\n" +
  13. " `status_cd` varchar(2) NOT NULL DEFAULT '0' COMMENT '数据状态,详细参考c_status表,0, 在用 1失效'\n" +
  14. ")";
  15. public static void main(String[] args) {
  16. // templateName 业务名称 业务编码名称生成后文件名 templateCode 主键 templateKey
  17. // 业务主键名称 templateKeyName=templateName+ID 主机驼峰 searchCode 主键名称 searchName
  18. // directories 放在前端那个目录下
  19. String newSql = createTableSql.substring(createTableSql.indexOf("(") + 1, createTableSql.lastIndexOf(")"));
  20. String tableName = createTableSql.substring(createTableSql.indexOf("TABLE") + 5, createTableSql.indexOf("("));
  21. tableName = tableName.replaceAll("`", "").trim();
  22. newSql = newSql.replaceAll("\n", "");
  23. String[] rowSqls = newSql.split("',");
  24. JSONObject param = new JSONObject();
  25. param.put("templateName", "");
  26. param.put("templateCode", "");
  27. param.put("templateKey", "");
  28. param.put("templateKeyName", "");
  29. param.put("searchCode", "");
  30. param.put("searchName", "");
  31. param.put("directories", "");
  32. JSONObject paramColumn = null;
  33. JSONArray conditions = new JSONArray();
  34. JSONArray paramColumns = new JSONArray();
  35. JSONObject condition = null;
  36. String key = "";
  37. for (String rowSql : rowSqls) {
  38. condition = new JSONObject();
  39. paramColumn = new JSONObject();
  40. key = rowSql.trim();
  41. key = key.substring(0, key.indexOf(" "));
  42. key = key.replaceAll("`", "");
  43. if ("UNIQUE".equals(key)) {
  44. continue;
  45. }
  46. if ("KEY".equals(key)) {
  47. continue;
  48. }
  49. if ("b_id".equals(key)) {
  50. continue;
  51. }
  52. if ("create_time".equals(key)) {
  53. continue;
  54. }
  55. String comment = rowSql.contains("COMMENT") ? rowSql.substring(rowSql.indexOf("COMMENT '") + 9) : StringUtil.lineToHump(key);
  56. comment = comment.trim();
  57. if (comment.contains(",")) {
  58. comment = comment.split(",")[0];
  59. }
  60. if (comment.contains(" ")) {
  61. comment = comment.split(" ")[0];
  62. }
  63. paramColumn.put("desc", comment);
  64. if (rowSql.toLowerCase().contains("not null")) {
  65. condition.put("name", comment);
  66. condition.put("inputType", "input");
  67. condition.put("code", StringUtil.lineToHump(key));
  68. condition.put("whereCondition", "equal");
  69. conditions.add(condition);
  70. paramColumn.put("desc", "必填," + comment);
  71. }
  72. String limit = rowSql.substring(rowSql.indexOf("(") + 1, rowSql.indexOf(")"));
  73. if (limit.contains(",")) {
  74. limit = limit.split(",")[0];
  75. }
  76. paramColumn.put("code", StringUtil.lineToHump(key));
  77. paramColumn.put("cnCode", comment);
  78. paramColumn.put("required", true);
  79. paramColumn.put("hasDefaultValue", false);
  80. paramColumn.put("inputType", "input");
  81. paramColumn.put("limit", "maxLength");
  82. paramColumn.put("limitParam", limit);
  83. paramColumn.put("limitErrInfo", comment + "不能超过" + limit);
  84. paramColumn.put("show", true);
  85. paramColumns.add(paramColumn);
  86. }
  87. param.put("columns", paramColumns);
  88. param.put("conditions", conditions);
  89. System.out.println(param.toJSONString());
  90. }
  91. }