1、新增一个所有好友的流量池

2、旧版场景获客数据迁移
3、场景获客功能兼容旧版数据
This commit is contained in:
wong
2026-01-07 10:41:39 +08:00
parent 228d59402f
commit b2e84a2259
15 changed files with 516 additions and 57 deletions

View File

@@ -322,30 +322,91 @@ class WorkbenchImportContactJob
if (empty($contactNum)) {
return false;
}
//过滤已删除的数据
$packageIds = Db::name('traffic_source_package')
->where(['isDel' => 0])
->whereIn('id', $pools)
->column('id');
// 检查是否包含"所有好友"packageId=0
$hasAllFriends = in_array(0, $pools) || in_array('0', $pools);
$normalPools = array_filter($pools, function($id) {
return $id !== 0 && $id !== '0';
});
$data = [];
// 处理"所有好友"特殊流量池
if ($hasAllFriends) {
$allFriendsData = $this->getAllFriendsForImportContact($workbench, $contactNum);
$data = array_merge($data, $allFriendsData);
}
// 处理普通流量池
if (!empty($normalPools)) {
//过滤已删除的数据
$packageIds = Db::name('traffic_source_package')
->where(['isDel' => 0])
->whereIn('id', $normalPools)
->column('id');
if (empty($packageIds)) {
if (!empty($packageIds)) {
$normalData = Db::name('traffic_source_package_item')->alias('tpi')
->join('traffic_pool tp', 'tp.identifier = tpi.identifier')
->join('traffic_source ts', 'ts.identifier = tpi.identifier','left')
->join('workbench_import_contact_item wici', 'wici.poolId = tp.id AND wici.workbenchId = '.$workbench->id,'left')
->where('tp.mobile', '>',0)
->where('wici.id','null')
->whereIn('tpi.packageId',$packageIds)
->field('tp.id,tpi.packageId,tp.mobile as phone,ts.name')
->order('tp.id DESC')
->group('tpi.identifier')
->limit($contactNum)
->select();
$data = array_merge($data, $normalData ?: []);
}
}
if (empty($data)) {
return false;
}
$data = Db::name('traffic_source_package_item')->alias('tpi')
->join('traffic_pool tp', 'tp.identifier = tpi.identifier')
->join('traffic_source ts', 'ts.identifier = tpi.identifier','left')
->join('workbench_import_contact_item wici', 'wici.poolId = tp.id AND wici.workbenchId = '.$workbench->id,'left')
->where('tp.mobile', '>',0)
->where('wici.id','null')
->whereIn('tpi.packageId',$packageIds)
->field('tp.id,tpi.packageId,tp.mobile as phone,ts.name')
->order('tp.id DESC')
->group('tpi.identifier')
->limit($contactNum)
->select();
return $data;
}
/**
* 获取"所有好友"流量池的联系人数据(用于通讯录导入)
* @param Workbench $workbench
* @param int $limit
* @return array
*/
protected function getAllFriendsForImportContact($workbench, $limit)
{
$companyId = $workbench->companyId ?? 0;
// 获取公司下所有设备的微信ID
$wechatIds = Db::name('device')->alias('d')
->join('(SELECT MAX(id) AS id, deviceId FROM ck_device_wechat_login WHERE companyId='.$companyId.' GROUP BY deviceId) dwl_max', 'dwl_max.deviceId = d.id')
->join('device_wechat_login dwl', 'dwl.id = dwl_max.id')
->where(['d.companyId' => $companyId, 'd.deleteTime' => 0])
->column('dwl.wechatId');
if (empty($wechatIds)) {
return [];
}
// 从 s2_wechat_friend 表获取好友,然后关联 traffic_pool 表获取手机号
$data = Db::table('s2_wechat_friend')->alias('wf')
->join('traffic_pool tp', 'tp.wechatId = wf.wechatId', 'left')
->join('traffic_source ts', 'ts.identifier = tp.identifier', 'left')
->join('workbench_import_contact_item wici', 'wici.poolId = tp.id AND wici.workbenchId = '.$workbench->id, 'left')
->where('wf.ownerWechatId', 'in', $wechatIds)
->where('wf.isDeleted', 0)
->where('tp.mobile', '>', 0)
->where('wici.id', 'null')
->field('tp.id,tp.mobile as phone,ts.name')
->field(Db::raw('0 as packageId')) // 标记为"所有好友"流量池
->order('tp.id DESC')
->group('tp.identifier')
->limit($limit)
->select();
return $data ?: [];
}
/**
* 记录任务开始