订单添加优化
This commit is contained in:
@@ -119,8 +119,7 @@ class PostCreateAddFriendPlanV1Controller extends Controller
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
// 开启事务
|
|
||||||
Db::startTrans();
|
|
||||||
try {
|
try {
|
||||||
// 插入数据
|
// 插入数据
|
||||||
$planId = Db::name('customer_acquisition_task')->insertGetId($data);
|
$planId = Db::name('customer_acquisition_task')->insertGetId($data);
|
||||||
@@ -162,17 +161,16 @@ class PostCreateAddFriendPlanV1Controller extends Controller
|
|||||||
if (count($data) > 1) {
|
if (count($data) > 1) {
|
||||||
array_shift($data); // 去掉表头
|
array_shift($data); // 去掉表头
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($data as $cols) {
|
foreach ($data as $cols) {
|
||||||
if (count($cols) >= 6) {
|
$rows[] = [
|
||||||
$rows[] = [
|
'name' => isset($cols[0]) ? trim($cols[0]) : '',
|
||||||
'name' => trim($cols[0]),
|
'phone' => isset($cols[1]) ? trim($cols[1]) : '',
|
||||||
'phone' => trim($cols[1]),
|
'wechat' => isset($cols[2]) ? trim($cols[2]) : '',
|
||||||
'wechat' => trim($cols[2]),
|
'source' => isset($cols[3]) ? trim($cols[3]) : '',
|
||||||
'source' => trim($cols[3]),
|
'orderAmount' => isset($cols[4]) ? trim($cols[4]) : '',
|
||||||
'orderAmount' => trim($cols[4]),
|
'orderDate' => isset($cols[5]) ? trim($cols[5]) : '',
|
||||||
'orderDate' => trim($cols[5]),
|
];
|
||||||
];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} elseif ($ext === 'csv') {
|
} elseif ($ext === 'csv') {
|
||||||
$content = file_get_contents($tmpFile);
|
$content = file_get_contents($tmpFile);
|
||||||
@@ -184,12 +182,12 @@ class PostCreateAddFriendPlanV1Controller extends Controller
|
|||||||
$cols = str_getcsv($line);
|
$cols = str_getcsv($line);
|
||||||
if (count($cols) >= 6) {
|
if (count($cols) >= 6) {
|
||||||
$rows[] = [
|
$rows[] = [
|
||||||
'name' => trim($cols[0]),
|
'name' => isset($cols[0]) ? trim($cols[0]) : '',
|
||||||
'phone' => trim($cols[1]),
|
'phone' => isset($cols[1]) ? trim($cols[1]) : '',
|
||||||
'wechat' => trim($cols[2]),
|
'wechat' => isset($cols[2]) ? trim($cols[2]) : '',
|
||||||
'source' => trim($cols[3]),
|
'source' => isset($cols[3]) ? trim($cols[3]) : '',
|
||||||
'orderAmount' => trim($cols[4]),
|
'orderAmount' => isset($cols[4]) ? trim($cols[4]) : '',
|
||||||
'orderDate' => trim($cols[5]),
|
'orderDate' => isset($cols[5]) ? trim($cols[5]) : '',
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -201,36 +199,61 @@ class PostCreateAddFriendPlanV1Controller extends Controller
|
|||||||
// 删除临时文件
|
// 删除临时文件
|
||||||
unlink($tmpFile);
|
unlink($tmpFile);
|
||||||
|
|
||||||
foreach($rows as $row){
|
// 1000条为一组进行批量处理
|
||||||
$phone = !empty($row['phone']) ? $row['phone'] : $row['wechat'];
|
$batchSize = 1000;
|
||||||
if(empty($phone)){
|
$totalRows = count($rows);
|
||||||
continue;
|
|
||||||
}
|
for ($i = 0; $i < $totalRows; $i += $batchSize) {
|
||||||
$ck_task_customer = Db::name('task_customer')
|
$batchRows = array_slice($rows, $i, $batchSize);
|
||||||
->where(['phone' => $phone,'task_id' => $planId])
|
|
||||||
->find();
|
if (!empty($batchRows)) {
|
||||||
if(!$ck_task_customer){
|
// 1. 提取当前批次的phone
|
||||||
$task_customer = Db::name('task_customer')
|
$phones = [];
|
||||||
->insert([
|
foreach ($batchRows as $row) {
|
||||||
'task_id' => $planId,
|
$phone = !empty($row['phone']) ? $row['phone'] : $row['wechat'];
|
||||||
'name' => $row['name'] ?? '',
|
if (!empty($phone)) {
|
||||||
'source' => $row['source'] ?? '',
|
$phones[] = $phone;
|
||||||
'phone' => $phone,
|
}
|
||||||
'tags' => json_encode([],JSON_UNESCAPED_UNICODE),
|
}
|
||||||
'siteTags' => json_encode([],JSON_UNESCAPED_UNICODE),
|
|
||||||
'created_at' => time(),
|
// 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], '添加计划任务成功');
|
return ResponseHelper::success(['planId' => $planId], '添加计划任务成功');
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user