订单添加优化
This commit is contained in:
@@ -119,8 +119,7 @@ class PostCreateAddFriendPlanV1Controller extends Controller
|
||||
|
||||
|
||||
|
||||
// 开启事务
|
||||
Db::startTrans();
|
||||
|
||||
try {
|
||||
// 插入数据
|
||||
$planId = Db::name('customer_acquisition_task')->insertGetId($data);
|
||||
@@ -162,17 +161,16 @@ class PostCreateAddFriendPlanV1Controller 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);
|
||||
@@ -184,12 +182,12 @@ class PostCreateAddFriendPlanV1Controller 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]) : '',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -201,36 +199,61 @@ class PostCreateAddFriendPlanV1Controller 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' => $planId])
|
||||
->find();
|
||||
if(!$ck_task_customer){
|
||||
$task_customer = Db::name('task_customer')
|
||||
->insert([
|
||||
'task_id' => $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' => $planId], '添加计划任务成功');
|
||||
|
||||
|
||||
Reference in New Issue
Block a user