|
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+<?php
|
|
|
2
|
+
|
|
|
3
|
+namespace e282486518\LaravelTableSync;
|
|
|
4
|
+
|
|
|
5
|
+use Illuminate\Database\Eloquent\Model;
|
|
|
6
|
+use Illuminate\Support\Arr;
|
|
|
7
|
+use Illuminate\Support\Carbon;
|
|
|
8
|
+use Illuminate\Support\Facades\Http;
|
|
|
9
|
+use Illuminate\Support\Facades\Log;
|
|
|
10
|
+use Illuminate\Support\Facades\Redis;
|
|
|
11
|
+
|
|
|
12
|
+/**
|
|
|
13
|
+ * 增量同步数据到数据表
|
|
|
14
|
+ */
|
|
|
15
|
+abstract class SyncService
|
|
|
16
|
+{
|
|
|
17
|
+ // A服务器API地址
|
|
|
18
|
+ protected string $apiUrl = '';
|
|
|
19
|
+
|
|
|
20
|
+ // API令牌
|
|
|
21
|
+ protected string $apiToken = '';
|
|
|
22
|
+
|
|
|
23
|
+ /**
|
|
|
24
|
+ * @var int 会议系统年份分类
|
|
|
25
|
+ */
|
|
|
26
|
+ protected int $year = 2025;
|
|
|
27
|
+
|
|
|
28
|
+ /**
|
|
|
29
|
+ * @var int 会议系统栏目分类
|
|
|
30
|
+ */
|
|
|
31
|
+ protected int $catid = 3;
|
|
|
32
|
+
|
|
|
33
|
+ /**
|
|
|
34
|
+ * @var string 主键名
|
|
|
35
|
+ */
|
|
|
36
|
+ protected string $key = 'id';
|
|
|
37
|
+
|
|
|
38
|
+ /**
|
|
|
39
|
+ * @var array|int[] 添加数据时, 本服务器额外增加的字段
|
|
|
40
|
+ */
|
|
|
41
|
+ protected array $mapping_add = [
|
|
|
42
|
+ 'is_pay' => 0, // 是否付款
|
|
|
43
|
+ 'is_check' => 0, // 是否报名审核
|
|
|
44
|
+ 'is_sign' => 1, // 签到时报名
|
|
|
45
|
+ 'expoid' => 1, // 展会ID
|
|
|
46
|
+ ];
|
|
|
47
|
+
|
|
|
48
|
+ /**
|
|
|
49
|
+ * @var array|string[] 远程服务器多余的字段, 写入/更新时需过滤掉
|
|
|
50
|
+ */
|
|
|
51
|
+ protected array $filter = ['agenda', 'guests', 'partner', 'creator'];
|
|
|
52
|
+
|
|
|
53
|
+ /**
|
|
|
54
|
+ * @var array 字段名称替换, 格式: [服务器字段名1 => 本地字段名1, 服务器字段名2 => 本地字段名2, ...]
|
|
|
55
|
+ */
|
|
|
56
|
+ protected array $mapping_replace = [];
|
|
|
57
|
+
|
|
|
58
|
+ /**
|
|
|
59
|
+ * @var array 有哪些字段需要特殊处理的, 将json字符串转化成json
|
|
|
60
|
+ */
|
|
|
61
|
+ protected array $json_fields = ['title', 'content', 'addrress'];
|
|
|
62
|
+
|
|
|
63
|
+ /**
|
|
|
64
|
+ * Redis key, 格式: key + 表名
|
|
|
65
|
+ */
|
|
|
66
|
+ const REDISKEY = 'sync_last_time_';
|
|
|
67
|
+
|
|
|
68
|
+ protected Model $_model;
|
|
|
69
|
+
|
|
|
70
|
+ public function __construct($model, $year = 2025, $catid = 1)
|
|
|
71
|
+ {
|
|
|
72
|
+ $this->_model = $model;
|
|
|
73
|
+ $this->year = $year;
|
|
|
74
|
+ $this->catid = $catid;
|
|
|
75
|
+ }
|
|
|
76
|
+
|
|
|
77
|
+ /**
|
|
|
78
|
+ * 执行数据同步
|
|
|
79
|
+ */
|
|
|
80
|
+ public function sync(): bool
|
|
|
81
|
+ {
|
|
|
82
|
+ try {
|
|
|
83
|
+ // 获取上次同步时间
|
|
|
84
|
+ $lastSyncTime = $this->getLastSyncTime();
|
|
|
85
|
+ Log::info("更新 {$this->getTable()} 表开始, 最后更新时间: " . $lastSyncTime->format('Y-m-d H:i:s'));
|
|
|
86
|
+
|
|
|
87
|
+ // 从A服务器获取更新数据
|
|
|
88
|
+ $response = $this->fetchUpdatesFromServer($lastSyncTime);
|
|
|
89
|
+
|
|
|
90
|
+ if ($response['err'] > 0 || empty($response['data'])) {
|
|
|
91
|
+ Log::info("远程 {$this->getTable()} 表无更新数据");
|
|
|
92
|
+ $this->updateLastSyncTime(Carbon::now());
|
|
|
93
|
+ return true;
|
|
|
94
|
+ }
|
|
|
95
|
+
|
|
|
96
|
+ // 处理获取到的数据
|
|
|
97
|
+ $this->processMeetingData($response['data']);
|
|
|
98
|
+
|
|
|
99
|
+ // 更新最后同步时间
|
|
|
100
|
+ $latestUpdate = $response['latest_update']
|
|
|
101
|
+ ? Carbon::createFromFormat('Y-m-d H:i:s', $response['latest_update'])
|
|
|
102
|
+ : Carbon::now();
|
|
|
103
|
+
|
|
|
104
|
+ $this->updateLastSyncTime($latestUpdate);
|
|
|
105
|
+
|
|
|
106
|
+ Log::info("更新 {$this->getTable()} 表成功. 最后更新时间: " . $latestUpdate->format('Y-m-d H:i:s'));
|
|
|
107
|
+ return true;
|
|
|
108
|
+ } catch (\Exception $e) {
|
|
|
109
|
+ Log::error("数据表 {$this->getTable()} 更新失败: " . $e->getMessage());
|
|
|
110
|
+ return false;
|
|
|
111
|
+ }
|
|
|
112
|
+ }
|
|
|
113
|
+
|
|
|
114
|
+ /**
|
|
|
115
|
+ * 从A服务器获取更新数据
|
|
|
116
|
+ * @throws \Exception
|
|
|
117
|
+ */
|
|
|
118
|
+ protected function fetchUpdatesFromServer(Carbon $since)
|
|
|
119
|
+ {
|
|
|
120
|
+ $response = Http::withOptions([
|
|
|
121
|
+ 'verify' => false, // 禁用 SSL 验证
|
|
|
122
|
+ ])->withHeaders([
|
|
|
123
|
+ 'Authorization' => 'Bearer ' . $this->apiToken,
|
|
|
124
|
+ 'Accept' => 'application/json'
|
|
|
125
|
+ ])->get($this->apiUrl, [
|
|
|
126
|
+ 'time' => $since->format('Y-m-d H:i:s'), // 最后更新时间
|
|
|
127
|
+ 'catid' => $this->catid, // 栏目
|
|
|
128
|
+ 'year' => $this->year, // 年份
|
|
|
129
|
+ ]);
|
|
|
130
|
+
|
|
|
131
|
+ if (!$response->successful()) {
|
|
|
132
|
+ throw new \Exception("远程获取更新数据失败: " . $response->body());
|
|
|
133
|
+ }
|
|
|
134
|
+
|
|
|
135
|
+ return $response->json();
|
|
|
136
|
+ }
|
|
|
137
|
+
|
|
|
138
|
+ /**
|
|
|
139
|
+ * 处理会议数据
|
|
|
140
|
+ */
|
|
|
141
|
+ protected function processMeetingData(array $meetings): void
|
|
|
142
|
+ {
|
|
|
143
|
+ foreach ($meetings as $meetingData) {
|
|
|
144
|
+ // 处理字段
|
|
|
145
|
+ $processedData = $this->processJsonFields($meetingData);
|
|
|
146
|
+
|
|
|
147
|
+ $id = $processedData[$this->key]; // 主键
|
|
|
148
|
+
|
|
|
149
|
+ // 状态为0表示需要删除
|
|
|
150
|
+ if ($this->is_delete($processedData)) {
|
|
|
151
|
+ $this->handleDeletion($id);
|
|
|
152
|
+ Log::info("删除数据, id=" . $id);
|
|
|
153
|
+ continue;
|
|
|
154
|
+ }
|
|
|
155
|
+
|
|
|
156
|
+ // 检查记录是否存在
|
|
|
157
|
+ $meeting = $this->_model->find($id);
|
|
|
158
|
+
|
|
|
159
|
+ if ($meeting) {
|
|
|
160
|
+ // 更新现有记录,保留B服务器特有字段
|
|
|
161
|
+ $this->updateMeeting($meeting, $processedData);
|
|
|
162
|
+ Log::info("更新数据, id=" . $id);
|
|
|
163
|
+ } else {
|
|
|
164
|
+ // 创建新记录,设置B服务器特有字段默认值
|
|
|
165
|
+ $this->createMeeting($processedData);
|
|
|
166
|
+ Log::info("创建数据, id=" . $id);
|
|
|
167
|
+ }
|
|
|
168
|
+ }
|
|
|
169
|
+ }
|
|
|
170
|
+
|
|
|
171
|
+ /**
|
|
|
172
|
+ * 判断是否被删除
|
|
|
173
|
+ * @param $data
|
|
|
174
|
+ * @return bool
|
|
|
175
|
+ */
|
|
|
176
|
+ protected function is_delete($data): bool
|
|
|
177
|
+ {
|
|
|
178
|
+ return $data['status'] == 0;
|
|
|
179
|
+ }
|
|
|
180
|
+
|
|
|
181
|
+ /**
|
|
|
182
|
+ * 处理JSON字段
|
|
|
183
|
+ */
|
|
|
184
|
+ protected function processJsonFields(array $data): array
|
|
|
185
|
+ {
|
|
|
186
|
+ // 先删除字段
|
|
|
187
|
+ $data = Arr::except($data, array_keys($this->mapping_add));
|
|
|
188
|
+ $data = Arr::except($data, $this->filter);
|
|
|
189
|
+ // 再替换字段名
|
|
|
190
|
+ foreach ($this->mapping_replace as $key_serv => $key_local) {
|
|
|
191
|
+ if (array_key_exists($key_serv, $data)) {
|
|
|
192
|
+ $data[$key_local] = Arr::get($data, $key_serv);
|
|
|
193
|
+ unset($data[$key_serv]);
|
|
|
194
|
+ }
|
|
|
195
|
+ }
|
|
|
196
|
+ // json字符串 => json
|
|
|
197
|
+ foreach ($this->json_fields as $field) {
|
|
|
198
|
+ if (isset($data[$field]) && is_string($data[$field])) {
|
|
|
199
|
+ $data[$field] = json_decode($data[$field], true);
|
|
|
200
|
+ }
|
|
|
201
|
+ }
|
|
|
202
|
+ return $data;
|
|
|
203
|
+ }
|
|
|
204
|
+
|
|
|
205
|
+ /**
|
|
|
206
|
+ * 创建新数据
|
|
|
207
|
+ */
|
|
|
208
|
+ protected function createMeeting(array $data): void
|
|
|
209
|
+ {
|
|
|
210
|
+ // 添加本服务特有的字段, 默认值
|
|
|
211
|
+ $data = array_merge($data, $this->mapping_add);
|
|
|
212
|
+ $this->_model->create($data);
|
|
|
213
|
+ }
|
|
|
214
|
+
|
|
|
215
|
+ /**
|
|
|
216
|
+ * 更新现有数据
|
|
|
217
|
+ */
|
|
|
218
|
+ protected function updateMeeting(Model $meeting, array $data): void
|
|
|
219
|
+ {
|
|
|
220
|
+ // 保留本地特有字段,只更新从远程获取的字段
|
|
|
221
|
+ $meeting->update($data);
|
|
|
222
|
+ $this->afterUpdate($meeting->id);
|
|
|
223
|
+ }
|
|
|
224
|
+
|
|
|
225
|
+ /**
|
|
|
226
|
+ * 更新数据
|
|
|
227
|
+ * @param $id
|
|
|
228
|
+ * @return void
|
|
|
229
|
+ */
|
|
|
230
|
+ protected function afterUpdate($id): void {
|
|
|
231
|
+ //$this->_model->getOne($id, true);
|
|
|
232
|
+ }
|
|
|
233
|
+
|
|
|
234
|
+ /**
|
|
|
235
|
+ * 删除数据, 物理删除
|
|
|
236
|
+ */
|
|
|
237
|
+ protected function handleDeletion($id): void
|
|
|
238
|
+ {
|
|
|
239
|
+ $meeting = $this->_model->find($id);
|
|
|
240
|
+ if ($meeting) {
|
|
|
241
|
+ $meeting->delete();
|
|
|
242
|
+ }
|
|
|
243
|
+ }
|
|
|
244
|
+
|
|
|
245
|
+ /**
|
|
|
246
|
+ * 获取上次同步时间
|
|
|
247
|
+ */
|
|
|
248
|
+ protected function getLastSyncTime(): bool|Carbon
|
|
|
249
|
+ {
|
|
|
250
|
+ $lastSync = Redis::get(self::REDISKEY . $this->getTable());
|
|
|
251
|
+
|
|
|
252
|
+ if (!$lastSync) {
|
|
|
253
|
+ // 如果没有同步记录,默认使用系统最早时间
|
|
|
254
|
+ return Carbon::createFromFormat('Y-m-d H:i:s', '2000-01-01 00:00:00');
|
|
|
255
|
+ }
|
|
|
256
|
+
|
|
|
257
|
+ return Carbon::createFromFormat('Y-m-d H:i:s', $lastSync);
|
|
|
258
|
+ }
|
|
|
259
|
+
|
|
|
260
|
+ /**
|
|
|
261
|
+ * 更新最后同步时间
|
|
|
262
|
+ */
|
|
|
263
|
+ protected function updateLastSyncTime(Carbon $time): void
|
|
|
264
|
+ {
|
|
|
265
|
+ Redis::set(self::REDISKEY . $this->getTable(), $time->format('Y-m-d H:i:s'));
|
|
|
266
|
+ }
|
|
|
267
|
+
|
|
|
268
|
+ protected function getTable(): string {
|
|
|
269
|
+ return $this->_model->getTable();
|
|
|
270
|
+ }
|
|
|
271
|
+}
|