From 1f29f6880c096dc16b8ebe394bbeca69b929f070 Mon Sep 17 00:00:00 2001 From: wong <106998207@qq.com> Date: Sat, 5 Jul 2025 16:09:24 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A1=A8=E5=8D=95=E5=AF=BC=E5=85=A5=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../PostUpdateAddFriendPlanV1Controller.php | 114 ++++++++++-------- 1 file changed, 66 insertions(+), 48 deletions(-) diff --git a/Server/application/cunkebao/controller/plan/PostUpdateAddFriendPlanV1Controller.php b/Server/application/cunkebao/controller/plan/PostUpdateAddFriendPlanV1Controller.php index 655180ed..832fd208 100644 --- a/Server/application/cunkebao/controller/plan/PostUpdateAddFriendPlanV1Controller.php +++ b/Server/application/cunkebao/controller/plan/PostUpdateAddFriendPlanV1Controller.php @@ -90,8 +90,7 @@ class PostUpdateAddFriendPlanV1Controller extends Controller 'updateTime'=> time(), ]; - // 开启事务 - Db::startTrans(); + try { // 更新数据 $result = Db::name('customer_acquisition_task') @@ -134,17 +133,16 @@ class PostUpdateAddFriendPlanV1Controller extends Controller if (count($data) > 1) { array_shift($data); // 去掉表头 } + foreach ($data as $cols) { - if (count($cols) >= 6) { - $rows[] = [ - 'name' => trim($cols[0]), - 'phone' => trim($cols[1]), - 'wechat' => trim($cols[2]), - 'source' => trim($cols[3]), - 'orderAmount' => trim($cols[4]), - 'orderDate' => trim($cols[5]), - ]; - } + $rows[] = [ + 'name' => isset($cols[0]) ? trim($cols[0]) : '', + 'phone' => isset($cols[1]) ? trim($cols[1]) : '', + 'wechat' => isset($cols[2]) ? trim($cols[2]) : '', + 'source' => isset($cols[3]) ? trim($cols[3]) : '', + 'orderAmount' => isset($cols[4]) ? trim($cols[4]) : '', + 'orderDate' => isset($cols[5]) ? trim($cols[5]) : '', + ]; } } elseif ($ext === 'csv') { $content = file_get_contents($tmpFile); @@ -156,12 +154,12 @@ class PostUpdateAddFriendPlanV1Controller extends Controller $cols = str_getcsv($line); if (count($cols) >= 6) { $rows[] = [ - 'name' => trim($cols[0]), - 'phone' => trim($cols[1]), - 'wechat' => trim($cols[2]), - 'source' => trim($cols[3]), - 'orderAmount' => trim($cols[4]), - 'orderDate' => trim($cols[5]), + 'name' => isset($cols[0]) ? trim($cols[0]) : '', + 'phone' => isset($cols[1]) ? trim($cols[1]) : '', + 'wechat' => isset($cols[2]) ? trim($cols[2]) : '', + 'source' => isset($cols[3]) ? trim($cols[3]) : '', + 'orderAmount' => isset($cols[4]) ? trim($cols[4]) : '', + 'orderDate' => isset($cols[5]) ? trim($cols[5]) : '', ]; } } @@ -172,43 +170,63 @@ class PostUpdateAddFriendPlanV1Controller extends Controller } // 删除临时文件 unlink($tmpFile); - - foreach($rows as $row){ - $phone = !empty($row['phone']) ? $row['phone'] : $row['wechat']; - if(empty($phone)){ - continue; - } - $ck_task_customer = Db::name('task_customer') - ->where(['phone' => $phone,'task_id' => $params['planId']]) - ->find(); - if(!$ck_task_customer){ - $task_customer = Db::name('task_customer') - ->insert([ - 'task_id' => $params['planId'], - 'name' => $row['name'] ?? '', - 'source' => $row['source'] ?? '', - 'phone' => $phone, - 'tags' => json_encode([],JSON_UNESCAPED_UNICODE), - 'siteTags' => json_encode([],JSON_UNESCAPED_UNICODE), - 'created_at' => time(), - ]); + + // 1000条为一组进行批量处理 + $batchSize = 1000; + $totalRows = count($rows); + + for ($i = 0; $i < $totalRows; $i += $batchSize) { + $batchRows = array_slice($rows, $i, $batchSize); + + if (!empty($batchRows)) { + // 1. 提取当前批次的phone + $phones = []; + foreach ($batchRows as $row) { + $phone = !empty($row['phone']) ? $row['phone'] : $row['wechat']; + if (!empty($phone)) { + $phones[] = $phone; + } + } + + // 2. 批量查询已存在的phone + $existingPhones = []; + if (!empty($phones)) { + $existing = Db::name('task_customer') + ->where('task_id', $params['planId']) + ->where('phone', 'in', $phones) + ->field('phone') + ->select(); + $existingPhones = array_column($existing, 'phone'); + } + + // 3. 过滤出新数据,批量插入 + $newData = []; + foreach ($batchRows as $row) { + $phone = !empty($row['phone']) ? $row['phone'] : $row['wechat']; + if (!empty($phone) && !in_array($phone, $existingPhones)) { + $newData[] = [ + 'task_id' => $params['planId'], + 'name' => $row['name'] ?? '', + 'source' => $row['source'] ?? '', + 'phone' => $phone, + 'tags' => json_encode([], JSON_UNESCAPED_UNICODE), + 'siteTags' => json_encode([], JSON_UNESCAPED_UNICODE), + 'created_at' => time(), + ]; + } + } + + // 4. 批量插入新数据 + if (!empty($newData)) { + Db::name('task_customer')->insertAll($newData); + } } } } } - - - - - - - - - // 提交事务 - Db::commit(); return ResponseHelper::success(['planId' => $params['planId']], '更新计划任务成功');