sync customer and device

This commit is contained in:
xavier
2025-05-19 16:16:58 +08:00
parent 5c0ba08572
commit 1d0eb15b84
2 changed files with 63 additions and 12 deletions

View File

@@ -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();
}
}

View File

@@ -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;
}
}
}