20260730173012
This commit is contained in:
1 parent
84673b3d3d
commit
4929b78434
25 files changed
+1796
-68
No files matched your search
@@ -0,0 +1,264 @@
|
||||
import java.sql.*;
|
||||
|
||||
/**
|
||||
* 从旧库 G3HY2025.dbo.s_columnLib 同步到新库 fms:
|
||||
* 1. col_Caption → s_module_field.b_name(字段名称)
|
||||
* 2. col_Visible → s_module_field_list.b_visible(列表显示,不存在则插入记录)
|
||||
* 3. col_edit → s_module_field_edit.b_visible(编辑显示,不存在则插入记录)
|
||||
*
|
||||
* 用法: java -cp ".;mssql-jdbc.jar" FieldNameSync <viewName>
|
||||
*/
|
||||
public class FieldNameSync {
|
||||
|
||||
static final String URL = "jdbc:sqlserver://118.89.70.199:1433;databaseName=fms;encrypt=true;trustServerCertificate=true";
|
||||
static final String USER = "sa";
|
||||
static final String PASSWORD = "zhang520..";
|
||||
|
||||
// 同步字段名称
|
||||
static final String SYNC_NAME = """
|
||||
UPDATE sf
|
||||
SET sf.b_name = LTRIM(RTRIM(sc.col_Caption))
|
||||
FROM dbo.s_module_field sf
|
||||
JOIN dbo.s_module m ON m.b_id = sf.b_module_id
|
||||
JOIN G3HY2025.dbo.s_columnLib sc
|
||||
ON sc.col_TableName COLLATE Chinese_PRC_CI_AS = m.b_viewtable
|
||||
AND sc.col_FieldName COLLATE Chinese_PRC_CI_AS = sf.b_field
|
||||
WHERE m.b_viewtable = ?
|
||||
AND sc.col_Caption IS NOT NULL
|
||||
AND LTRIM(RTRIM(sc.col_Caption)) != ''
|
||||
""";
|
||||
|
||||
// 同步列表显示:更新已有记录
|
||||
static final String SYNC_LIST_UPDATE = """
|
||||
UPDATE sfl
|
||||
SET sfl.b_visible = CASE WHEN sc.col_Visible = '1' THEN 1 ELSE 0 END
|
||||
FROM dbo.s_module_field_list sfl
|
||||
JOIN dbo.s_module_field sf ON sf.b_id = sfl.b_field_id
|
||||
JOIN dbo.s_module m ON m.b_id = sf.b_module_id
|
||||
JOIN G3HY2025.dbo.s_columnLib sc
|
||||
ON sc.col_TableName COLLATE Chinese_PRC_CI_AS = m.b_viewtable
|
||||
AND sc.col_FieldName COLLATE Chinese_PRC_CI_AS = sf.b_field
|
||||
WHERE m.b_viewtable = ?
|
||||
AND sc.col_Visible IS NOT NULL
|
||||
AND LTRIM(RTRIM(sc.col_Visible)) != ''
|
||||
""";
|
||||
|
||||
// 查询需要插入列表配置的字段
|
||||
static final String LIST_INSERT_CANDIDATES = """
|
||||
SELECT sf.b_id AS field_id, m.b_id AS module_id,
|
||||
CASE WHEN sc.col_Visible = '1' THEN 1 ELSE 0 END AS visible
|
||||
FROM dbo.s_module_field sf
|
||||
JOIN dbo.s_module m ON m.b_id = sf.b_module_id
|
||||
JOIN G3HY2025.dbo.s_columnLib sc
|
||||
ON sc.col_TableName COLLATE Chinese_PRC_CI_AS = m.b_viewtable
|
||||
AND sc.col_FieldName COLLATE Chinese_PRC_CI_AS = sf.b_field
|
||||
WHERE m.b_viewtable = ?
|
||||
AND sc.col_Visible IS NOT NULL
|
||||
AND LTRIM(RTRIM(sc.col_Visible)) != ''
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM dbo.s_module_field_list sfl
|
||||
WHERE sfl.b_field_id = sf.b_id
|
||||
)
|
||||
""";
|
||||
|
||||
static final String INSERT_LIST = """
|
||||
INSERT INTO dbo.s_module_field_list (b_id, b_module_id, b_field_id, b_visible, b_xh)
|
||||
VALUES (?, ?, ?, ?, 0)
|
||||
""";
|
||||
|
||||
// 同步编辑显示:更新已有记录
|
||||
static final String SYNC_EDIT_UPDATE = """
|
||||
UPDATE sfe
|
||||
SET sfe.b_visible = CASE WHEN sc.col_edit = '1' THEN 1 ELSE 0 END
|
||||
FROM dbo.s_module_field_edit sfe
|
||||
JOIN dbo.s_module_field sf ON sf.b_id = sfe.b_field_id
|
||||
JOIN dbo.s_module m ON m.b_id = sf.b_module_id
|
||||
JOIN G3HY2025.dbo.s_columnLib sc
|
||||
ON sc.col_TableName COLLATE Chinese_PRC_CI_AS = m.b_viewtable
|
||||
AND sc.col_FieldName COLLATE Chinese_PRC_CI_AS = sf.b_field
|
||||
WHERE m.b_viewtable = ?
|
||||
AND sc.col_edit IS NOT NULL
|
||||
AND LTRIM(RTRIM(sc.col_edit)) != ''
|
||||
""";
|
||||
|
||||
static final String EDIT_INSERT_CANDIDATES = """
|
||||
SELECT sf.b_id AS field_id, m.b_id AS module_id,
|
||||
CASE WHEN sc.col_edit = '1' THEN 1 ELSE 0 END AS visible
|
||||
FROM dbo.s_module_field sf
|
||||
JOIN dbo.s_module m ON m.b_id = sf.b_module_id
|
||||
JOIN G3HY2025.dbo.s_columnLib sc
|
||||
ON sc.col_TableName COLLATE Chinese_PRC_CI_AS = m.b_viewtable
|
||||
AND sc.col_FieldName COLLATE Chinese_PRC_CI_AS = sf.b_field
|
||||
WHERE m.b_viewtable = ?
|
||||
AND sc.col_edit IS NOT NULL
|
||||
AND LTRIM(RTRIM(sc.col_edit)) != ''
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM dbo.s_module_field_edit sfe
|
||||
WHERE sfe.b_field_id = sf.b_id
|
||||
)
|
||||
""";
|
||||
|
||||
static final String INSERT_EDIT = """
|
||||
INSERT INTO dbo.s_module_field_edit (b_id, b_module_id, b_field_id, b_visible, b_required, b_readonly, b_disabled, b_xh)
|
||||
VALUES (?, ?, ?, ?, 0, 0, 0, 0)
|
||||
""";
|
||||
|
||||
// 查询模块 ID
|
||||
static final String QUERY_MODULE_ID = "SELECT b_id FROM dbo.s_module WHERE b_viewtable = ?";
|
||||
|
||||
// 查询列表/编辑配置现状
|
||||
static final String QUERY_CONFIG = """
|
||||
SELECT sf.b_field, sf.b_name,
|
||||
sfl.b_visible AS list_visible,
|
||||
sfe.b_visible AS edit_visible
|
||||
FROM dbo.s_module_field sf
|
||||
JOIN dbo.s_module m ON m.b_id = sf.b_module_id
|
||||
LEFT JOIN dbo.s_module_field_list sfl ON sfl.b_field_id = sf.b_id
|
||||
LEFT JOIN dbo.s_module_field_edit sfe ON sfe.b_field_id = sf.b_id
|
||||
WHERE m.b_viewtable = ?
|
||||
ORDER BY sf.b_field
|
||||
""";
|
||||
|
||||
static long nextListId;
|
||||
static long nextEditId;
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
if (args.length == 0) {
|
||||
System.err.println("用法: java FieldNameSync <viewName>");
|
||||
System.exit(1);
|
||||
}
|
||||
String viewName = args[0].trim();
|
||||
if (!viewName.matches("^[A-Za-z_][A-Za-z0-9_]{0,127}$")) {
|
||||
System.err.println("非法视图名: " + viewName);
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
System.out.println("连接到 fms 数据库...");
|
||||
try (Connection conn = DriverManager.getConnection(URL, USER, PASSWORD)) {
|
||||
conn.setAutoCommit(false);
|
||||
|
||||
// 查询模块是否存在
|
||||
Long moduleId = null;
|
||||
try (PreparedStatement ps = conn.prepareStatement(QUERY_MODULE_ID)) {
|
||||
ps.setString(1, viewName);
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
if (rs.next()) moduleId = rs.getLong(1);
|
||||
}
|
||||
}
|
||||
if (moduleId == null) {
|
||||
System.err.println("模块 " + viewName + " 不存在!");
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
// 初始化 ID 生成器
|
||||
try (Statement st = conn.createStatement()) {
|
||||
try (ResultSet rs = st.executeQuery("SELECT ISNULL(MAX(b_id),0) FROM dbo.s_module_field_list")) {
|
||||
rs.next(); nextListId = rs.getLong(1) + 1;
|
||||
}
|
||||
try (ResultSet rs = st.executeQuery("SELECT ISNULL(MAX(b_id),0) FROM dbo.s_module_field_edit")) {
|
||||
rs.next(); nextEditId = rs.getLong(1) + 1;
|
||||
}
|
||||
}
|
||||
|
||||
// === 1. 同步前 ===
|
||||
System.out.println("\n=== 同步前 ===");
|
||||
printConfig(conn, viewName);
|
||||
|
||||
// === 2. 字段名称 ===
|
||||
System.out.println("\n=== 1) 字段名称 (col_Caption → b_name) ===");
|
||||
try (PreparedStatement ps = conn.prepareStatement(SYNC_NAME)) {
|
||||
ps.setString(1, viewName);
|
||||
System.out.println(" 更新: " + ps.executeUpdate() + " 条");
|
||||
}
|
||||
|
||||
// === 3. 列表显示 ===
|
||||
System.out.println("\n=== 2) 列表显示 (col_Visible → s_module_field_list.b_visible) ===");
|
||||
try (PreparedStatement ps = conn.prepareStatement(SYNC_LIST_UPDATE)) {
|
||||
ps.setString(1, viewName);
|
||||
System.out.println(" 更新已有: " + ps.executeUpdate() + " 条");
|
||||
}
|
||||
java.util.ArrayList<long[]> listCandidates = new java.util.ArrayList<>();
|
||||
try (PreparedStatement ps = conn.prepareStatement(LIST_INSERT_CANDIDATES)) {
|
||||
ps.setString(1, viewName);
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
while (rs.next()) {
|
||||
listCandidates.add(new long[]{rs.getLong("field_id"), rs.getLong("module_id"), rs.getLong("visible")});
|
||||
}
|
||||
}
|
||||
}
|
||||
try (PreparedStatement ps = conn.prepareStatement(INSERT_LIST)) {
|
||||
for (long[] row : listCandidates) {
|
||||
ps.setLong(1, nextListId++);
|
||||
ps.setLong(2, row[1]);
|
||||
ps.setLong(3, row[0]);
|
||||
ps.setInt(4, (int) row[2]);
|
||||
ps.addBatch();
|
||||
}
|
||||
if (!listCandidates.isEmpty()) {
|
||||
ps.executeBatch();
|
||||
}
|
||||
}
|
||||
System.out.println(" 新增: " + listCandidates.size() + " 条");
|
||||
|
||||
// === 4. 编辑显示 ===
|
||||
System.out.println("\n=== 3) 编辑显示 (col_edit → s_module_field_edit.b_visible) ===");
|
||||
try (PreparedStatement ps = conn.prepareStatement(SYNC_EDIT_UPDATE)) {
|
||||
ps.setString(1, viewName);
|
||||
System.out.println(" 更新已有: " + ps.executeUpdate() + " 条");
|
||||
}
|
||||
java.util.ArrayList<long[]> editCandidates = new java.util.ArrayList<>();
|
||||
try (PreparedStatement ps = conn.prepareStatement(EDIT_INSERT_CANDIDATES)) {
|
||||
ps.setString(1, viewName);
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
while (rs.next()) {
|
||||
editCandidates.add(new long[]{rs.getLong("field_id"), rs.getLong("module_id"), rs.getLong("visible")});
|
||||
}
|
||||
}
|
||||
}
|
||||
try (PreparedStatement ps = conn.prepareStatement(INSERT_EDIT)) {
|
||||
for (long[] row : editCandidates) {
|
||||
ps.setLong(1, nextEditId++);
|
||||
ps.setLong(2, row[1]);
|
||||
ps.setLong(3, row[0]);
|
||||
ps.setInt(4, (int) row[2]);
|
||||
ps.addBatch();
|
||||
}
|
||||
if (!editCandidates.isEmpty()) {
|
||||
ps.executeBatch();
|
||||
}
|
||||
}
|
||||
System.out.println(" 新增: " + editCandidates.size() + " 条");
|
||||
|
||||
conn.commit();
|
||||
System.out.println("\n=== 事务已提交 ===");
|
||||
|
||||
// === 5. 验证 ===
|
||||
System.out.println("\n=== 同步后 ===");
|
||||
printConfig(conn, viewName);
|
||||
}
|
||||
|
||||
System.out.println("\n完成.");
|
||||
}
|
||||
|
||||
static void printConfig(Connection conn, String viewName) throws SQLException {
|
||||
System.out.printf(" %-30s %-16s %-8s %-8s%n", "b_field", "b_name", "list", "edit");
|
||||
System.out.println(" " + "-".repeat(66));
|
||||
try (PreparedStatement ps = conn.prepareStatement(QUERY_CONFIG)) {
|
||||
ps.setString(1, viewName);
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
while (rs.next()) {
|
||||
String listVis = rs.getString("list_visible");
|
||||
String editVis = rs.getString("edit_visible");
|
||||
String listStr = listVis == null ? "--" : ("1".equals(listVis) ? "显示" : "隐藏");
|
||||
String editStr = editVis == null ? "--" : ("1".equals(editVis) ? "显示" : "隐藏");
|
||||
String name = rs.getString("b_name");
|
||||
System.out.printf(" %-30s %-16s %-8s %-8s%n",
|
||||
rs.getString("b_field"),
|
||||
name != null ? name : "(null)",
|
||||
listStr,
|
||||
editStr);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
# FieldNameSync — 模块字段配置迁移工具
|
||||
|
||||
从旧库 `G3HY2025.dbo.s_columnLib` 同步字段配置到新库 `fms`。
|
||||
|
||||
## 同步的三项内容
|
||||
|
||||
| 序号 | 旧库来源 | 新库目标 | 说明 |
|
||||
| --- | --- | --- | --- |
|
||||
| 1 | `col_Caption` | `s_module_field.b_name` | 字段中文名称 |
|
||||
| 2 | `col_Visible` | `s_module_field_list.b_visible` | 列表是否显示(1=显示, 0=隐藏) |
|
||||
| 3 | `col_edit` | `s_module_field_edit.b_visible` | 编辑表单是否显示(1=显示, 0=隐藏) |
|
||||
|
||||
## 匹配规则
|
||||
|
||||
通过视图名和字段名关联新旧库:
|
||||
|
||||
```
|
||||
旧库 s_columnLib.col_TableName = 新库 s_module.b_viewtable
|
||||
旧库 s_columnLib.col_FieldName = 新库 s_module_field.b_field
|
||||
```
|
||||
|
||||
- 旧库字段匹配不上的,对应配置**不做修改**(保留原值)
|
||||
- 列表配置或编辑配置记录不存在时,**自动插入**新记录
|
||||
|
||||
## 前置条件
|
||||
|
||||
### 1. JDK 21
|
||||
|
||||
工具使用 Java 文本块语法(`"""`),需要 JDK 21+ 编译。
|
||||
|
||||
### 2. mssql-jdbc 驱动
|
||||
|
||||
确保本地 Maven 仓库存在驱动 jar:
|
||||
```
|
||||
%USERPROFILE%\.m2\repository\com\microsoft\sqlserver\mssql-jdbc\13.4.0.jre11\mssql-jdbc-13.4.0.jre11.jar
|
||||
```
|
||||
|
||||
如果不存在,从项目根目录 `fms-api/` 执行 `mvnw dependency:resolve` 下载。
|
||||
|
||||
### 3. 数据库连接
|
||||
|
||||
工具硬编码连接 `118.89.70.199:1433`,数据库 `fms`,直连新旧两库(同一实例)。
|
||||
|
||||
### 4. 新库已有 `s_module_field_edit.b_visible` 列
|
||||
|
||||
如果报 `Invalid column name 'b_visible'`,执行以下 SQL 添加列:
|
||||
```sql
|
||||
ALTER TABLE dbo.s_module_field_edit ADD b_visible int NOT NULL DEFAULT 1;
|
||||
```
|
||||
|
||||
### 5. 模块已在 s_module 中注册
|
||||
|
||||
目标模块的 `b_viewtable` 必须已在 `s_module` 表中配置,且已有 `s_module_field` 记录。
|
||||
|
||||
## 编译和运行
|
||||
|
||||
```bash
|
||||
# 进入迁移工具目录
|
||||
cd fms-api/tools/migration
|
||||
|
||||
# 设置 JDK 21
|
||||
export JAVA_HOME=/path/to/jdk-21 # Linux/Mac
|
||||
set JAVA_HOME=D:\devtool\jdk\jdk-21 # Windows
|
||||
|
||||
# 编译
|
||||
javac -cp "%USERPROFILE%\.m2\repository\com\microsoft\sqlserver\mssql-jdbc\13.4.0.jre11\mssql-jdbc-13.4.0.jre11.jar" FieldNameSync.java
|
||||
|
||||
# 运行(以 v_b_othercompany 为例)
|
||||
java -cp ".;%USERPROFILE%\.m2\repository\com\microsoft\sqlserver\mssql-jdbc\13.4.0.jre11\mssql-jdbc-13.4.0.jre11.jar" FieldNameSync v_b_othercompany
|
||||
```
|
||||
|
||||
## 输出示例
|
||||
|
||||
```
|
||||
=== 同步前 ===
|
||||
b_field b_name list edit
|
||||
------------------------------------------------------------------
|
||||
b_id 系统编号 显示 显示
|
||||
b_name 公司全称 显示 显示
|
||||
...
|
||||
|
||||
=== 1) 字段名称 (col_Caption → b_name) ===
|
||||
更新: 47 条
|
||||
|
||||
=== 2) 列表显示 (col_Visible → s_module_field_list.b_visible) ===
|
||||
更新已有: 50 条
|
||||
新增: 0 条
|
||||
|
||||
=== 3) 编辑显示 (col_edit → s_module_field_edit.b_visible) ===
|
||||
更新已有: 44 条
|
||||
新增: 0 条
|
||||
|
||||
=== 事务已提交 ===
|
||||
```
|
||||
|
||||
## 用 AI 助手调用
|
||||
|
||||
如果通过 AI 助手(如 CodeBuddy)执行,告诉 AI 以下步骤即可:
|
||||
|
||||
> 帮我用 FieldNameSync 工具同步视图 `<viewName>` 的字段配置。
|
||||
>
|
||||
> 1. 进入 `fms-api/tools/migration/` 目录
|
||||
> 2. 用 JDK 21 编译:`javac -cp "mssql-jdbc 驱动 jar 路径" FieldNameSync.java`
|
||||
> 3. 运行:`java -cp ".;驱动 jar 路径" FieldNameSync <viewName>`
|
||||
> 4. 运行完毕后删除 `FieldNameSync.class` 文件
|
||||
|
||||
## 已有同步记录
|
||||
|
||||
| 视图名 | 同步日期 | 字段数 | 备注 |
|
||||
| --- | --- | --- | --- |
|
||||
| `v_b_othercompany` | 2026-07-30 | 50 | 已同步 |
|
||||
| `v_b_contact` | 2026-07-30 | 36 | 已同步 |
|
||||
Reference in new issue
Block a user