|
|
@@ -4,12 +4,13 @@ import com.ruoyi.monitoring.domain.CmcChannel;
|
|
4
|
4
|
import com.ruoyi.monitoring.domain.CmcTemperature;
|
|
5
|
5
|
import com.ruoyi.monitoring.mapper.CmcChannelMapper;
|
|
6
|
6
|
import com.ruoyi.monitoring.mapper.CmcTemperatureMapper;
|
|
7
|
|
-import com.ruoyi.monitoring.mapper.access.AccessChannelMapper;
|
|
8
|
|
-import com.ruoyi.monitoring.mapper.access.AccessDataMapper;
|
|
9
|
7
|
import com.ruoyi.monitoring.service.IAccessSyncService;
|
|
|
8
|
+import org.noear.solon.Solon;
|
|
10
|
9
|
import org.noear.solon.annotation.Component;
|
|
11
|
10
|
import org.noear.solon.annotation.Inject;
|
|
12
|
11
|
|
|
|
12
|
+import java.sql.*;
|
|
|
13
|
+import java.util.ArrayList;
|
|
13
|
14
|
import java.util.List;
|
|
14
|
15
|
|
|
15
|
16
|
@Component
|
|
|
@@ -18,12 +19,6 @@ public class AccessSyncServiceImpl implements IAccessSyncService {
|
|
18
|
19
|
// 每批插入的记录数
|
|
19
|
20
|
private static final int BATCH_SIZE = 5000;
|
|
20
|
21
|
|
|
21
|
|
- @Inject
|
|
22
|
|
- AccessChannelMapper accessChannelMapper;
|
|
23
|
|
-
|
|
24
|
|
- @Inject
|
|
25
|
|
- AccessDataMapper accessDataMapper;
|
|
26
|
|
-
|
|
27
|
22
|
@Inject
|
|
28
|
23
|
CmcChannelMapper sqliteChannelMapper;
|
|
29
|
24
|
|
|
|
@@ -31,53 +26,106 @@ public class AccessSyncServiceImpl implements IAccessSyncService {
|
|
31
|
26
|
CmcTemperatureMapper sqliteDataMapper;
|
|
32
|
27
|
|
|
33
|
28
|
@Override
|
|
34
|
|
- public void syncAccessToSqlite() {
|
|
|
29
|
+ public void syncAccessToSqlite() throws Exception {
|
|
35
|
30
|
System.out.println("开始同步Access数据到SQLite...");
|
|
36
|
|
-
|
|
37
|
|
- try {
|
|
38
|
|
- // 同步 Channel:增量,按ID递增同步
|
|
|
31
|
+ Class.forName(Solon.cfg().getProperty("mybatis-flex.datasource.access.driverClassName"));
|
|
|
32
|
+
|
|
|
33
|
+ try (Connection conn = DriverManager.getConnection(
|
|
|
34
|
+ Solon.cfg().getProperty("mybatis-flex.datasource.access.jdbcUrl"), "", "")) {
|
|
|
35
|
+
|
|
|
36
|
+ // 直接同步 Channel 表
|
|
39
|
37
|
System.out.println("开始同步Channel表...");
|
|
40
|
|
- Long channelMaxId = sqliteChannelMapper.selectMaxId();
|
|
41
|
|
- if (channelMaxId == null) {
|
|
42
|
|
- channelMaxId = 0L;
|
|
43
|
|
- }
|
|
|
38
|
+ Integer channelMaxId = sqliteChannelMapper.selectMaxId();
|
|
44
|
39
|
System.out.println("SQLite Channel表当前最大ID: " + channelMaxId);
|
|
45
|
|
- List<CmcChannel> newChannels = accessChannelMapper.listByIdRange(channelMaxId);
|
|
46
|
|
- if (newChannels != null && !newChannels.isEmpty()) {
|
|
|
40
|
+
|
|
|
41
|
+ List<CmcChannel> newChannels = new ArrayList<>();
|
|
|
42
|
+
|
|
|
43
|
+ String channelQuery = "SELECT ID, Sensor_No, Info, Ver, Sn, Ca, Cb, Units, LastDT " +
|
|
|
44
|
+ "FROM Channel " +
|
|
|
45
|
+ "WHERE ID > ? " +
|
|
|
46
|
+ "ORDER BY ID ASC";
|
|
|
47
|
+
|
|
|
48
|
+ try (PreparedStatement pstmt = conn.prepareStatement(channelQuery)) {
|
|
|
49
|
+ pstmt.setInt(1, channelMaxId);
|
|
|
50
|
+ try (ResultSet rs = pstmt.executeQuery()) {
|
|
|
51
|
+ while (rs.next()) {
|
|
|
52
|
+ CmcChannel channel = new CmcChannel();
|
|
|
53
|
+ channel.setId(rs.getInt("ID"));
|
|
|
54
|
+ channel.setSensorNo(rs.getInt("Sensor_No"));
|
|
|
55
|
+ channel.setInfo(rs.getString("Info"));
|
|
|
56
|
+ channel.setVer(rs.getInt("Ver"));
|
|
|
57
|
+ channel.setSn(rs.getString("Sn"));
|
|
|
58
|
+ channel.setCa(rs.getInt("Ca"));
|
|
|
59
|
+ channel.setCb(rs.getInt("Cb"));
|
|
|
60
|
+ channel.setUnits(rs.getString("Units"));
|
|
|
61
|
+ channel.setLastDT(rs.getString("LastDT"));
|
|
|
62
|
+ newChannels.add(channel);
|
|
|
63
|
+ }
|
|
|
64
|
+ }
|
|
|
65
|
+ }
|
|
|
66
|
+
|
|
|
67
|
+ if (!newChannels.isEmpty()) {
|
|
47
|
68
|
System.out.println("从Access读取到" + newChannels.size() + "条新Channel记录(ID > " + channelMaxId + ")");
|
|
48
|
|
- // 分批插入
|
|
49
|
69
|
batchInsertChannels(newChannels);
|
|
50
|
70
|
System.out.println("Channel表同步完成,共同步" + newChannels.size() + "条记录");
|
|
51
|
71
|
} else {
|
|
52
|
72
|
System.out.println("没有新的Channel记录需要同步(ID > " + channelMaxId + ")");
|
|
53
|
73
|
}
|
|
54
|
74
|
|
|
55
|
|
- // 同步 Data:增量,按ID递增同步
|
|
|
75
|
+ // 同步 Data 表
|
|
56
|
76
|
System.out.println("开始同步Data表...");
|
|
57
|
|
- Long dataMaxId = sqliteDataMapper.selectMaxId();
|
|
58
|
|
- if (dataMaxId == null) {
|
|
59
|
|
- dataMaxId = 0L;
|
|
60
|
|
- }
|
|
|
77
|
+ Integer dataMaxId = sqliteDataMapper.selectMaxId();
|
|
61
|
78
|
System.out.println("SQLite Data表当前最大ID: " + dataMaxId);
|
|
62
|
|
- List<CmcTemperature> newData = accessDataMapper.listByIdRange(dataMaxId);
|
|
63
|
|
- for (CmcTemperature cmcTemperature : newData) {
|
|
64
|
|
- cmcTemperature.setDatetime(cmcTemperature.getDatetime().replace(".000000", ""));
|
|
65
|
|
- cmcTemperature.setData(cmcTemperature.getData().replace("00000", ""));
|
|
|
79
|
+
|
|
|
80
|
+ List<CmcTemperature> newData = new ArrayList<>();
|
|
|
81
|
+ String dataQuery = "SELECT ID, Group_No, Sensor_No, DateTime, Data, Volt, CSQ " +
|
|
|
82
|
+ "FROM Data " +
|
|
|
83
|
+ "WHERE ID > ? " +
|
|
|
84
|
+ "ORDER BY ID ASC";
|
|
|
85
|
+
|
|
|
86
|
+ try (PreparedStatement pstmt = conn.prepareStatement(dataQuery)) {
|
|
|
87
|
+ pstmt.setInt(1, dataMaxId);
|
|
|
88
|
+ try (ResultSet rs = pstmt.executeQuery()) {
|
|
|
89
|
+ while (rs.next()) {
|
|
|
90
|
+ CmcTemperature data = new CmcTemperature();
|
|
|
91
|
+ data.setId(rs.getInt("ID"));
|
|
|
92
|
+ data.setGroupNo(rs.getInt("Group_No"));
|
|
|
93
|
+ data.setSensorNo(rs.getInt("Sensor_No"));
|
|
|
94
|
+
|
|
|
95
|
+ // 处理日期时间字段
|
|
|
96
|
+ String datetime = rs.getString("DateTime");
|
|
|
97
|
+ if (datetime != null) {
|
|
|
98
|
+ data.setDatetime(datetime.replace(".000000", ""));
|
|
|
99
|
+ } else {
|
|
|
100
|
+ data.setDatetime("");
|
|
|
101
|
+ }
|
|
|
102
|
+
|
|
|
103
|
+ // 处理数据字段
|
|
|
104
|
+ String dataValue = rs.getString("Data");
|
|
|
105
|
+ if (dataValue != null) {
|
|
|
106
|
+ data.setData(dataValue.replace("00000", ""));
|
|
|
107
|
+ } else {
|
|
|
108
|
+ data.setData("");
|
|
|
109
|
+ }
|
|
|
110
|
+
|
|
|
111
|
+ data.setVolt(rs.getString("Volt"));
|
|
|
112
|
+ data.setCSQ(rs.getInt("CSQ"));
|
|
|
113
|
+ newData.add(data);
|
|
|
114
|
+ }
|
|
|
115
|
+ }
|
|
66
|
116
|
}
|
|
67
|
|
- if (newData != null && !newData.isEmpty()) {
|
|
|
117
|
+
|
|
|
118
|
+ if (!newData.isEmpty()) {
|
|
68
|
119
|
System.out.println("从Access读取到" + newData.size() + "条新Data记录(ID > " + dataMaxId + ")");
|
|
69
|
|
- // 分批插入
|
|
70
|
120
|
batchInsertData(newData);
|
|
71
|
121
|
System.out.println("Data表同步完成,共同步" + newData.size() + "条新记录");
|
|
72
|
122
|
} else {
|
|
73
|
123
|
System.out.println("没有新的Data记录需要同步(ID > " + dataMaxId + ")");
|
|
74
|
124
|
}
|
|
75
|
|
-
|
|
|
125
|
+
|
|
76
|
126
|
System.out.println("Access数据同步到SQLite完成");
|
|
77
|
|
- } catch (Exception e) {
|
|
78
|
|
- System.out.println("同步Access数据到SQLite失败: " + e.getMessage());
|
|
79
|
|
- e.printStackTrace();
|
|
80
|
|
- throw e;
|
|
|
127
|
+ } catch (SQLException e) {
|
|
|
128
|
+ throw new RuntimeException("Access数据库查询失败", e);
|
|
81
|
129
|
}
|
|
82
|
130
|
}
|
|
83
|
131
|
|
|
|
@@ -87,12 +135,12 @@ public class AccessSyncServiceImpl implements IAccessSyncService {
|
|
87
|
135
|
private void batchInsertChannels(List<CmcChannel> channels) {
|
|
88
|
136
|
int total = channels.size();
|
|
89
|
137
|
int batchCount = (total + BATCH_SIZE - 1) / BATCH_SIZE;
|
|
90
|
|
-
|
|
|
138
|
+
|
|
91
|
139
|
for (int i = 0; i < batchCount; i++) {
|
|
92
|
140
|
int fromIndex = i * BATCH_SIZE;
|
|
93
|
141
|
int toIndex = Math.min((i + 1) * BATCH_SIZE, total);
|
|
94
|
142
|
List<CmcChannel> batch = channels.subList(fromIndex, toIndex);
|
|
95
|
|
-
|
|
|
143
|
+
|
|
96
|
144
|
sqliteChannelMapper.batchInsert(batch);
|
|
97
|
145
|
System.out.println("Channel表已插入第 " + (i + 1) + "/" + batchCount + " 批,共 " + batch.size() + " 条记录");
|
|
98
|
146
|
}
|
|
|
@@ -104,16 +152,14 @@ public class AccessSyncServiceImpl implements IAccessSyncService {
|
|
104
|
152
|
private void batchInsertData(List<CmcTemperature> dataList) {
|
|
105
|
153
|
int total = dataList.size();
|
|
106
|
154
|
int batchCount = (total + BATCH_SIZE - 1) / BATCH_SIZE;
|
|
107
|
|
-
|
|
|
155
|
+
|
|
108
|
156
|
for (int i = 0; i < batchCount; i++) {
|
|
109
|
157
|
int fromIndex = i * BATCH_SIZE;
|
|
110
|
158
|
int toIndex = Math.min((i + 1) * BATCH_SIZE, total);
|
|
111
|
159
|
List<CmcTemperature> batch = dataList.subList(fromIndex, toIndex);
|
|
112
|
|
-
|
|
|
160
|
+
|
|
113
|
161
|
sqliteDataMapper.batchInsert(batch);
|
|
114
|
162
|
System.out.println("Data表已插入第 " + (i + 1) + "/" + batchCount + " 批,共 " + batch.size() + " 条记录");
|
|
115
|
163
|
}
|
|
116
|
164
|
}
|
|
117
|
|
-}
|
|
118
|
|
-
|
|
119
|
|
-
|
|
|
165
|
+}
|