|
|
@@ -0,0 +1,743 @@
|
|
|
1
|
+<?php
|
|
|
2
|
+declare (strict_types=1);
|
|
|
3
|
+
|
|
|
4
|
+namespace app\command;
|
|
|
5
|
+
|
|
|
6
|
+use app\common\enum\douhuomall\DouhuomallEnum;
|
|
|
7
|
+use app\common\enum\store\ProductEnum;
|
|
|
8
|
+use app\traits\GongApiRequest;
|
|
|
9
|
+use think\console\Command;
|
|
|
10
|
+use think\console\Input;
|
|
|
11
|
+use think\console\Output;
|
|
|
12
|
+use think\db\exception\DataNotFoundException;
|
|
|
13
|
+use think\db\exception\DbException;
|
|
|
14
|
+use think\db\exception\ModelNotFoundException;
|
|
|
15
|
+use think\facade\Db;
|
|
|
16
|
+use think\facade\Log;
|
|
|
17
|
+
|
|
|
18
|
+class WiwibaoProductSync extends Command
|
|
|
19
|
+{
|
|
|
20
|
+
|
|
|
21
|
+ use GongApiRequest;
|
|
|
22
|
+
|
|
|
23
|
+ // 配置参数
|
|
|
24
|
+ protected $config = [
|
|
|
25
|
+ 'page_size' => 100, // 每页拉取数量
|
|
|
26
|
+ 'max_pages' => 0, // 最大页数限制
|
|
|
27
|
+ 'price_change_rate' => 0.0, // 价格变动比率阈值(10%)
|
|
|
28
|
+ ];
|
|
|
29
|
+
|
|
|
30
|
+ protected function configure()
|
|
|
31
|
+ {
|
|
|
32
|
+ // 指令配置
|
|
|
33
|
+ $this->setName('wiwibaoproductsync')
|
|
|
34
|
+ ->setDescription('the wiwibaoproductsync command');
|
|
|
35
|
+ }
|
|
|
36
|
+
|
|
|
37
|
+ protected function execute(Input $input, Output $output)
|
|
|
38
|
+ {
|
|
|
39
|
+ $output->writeln('定时任务执行中...');
|
|
|
40
|
+
|
|
|
41
|
+ $this->syncGoodsFromGong();
|
|
|
42
|
+
|
|
|
43
|
+ $output->writeln('定时任务执行完成');
|
|
|
44
|
+ }
|
|
|
45
|
+
|
|
|
46
|
+ /**
|
|
|
47
|
+ * 同步微唯宝商品数据(主方法)
|
|
|
48
|
+ */
|
|
|
49
|
+ public function syncGoodsFromGong()
|
|
|
50
|
+ {
|
|
|
51
|
+ try {
|
|
|
52
|
+ set_time_limit(0);
|
|
|
53
|
+
|
|
|
54
|
+ // 1. 批量拉取所有商品数据
|
|
|
55
|
+ $allProducts = $this->fetchAllProducts();
|
|
|
56
|
+
|
|
|
57
|
+ if (empty($allProducts)) {
|
|
|
58
|
+ Log::info('未拉取到任何商品数据');
|
|
|
59
|
+ return 'success';
|
|
|
60
|
+ }
|
|
|
61
|
+
|
|
|
62
|
+ // 2. 获取本地所有SPU ID
|
|
|
63
|
+ $localSpuIds = Db::name('douhuomall')->column('spu_id');
|
|
|
64
|
+ $syncedSpuIds = [];
|
|
|
65
|
+
|
|
|
66
|
+ // 3. 批量处理商品数据
|
|
|
67
|
+ foreach ($allProducts as $product) {
|
|
|
68
|
+ try {
|
|
|
69
|
+ $spuId = $this->processSingleProduct($product);
|
|
|
70
|
+ if ($spuId) {
|
|
|
71
|
+ $syncedSpuIds[] = $spuId;
|
|
|
72
|
+ }
|
|
|
73
|
+ } catch (\Exception $e) {
|
|
|
74
|
+ Log::error("处理商品 {$product['goods_id']} 失败: " . $e->getMessage());
|
|
|
75
|
+ continue;
|
|
|
76
|
+ }
|
|
|
77
|
+ }
|
|
|
78
|
+
|
|
|
79
|
+ // 4. 处理需要下架的商品(本地有但第三方已下架)
|
|
|
80
|
+ $this->handleOfflineProducts($localSpuIds, $syncedSpuIds);
|
|
|
81
|
+
|
|
|
82
|
+ return 'success';
|
|
|
83
|
+
|
|
|
84
|
+ } catch (\Exception $e) {
|
|
|
85
|
+ $this->logError('商品同步失败', $e);
|
|
|
86
|
+ return 'error';
|
|
|
87
|
+ }
|
|
|
88
|
+ }
|
|
|
89
|
+
|
|
|
90
|
+ /**
|
|
|
91
|
+ * 批量拉取所有商品数据
|
|
|
92
|
+ */
|
|
|
93
|
+ protected function fetchAllProducts()
|
|
|
94
|
+ {
|
|
|
95
|
+ $allData = [];
|
|
|
96
|
+
|
|
|
97
|
+ // 第一页
|
|
|
98
|
+ $firstPageData = GongApiRequest::get_goods_list(1, $this->config['page_size']);
|
|
|
99
|
+ if (empty($firstPageData['list'])) {
|
|
|
100
|
+ return [];
|
|
|
101
|
+ }
|
|
|
102
|
+
|
|
|
103
|
+ $allData = $firstPageData['list'];
|
|
|
104
|
+ $totalPages = $firstPageData['pages'] ?? 1;
|
|
|
105
|
+ if (!empty($this->config['max_pages'])) {
|
|
|
106
|
+ $totalPages = min($totalPages, $this->config['max_pages']);
|
|
|
107
|
+ }
|
|
|
108
|
+
|
|
|
109
|
+ // 并行获取剩余页面
|
|
|
110
|
+ for ($page = 2; $page <= $totalPages; $page++) {
|
|
|
111
|
+ $pageData = GongApiRequest::get_goods_list($page, $this->config['page_size']);
|
|
|
112
|
+ if (!empty($pageData['list'])) {
|
|
|
113
|
+ $allData = array_merge($allData, $pageData['list']);
|
|
|
114
|
+ }
|
|
|
115
|
+ }
|
|
|
116
|
+
|
|
|
117
|
+ return $allData;
|
|
|
118
|
+ }
|
|
|
119
|
+
|
|
|
120
|
+ /**
|
|
|
121
|
+ * 处理单个商品
|
|
|
122
|
+ */
|
|
|
123
|
+ protected function processSingleProduct($productData)
|
|
|
124
|
+ {
|
|
|
125
|
+ $goodsId = $productData['goods_id'];
|
|
|
126
|
+
|
|
|
127
|
+ // 1. 获取商品详情
|
|
|
128
|
+ $goodsInfo = GongApiRequest::get_goods_info($goodsId);
|
|
|
129
|
+ if (!$goodsInfo) {
|
|
|
130
|
+ return null;
|
|
|
131
|
+ }
|
|
|
132
|
+
|
|
|
133
|
+ // 2. 处理图片信息
|
|
|
134
|
+ $imageData = $this->processProductImages($goodsId, $goodsInfo);
|
|
|
135
|
+
|
|
|
136
|
+ // 3. 处理SKU信息
|
|
|
137
|
+ $skuInfo = $this->processSkuInfo($goodsInfo['sku_list'] ?? [], $goodsId);
|
|
|
138
|
+
|
|
|
139
|
+ // 4. 构建保存数据
|
|
|
140
|
+ $saveData = $this->buildSaveData($goodsInfo, $skuInfo, $imageData);
|
|
|
141
|
+
|
|
|
142
|
+ // 5. 保存到本地(使用原有的add方法逻辑)
|
|
|
143
|
+ $spuId = $this->addToDouhuomall($saveData);
|
|
|
144
|
+
|
|
|
145
|
+ // 6. 检查并处理已上架商品的价格变动
|
|
|
146
|
+ $this->checkAndProcessOnlineProduct($spuId, $saveData);
|
|
|
147
|
+
|
|
|
148
|
+ return $spuId;
|
|
|
149
|
+ }
|
|
|
150
|
+
|
|
|
151
|
+ /**
|
|
|
152
|
+ * 处理商品图片信息
|
|
|
153
|
+ */
|
|
|
154
|
+ protected function processProductImages($goodsId, $goodsInfo)
|
|
|
155
|
+ {
|
|
|
156
|
+ // 如果你需要保存图片到本地,可以在这里实现
|
|
|
157
|
+ // 否则直接返回URL
|
|
|
158
|
+ return [
|
|
|
159
|
+ 'cover_url' => $goodsInfo['main_img'] ?? '',
|
|
|
160
|
+ 'detail_images' => $goodsInfo['detail_img'] ?? [],
|
|
|
161
|
+ 'detail_img_list' => $goodsInfo['detail_img'] ?? [] // 为了兼容以前写的代码,建议保留这个数据项,之前用的是detail_img_list字段,但我感觉他们意义一样
|
|
|
162
|
+ ];
|
|
|
163
|
+ }
|
|
|
164
|
+
|
|
|
165
|
+ /**
|
|
|
166
|
+ * 处理SKU信息
|
|
|
167
|
+ */
|
|
|
168
|
+ protected function processSkuInfo($skuList, $goodsId)
|
|
|
169
|
+ {
|
|
|
170
|
+ if (empty($skuList)) {
|
|
|
171
|
+ return [];
|
|
|
172
|
+ }
|
|
|
173
|
+
|
|
|
174
|
+ $processedSkus = [];
|
|
|
175
|
+ foreach ($skuList as $sku) {
|
|
|
176
|
+ if (empty($sku['plat_price'])) {
|
|
|
177
|
+ continue;
|
|
|
178
|
+ }
|
|
|
179
|
+
|
|
|
180
|
+ $processedSku = [
|
|
|
181
|
+ 'sku_id' => $sku['sku_id'] ?? 0,
|
|
|
182
|
+ 'plat_price' => $sku['plat_price'],
|
|
|
183
|
+ 'img_url' => $sku['main_img'],
|
|
|
184
|
+ 'retail_price' => $sku['retail_price'] ?? 0,
|
|
|
185
|
+ 'cost_price' => $this->calculateCostPrice($sku['plat_price']),
|
|
|
186
|
+ 'market_price' => $this->calculateMarketPrice($sku['plat_price']),
|
|
|
187
|
+ 'profit' => $this->calculateProfit($sku['plat_price']),
|
|
|
188
|
+ 'mer_profit' => $this->calculateMerProfit($sku['plat_price']),
|
|
|
189
|
+ 'yanglaojin' => '0.00',
|
|
|
190
|
+ 'main_img' => $sku['main_img'] ?? '',
|
|
|
191
|
+ 'attribute_json' => !empty($sku['attr']) ? $sku['attr'] : [['val' => '默认', 'name' => '默认']]
|
|
|
192
|
+ ];
|
|
|
193
|
+
|
|
|
194
|
+ $processedSkus[] = array_merge($sku, $processedSku);
|
|
|
195
|
+ }
|
|
|
196
|
+
|
|
|
197
|
+ return $processedSkus;
|
|
|
198
|
+ }
|
|
|
199
|
+
|
|
|
200
|
+ /**
|
|
|
201
|
+ * 构建保存数据
|
|
|
202
|
+ */
|
|
|
203
|
+ protected function buildSaveData($goodsInfo, $skuInfo, $imageData)
|
|
|
204
|
+ {
|
|
|
205
|
+ // 构建详情HTML
|
|
|
206
|
+ $detailHtml = $this->buildDetailHtml($imageData['detail_images']);
|
|
|
207
|
+
|
|
|
208
|
+ return [
|
|
|
209
|
+ 'spu_id' => $goodsInfo['goods_id'],
|
|
|
210
|
+ 'skuId_info' => $skuInfo,
|
|
|
211
|
+ 'sys_id' => '112',
|
|
|
212
|
+ 'goods_id' => $goodsInfo['goods_id'],
|
|
|
213
|
+ 'spuId_info' => array_merge(
|
|
|
214
|
+ $goodsInfo,
|
|
|
215
|
+ [
|
|
|
216
|
+ 'cover_url' => $imageData['cover_url'],
|
|
|
217
|
+ 'detail' => $detailHtml,
|
|
|
218
|
+ 'detail_img_list' => $imageData['detail_img_list']
|
|
|
219
|
+ ]
|
|
|
220
|
+ ),
|
|
|
221
|
+ 'sku_id' => $skuInfo[0]['sku_id'] ?? 0,
|
|
|
222
|
+ 'status' => $goodsInfo['status'] ?? 0,
|
|
|
223
|
+ 'cate_ids' => $goodsInfo['cate_ids'] ?? '',
|
|
|
224
|
+ 'title' => $goodsInfo['spu_name'] ?? '',
|
|
|
225
|
+ 'cxb_cate_id' => 0
|
|
|
226
|
+ ];
|
|
|
227
|
+ }
|
|
|
228
|
+
|
|
|
229
|
+ /**
|
|
|
230
|
+ * 构建详情HTML
|
|
|
231
|
+ */
|
|
|
232
|
+ protected function buildDetailHtml($detailImages)
|
|
|
233
|
+ {
|
|
|
234
|
+ if (empty($detailImages)) {
|
|
|
235
|
+ return '';
|
|
|
236
|
+ }
|
|
|
237
|
+
|
|
|
238
|
+ $detailHtml = '';
|
|
|
239
|
+ foreach ((array)$detailImages as $imgUrl) {
|
|
|
240
|
+ $detailHtml .= "<img src='{$imgUrl}' referrerpolicy='no-referrer' />";
|
|
|
241
|
+ }
|
|
|
242
|
+
|
|
|
243
|
+ return $detailHtml;
|
|
|
244
|
+ }
|
|
|
245
|
+
|
|
|
246
|
+ /**
|
|
|
247
|
+ * 保存到douhuomall表(基于原有add方法)
|
|
|
248
|
+ */
|
|
|
249
|
+ protected function addToDouhuomall($data)
|
|
|
250
|
+ {
|
|
|
251
|
+ try {
|
|
|
252
|
+ $findData = Db::name('douhuomall')->where('spu_id', $data['spu_id'])->find();
|
|
|
253
|
+
|
|
|
254
|
+ // 对SKU按成本价排序,获取最低成本价的SKU
|
|
|
255
|
+ $skuInfo = $this->arrSort($data['skuId_info'], 'cost_price', SORT_ASC);
|
|
|
256
|
+
|
|
|
257
|
+ // 计算价格字段
|
|
|
258
|
+ $data['cost_price'] = $skuInfo[0]['cost_price'] ?? 0;
|
|
|
259
|
+ $data['market_price'] = $skuInfo[0]['market_price'] ?? 0;
|
|
|
260
|
+ $data['ot_price'] = $skuInfo[0]['retail_price'] ?? 0;
|
|
|
261
|
+ $data['mer_profit'] = $skuInfo[0]['mer_profit'] ?? 0;
|
|
|
262
|
+ $data['pension'] = $skuInfo[0]['yanglaojin'] ?? 0;
|
|
|
263
|
+ $data['profit'] = (empty($data['cost_price']) || empty($data['market_price']))
|
|
|
264
|
+ ? 0
|
|
|
265
|
+ : bcdiv((string)$data['market_price'], (string)$data['cost_price'], 2);
|
|
|
266
|
+
|
|
|
267
|
+ $now = date('Y-m-d H:i:s');
|
|
|
268
|
+
|
|
|
269
|
+ if (empty($findData)) {
|
|
|
270
|
+ // 新增
|
|
|
271
|
+ $insertData = [
|
|
|
272
|
+ 'spu_id' => $data['spu_id'],
|
|
|
273
|
+ 'skuId_info' => json_encode($data['skuId_info'], JSON_UNESCAPED_UNICODE),
|
|
|
274
|
+ 'sys_id' => $data['sys_id'],
|
|
|
275
|
+ 'goods_id' => $data['goods_id'],
|
|
|
276
|
+ 'spuId_info' => json_encode($data['spuId_info'], JSON_UNESCAPED_UNICODE),
|
|
|
277
|
+ 'create_time' => $now,
|
|
|
278
|
+ 'update_time' => $now,
|
|
|
279
|
+ 'sku_id' => $data['sku_id'],
|
|
|
280
|
+ 'status' => $data['status'],
|
|
|
281
|
+ 'cate_ids' => $data['cate_ids'],
|
|
|
282
|
+ 'title' => $data['title'],
|
|
|
283
|
+ 'cost_price' => $data['cost_price'],
|
|
|
284
|
+ 'market_price' => $data['market_price'],
|
|
|
285
|
+ 'ot_price' => $data['ot_price'],
|
|
|
286
|
+ 'profit' => $data['profit'],
|
|
|
287
|
+ 'mer_profit' => $data['mer_profit'],
|
|
|
288
|
+ 'pension' => $data['pension'],
|
|
|
289
|
+ 'cxb_cate_id' => $data['cxb_cate_id']
|
|
|
290
|
+ ];
|
|
|
291
|
+ Db::name('douhuomall')->insert($insertData);
|
|
|
292
|
+ } else {
|
|
|
293
|
+ // 更新
|
|
|
294
|
+ // 检查是否需要恢复已删除商品的上架状态
|
|
|
295
|
+ $shouldRestore = $this->shouldRestoreProduct($findData, $data);
|
|
|
296
|
+ if ($shouldRestore) {
|
|
|
297
|
+ $this->restoreProduct($data['spu_id']);
|
|
|
298
|
+ }
|
|
|
299
|
+ $updateData = [
|
|
|
300
|
+ 'status' => $data['status'],
|
|
|
301
|
+ 'update_time' => $now,
|
|
|
302
|
+ 'cost_price' => $data['cost_price'],
|
|
|
303
|
+ 'market_price' => $data['market_price'],
|
|
|
304
|
+ 'ot_price' => $data['ot_price'],
|
|
|
305
|
+ 'mer_profit' => $data['mer_profit'],
|
|
|
306
|
+ 'pension' => $data['pension'],
|
|
|
307
|
+ 'profit' => $data['profit'],
|
|
|
308
|
+ 'skuId_info' => json_encode($data['skuId_info'], JSON_UNESCAPED_UNICODE),
|
|
|
309
|
+ 'spuId_info' => json_encode($data['spuId_info'], JSON_UNESCAPED_UNICODE),
|
|
|
310
|
+ 'title' => $data['title'],
|
|
|
311
|
+ 'cate_ids' => $data['cate_ids'],
|
|
|
312
|
+ 'cxb_cate_id' => $data['cxb_cate_id']
|
|
|
313
|
+ ];
|
|
|
314
|
+ Db::name('douhuomall')
|
|
|
315
|
+ ->where('spu_id', $findData['spu_id'])
|
|
|
316
|
+ ->update($updateData);
|
|
|
317
|
+ }
|
|
|
318
|
+
|
|
|
319
|
+ return $data['spu_id'];
|
|
|
320
|
+
|
|
|
321
|
+ } catch (\Exception $e) {
|
|
|
322
|
+ Log::error("保存商品到douhuomall失败: " . $e->getMessage());
|
|
|
323
|
+ throw $e;
|
|
|
324
|
+ }
|
|
|
325
|
+ }
|
|
|
326
|
+
|
|
|
327
|
+ /**
|
|
|
328
|
+ * 检查是否需要恢复已删除商品的上架状态
|
|
|
329
|
+ */
|
|
|
330
|
+ protected function shouldRestoreProduct($findData, $newData)
|
|
|
331
|
+ {
|
|
|
332
|
+ // 原有逻辑:如果货物属于已被删除状态,并且新更新进来的状态非删除状态
|
|
|
333
|
+ // 并且成本价、零售价、市场价、利润率没有变动的话
|
|
|
334
|
+ if ($findData['status'] == DouhuomallEnum::STATUS['Delete']['code']
|
|
|
335
|
+ && $newData['status'] !== DouhuomallEnum::STATUS['Delete']['code']
|
|
|
336
|
+ && $newData['cost_price'] == $findData['cost_price']
|
|
|
337
|
+ && $newData['ot_price'] == $findData['ot_price']
|
|
|
338
|
+ && $newData['market_price'] == $findData['market_price']
|
|
|
339
|
+ && $newData['profit'] == $findData['profit']) {
|
|
|
340
|
+
|
|
|
341
|
+ // 检查是否有已下架的商品
|
|
|
342
|
+ $count = Db::name('store_product')
|
|
|
343
|
+ ->where('spu_id', $newData['spu_id'])
|
|
|
344
|
+ ->where('is_show', ProductEnum::IS_SHOW['No']['code'])
|
|
|
345
|
+ ->where('is_status', ProductEnum::IS_STATUS['Automatic']['code'])
|
|
|
346
|
+ ->count();
|
|
|
347
|
+
|
|
|
348
|
+ return $count > 0;
|
|
|
349
|
+ }
|
|
|
350
|
+
|
|
|
351
|
+ return false;
|
|
|
352
|
+ }
|
|
|
353
|
+
|
|
|
354
|
+ /**
|
|
|
355
|
+ * 恢复商品上架状态
|
|
|
356
|
+ */
|
|
|
357
|
+ protected function restoreProduct($spuId)
|
|
|
358
|
+ {
|
|
|
359
|
+ Db::name('store_product')
|
|
|
360
|
+ ->where('spu_id', $spuId)
|
|
|
361
|
+ ->update([
|
|
|
362
|
+ 'is_show' => ProductEnum::IS_SHOW['No']['code'],
|
|
|
363
|
+ 'status' => ProductEnum::STATUS['Approved']['code']
|
|
|
364
|
+ ]);
|
|
|
365
|
+
|
|
|
366
|
+ Log::info("恢复商品上架(恢复到仓库中)状态: spu_id={$spuId}");
|
|
|
367
|
+ }
|
|
|
368
|
+
|
|
|
369
|
+ /**
|
|
|
370
|
+ * 检查并处理已上架商品
|
|
|
371
|
+ */
|
|
|
372
|
+ protected function checkAndProcessOnlineProduct($spuId, $syncData)
|
|
|
373
|
+ {
|
|
|
374
|
+ // 检查商品是否已上架
|
|
|
375
|
+ try {
|
|
|
376
|
+ $productList = Db::name('store_product')
|
|
|
377
|
+ ->where('is_show', ProductEnum::IS_SHOW['Yes']['code'])
|
|
|
378
|
+ ->where('spu_id', $spuId)
|
|
|
379
|
+ ->select()
|
|
|
380
|
+ ->toArray();
|
|
|
381
|
+
|
|
|
382
|
+ if (empty($productList)) {
|
|
|
383
|
+ return;
|
|
|
384
|
+ }
|
|
|
385
|
+
|
|
|
386
|
+ // 解析同步数据中的价格
|
|
|
387
|
+ $syncPrices = [];
|
|
|
388
|
+ foreach ($syncData['skuId_info'] as $sku) {
|
|
|
389
|
+ $syncPrices[] = [
|
|
|
390
|
+ 'sku_id' => $sku['sku_id'] ?? '',
|
|
|
391
|
+ 'market_price' => $sku['market_price'] ?? 0,
|
|
|
392
|
+ 'cost_price' => $sku['cost_price'] ?? 0,
|
|
|
393
|
+ 'retail_price' => $sku['retail_price'] ?? 0
|
|
|
394
|
+ ];
|
|
|
395
|
+ }
|
|
|
396
|
+
|
|
|
397
|
+ foreach ($productList as $product) {
|
|
|
398
|
+ // 获取本地商品价格信息
|
|
|
399
|
+ // 获取SKU价格
|
|
|
400
|
+ $skus = Db::name('store_product_attr_value')
|
|
|
401
|
+ ->where('product_id', $product['product_id'])
|
|
|
402
|
+ ->field('gong_sku_id, sku, price, cost')
|
|
|
403
|
+ ->select()
|
|
|
404
|
+ ->toArray();
|
|
|
405
|
+ $localPrices = [
|
|
|
406
|
+ 'product_price' => $product['price'] ?? 0,
|
|
|
407
|
+ 'product_cost' => $product['cost'] ?? 0,
|
|
|
408
|
+ 'product_ot_price' => $product['ot_price'] ?? 0,
|
|
|
409
|
+ 'skus' => $skus
|
|
|
410
|
+ ];
|
|
|
411
|
+
|
|
|
412
|
+ // 比较价格,判断是否需要下架
|
|
|
413
|
+ $priceChanged = $this->checkPriceChange($localPrices, $syncPrices);
|
|
|
414
|
+
|
|
|
415
|
+ if ($priceChanged) {
|
|
|
416
|
+ // 价格变动超过阈值,下架商品并记录
|
|
|
417
|
+ $this->offlineProductWithRecord($product, $syncData, $localPrices, $syncPrices);
|
|
|
418
|
+ } else {
|
|
|
419
|
+ // 价格未变动或变动很小,只更新图片
|
|
|
420
|
+ $this->updateProductImages($spuId);
|
|
|
421
|
+ }
|
|
|
422
|
+ }
|
|
|
423
|
+ } catch (DataNotFoundException|ModelNotFoundException|DbException $e) {
|
|
|
424
|
+
|
|
|
425
|
+ }
|
|
|
426
|
+ }
|
|
|
427
|
+
|
|
|
428
|
+ /**
|
|
|
429
|
+ * 检查价格是否变动超过阈值
|
|
|
430
|
+ */
|
|
|
431
|
+ protected function checkPriceChange($localPrices, $syncPrices)
|
|
|
432
|
+ {
|
|
|
433
|
+ // 如果没有SKU,比较商品主价格
|
|
|
434
|
+ if (empty($localPrices['skus'])) {
|
|
|
435
|
+ $localPrice = $localPrices['product_price'];
|
|
|
436
|
+ $syncPrice = $syncPrices[0]['market_price'] ?? 0;
|
|
|
437
|
+
|
|
|
438
|
+ if ($localPrice > 0 && $syncPrice > 0) {
|
|
|
439
|
+ $changeRate = abs(($syncPrice - $localPrice) / $localPrice);
|
|
|
440
|
+ return $changeRate > $this->config['price_change_rate'];
|
|
|
441
|
+ }
|
|
|
442
|
+ return false;
|
|
|
443
|
+ }
|
|
|
444
|
+
|
|
|
445
|
+ // 如果有SKU,匹配SKU进行比较
|
|
|
446
|
+ foreach ($localPrices['skus'] as $localSku) {
|
|
|
447
|
+ foreach ($syncPrices as $syncSku) {
|
|
|
448
|
+ // 尝试匹配SKU
|
|
|
449
|
+ if ($this->matchSku($localSku, $syncSku)) {
|
|
|
450
|
+ $localPrice = $localSku['price'] ?? 0;
|
|
|
451
|
+ $syncPrice = $syncSku['market_price'] ?? 0;
|
|
|
452
|
+
|
|
|
453
|
+ if ($localPrice > 0 && $syncPrice > 0) {
|
|
|
454
|
+ $changeRate = abs(($syncPrice - $localPrice) / $localPrice);
|
|
|
455
|
+ if ($changeRate > $this->config['price_change_rate']) {
|
|
|
456
|
+ return true;
|
|
|
457
|
+ }
|
|
|
458
|
+ }
|
|
|
459
|
+ }
|
|
|
460
|
+ }
|
|
|
461
|
+ }
|
|
|
462
|
+
|
|
|
463
|
+ return false;
|
|
|
464
|
+ }
|
|
|
465
|
+
|
|
|
466
|
+ /**
|
|
|
467
|
+ * 匹配SKU(根据业务逻辑实现)
|
|
|
468
|
+ */
|
|
|
469
|
+ protected function matchSku($localSku, $syncSku)
|
|
|
470
|
+ {
|
|
|
471
|
+ // 根据SKU ID匹配
|
|
|
472
|
+ if (!empty($localSku['gong_sku_id']) && !empty($syncSku['sku_id'])) {
|
|
|
473
|
+ return $localSku['gong_sku_id'] == $syncSku['sku_id'];
|
|
|
474
|
+ }
|
|
|
475
|
+
|
|
|
476
|
+ // 可以根据其他属性匹配,如规格等
|
|
|
477
|
+ return false;
|
|
|
478
|
+ }
|
|
|
479
|
+
|
|
|
480
|
+ /**
|
|
|
481
|
+ * 下架商品并记录变动
|
|
|
482
|
+ */
|
|
|
483
|
+ protected function offlineProductWithRecord($product, $syncData, $localPrices, $syncPrices)
|
|
|
484
|
+ {
|
|
|
485
|
+ $productId = $product['product_id'];
|
|
|
486
|
+ $spuId = $product['spu_id'];
|
|
|
487
|
+
|
|
|
488
|
+ // 1. 收集变动前数据
|
|
|
489
|
+ $beforeData = [
|
|
|
490
|
+ 'product_id' => $productId,
|
|
|
491
|
+ 'spu_id' => $spuId,
|
|
|
492
|
+ 'title' => $product['store_name'] ?? '',
|
|
|
493
|
+ 'status' => $product['is_show'] ?? 0,
|
|
|
494
|
+ 'price_info' => $localPrices,
|
|
|
495
|
+ 'images' => [
|
|
|
496
|
+ 'image' => $product['image'] ?? '',
|
|
|
497
|
+ 'slider_image' => $product['slider_image'] ?? ''
|
|
|
498
|
+ ],
|
|
|
499
|
+ 'record_time' => date('Y-m-d H:i:s')
|
|
|
500
|
+ ];
|
|
|
501
|
+
|
|
|
502
|
+ // 2. 下架商品
|
|
|
503
|
+ Db::name('store_product')
|
|
|
504
|
+ ->where('product_id', $productId)
|
|
|
505
|
+ ->update([
|
|
|
506
|
+ 'is_show' => ProductEnum::IS_SHOW['No']['code'],
|
|
|
507
|
+ 'status' => ProductEnum::STATUS['Withdrawn']['code'],
|
|
|
508
|
+ 'is_status' => ProductEnum::IS_STATUS['Automatic']['code']
|
|
|
509
|
+ ]);
|
|
|
510
|
+
|
|
|
511
|
+ // 3. 收集变动后数据
|
|
|
512
|
+ $afterData = [
|
|
|
513
|
+ 'spu_id' => $spuId,
|
|
|
514
|
+ 'sync_price_info' => $syncPrices,
|
|
|
515
|
+ 'sync_title' => $syncData['title'],
|
|
|
516
|
+ 'sync_status' => $syncData['status'],
|
|
|
517
|
+ 'sync_time' => date('Y-m-d H:i:s')
|
|
|
518
|
+ ];
|
|
|
519
|
+
|
|
|
520
|
+ // 4. 记录变动日志
|
|
|
521
|
+ $this->recordChangeLog(
|
|
|
522
|
+ $productId,
|
|
|
523
|
+ $spuId,
|
|
|
524
|
+ 'price_change',
|
|
|
525
|
+ '第三方平台价格变动超过阈值',
|
|
|
526
|
+ $beforeData,
|
|
|
527
|
+ $afterData
|
|
|
528
|
+ );
|
|
|
529
|
+
|
|
|
530
|
+ Log::info("商品下架: product_id={$productId}, spu_id={$spuId}, 原因: 价格变动");
|
|
|
531
|
+ }
|
|
|
532
|
+
|
|
|
533
|
+ /**
|
|
|
534
|
+ * 更新商品图片(基于原有updata_product_img方法)
|
|
|
535
|
+ */
|
|
|
536
|
+ protected function updateProductImages($spuId)
|
|
|
537
|
+ {
|
|
|
538
|
+ // 1. 验证该SPU是否已上架
|
|
|
539
|
+ $productId = Db::name('store_product')
|
|
|
540
|
+ ->where('spu_id', $spuId)
|
|
|
541
|
+ ->value('product_id');
|
|
|
542
|
+
|
|
|
543
|
+ if (!$productId) {
|
|
|
544
|
+ return true;
|
|
|
545
|
+ }
|
|
|
546
|
+
|
|
|
547
|
+ // 2. 查找微唯宝商品
|
|
|
548
|
+ $data = Db::name('douhuomall')
|
|
|
549
|
+ ->where('spu_id', $spuId)
|
|
|
550
|
+ ->find();
|
|
|
551
|
+
|
|
|
552
|
+ if (!$data) {
|
|
|
553
|
+ return false;
|
|
|
554
|
+ }
|
|
|
555
|
+
|
|
|
556
|
+ // 3. 解析商品信息
|
|
|
557
|
+ $spuIdInfo = json_decode($data['spuId_info'], true);
|
|
|
558
|
+
|
|
|
559
|
+ // 4. 处理图片数据
|
|
|
560
|
+ $sliderImage = $spuIdInfo['detail_img_list'] ?: '[]';
|
|
|
561
|
+ $image = $spuIdInfo['cover_url'] ?? '';
|
|
|
562
|
+
|
|
|
563
|
+ if (is_string($sliderImage)) {
|
|
|
564
|
+ $sliderImage = implode(',', json_decode($sliderImage, true));
|
|
|
565
|
+ } else if (is_array($sliderImage)) {
|
|
|
566
|
+ $sliderImage = implode(',', $sliderImage);
|
|
|
567
|
+ } else {
|
|
|
568
|
+ $sliderImage = $image;
|
|
|
569
|
+ }
|
|
|
570
|
+
|
|
|
571
|
+ // 5. 更新产品图片数据
|
|
|
572
|
+ $updateData = [
|
|
|
573
|
+ 'image' => $image,
|
|
|
574
|
+ 'slider_image' => $sliderImage,
|
|
|
575
|
+ ];
|
|
|
576
|
+
|
|
|
577
|
+ Db::name('store_product')
|
|
|
578
|
+ ->where('product_id', $productId)
|
|
|
579
|
+ ->update($updateData);
|
|
|
580
|
+
|
|
|
581
|
+ // 6. 更新商品详情
|
|
|
582
|
+ $detail = $spuIdInfo['detail'] ?? '';
|
|
|
583
|
+ if (!is_null(json_decode($detail))) {
|
|
|
584
|
+ $detailList = json_decode($detail);
|
|
|
585
|
+ $newDetail = '';
|
|
|
586
|
+ foreach ($detailList as $value) {
|
|
|
587
|
+ $newDetail .= "<img src='{$value}' referrerpolicy='no-referrer' /></img>";
|
|
|
588
|
+ }
|
|
|
589
|
+ $detail = $newDetail;
|
|
|
590
|
+ }
|
|
|
591
|
+
|
|
|
592
|
+ Db::name('store_product_content')
|
|
|
593
|
+ ->where('product_id', $productId)
|
|
|
594
|
+ ->update(['content' => $detail]);
|
|
|
595
|
+
|
|
|
596
|
+ return true;
|
|
|
597
|
+ }
|
|
|
598
|
+
|
|
|
599
|
+ /**
|
|
|
600
|
+ * 处理需要下架的商品(第三方已下架)
|
|
|
601
|
+ */
|
|
|
602
|
+ protected function handleOfflineProducts($localSpuIds, $syncedSpuIds)
|
|
|
603
|
+ {
|
|
|
604
|
+ $offlineSpuIds = array_diff($localSpuIds, $syncedSpuIds);
|
|
|
605
|
+
|
|
|
606
|
+ if (empty($offlineSpuIds)) {
|
|
|
607
|
+ return;
|
|
|
608
|
+ }
|
|
|
609
|
+
|
|
|
610
|
+ foreach ($offlineSpuIds as $spuId) {
|
|
|
611
|
+ $this->offlineProductBySpuId($spuId, 'third_party_offline');
|
|
|
612
|
+ }
|
|
|
613
|
+
|
|
|
614
|
+ Log::info("下架第三方已下架商品" . json_encode(['count' => count($offlineSpuIds), 'spu_ids' => $offlineSpuIds]));
|
|
|
615
|
+ }
|
|
|
616
|
+
|
|
|
617
|
+ /**
|
|
|
618
|
+ * 根据SPU ID下架商品
|
|
|
619
|
+ */
|
|
|
620
|
+ protected function offlineProductBySpuId($spuId, $reason)
|
|
|
621
|
+ {
|
|
|
622
|
+ // 查找已上架的商品
|
|
|
623
|
+ $products = Db::name('store_product')
|
|
|
624
|
+ ->where('spu_id', $spuId)
|
|
|
625
|
+ ->select();
|
|
|
626
|
+
|
|
|
627
|
+ foreach ($products as $product) {
|
|
|
628
|
+ // 记录下架前数据
|
|
|
629
|
+ $beforeData = $product;
|
|
|
630
|
+
|
|
|
631
|
+ // 下架商品
|
|
|
632
|
+ Db::name('store_product')
|
|
|
633
|
+ ->where('product_id', $product['product_id'])
|
|
|
634
|
+ ->update([
|
|
|
635
|
+ 'is_show' => ProductEnum::IS_SHOW['No']['code'],
|
|
|
636
|
+ 'status' => ProductEnum::STATUS['Withdrawn']['code'],
|
|
|
637
|
+ 'is_status' => ProductEnum::IS_STATUS['Automatic']['code']
|
|
|
638
|
+ ]);
|
|
|
639
|
+
|
|
|
640
|
+ // 记录日志
|
|
|
641
|
+ $this->recordChangeLog(
|
|
|
642
|
+ $product['product_id'],
|
|
|
643
|
+ $spuId,
|
|
|
644
|
+ 'third_party_offline',
|
|
|
645
|
+ $reason,
|
|
|
646
|
+ $beforeData,
|
|
|
647
|
+ []
|
|
|
648
|
+ );
|
|
|
649
|
+ }
|
|
|
650
|
+
|
|
|
651
|
+ // 从douhuomall表中删除
|
|
|
652
|
+ Db::name('douhuomall')->where('spu_id', $spuId)->delete();
|
|
|
653
|
+ }
|
|
|
654
|
+
|
|
|
655
|
+ /**
|
|
|
656
|
+ * 记录变动日志
|
|
|
657
|
+ */
|
|
|
658
|
+ protected function recordChangeLog($productId, $spuId, $changeType, $reason, $beforeData, $afterData)
|
|
|
659
|
+ {
|
|
|
660
|
+ $logData = [
|
|
|
661
|
+ 'product_id' => $productId,
|
|
|
662
|
+ 'spu_id' => $spuId,
|
|
|
663
|
+ 'change_type' => $changeType,
|
|
|
664
|
+ 'change_reason' => $reason,
|
|
|
665
|
+ 'before_data' => json_encode($beforeData, JSON_UNESCAPED_UNICODE),
|
|
|
666
|
+ 'after_data' => json_encode($afterData, JSON_UNESCAPED_UNICODE),
|
|
|
667
|
+ 'change_time' => time(),
|
|
|
668
|
+ 'create_time' => time()
|
|
|
669
|
+ ];
|
|
|
670
|
+
|
|
|
671
|
+ Db::name('product_change_log')->insert($logData);
|
|
|
672
|
+ }
|
|
|
673
|
+
|
|
|
674
|
+ /**
|
|
|
675
|
+ * 数组排序(从原有add方法中提取)
|
|
|
676
|
+ */
|
|
|
677
|
+ protected function arrSort($array, $keys, $sort = SORT_DESC)
|
|
|
678
|
+ {
|
|
|
679
|
+ $keysValue = [];
|
|
|
680
|
+ foreach ($array as $k => $v) {
|
|
|
681
|
+ $keysValue[$k] = $v[$keys] ?? 0;
|
|
|
682
|
+ }
|
|
|
683
|
+ array_multisort($keysValue, $sort, $array);
|
|
|
684
|
+ return $array;
|
|
|
685
|
+ }
|
|
|
686
|
+
|
|
|
687
|
+ /**
|
|
|
688
|
+ * 计算成本价
|
|
|
689
|
+ */
|
|
|
690
|
+ protected function calculateCostPrice($platPrice)
|
|
|
691
|
+ {
|
|
|
692
|
+ return bcadd((string)$platPrice, bcmul((string)$platPrice, '0.03', 4), 2);
|
|
|
693
|
+ }
|
|
|
694
|
+
|
|
|
695
|
+ /**
|
|
|
696
|
+ * 计算市场价
|
|
|
697
|
+ */
|
|
|
698
|
+ protected function calculateMarketPrice($platPrice)
|
|
|
699
|
+ {
|
|
|
700
|
+ $costPrice = $this->calculateCostPrice($platPrice);
|
|
|
701
|
+ return bcadd($costPrice, bcmul($costPrice, '0.2', 4), 2);
|
|
|
702
|
+ }
|
|
|
703
|
+
|
|
|
704
|
+ /**
|
|
|
705
|
+ * 计算利润率
|
|
|
706
|
+ */
|
|
|
707
|
+ protected function calculateProfit($platPrice)
|
|
|
708
|
+ {
|
|
|
709
|
+ $costPrice = $this->calculateCostPrice($platPrice);
|
|
|
710
|
+ $marketPrice = $this->calculateMarketPrice($platPrice);
|
|
|
711
|
+ return bcdiv($marketPrice, $costPrice, 2);
|
|
|
712
|
+ }
|
|
|
713
|
+
|
|
|
714
|
+ /**
|
|
|
715
|
+ * 计算毛利
|
|
|
716
|
+ */
|
|
|
717
|
+ protected function calculateMerProfit($platPrice)
|
|
|
718
|
+ {
|
|
|
719
|
+ $costPrice = $this->calculateCostPrice($platPrice);
|
|
|
720
|
+ $marketPrice = $this->calculateMarketPrice($platPrice);
|
|
|
721
|
+ return bcsub($marketPrice, $costPrice, 2);
|
|
|
722
|
+ }
|
|
|
723
|
+
|
|
|
724
|
+ /**
|
|
|
725
|
+ * 记录错误
|
|
|
726
|
+ */
|
|
|
727
|
+ protected function logError($message, \Exception $e)
|
|
|
728
|
+ {
|
|
|
729
|
+ $errorInfo = [
|
|
|
730
|
+ 'message' => $message,
|
|
|
731
|
+ 'file' => $e->getFile(),
|
|
|
732
|
+ 'line' => $e->getLine(),
|
|
|
733
|
+ 'error' => $e->getMessage()
|
|
|
734
|
+ ];
|
|
|
735
|
+
|
|
|
736
|
+ Log::error('商品同步错误' . json_encode($errorInfo, JSON_UNESCAPED_UNICODE));
|
|
|
737
|
+
|
|
|
738
|
+ Db::name('log')->insert([
|
|
|
739
|
+ 'data' => json_encode($errorInfo, JSON_UNESCAPED_UNICODE),
|
|
|
740
|
+ 'time' => date('Y-m-d H:i:s')
|
|
|
741
|
+ ]);
|
|
|
742
|
+ }
|
|
|
743
|
+}
|