场景获客优化

This commit is contained in:
wong
2025-08-12 15:02:00 +08:00
parent f6b620383c
commit 67fac4b2f2
3 changed files with 82 additions and 16 deletions

View File

@@ -118,19 +118,19 @@ class GetAddFriendPlanDetailV1Controller extends Controller
// 解析JSON字段
$sceneConf = json_decode($plan['sceneConf'], true) ?: [];
$sceneConf['wechatGroups'] = !empty($sceneConf['groupSelected']) ? $sceneConf['groupSelected'] : [];
$reqConf = json_decode($plan['reqConf'], true) ?: [];
$reqConf['deveiceGroups'] = $reqConf['device'];
$msgConf = json_decode($plan['msgConf'], true) ?: [];
$tagConf = json_decode($plan['tagConf'], true) ?: [];
if(!empty($sceneConf['wechatGroups'])){
$groupList = Db::name('wechat_group')->alias('wg')
->join('wechat_account wa', 'wa.wechatId = wg.ownerWechatId')
->where('wg.id', 'in', $sceneConf['wechatGroups'])
->order('wg.id', 'desc')
->field('wg.id,wg.name as groupName,wg.ownerWechatId,wa.nickName,wa.avatar,wa.alias,wg.avatar as groupAvatar')
->field('wg.id,wg.name,wg.chatroomId,wg.ownerWechatId,wa.nickName as ownerNickName,wa.avatar as ownerAvatar,wa.alias as ownerAlias,wg.avatar')
->select();
$sceneConf['wechatGroupsOptions'] = $groupList;
}else{
@@ -138,8 +138,6 @@ class GetAddFriendPlanDetailV1Controller extends Controller
}
if (!empty($reqConf['deveiceGroups'])){
$deveiceGroupsOptions = DeviceModel::alias('d')
->field([

View File

@@ -55,7 +55,6 @@ class PostCreateAddFriendPlanV1Controller extends BaseController
{
try {
$params = $this->request->param();
// 验证必填字段
if (empty($params['name'])) {
@@ -120,7 +119,7 @@ class PostCreateAddFriendPlanV1Controller extends BaseController
'tagConf' => json_encode($tagConf, JSON_UNESCAPED_UNICODE),
'userId' => $this->getUserInfo('id'),
'companyId' => $this->getUserInfo('companyId'),
'status' => 1,
'status' => !empty($params['status']) ? 1 : 0,
'apiKey' => $this->generateApiKey(), // 生成API密钥
'createTime'=> time(),
'updateTime'=> time(),
@@ -263,8 +262,80 @@ class PostCreateAddFriendPlanV1Controller extends BaseController
}
//群获客
if($params['sceneId'] == 7){
if ($params['sceneId'] == 7) {
if (!empty($params['wechatGroups']) && is_array($params['wechatGroups'])) {
$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['wechatGroups'])
->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', $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' => $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);
}
}
}
}
}

View File

@@ -2,6 +2,7 @@
namespace app\cunkebao\controller\plan;
use app\cunkebao\controller\BaseController;
use library\ResponseHelper;
use think\Controller;
use think\Db;
@@ -9,7 +10,7 @@ use think\Db;
/**
* 更新获客计划控制器
*/
class PostUpdateAddFriendPlanV1Controller extends Controller
class PostUpdateAddFriendPlanV1Controller extends BaseController
{
/**
* 更新计划任务
@@ -62,12 +63,6 @@ class PostUpdateAddFriendPlanV1Controller extends Controller
'endTime' => $params['endTime'] ?? '',
];
if (isset($params['wechatGroups'])){
$params['wechatGroups'] = $params['groupSelected'];
}
// 其余参数归为sceneConf
$sceneConf = $params;
unset(
@@ -101,10 +96,12 @@ class PostUpdateAddFriendPlanV1Controller extends Controller
'reqConf' => json_encode($reqConf, JSON_UNESCAPED_UNICODE),
'msgConf' => json_encode($msgConf, JSON_UNESCAPED_UNICODE),
'tagConf' => json_encode($tagConf, JSON_UNESCAPED_UNICODE),
'status' => !empty($params['status']) ? 1 : 0,
'updateTime' => time(),
];
try {
// 更新数据
$result = Db::name('customer_acquisition_task')
@@ -242,11 +239,11 @@ class PostUpdateAddFriendPlanV1Controller extends Controller
//群获客
if ($params['sceneId'] == 7) {
if (!empty($params['groupSelected']) && is_array($params['groupSelected'])) {
if (!empty($params['wechatGroups']) && is_array($params['wechatGroups'])) {
$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'])
->whereIn('gm.groupId', $params['wechatGroups'])
->group('gm.identifier')
->column('wa.id,wa.wechatId,wa.alias,wa.phone');