From 1d0eb15b843baa5790482fcfb4f56d4a7e135397 Mon Sep 17 00:00:00 2001 From: xavier Date: Mon, 19 May 2025 16:16:58 +0800 Subject: [PATCH] sync customer and device --- .../command/SyncWechatDataToCkbTask.php | 14 +++++ .../Adapters/ChuKeBao/Adapter.php | 61 +++++++++++++++---- 2 files changed, 63 insertions(+), 12 deletions(-) diff --git a/Server/application/command/SyncWechatDataToCkbTask.php b/Server/application/command/SyncWechatDataToCkbTask.php index dea790fd..d7842981 100644 --- a/Server/application/command/SyncWechatDataToCkbTask.php +++ b/Server/application/command/SyncWechatDataToCkbTask.php @@ -49,6 +49,8 @@ class SyncWechatDataToCkbTask extends Command $this->syncWechatAccount($ChuKeBaoAdapter); $this->syncWechatFriend($ChuKeBaoAdapter); $this->syncWechatDeviceLoginLog($ChuKeBaoAdapter); + $this->syncWechatDevice($ChuKeBaoAdapter); + $this->syncWechatCustomer($ChuKeBaoAdapter); $output->writeln("同步任务 sync_wechat_to_ckb 已结束"); return true; @@ -76,4 +78,16 @@ class SyncWechatDataToCkbTask extends Command { return $ChuKeBaoAdapter->syncWechatDeviceLoginLog(); } + + // syncDevice + protected function syncWechatDevice(ChuKeBaoAdapter $ChuKeBaoAdapter) + { + return $ChuKeBaoAdapter->syncDevice(); + } + + // syncWechatCustomer + protected function syncWechatCustomer(ChuKeBaoAdapter $ChuKeBaoAdapter) + { + return $ChuKeBaoAdapter->syncWechatCustomer(); + } } \ No newline at end of file diff --git a/Server/extend/WeChatDeviceApi/Adapters/ChuKeBao/Adapter.php b/Server/extend/WeChatDeviceApi/Adapters/ChuKeBao/Adapter.php index cae1dae8..66e0cd58 100644 --- a/Server/extend/WeChatDeviceApi/Adapters/ChuKeBao/Adapter.php +++ b/Server/extend/WeChatDeviceApi/Adapters/ChuKeBao/Adapter.php @@ -461,18 +461,18 @@ class Adapter implements WeChatServiceInterface $friendShip['groupNumber'] = $groupCount; // 8. 构建weight JSON数据 (每天只计算一次) - $weight = []; - if ($existingRecord && !empty($existingRecord['weight'])) { - $weight = json_decode($existingRecord['weight'], true) ?: []; + // $weight = []; + // if ($existingRecord && !empty($existingRecord['weight'])) { + // $weight = json_decode($existingRecord['weight'], true) ?: []; - // 如果不是今天更新的,重新计算权重 - $lastUpdateDate = date('Y-m-d', $existingRecord['updateTime'] ?? 0); - if ($lastUpdateDate !== date('Y-m-d')) { - $weight = $this->calculateCustomerWeight($basic, $activity, $friendShip); - } - } else { - $weight = $this->calculateCustomerWeight($basic, $activity, $friendShip); - } + // // 如果不是今天更新的,重新计算权重 + // $lastUpdateDate = date('Y-m-d', $existingRecord['updateTime'] ?? 0); + // if ($lastUpdateDate !== date('Y-m-d')) { + // $weight = $this->calculateCustomerWeight($basic, $activity, $friendShip); + // } + // } else { + // $weight = $this->calculateCustomerWeight($basic, $activity, $friendShip); + // } // 9. 准备更新或插入的数据 $data = [ @@ -481,7 +481,7 @@ class Adapter implements WeChatServiceInterface 'basic' => json_encode($basic), 'activity' => json_encode($activity), 'friendShip' => json_encode($friendShip), - 'weight' => json_encode($weight), + // 'weight' => json_encode($weight), 'updateTime' => time() ]; @@ -573,4 +573,41 @@ class Adapter implements WeChatServiceInterface 'addLimit' => $addLimit ]; } + + /** + * 同步设备信息到ck_device表 + * 数据量不大,仅同步一次所有设备 + * + * @return int 影响的行数 + */ + public function syncDevice() + { + try { + $sql = "INSERT INTO ck_device(`id`, `imei`, `model`, phone, operatingSystem, memo, alive, brand, rooted, xPosed, softwareVersion, extra, createTime, updateTime, deleteTime, companyId) + SELECT + d.id, d.imei, d.model, d.phone, d.operatingSystem, d.memo, d.alive, d.brand, d.rooted, d.xPosed, d.softwareVersion, d.extra, d.createTime, d.lastUpdateTime, d.deleteTime, a.departmentId companyId + FROM s2_device d + JOIN s2_company_account a ON d.currentAccountId = a.id + ON DUPLICATE KEY UPDATE + `model` = VALUES(`model`), + `phone` = VALUES(`phone`), + `operatingSystem` = VALUES(`operatingSystem`), + `memo` = VALUES(`memo`), + `alive` = VALUES(`alive`), + `brand` = VALUES(`brand`), + `rooted` = VALUES(`rooted`), + `xPosed` = VALUES(`xPosed`), + `softwareVersion` = VALUES(`softwareVersion`), + `extra` = VALUES(`extra`), + `updateTime` = VALUES(`updateTime`), + `deleteTime` = VALUES(`deleteTime`), + `companyId` = VALUES(`companyId`)"; + + $affected = Db::execute($sql); + return $affected; + } catch (\Exception $e) { + Log::error("同步设备信息异常: " . $e->getMessage() . ", 堆栈: " . $e->getTraceAsString()); + return false; + } + } }