InvestigateIndustryConfig.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. import java.sql.*;
  2. import java.util.*;
  3. /**
  4. * 行业配置调研程序:
  5. * 1) 列出 company 表中所有企业及其行业字段
  6. * 2) 列出 lobster_workflow / company_workflow_lobster 中所有工作流及其行业/场景
  7. * 3) 查找所有行业相关字典表/配置表
  8. *
  9. * 用法:
  10. * javac -encoding UTF-8 -cp "C:\Users\77944\.m2\repository\com\mysql\mysql-connector-j\8.0.33\mysql-connector-j-8.0.33.jar" InvestigateIndustryConfig.java
  11. * java -cp ".;C:\Users\77944\.m2\repository\com\mysql\mysql-connector-j\8.0.33\mysql-connector-j-8.0.33.jar" InvestigateIndustryConfig
  12. */
  13. public class InvestigateIndustryConfig {
  14. private static final String HOST = "cq-cdb-8fjmemkb.sql.tencentcdb.com";
  15. private static final int PORT = 27220;
  16. private static final String USER = "root";
  17. private static final String PWD = "Ylrz_1q2w3e4r5t6y";
  18. private static final String TENANT_DB = "ylrz_saas_tenant_1";
  19. public static void main(String[] args) throws Exception {
  20. Class.forName("com.mysql.cj.jdbc.Driver");
  21. String url = "jdbc:mysql://" + HOST + ":" + PORT + "/" + TENANT_DB
  22. + "?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false";
  23. try (Connection c = DriverManager.getConnection(url, USER, PWD)) {
  24. System.out.println("========================================================");
  25. System.out.println(" 连接租户库: " + TENANT_DB);
  26. System.out.println("========================================================");
  27. // 0) 列出所有表
  28. printAllTables(c);
  29. // 1) company 表(或 tenant_company)企业及行业字段
  30. printSection("1) 企业表(company/tenant_company)行业字段");
  31. printCompanyIndustry(c);
  32. // 2) lobster_workflow 表中所有工作流及其行业/场景
  33. printSection("2) 龙虾工作流表(lobster_workflow / company_workflow_lobster)行业字段");
  34. printWorkflowIndustry(c);
  35. // 3) 所有行业相关字典/配置表
  36. printSection("3) 行业相关字典/配置表");
  37. printIndustryDicts(c);
  38. }
  39. }
  40. private static void printAllTables(Connection c) {
  41. System.out.println("\n=== 0) 所有表名(含 lobster / industry / company 字眼的)===");
  42. try (Statement st = c.createStatement();
  43. ResultSet rs = st.executeQuery(
  44. "SELECT table_name FROM information_schema.tables WHERE table_schema=DATABASE() "
  45. + "AND (table_name LIKE '%lobster%' OR table_name LIKE '%industry%' "
  46. + "OR table_name LIKE '%company%' OR table_name LIKE '%workflow%' "
  47. + "OR table_name LIKE '%dict%' OR table_name LIKE '%corpus%') "
  48. + "ORDER BY table_name")) {
  49. while (rs.next()) {
  50. System.out.println(" " + rs.getString(1));
  51. }
  52. } catch (SQLException e) {
  53. System.out.println(" [ERR] " + e.getMessage());
  54. }
  55. }
  56. private static void printCompanyIndustry(Connection c) {
  57. // 探测 company 表的列名
  58. String[] candidates = {"company", "tenant_company"};
  59. for (String t : candidates) {
  60. if (!tableExists(c, t)) continue;
  61. System.out.println("\n-- 表: " + t);
  62. List<String> cols = tableColumns(c, t);
  63. System.out.println(" 列: " + cols);
  64. // 找出与行业相关的列
  65. List<String> industryCols = new ArrayList<>();
  66. for (String col : cols) {
  67. String l = col.toLowerCase();
  68. if (l.contains("industry") || l.contains("scene")
  69. || l.contains("type") || l.contains("category")
  70. || l.contains("business")) {
  71. industryCols.add(col);
  72. }
  73. }
  74. System.out.println(" 行业相关列: " + industryCols);
  75. if (industryCols.isEmpty()) {
  76. continue;
  77. }
  78. StringBuilder sql = new StringBuilder("SELECT id, company_name");
  79. for (String col : industryCols) {
  80. if (!col.equalsIgnoreCase("id") && !col.equalsIgnoreCase("company_name")) {
  81. sql.append(", ").append(col);
  82. }
  83. }
  84. sql.append(" FROM ").append(t).append(" WHERE del_flag='0' OR del_flag IS NULL ORDER BY id LIMIT 100");
  85. System.out.println(" SQL: " + sql);
  86. try (Statement st = c.createStatement();
  87. ResultSet rs = st.executeQuery(sql.toString())) {
  88. int cnt = 0;
  89. while (rs.next()) {
  90. StringBuilder row = new StringBuilder();
  91. int n = rs.getMetaData().getColumnCount();
  92. for (int i = 1; i <= n; i++) {
  93. if (i > 1) row.append(" | ");
  94. row.append(rs.getMetaData().getColumnLabel(i))
  95. .append("=").append(rs.getString(i));
  96. }
  97. System.out.println(" " + row);
  98. cnt++;
  99. }
  100. System.out.println(" 共 " + cnt + " 行");
  101. } catch (SQLException e) {
  102. System.out.println(" [ERR] " + e.getMessage());
  103. }
  104. }
  105. }
  106. private static void printWorkflowIndustry(Connection c) {
  107. String[] candidates = {"lobster_workflow", "company_workflow_lobster", "lobster_workflow_template"};
  108. for (String t : candidates) {
  109. if (!tableExists(c, t)) continue;
  110. System.out.println("\n-- 表: " + t);
  111. List<String> cols = tableColumns(c, t);
  112. System.out.println(" 列: " + cols);
  113. // 选取 id/name/industry/scene 等关键列
  114. List<String> wantCols = new ArrayList<>();
  115. for (String col : cols) {
  116. String l = col.toLowerCase();
  117. if (l.equals("id") || l.contains("name") || l.contains("industry")
  118. || l.contains("scene") || l.contains("type")
  119. || l.equals("status") || l.equals("template_code")) {
  120. wantCols.add(col);
  121. }
  122. }
  123. if (wantCols.isEmpty()) continue;
  124. String sql = "SELECT " + String.join(", ", wantCols)
  125. + " FROM " + t + " WHERE del_flag='0' OR del_flag IS NULL ORDER BY id LIMIT 200";
  126. System.out.println(" SQL: " + sql);
  127. try (Statement st = c.createStatement();
  128. ResultSet rs = st.executeQuery(sql)) {
  129. int cnt = 0;
  130. while (rs.next()) {
  131. StringBuilder row = new StringBuilder();
  132. int n = rs.getMetaData().getColumnCount();
  133. for (int i = 1; i <= n; i++) {
  134. if (i > 1) row.append(" | ");
  135. row.append(rs.getMetaData().getColumnLabel(i))
  136. .append("=").append(rs.getString(i));
  137. }
  138. System.out.println(" " + row);
  139. cnt++;
  140. }
  141. System.out.println(" 共 " + cnt + " 行");
  142. } catch (SQLException e) {
  143. System.out.println(" [ERR] " + e.getMessage());
  144. }
  145. }
  146. }
  147. private static void printIndustryDicts(Connection c) {
  148. // 1. sys_dict_type / sys_dict_data 中的行业相关
  149. String[] dictTables = {"sys_dict_type", "sys_dict_data"};
  150. for (String t : dictTables) {
  151. if (!tableExists(c, t)) continue;
  152. System.out.println("\n-- 表: " + t);
  153. List<String> cols = tableColumns(c, t);
  154. String keyCol = cols.contains("dict_type") ? "dict_type" : (cols.contains("dict_key") ? "dict_key" : null);
  155. String labelCol = cols.contains("dict_label") ? "dict_label" : null;
  156. String valueCol = cols.contains("dict_value") ? "dict_value" : null;
  157. if (keyCol == null) {
  158. System.out.println(" 跳过:找不到 dict_type/dict_key 列");
  159. continue;
  160. }
  161. // 找 dict_type 含 industry/行业/lobster 的
  162. String sql = "SELECT * FROM " + t + " WHERE " + keyCol
  163. + " LIKE '%industry%' OR " + keyCol + " LIKE '%lobster%' OR "
  164. + (labelCol != null ? labelCol + " LIKE '%行业%' OR " : "")
  165. + (labelCol != null ? labelCol + " LIKE '%龙虾%' OR " : "")
  166. + keyCol + " LIKE '%scene%' LIMIT 200";
  167. System.out.println(" SQL: " + sql);
  168. try (Statement st = c.createStatement();
  169. ResultSet rs = st.executeQuery(sql)) {
  170. int cnt = 0;
  171. while (rs.next()) {
  172. StringBuilder row = new StringBuilder();
  173. int n = rs.getMetaData().getColumnCount();
  174. for (int i = 1; i <= n; i++) {
  175. if (i > 1) row.append(" | ");
  176. row.append(rs.getMetaData().getColumnLabel(i))
  177. .append("=").append(rs.getString(i));
  178. }
  179. System.out.println(" " + row);
  180. cnt++;
  181. }
  182. System.out.println(" 共 " + cnt + " 行");
  183. } catch (SQLException e) {
  184. System.out.println(" [ERR] " + e.getMessage());
  185. }
  186. }
  187. // 2. lobster 相关配置/字典表
  188. try (Statement st = c.createStatement();
  189. ResultSet rs = st.executeQuery(
  190. "SELECT table_name FROM information_schema.tables WHERE table_schema=DATABASE() "
  191. + "AND (table_name LIKE 'lobster_%' OR table_name LIKE '%industry_corpus%' "
  192. + "OR table_name LIKE '%tenant_industry%' OR table_name LIKE '%industry_pack%') "
  193. + "ORDER BY table_name")) {
  194. System.out.println("\n-- 行业/龙虾相关配置表清单:");
  195. List<String> tables = new ArrayList<>();
  196. while (rs.next()) {
  197. tables.add(rs.getString(1));
  198. System.out.println(" " + rs.getString(1));
  199. }
  200. // 对每个表给出列信息和前若干行
  201. for (String t : tables) {
  202. List<String> cols = tableColumns(c, t);
  203. System.out.println("\n >>> 表 " + t + " 列: " + cols);
  204. String sql = "SELECT * FROM " + t + " LIMIT 5";
  205. try (Statement st2 = c.createStatement();
  206. ResultSet rs2 = st2.executeQuery(sql)) {
  207. while (rs2.next()) {
  208. StringBuilder row = new StringBuilder();
  209. int n = rs2.getMetaData().getColumnCount();
  210. for (int i = 1; i <= n; i++) {
  211. if (i > 1) row.append(" | ");
  212. String v = rs2.getString(i);
  213. if (v != null && v.length() > 80) v = v.substring(0, 80) + "...";
  214. row.append(rs2.getMetaData().getColumnLabel(i)).append("=").append(v);
  215. }
  216. System.out.println(" " + row);
  217. }
  218. } catch (SQLException e) {
  219. System.out.println(" [ERR] " + e.getMessage());
  220. }
  221. }
  222. } catch (SQLException e) {
  223. System.out.println(" [ERR] " + e.getMessage());
  224. }
  225. }
  226. private static boolean tableExists(Connection c, String table) {
  227. try (Statement st = c.createStatement();
  228. ResultSet rs = st.executeQuery(
  229. "SELECT 1 FROM information_schema.tables WHERE table_schema=DATABASE() AND table_name='"
  230. + table + "'")) {
  231. return rs.next();
  232. } catch (SQLException e) {
  233. return false;
  234. }
  235. }
  236. private static List<String> tableColumns(Connection c, String table) {
  237. List<String> cols = new ArrayList<>();
  238. try (Statement st = c.createStatement();
  239. ResultSet rs = st.executeQuery(
  240. "SELECT column_name FROM information_schema.columns WHERE table_schema=DATABASE() "
  241. + "AND table_name='" + table + "' ORDER BY ordinal_position")) {
  242. while (rs.next()) {
  243. cols.add(rs.getString(1));
  244. }
  245. } catch (SQLException e) {
  246. System.out.println(" [columns ERR] " + e.getMessage());
  247. }
  248. return cols;
  249. }
  250. private static void printSection(String title) {
  251. System.out.println("\n========================================================");
  252. System.out.println(" " + title);
  253. System.out.println("========================================================");
  254. }
  255. }