From db221008465610557119439084d301cd4d08964f Mon Sep 17 00:00:00 2001 From: wong <106998207@qq.com> Date: Wed, 15 Oct 2025 16:39:10 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E5=90=8C=E6=AD=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/controller/AccountController.php | 27 +++- .../controller/WechatChatroomController.php | 73 ++++++++-- .../api/controller/WechatFriendController.php | 135 ++++++++++-------- .../api/model/WechatFriendModel.php | 5 + Server/application/job/WechatFriendJob.php | 2 +- 5 files changed, 166 insertions(+), 76 deletions(-) diff --git a/Server/application/api/controller/AccountController.php b/Server/application/api/controller/AccountController.php index 02281e17..d2819b65 100644 --- a/Server/application/api/controller/AccountController.php +++ b/Server/application/api/controller/AccountController.php @@ -708,7 +708,7 @@ class AccountController extends BaseController $deleteTime = isset($item['deleteTime']) ? strtotime($item['deleteTime']) : null; $sqlData = []; foreach ($data as $item) { - $sqlData[] = [ + $rows[] = [ 'id' => $item['id'], 'accountType' => isset($item['accountType']) ? $item['accountType'] : 0, 'status' => isset($item['status']) ? $item['status'] : 0, @@ -733,9 +733,28 @@ class AccountController extends BaseController ]; } + // 拆分插入/更新:按主键 id 判断 + $ids = array_column($rows, 'id'); + $existingIds = CompanyAccountModel::whereIn('id', $ids)->column('id'); + $existSet = array_flip($existingIds); - // 使用tenantId作为唯一性判断 - $account = new CompanyAccountModel(); - $account->saveAll($account); + $toInsert = []; + $toUpdate = []; + foreach ($rows as $r) { + if (isset($existSet[$r['id']])) $toUpdate[] = $r; else $toInsert[] = $r; + } + + $changed = false; + if (!empty($toInsert)) { + $m = new CompanyAccountModel(); + $m->insertAll($toInsert, true); // 允许外部主键 + $changed = true; + } + if (!empty($toUpdate)) { + $m = new CompanyAccountModel(); + $res = $m->saveAll($toUpdate); + if ($res) $changed = true; + } + return $changed; } } \ No newline at end of file diff --git a/Server/application/api/controller/WechatChatroomController.php b/Server/application/api/controller/WechatChatroomController.php index a038dee6..82d28691 100644 --- a/Server/application/api/controller/WechatChatroomController.php +++ b/Server/application/api/controller/WechatChatroomController.php @@ -80,7 +80,6 @@ class WechatChatroomController extends BaseController */ private function saveChatroom($data) { - $sqlData = []; foreach ($data as $item) { $sqlData[] = [ @@ -112,19 +111,38 @@ class WechatChatroomController extends BaseController ]; } - // 使用chatroomId和wechatAccountId的组合作为唯一性判断 - $chatroom = new WechatChatroomModel(); - $chatroom->saveAll($sqlData); - if ($chatroom) { - if ($chatroom->isUpdate()){ - return true; - }else{ - return false; - } - - }else{ + if (empty($sqlData)) { return false; } + + // 先查已存在ID,拆分插入与更新两批(参考 WechatFriendController) + $ids = array_column($sqlData, 'id'); + $existingIds = WechatChatroomModel::whereIn('id', $ids)->column('id'); + $existingSet = array_flip($existingIds); + + $toInsert = []; + $toUpdate = []; + foreach ($sqlData as $row) { + if (isset($existingSet[$row['id']])) { + $toUpdate[] = $row; + } else { + $toInsert[] = $row; + } + } + + $isUpdate = false; + if (!empty($toInsert)) { + $m = new WechatChatroomModel(); + // 批量插入(允许外部主键) + $m->insertAll($toInsert, true); + $isUpdate = false; + } + if (!empty($toUpdate)) { + $m = new WechatChatroomModel(); + $m->saveAll($toUpdate); + $isUpdate = true; + } + return $isUpdate; } /** @@ -210,8 +228,35 @@ class WechatChatroomController extends BaseController ]; } - $member = new WechatChatroomMemberModel(); - $member->saveAll($sqlData); + if (empty($sqlData)) { + return false; + } + + // 使用 (chatroomId, wechatId) 组合作为唯一性判断,拆分插入与更新 + $existing = WechatChatroomMemberModel::where('chatroomId', $wechatChatroomId) + ->column('id,wechatId', 'wechatId'); + + $toInsert = []; + $toUpdate = []; + foreach ($sqlData as $row) { + $wid = $row['wechatId']; + if ($wid !== '' && isset($existing[$wid])) { + // 带上主键以便 saveAll 根据主键更新 + $row['id'] = $existing[$wid]['id'] ?? $existing[$wid]; + $toUpdate[] = $row; + } else { + $toInsert[] = $row; + } + } + + if (!empty($toInsert)) { + $m = new WechatChatroomMemberModel(); + $m->insertAll($toInsert, true); + } + if (!empty($toUpdate)) { + $m = new WechatChatroomMemberModel(); + $m->saveAll($toUpdate); + } } /** diff --git a/Server/application/api/controller/WechatFriendController.php b/Server/application/api/controller/WechatFriendController.php index 18fbe438..6bc2874b 100644 --- a/Server/application/api/controller/WechatFriendController.php +++ b/Server/application/api/controller/WechatFriendController.php @@ -3,6 +3,7 @@ namespace app\api\controller; use app\api\model\WechatFriendModel; +use think\Db; use think\facade\Request; use think\facade\Log; @@ -75,15 +76,12 @@ class WechatFriendController extends BaseController // 发送请求获取好友列表 $result = requestCurl($this->baseUrl . 'api/WechatFriend/friendlistData', $params, 'POST', $header, 'json'); $response = handleApiResponse($result); - // 保存数据到数据库 if (is_array($response)) { $isUpdate = false; - foreach ($response as $item) { - $updated = $this->saveFriend($item); - if($updated && $isDel == 0){ - $isUpdate = true; - } + $updated = $this->saveFriend($response); + if($updated && $isDel == 0){ + $isUpdate = true; } } @@ -107,59 +105,82 @@ class WechatFriendController extends BaseController * @param array $item 微信好友数据 * @return bool 是否创建或更新了记录 */ - private function saveFriend($item) + private function saveFriend($data) { - $data = [ - 'id' => $item['id'], - 'wechatAccountId' => $item['wechatAccountId'], - 'alias' => $item['alias'], - 'wechatId' => $item['wechatId'], - 'conRemark' => $item['conRemark'], - 'nickname' => $item['nickname'], - 'pyInitial' => $item['pyInitial'], - 'quanPin' => $item['quanPin'], - 'avatar' => $item['avatar'], - 'gender' => $item['gender'], - 'region' => $item['region'], - 'addFrom' => $item['addFrom'], - 'labels' => is_array($item['labels']) ? json_encode($item['labels']) : json_encode([]), - 'siteLabels' => json_encode([]), - 'signature' => $item['signature'], - 'isDeleted' => $item['isDeleted'], - 'isPassed' => $item['isPassed'], - 'deleteTime' => !empty($item['isDeleted']) ? strtotime($item['deleteTime']) : 0, - 'accountId' => $item['accountId'], - 'extendFields' => is_array($item['extendFields']) ? json_encode($item['extendFields']) : json_encode([]), - 'accountUserName' => $item['accountUserName'], - 'accountRealName' => $item['accountRealName'], - 'accountNickname' => $item['accountNickname'], - 'ownerAlias' => $item['ownerAlias'], - 'ownerWechatId' => $item['ownerWechatId'], - 'ownerNickname' => $item['ownerNickname'], - 'ownerAvatar' => $item['ownerAvatar'], - 'phone' => $item['phone'], - 'thirdParty' => is_array($item['thirdParty']) ? json_encode($item['thirdParty']) : json_encode([]), - 'groupId' => $item['groupId'], - 'passTime' => !empty($item['isPassed']) && $item['passTime'] != '0001-01-01T00:00:00' ? strtotime($item['passTime']) : 0, - 'additionalPicture' => $item['additionalPicture'], - 'desc' => $item['desc'], - 'country' => $item['country'], - 'privince' => isset($item['privince']) ? $item['privince'] : '', - 'city' => isset($item['city']) ? $item['city'] : '', - 'createTime' => isset($item['createTime']) ? strtotime($item['createTime']) : 0, - 'updateTime' => time() - ]; - - // 使用ID作为唯一性判断 - $friend = WechatFriendModel::where('id', $item['id'])->find(); - - if ($friend) { - unset($data['siteLabels']); - $friend->save($data); - return true; - } else { - WechatFriendModel::create($data); + $sqlData = []; + foreach ($data as $item) { + $sqlData[] = [ + 'id' => $item['id'], + 'wechatAccountId' => $item['wechatAccountId'], + 'alias' => $item['alias'], + 'wechatId' => $item['wechatId'], + 'conRemark' => $item['conRemark'], + 'nickname' => $item['nickname'], + 'pyInitial' => $item['pyInitial'], + 'quanPin' => $item['quanPin'], + 'avatar' => $item['avatar'], + 'gender' => $item['gender'], + 'region' => $item['region'], + 'addFrom' => $item['addFrom'], + 'labels' => is_array($item['labels']) ? json_encode($item['labels']) : json_encode([]), + 'siteLabels' => json_encode([]), + 'signature' => $item['signature'], + 'isDeleted' => $item['isDeleted'], + 'isPassed' => $item['isPassed'], + 'deleteTime' => !empty($item['isDeleted']) ? strtotime($item['deleteTime']) : 0, + 'accountId' => $item['accountId'], + 'extendFields' => is_array($item['extendFields']) ? json_encode($item['extendFields']) : json_encode([]), + 'accountUserName' => $item['accountUserName'], + 'accountRealName' => $item['accountRealName'], + 'accountNickname' => $item['accountNickname'], + 'ownerAlias' => $item['ownerAlias'], + 'ownerWechatId' => $item['ownerWechatId'], + 'ownerNickname' => $item['ownerNickname'], + 'ownerAvatar' => $item['ownerAvatar'], + 'phone' => $item['phone'], + 'thirdParty' => is_array($item['thirdParty']) ? json_encode($item['thirdParty']) : json_encode([]), + 'groupId' => $item['groupId'], + 'passTime' => !empty($item['isPassed']) && $item['passTime'] != '0001-01-01T00:00:00' ? strtotime($item['passTime']) : 0, + 'additionalPicture' => $item['additionalPicture'], + 'desc' => $item['desc'], + 'country' => $item['country'], + 'privince' => isset($item['privince']) ? $item['privince'] : '', + 'city' => isset($item['city']) ? $item['city'] : '', + 'createTime' => isset($item['createTime']) ? strtotime($item['createTime']) : 0, + 'updateTime' => time() + ]; + } + if (empty($sqlData)) { return false; } + + // 先查已存在ID,拆分插入与更新两批 + $ids = array_column($sqlData, 'id'); + $existingIds = WechatFriendModel::whereIn('id', $ids)->column('id'); + $existingSet = array_flip($existingIds); + + $toInsert = []; + $toUpdate = []; + foreach ($sqlData as $row) { + if (isset($existingSet[$row['id']])) { + $toUpdate[] = $row; + } else { + $toInsert[] = $row; + } + } + + $isUpdate = false; + if (!empty($toInsert)) { + $m = new WechatFriendModel(); + $m->insertAll($toInsert,true); + $isUpdate = false; + } + if (!empty($toUpdate)) { + // 批量更新使用 saveAll(按主键更新) + $m = new WechatFriendModel(); + $m->saveAll($toUpdate); + $isUpdate = true; + } + return $isUpdate; } } \ No newline at end of file diff --git a/Server/application/api/model/WechatFriendModel.php b/Server/application/api/model/WechatFriendModel.php index 43c9c6cc..e99dd476 100644 --- a/Server/application/api/model/WechatFriendModel.php +++ b/Server/application/api/model/WechatFriendModel.php @@ -8,4 +8,9 @@ class WechatFriendModel extends Model { // 设置表名 protected $table = 's2_wechat_friend'; + protected $pk = 'id'; + + /*protected $pk = [ + 'uk_owner_wechat_account' => ['ownerWechatId', 'wechatId','wechatAccountId'] // uk_owner_wechat_account 是数据库中组合唯一键的名称 + ];*/ } \ No newline at end of file diff --git a/Server/application/job/WechatFriendJob.php b/Server/application/job/WechatFriendJob.php index 88bbd1da..e6af5fdc 100644 --- a/Server/application/job/WechatFriendJob.php +++ b/Server/application/job/WechatFriendJob.php @@ -79,7 +79,7 @@ class WechatFriendJob 'preFriendId' => $preFriendId, ], true, $isDel); $response = json_decode($result, true); - + // 判断是否成功 if ($response['code'] == 200) { $data = $response['data'];