代码同步

This commit is contained in:
wong
2025-10-15 16:39:10 +08:00
parent d26e5a324d
commit db22100846
5 changed files with 166 additions and 76 deletions

View File

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