流量池添加及场景获客统计数据

This commit is contained in:
wong
2025-08-15 15:37:00 +08:00
parent e53f31487e
commit 25ae2458f0
6 changed files with 351 additions and 70 deletions

View File

@@ -22,35 +22,49 @@ class GetPotentialListWithInCompanyV1Controller extends BaseController
*/
protected function makeWhere(array $params = []): array
{
if (!empty($keyword = $this->request->param('keyword'))) {
$keyword = $this->request->param('keyword','');
$device = $this->request->param('deviceId');
$status = $this->request->param('addStatus','');
$taskId = $this->request->param('taskId','');
$packageId = $this->request->param('packageId','');
$where = [];
if (!empty($keyword)) {
$where[] = ['p.identifier|wa.nickname|wa.phone|wa.wechatId|wa.alias', 'like', '%' . $keyword . '%'];
}
// 状态筛选
if ($status = $this->request->param('addStatus')) {
$where['s.status'] = $status;
} else {
$where['s.status'] = array('<>', TrafficSourceModel::STATUS_PASSED);
if (!empty($status)) {
if ($status == 1){
$where[] = ['s.status','=',4];
}elseif ($status == 2){
$where[] = ['s.status','=',0];
}elseif ($status == -1){
$where[] = ['s.status','=',2];
}elseif ($status == 3){
$where[] = ['s.status','=',2];
}
}
// 来源的筛选
if ($fromd = $this->request->param('packageId')) {
if ($fromd != -1) {
$where['tsp.id'] = $fromd;
if ($packageId) {
if ($packageId != -1) {
$where[] = ['tsp.id','=',$packageId];
} else {
$where[] = ['tsp.id', null];
$where[] = ['tsp.id','=', null];
}
}
if ($device = $this->request->param('deviceId')) {
$where['d.deviceId'] = $device;
if (!empty($device)) {
$where[] = ['d.deviceId','=',$device];
}
if (!empty($taskId)){
$where[] = ['t.sceneId','=',$taskId];
}
$where[] = ['s.companyId','=',$this->getUserInfo('companyId')];
$where['s.companyId'] = $this->getUserInfo('companyId');
return array_merge($where, $params);
return $where;
}
/**
@@ -59,67 +73,58 @@ class GetPotentialListWithInCompanyV1Controller extends BaseController
* @param array $where
* @return \think\Paginator
*/
protected function getPoolListByCompanyId(array $where)
protected function getPoolListByCompanyId(array $where,$isPage = true)
{
$query = TrafficPoolModel::alias('p')
->field(
[
'p.id', 'p.identifier', 'p.mobile', 'p.wechatId', 'p.identifier',
's.fromd', 's.status', 's.createTime', 's.companyId', 's.sourceId', 's.type',
'wa.nickname', 'wa.avatar', 'wa.gender', 'wa.phone',
'wa.nickname', 'wa.avatar', 'wa.gender', 'wa.phone','wa.alias'
]
)
->join('traffic_source s', 'p.identifier=s.identifier', 'left')
->join('traffic_source s', 'p.identifier=s.identifier')
->join('wechat_account wa', 'p.identifier=wa.wechatId', 'left')
->join('traffic_source_package_item tspi', 'p.identifier = tspi.identifier AND s.companyId = tspi.companyId', 'left')
->join('traffic_source_package tsp', 'tspi.packageId=tsp.id', 'left')
->join('device_wechat_login d', 's.sourceId=d.wechatId', 'left')
->order('p.id DESC,s.id DESC')
->group('p.identifier');
foreach ($where as $key => $value) {
if (is_numeric($key) && is_array($value) && isset($value[0]) && $value[0] === 'exp') {
$query->whereExp('', $value[1]);
continue;
}
if (is_array($value)) {
$query->where($key, ...$value);
continue;
}
$query->where($key, $value);
}
$result = $query->paginate($this->request->param('limit/d', 10), false, ['page' => $this->request->param('page/d', 1)]);
$list = $result->items();
$total = $result->total();
foreach ($list as &$item) {
//流量池筛选
$package = Db::name('traffic_source_package_item')->alias('tspi')
->join('traffic_source_package p', 'tspi.packageId=p.id AND tspi.companyId=p.companyId')
->where(['tspi.companyId' => $item->companyId, 'tspi.identifier' => $item->identifier])
->column('p.name');
$package2 = Db::name('traffic_source_package_item')->alias('tspi')
->join('traffic_source_package p', 'tspi.packageId=p.id')
->where(['tspi.companyId' => $item->companyId, 'tspi.identifier' => $item->identifier, 'p.isSys' => 1])
->column('p.name');
$packages = array_merge($package, $package2);
$item['packages'] = $packages;
->where($where);
if ($item->type == 1) {
$tag = Db::name('wechat_friendship')->where(['wechatId' => $item->wechatId])->column('tags');
$tags = [];
foreach ($tag as $k => $v) {
$v = json_decode($v, true);
if (!empty($v)) {
$tags = array_merge($tags, $v);
$result = $query->order('p.id DESC,s.id DESC')->group('p.identifier');
if ($isPage) {
$result = $query->paginate($this->request->param('limit/d', 10), false, ['page' => $this->request->param('page/d', 1)]);
$list = $result->items();
$total = $result->total();
}else{
$list = $result->select();
$total = '';
}
if ($isPage) {
foreach ($list as &$item) {
//流量池筛选
$package = Db::name('traffic_source_package_item')->alias('tspi')
->join('traffic_source_package p', 'tspi.packageId=p.id AND tspi.companyId=p.companyId')
->where(['tspi.identifier' => $item->identifier])
->whereIn('tspi.companyId', [0, $item->companyId])
->column('p.name');
$item['packages'] = $package;
if ($item->type == 1) {
$tag = Db::name('wechat_friendship')->where(['wechatId' => $item->wechatId])->column('tags');
$tags = [];
foreach ($tag as $k => $v) {
$v = json_decode($v, true);
if (!empty($v)) {
$tags = array_merge($tags, $v);
}
}
$item['tags'] = $tags;
}
$item['tags'] = $tags;
}
}
}
unset($item);
$data = ['list' => $list, 'total' => $total];
@@ -356,6 +361,112 @@ class GetPotentialListWithInCompanyV1Controller extends BaseController
return ResponseHelper::success(['wechat' => $tags, 'siteLabels' => $siteLabels]);
}
public function getPackage()
{
$companyId = $this->getUserInfo('companyId');
$package = Db::name('traffic_source_package')
->whereIn('companyId', [$companyId,0])
->field('id,name')
->select();
return ResponseHelper::success($package);
}
public function addPackage()
{
try {
$addPackageId = $this->request->param('addPackageId','');
$packageName = $this->request->param('packageName','');
$userIds = $this->request->param('userIds',[]);
$companyId = $this->getUserInfo('companyId');
$userId = $this->getUserInfo('id');
if (empty($addPackageId) && empty($packageName)){
return ResponseHelper::error('存储的流量池不能为空');
}
if (!empty($addPackageId)){
$package = Db::name('traffic_source_package')
->where(['id' => $addPackageId,'isDel' => 0])
->whereIn('companyId', [$companyId,0])
->field('id,name')
->find();
if (empty($package)){
return ResponseHelper::error('该流量池不存在');
}
$packageId = $package['id'];
}else{
$package = Db::name('traffic_source_package')
->where(['isDel' => 0,'name' => $packageName])
->whereIn('companyId', [$companyId,0])
->field('id,name')
->find();
if (!empty($package)){
return ResponseHelper::error('该流量池名称已存在');
}
$packageId = Db::name('traffic_source_package')->insertGetId(['userId' => $userId,'companyId' => $companyId,'name' => $packageName,'isDel' => 0]);
}
if (!empty($userIds)){
if (!is_array($userIds)){
return ResponseHelper::error('选择的用户类型错误');
}
$result = Db::name('traffic_pool')->alias('tp')
->join('traffic_source tc','tp.identifier=tc.identifier')
->whereIn('tp.id',$userIds)
->where(['companyId' => $companyId])
->group('tp.identifier')
->column('tc.identifier');
}else{
$result = $this->getPoolListByCompanyId($this->makeWhere(),false);
$result = json_decode($result, true);
$result = array_column($result['list'],'identifier');
}
// 1000条为一组进行批量处理
$batchSize = 1000;
$totalRows = count($result);
for ($i = 0; $i < $totalRows; $i += $batchSize) {
$batchRows = array_slice($result, $i, $batchSize);
if (!empty($batchRows)) {
// 2. 批量查询已存在的手机
$existingPhones = [];
$existing = Db::name('traffic_source_package_item')
->where(['companyId' => $companyId,'packageId' => $packageId])
->whereIn('identifier', $batchRows)
->field('identifier')
->select();
$existingPhones = array_column($existing, 'identifier');
// 3. 过滤出新数据,批量插入
$newData = [];
foreach ($batchRows as $row) {
if (!in_array($row, $existingPhones)) {
$newData[] = [
'packageId' => $packageId,
'companyId' => $companyId,
'identifier' => $row,
'createTime' => time(),
];
}
}
// 4. 批量插入新数据
if (!empty($newData)) {
Db::name('traffic_source_package_item')->insertAll($newData);
}
}
}
return ResponseHelper::success('添加成功');
} catch (\Exception $e) {
return ResponseHelper::error($e->getMessage(), $e->getCode());
}
}
/* public function editUserTags()
{
$userId = $this->request->param('userId', '');