diff --git a/Server/application/command/SyncWechatDataToCkbTask.php b/Server/application/command/SyncWechatDataToCkbTask.php index 9980a630..4e1bc482 100644 --- a/Server/application/command/SyncWechatDataToCkbTask.php +++ b/Server/application/command/SyncWechatDataToCkbTask.php @@ -52,6 +52,8 @@ class SyncWechatDataToCkbTask extends Command $this->syncWechatDevice($ChuKeBaoAdapter); $this->syncWechatCustomer($ChuKeBaoAdapter); $this->syncWechatFriendToTrafficPoolBatch($ChuKeBaoAdapter); + $this->syncTrafficSourceUser($ChuKeBaoAdapter); + $this->syncTrafficSourceGroup($ChuKeBaoAdapter); $output->writeln("同步任务 sync_wechat_to_ckb 已结束"); return true; @@ -96,4 +98,15 @@ class SyncWechatDataToCkbTask extends Command { return $ChuKeBaoAdapter->syncWechatFriendToTrafficPoolBatch(); } + protected function syncTrafficSourceUser(ChuKeBaoAdapter $ChuKeBaoAdapter) + { + return $ChuKeBaoAdapter->syncTrafficSourceUser(); + } + + protected function syncTrafficSourceGroup(ChuKeBaoAdapter $ChuKeBaoAdapter) + { + return $ChuKeBaoAdapter->syncTrafficSourceGroup(); + } + + } \ 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 ba111c73..8da320aa 100644 --- a/Server/extend/WeChatDeviceApi/Adapters/ChuKeBao/Adapter.php +++ b/Server/extend/WeChatDeviceApi/Adapters/ChuKeBao/Adapter.php @@ -1225,4 +1225,70 @@ class Adapter implements WeChatServiceInterface return false; } } + + public function syncTrafficSourceUser() + { + $sql = "insert into ck_traffic_source(`identifier`,companyId,`fromd`,`sourceId`) + SELECT + f.wechatId identifier, + c.departmentId companyId, + f.ownerNickname fromd, + f.ownerWechatId sourceId + FROM + s2_wechat_friend f + LEFT JOIN s2_wechat_account a ON f.wechatAccountId = a.id + LEFT JOIN s2_company_account c on c.id = a.deviceAccountId + ORDER BY f.id DESC + LIMIT ?, ? + ON DUPLICATE KEY UPDATE + identifier=VALUES(identifier), + companyId=VALUES(companyId), + sourceId=VALUES(sourceId)"; + + + $offset = 0; + $limit = 2000; + $usleepTime = 50000; + do { + $affected = Db::execute($sql, [$offset, $limit]); + $offset += $limit; + if ($affected > 0) { + usleep($usleepTime); + } + } while ($affected > 0); + } + + public function syncTrafficSourceGroup() + { + $sql = "insert into ck_traffic_source(`identifier`,companyId,`fromd`,`sourceId`) + SELECT + m.wechatId identifier, + c.departmentId companyId, + r.nickname fromd, + f.ownerWechatId sourceId + FROM + s2_wechat_chatroom_member m + JOIN s2_wechat_chatroom r ON m.chatroomId = r.chatroomId + LEFT JOIN s2_wechat_account a ON a.id = r.wechatAccountId + LEFT JOIN s2_company_account c on c.id = a.deviceAccountId + ORDER BY m.id DESC + GROUP BY m.wechatId + LIMIT ?, ? + ON DUPLICATE KEY UPDATE + identifier=VALUES(identifier), + companyId=VALUES(companyId), + sourceId=VALUES(sourceId)"; + + + $offset = 0; + $limit = 2000; + $usleepTime = 50000; + do { + $affected = Db::execute($sql, [$offset, $limit]); + $offset += $limit; + if ($affected > 0) { + usleep($usleepTime); + } + } while ($affected > 0); + } }