工作台及场景获客字段同步
This commit is contained in:
@@ -20,21 +20,21 @@ class PostUpdateAddFriendPlanV1Controller extends Controller
|
||||
{
|
||||
try {
|
||||
$params = $this->request->param();
|
||||
|
||||
|
||||
// 验证必填字段
|
||||
if (empty($params['planId'])) {
|
||||
return ResponseHelper::error('计划ID不能为空', 400);
|
||||
}
|
||||
|
||||
|
||||
if (empty($params['name'])) {
|
||||
return ResponseHelper::error('计划名称不能为空', 400);
|
||||
}
|
||||
|
||||
|
||||
if (empty($params['sceneId'])) {
|
||||
return ResponseHelper::error('场景ID不能为空', 400);
|
||||
}
|
||||
|
||||
if (empty($params['device'])) {
|
||||
|
||||
if (empty($params['deveiceGroups'])) {
|
||||
return ResponseHelper::error('请选择设备', 400);
|
||||
}
|
||||
|
||||
@@ -51,17 +51,23 @@ class PostUpdateAddFriendPlanV1Controller extends Controller
|
||||
$msgConf = isset($params['messagePlans']) ? $params['messagePlans'] : [];
|
||||
$tagConf = [
|
||||
'scenarioTags' => $params['scenarioTags'] ?? [],
|
||||
'customTags' => $params['customTags'] ?? [],
|
||||
'customTags' => $params['customTags'] ?? [],
|
||||
];
|
||||
$reqConf = [
|
||||
'device' => $params['device'] ?? [],
|
||||
'remarkType' => $params['remarkType'] ?? '',
|
||||
'greeting' => $params['greeting'] ?? '',
|
||||
'device' => $params['deveiceGroups'] ?? [],
|
||||
'remarkType' => $params['remarkType'] ?? '',
|
||||
'greeting' => $params['greeting'] ?? '',
|
||||
'addFriendInterval' => $params['addFriendInterval'] ?? '',
|
||||
'startTime' => $params['startTime'] ?? '',
|
||||
'endTime' => $params['endTime'] ?? '',
|
||||
'startTime' => $params['startTime'] ?? '',
|
||||
'endTime' => $params['endTime'] ?? '',
|
||||
];
|
||||
|
||||
|
||||
if (isset($params['wechatGroups'])){
|
||||
$params['wechatGroups'] = $params['groupSelected'];
|
||||
}
|
||||
|
||||
|
||||
|
||||
// 其余参数归为sceneConf
|
||||
$sceneConf = $params;
|
||||
unset(
|
||||
@@ -75,7 +81,7 @@ class PostUpdateAddFriendPlanV1Controller extends Controller
|
||||
$sceneConf['messagePlans'],
|
||||
$sceneConf['scenarioTags'],
|
||||
$sceneConf['customTags'],
|
||||
$sceneConf['device'],
|
||||
$sceneConf['deveiceGroups'],
|
||||
$sceneConf['orderTableFileName'],
|
||||
$sceneConf['userInfo'],
|
||||
$sceneConf['textUrl'],
|
||||
@@ -89,162 +95,236 @@ class PostUpdateAddFriendPlanV1Controller extends Controller
|
||||
|
||||
// 构建更新数据
|
||||
$data = [
|
||||
'name' => $params['name'],
|
||||
'sceneId' => $params['sceneId'],
|
||||
'name' => $params['name'],
|
||||
'sceneId' => $params['sceneId'],
|
||||
'sceneConf' => json_encode($sceneConf, JSON_UNESCAPED_UNICODE),
|
||||
'reqConf' => json_encode($reqConf, JSON_UNESCAPED_UNICODE),
|
||||
'msgConf' => json_encode($msgConf, JSON_UNESCAPED_UNICODE),
|
||||
'tagConf' => json_encode($tagConf, JSON_UNESCAPED_UNICODE),
|
||||
'updateTime'=> time(),
|
||||
'reqConf' => json_encode($reqConf, JSON_UNESCAPED_UNICODE),
|
||||
'msgConf' => json_encode($msgConf, JSON_UNESCAPED_UNICODE),
|
||||
'tagConf' => json_encode($tagConf, JSON_UNESCAPED_UNICODE),
|
||||
'updateTime' => time(),
|
||||
];
|
||||
|
||||
|
||||
|
||||
try {
|
||||
// 更新数据
|
||||
$result = Db::name('customer_acquisition_task')
|
||||
->where('id', $params['planId'])
|
||||
->update($data);
|
||||
|
||||
|
||||
if ($result === false) {
|
||||
throw new \Exception('更新计划失败');
|
||||
}
|
||||
|
||||
//订单
|
||||
if($params['sceneId'] == 2){
|
||||
if(!empty($params['orderTableFile'])){
|
||||
// 先下载到本地临时文件,再分析,最后删除
|
||||
$originPath = $params['orderTableFile'];
|
||||
$tmpFile = tempnam(sys_get_temp_dir(), 'order_');
|
||||
// 判断是否为远程文件
|
||||
if (preg_match('/^https?:\/\//i', $originPath)) {
|
||||
// 远程URL,下载到本地
|
||||
$fileContent = file_get_contents($originPath);
|
||||
if ($fileContent === false) {
|
||||
exit('远程文件下载失败: ' . $originPath);
|
||||
}
|
||||
file_put_contents($tmpFile, $fileContent);
|
||||
} else {
|
||||
// 本地文件,直接copy
|
||||
if (!file_exists($originPath)) {
|
||||
exit('文件不存在: ' . $originPath);
|
||||
}
|
||||
copy($originPath, $tmpFile);
|
||||
//订单
|
||||
if ($params['sceneId'] == 2) {
|
||||
if (!empty($params['orderTableFile'])) {
|
||||
// 先下载到本地临时文件,再分析,最后删除
|
||||
$originPath = $params['orderTableFile'];
|
||||
$tmpFile = tempnam(sys_get_temp_dir(), 'order_');
|
||||
// 判断是否为远程文件
|
||||
if (preg_match('/^https?:\/\//i', $originPath)) {
|
||||
// 远程URL,下载到本地
|
||||
$fileContent = file_get_contents($originPath);
|
||||
if ($fileContent === false) {
|
||||
exit('远程文件下载失败: ' . $originPath);
|
||||
}
|
||||
file_put_contents($tmpFile, $fileContent);
|
||||
} else {
|
||||
// 本地文件,直接copy
|
||||
if (!file_exists($originPath)) {
|
||||
exit('文件不存在: ' . $originPath);
|
||||
}
|
||||
copy($originPath, $tmpFile);
|
||||
}
|
||||
// 解析临时文件
|
||||
$ext = strtolower(pathinfo($originPath, PATHINFO_EXTENSION));
|
||||
$rows = [];
|
||||
if (in_array($ext, ['xls', 'xlsx'])) {
|
||||
// 直接用composer自动加载的PHPExcel
|
||||
$excel = \PHPExcel_IOFactory::load($tmpFile);
|
||||
$sheet = $excel->getActiveSheet();
|
||||
$data = $sheet->toArray();
|
||||
if (count($data) > 1) {
|
||||
array_shift($data); // 去掉表头
|
||||
}
|
||||
// 解析临时文件
|
||||
$ext = strtolower(pathinfo($originPath, PATHINFO_EXTENSION));
|
||||
$rows = [];
|
||||
if (in_array($ext, ['xls', 'xlsx'])) {
|
||||
// 直接用composer自动加载的PHPExcel
|
||||
$excel = \PHPExcel_IOFactory::load($tmpFile);
|
||||
$sheet = $excel->getActiveSheet();
|
||||
$data = $sheet->toArray();
|
||||
if (count($data) > 1) {
|
||||
array_shift($data); // 去掉表头
|
||||
}
|
||||
|
||||
foreach ($data as $cols) {
|
||||
$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);
|
||||
$lines = preg_split('/\r\n|\r|\n/', $content);
|
||||
if (count($lines) > 1) {
|
||||
array_shift($lines); // 去掉表头
|
||||
foreach ($lines as $line) {
|
||||
if (trim($line) === '') continue;
|
||||
$cols = str_getcsv($line);
|
||||
if (count($cols) >= 6) {
|
||||
$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]) : '',
|
||||
];
|
||||
}
|
||||
foreach ($data as $cols) {
|
||||
$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);
|
||||
$lines = preg_split('/\r\n|\r|\n/', $content);
|
||||
if (count($lines) > 1) {
|
||||
array_shift($lines); // 去掉表头
|
||||
foreach ($lines as $line) {
|
||||
if (trim($line) === '') continue;
|
||||
$cols = str_getcsv($line);
|
||||
if (count($cols) >= 6) {
|
||||
$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]) : '',
|
||||
];
|
||||
}
|
||||
}
|
||||
} else {
|
||||
unlink($tmpFile);
|
||||
exit('暂不支持的文件类型: ' . $ext);
|
||||
}
|
||||
// 删除临时文件
|
||||
} else {
|
||||
unlink($tmpFile);
|
||||
exit('暂不支持的文件类型: ' . $ext);
|
||||
}
|
||||
// 删除临时文件
|
||||
unlink($tmpFile);
|
||||
|
||||
// 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),
|
||||
'createTime' => time(),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
// 4. 批量插入新数据
|
||||
if (!empty($newData)) {
|
||||
Db::name('task_customer')->insertAll($newData);
|
||||
// 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),
|
||||
'createTime' => time(),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
// 4. 批量插入新数据
|
||||
if (!empty($newData)) {
|
||||
Db::name('task_customer')->insertAll($newData);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
//群获客
|
||||
if ($params['sceneId'] == 7) {
|
||||
if (!empty($params['groupSelected']) && is_array($params['groupSelected'])) {
|
||||
$rows = Db::name('wechat_group_member')->alias('gm')
|
||||
->join('wechat_account wa', 'gm.identifier = wa.wechatId')
|
||||
->where('gm.companyId', $this->getUserInfo('companyId'))
|
||||
->whereIn('gm.groupId', $params['groupSelected'])
|
||||
->group('gm.identifier')
|
||||
->column('wa.id,wa.wechatId,wa.alias,wa.phone');
|
||||
|
||||
|
||||
// 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) {
|
||||
if (!empty($row['phone'])) {
|
||||
$phone = !empty($row['phone']);
|
||||
} elseif (!empty($row['alias'])) {
|
||||
$phone = $row['alias'];
|
||||
} else {
|
||||
$phone = $row['wechatId'];
|
||||
}
|
||||
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) {
|
||||
if (!empty($row['phone'])) {
|
||||
$phone = !empty($row['phone']);
|
||||
} elseif (!empty($row['alias'])) {
|
||||
$phone = $row['alias'];
|
||||
} else {
|
||||
$phone = $row['wechatId'];
|
||||
}
|
||||
if (!empty($phone) && !in_array($phone, $existingPhones)) {
|
||||
$newData[] = [
|
||||
'task_id' => $params['planId'],
|
||||
'name' => '',
|
||||
'source' => '场景获客_' . $params['name'] ?? '',
|
||||
'phone' => $phone,
|
||||
'tags' => json_encode([], JSON_UNESCAPED_UNICODE),
|
||||
'siteTags' => json_encode([], JSON_UNESCAPED_UNICODE),
|
||||
'createTime' => time(),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
// 4. 批量插入新数据
|
||||
if (!empty($newData)) {
|
||||
Db::name('task_customer')->insertAll($newData);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
return ResponseHelper::success(['planId' => $params['planId']], '更新计划任务成功');
|
||||
|
||||
|
||||
} catch (\Exception $e) {
|
||||
// 回滚事务
|
||||
Db::rollback();
|
||||
throw $e;
|
||||
}
|
||||
|
||||
|
||||
} catch (\Exception $e) {
|
||||
return ResponseHelper::error('系统错误: ' . $e->getMessage(), 500);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user