ApplyLobsterIntentKbMigration.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import java.nio.charset.StandardCharsets;
  2. import java.nio.file.Files;
  3. import java.nio.file.Path;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.PreparedStatement;
  7. import java.sql.ResultSet;
  8. import java.sql.Statement;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11. /** Apply V20260724_01 DDL + UTF-8 safe JDBC seeds to tenant DB(s). */
  12. public class ApplyLobsterIntentKbMigration {
  13. private static final String HOST = "cq-cdb-8fjmemkb.sql.tencentcdb.com:27220";
  14. private static final String USER = "root";
  15. private static final String PASS = "Ylrz_1q2w3e4r5t6y";
  16. private static final Path SQL_PATH = Path.of("d:/ylrz_saas_new/java/fs-agent/src/main/resources/db/migration/tenant/V20260724_01__lobster_intent_kb_and_fixed_reply.sql");
  17. private static final String REPLY_HANDOFF =
  18. "\u597d\u7684\uff0c\u5df2\u4e3a\u60a8\u5b89\u6392\u4e13\u4eba\u8ddf\u8fdb\uff0c\u8bf7\u7a0d\u5019\u3002";
  19. private static final String REPLY_VIDEO =
  20. "\u6536\u5230\u60a8\u7684\u89c6\u9891\u4e86\uff0c\u6211\u5df2\u8f6c\u4ea4\u540c\u4e8b\u67e5\u770b\uff0c\u7a0d\u540e\u56de\u590d\u60a8\u3002";
  21. private static final String REPLY_FILE =
  22. "\u6536\u5230\u60a8\u7684\u6587\u4ef6\u4e86\uff0c\u6211\u5df2\u8f6c\u4ea4\u540c\u4e8b\u5904\u7406\uff0c\u7a0d\u540e\u56de\u590d\u60a8\u3002";
  23. public static void main(String[] args) throws Exception {
  24. Class.forName("com.mysql.cj.jdbc.Driver");
  25. String target = args.length > 0 ? args[0] : "fs_tenant_cs1";
  26. String ddl = loadSql();
  27. List<String> dbs = new ArrayList<>();
  28. if ("all".equalsIgnoreCase(target)) {
  29. String rootUrl = "jdbc:mysql://" + HOST + "/?useSSL=false&serverTimezone=GMT%2B8&characterEncoding=utf8";
  30. try (Connection c = DriverManager.getConnection(rootUrl, USER, PASS);
  31. Statement st = c.createStatement();
  32. ResultSet rs = st.executeQuery("SHOW DATABASES LIKE 'fs_tenant_%'")) {
  33. while (rs.next()) {
  34. dbs.add(rs.getString(1));
  35. }
  36. }
  37. System.out.println("Found " + dbs.size() + " tenant databases");
  38. } else {
  39. dbs.add(target);
  40. }
  41. int ok = 0;
  42. int fail = 0;
  43. for (String db : dbs) {
  44. try {
  45. apply(db, ddl);
  46. ok++;
  47. } catch (Exception e) {
  48. fail++;
  49. System.err.println("[FAIL] " + db + ": " + e.getMessage());
  50. }
  51. }
  52. System.out.println("Done. ok=" + ok + " fail=" + fail);
  53. }
  54. private static void apply(String db, String ddl) throws Exception {
  55. String url = "jdbc:mysql://" + HOST + "/" + db
  56. + "?useSSL=false&serverTimezone=GMT%2B8&characterEncoding=utf8&allowMultiQueries=true";
  57. try (Connection c = DriverManager.getConnection(url, USER, PASS)) {
  58. try (Statement st = c.createStatement()) {
  59. st.execute(ddl);
  60. }
  61. seedViaJdbc(c);
  62. try (Statement st = c.createStatement();
  63. ResultSet rs = st.executeQuery(
  64. "SELECT (SELECT COUNT(*) FROM lobster_intent_kb_mapping) kb_cnt,"
  65. + " (SELECT COUNT(*) FROM lobster_fixed_reply_rule) reply_cnt")) {
  66. if (rs.next()) {
  67. System.out.println("[OK] " + db + " kb=" + rs.getInt(1) + " fixedReply=" + rs.getInt(2));
  68. }
  69. }
  70. }
  71. }
  72. private static void seedViaJdbc(Connection c) throws Exception {
  73. upsertKbMapping(c, "*", "knowledge", 0);
  74. upsertFixedReply(c, "signal", "HANDOFF", REPLY_HANDOFF, 10);
  75. upsertFixedReply(c, "signal", "MEDIA_VIDEO", REPLY_VIDEO, 10);
  76. upsertFixedReply(c, "signal", "MEDIA_FILE", REPLY_FILE, 10);
  77. }
  78. private static void upsertKbMapping(Connection c, String intent, String category, int priority) throws Exception {
  79. String update = "UPDATE lobster_intent_kb_mapping SET kb_category=?, priority=?, enabled=1 "
  80. + "WHERE company_id=0 AND intent=?";
  81. try (PreparedStatement ps = c.prepareStatement(update)) {
  82. ps.setString(1, category);
  83. ps.setInt(2, priority);
  84. ps.setString(3, intent);
  85. if (ps.executeUpdate() > 0) {
  86. return;
  87. }
  88. }
  89. String insert = "INSERT INTO lobster_intent_kb_mapping "
  90. + "(company_id, intent, kb_category, priority, enabled, create_time) VALUES (0,?,?,?,1,NOW())";
  91. try (PreparedStatement ps = c.prepareStatement(insert)) {
  92. ps.setString(1, intent);
  93. ps.setString(2, category);
  94. ps.setInt(3, priority);
  95. ps.executeUpdate();
  96. }
  97. }
  98. private static void upsertFixedReply(Connection c, String matchType, String matchKey,
  99. String reply, int priority) throws Exception {
  100. String update = "UPDATE lobster_fixed_reply_rule SET reply_template=?, priority=?, enabled=1 "
  101. + "WHERE company_id=0 AND match_type=? AND match_key=?";
  102. try (PreparedStatement ps = c.prepareStatement(update)) {
  103. ps.setString(1, reply);
  104. ps.setInt(2, priority);
  105. ps.setString(3, matchType);
  106. ps.setString(4, matchKey);
  107. if (ps.executeUpdate() > 0) {
  108. return;
  109. }
  110. }
  111. String insert = "INSERT INTO lobster_fixed_reply_rule "
  112. + "(company_id, match_type, match_key, reply_template, priority, enabled, create_time) "
  113. + "VALUES (0,?,?,?,?,1,NOW())";
  114. try (PreparedStatement ps = c.prepareStatement(insert)) {
  115. ps.setString(1, matchType);
  116. ps.setString(2, matchKey);
  117. ps.setString(3, reply);
  118. ps.setInt(4, priority);
  119. ps.executeUpdate();
  120. }
  121. }
  122. private static String loadSql() throws Exception {
  123. byte[] bytes = Files.readAllBytes(SQL_PATH);
  124. String sql = new String(bytes, StandardCharsets.UTF_8);
  125. if (!sql.isEmpty() && sql.charAt(0) == '\uFEFF') {
  126. sql = sql.substring(1);
  127. }
  128. return sql;
  129. }
  130. }