1、新增一个所有好友的流量池
2、旧版场景获客数据迁移 3、场景获客功能兼容旧版数据
This commit is contained in:
@@ -515,14 +515,100 @@ class WorkbenchGroupPushJob
|
||||
}
|
||||
|
||||
$companyId = $workbench->companyId ?? 0;
|
||||
|
||||
// 检查是否包含"所有好友"(packageId=0)
|
||||
$hasAllFriends = in_array(0, $trafficPools) || in_array('0', $trafficPools);
|
||||
$normalPools = array_filter($trafficPools, function($id) {
|
||||
return $id !== 0 && $id !== '0';
|
||||
});
|
||||
|
||||
$friends = [];
|
||||
|
||||
// 处理"所有好友"特殊流量池
|
||||
if ($hasAllFriends) {
|
||||
$allFriends = $this->getAllFriendsByCompany($companyId, $ownerWechatIds);
|
||||
$friends = array_merge($friends, $allFriends);
|
||||
}
|
||||
|
||||
// 处理普通流量池
|
||||
if (!empty($normalPools)) {
|
||||
$normalFriends = $this->getFriendsByNormalPools($normalPools, $companyId, $ownerWechatIds);
|
||||
$friends = array_merge($friends, $normalFriends);
|
||||
}
|
||||
|
||||
// 去重
|
||||
$uniqueFriends = [];
|
||||
$seenIds = [];
|
||||
foreach ($friends as $friend) {
|
||||
$friendId = $friend['id'] ?? null;
|
||||
if ($friendId && !in_array($friendId, $seenIds)) {
|
||||
$seenIds[] = $friendId;
|
||||
$uniqueFriends[] = $friend;
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($uniqueFriends)) {
|
||||
Log::info('好友推送:流量池未匹配到好友');
|
||||
return [];
|
||||
}
|
||||
|
||||
return $uniqueFriends;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取公司下所有好友(特殊流量池 packageId=0)
|
||||
* @param int $companyId
|
||||
* @param array $ownerWechatIds
|
||||
* @return array
|
||||
*/
|
||||
protected function getAllFriendsByCompany($companyId, array $ownerWechatIds = [])
|
||||
{
|
||||
// 获取公司下所有设备的微信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 [];
|
||||
}
|
||||
|
||||
$query = Db::table('s2_wechat_friend')->alias('wf')
|
||||
->join(['s2_wechat_account' => 'wa'], 'wa.id = wf.wechatAccountId', 'left')
|
||||
->where('wf.ownerWechatId', 'in', $wechatIds)
|
||||
->where('wf.isDeleted', 0)
|
||||
->whereNotNull('wf.id')
|
||||
->whereNotNull('wf.wechatAccountId');
|
||||
|
||||
if (!empty($ownerWechatIds)) {
|
||||
$query->whereIn('wf.wechatAccountId', $ownerWechatIds);
|
||||
}
|
||||
|
||||
$friends = $query
|
||||
->field('wf.id,wf.wechatAccountId,wf.wechatId,wf.ownerWechatId')
|
||||
->group('wf.id')
|
||||
->select();
|
||||
|
||||
return $friends ?: [];
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据普通流量池获取好友信息
|
||||
* @param array $packageIds
|
||||
* @param int $companyId
|
||||
* @param array $ownerWechatIds
|
||||
* @return array
|
||||
*/
|
||||
protected function getFriendsByNormalPools(array $packageIds, $companyId, array $ownerWechatIds = [])
|
||||
{
|
||||
$query = Db::name('traffic_source_package_item')
|
||||
->alias('tspi')
|
||||
->leftJoin('traffic_source_package tsp', 'tsp.id = tspi.packageId')
|
||||
->leftJoin('traffic_pool tp', 'tp.identifier = tspi.identifier')
|
||||
->leftJoin(['s2_wechat_friend' => 'wf'], 'wf.wechatId = tp.wechatId')
|
||||
->leftJoin(['s2_wechat_account' => 'wa'], 'wa.id = wf.wechatAccountId')
|
||||
->whereIn('tspi.packageId', $trafficPools)
|
||||
->whereIn('tspi.packageId', $packageIds)
|
||||
->where('tsp.isDel', 0)
|
||||
->where('wf.isDeleted', 0)
|
||||
->whereNotNull('wf.id')
|
||||
@@ -543,16 +629,7 @@ class WorkbenchGroupPushJob
|
||||
->group('wf.id')
|
||||
->select();
|
||||
|
||||
if (empty($friends)) {
|
||||
Log::info('好友推送:流量池未匹配到好友');
|
||||
return [];
|
||||
}
|
||||
|
||||
if ($friends === false) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return $friends;
|
||||
return $friends ?: [];
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user