Files
cunkebao_v3/Server/application/cunkebao/controller/traffic/GetPotentialListWithInCompanyV1Controller.php

386 lines
14 KiB
PHP
Raw Normal View History

<?php
namespace app\cunkebao\controller\traffic;
use app\common\model\TrafficPool as TrafficPoolModel;
use app\common\model\TrafficSource as TrafficSourceModel;
use app\common\model\WechatFriendShip as WechatFriendShipModel;
use app\cunkebao\controller\BaseController;
use library\ResponseHelper;
2025-07-23 16:00:01 +08:00
use think\Db;
/**
* 流量池控制器
*/
class GetPotentialListWithInCompanyV1Controller extends BaseController
{
/**
* 构建查询条件
*
* @param array $params
* @return array
*/
protected function makeWhere(array $params = []): array
{
if (!empty($keyword = $this->request->param('keyword'))) {
2025-07-29 17:04:00 +08:00
$where[] = ['p.identifier|wa.nickname|wa.phone|wa.wechatId|wa.alias', 'like', '%' . $keyword . '%'];
}
// 状态筛选
2025-07-23 16:00:01 +08:00
if ($status = $this->request->param('addStatus')) {
$where['s.status'] = $status;
} else {
$where['s.status'] = array('<>', TrafficSourceModel::STATUS_PASSED);
}
// 来源的筛选
2025-07-23 16:00:01 +08:00
if ($fromd = $this->request->param('packageId')) {
2025-07-29 17:04:00 +08:00
if ($fromd != -1) {
2025-07-23 16:00:01 +08:00
$where['tsp.id'] = $fromd;
2025-07-29 17:04:00 +08:00
} else {
$where[] = ['tsp.id', null];
2025-07-23 16:00:01 +08:00
}
}
2025-07-23 16:00:01 +08:00
if ($device = $this->request->param('device')) {
$where['d.deviceId'] = $device;
}
$where['s.companyId'] = $this->getUserInfo('companyId');
return array_merge($where, $params);
}
/**
* 获取流量池列表
*
* @param array $where
* @return \think\Paginator
*/
2025-07-23 16:00:01 +08:00
protected function getPoolListByCompanyId(array $where)
{
$query = TrafficPoolModel::alias('p')
->field(
[
2025-07-29 17:04:00 +08:00
'p.id', 'p.identifier', 'p.mobile', 'p.wechatId', 'p.identifier',
's.fromd', 's.status', 's.createTime', 's.companyId', 's.sourceId', 's.type',
2025-07-23 16:00:01 +08:00
'wa.nickname', 'wa.avatar', 'wa.gender', 'wa.phone',
]
)
2025-07-29 17:04:00 +08:00
->join('traffic_source s', 'p.identifier=s.identifier', 'left')
->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')
2025-07-23 16:00:01 +08:00
->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);
}
2025-07-29 17:04:00 +08:00
$result = $query->paginate($this->request->param('limit/d', 10), false, ['page' => $this->request->param('page/d', 1)]);
2025-07-23 16:00:01 +08:00
$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')
2025-07-29 17:04:00 +08:00
->where(['tspi.companyId' => $item->companyId, 'tspi.identifier' => $item->identifier])
2025-07-23 16:00:01 +08:00
->column('p.name');
$package2 = Db::name('traffic_source_package_item')->alias('tspi')
->join('traffic_source_package p', 'tspi.packageId=p.id')
2025-07-29 17:04:00 +08:00
->where(['tspi.companyId' => $item->companyId, 'tspi.identifier' => $item->identifier, 'p.isSys' => 1])
2025-07-23 16:00:01 +08:00
->column('p.name');
$packages = array_merge($package, $package2);
$item['packages'] = $packages;
2025-07-29 17:04:00 +08:00
if ($item->type == 1) {
2025-07-23 16:00:01 +08:00
$tag = Db::name('wechat_friendship')->where(['wechatId' => $item->wechatId])->column('tags');
$tags = [];
foreach ($tag as $k => $v) {
2025-07-29 17:04:00 +08:00
$v = json_decode($v, true);
if (!empty($v)) {
$tags = array_merge($tags, $v);
}
2025-07-23 16:00:01 +08:00
}
$item['tags'] = $tags;
}
}
unset($item);
$data = ['list' => $list, 'total' => $total];
return json_encode($data, JSON_UNESCAPED_UNICODE);
}
/**
* 获取流量池列表
*
* @return \think\response\Json
*/
public function index()
{
try {
2025-07-29 17:04:00 +08:00
$result = $this->getPoolListByCompanyId($this->makeWhere());
2025-07-23 16:00:01 +08:00
$result = json_decode($result, true);
return ResponseHelper::success(
[
2025-07-29 17:04:00 +08:00
'list' => $result['list'],
2025-07-23 16:00:01 +08:00
'total' => $result['total'],
]
);
} catch (\Exception $e) {
return ResponseHelper::error($e->getMessage(), $e->getCode());
}
}
2025-07-23 16:00:01 +08:00
2025-07-29 17:04:00 +08:00
public function getUser()
{
$userId = $this->request->param('userId', '');
2025-07-30 17:51:18 +08:00
$companyId = $this->getUserInfo('companyId');
2025-07-29 17:04:00 +08:00
if (empty($userId)) {
return json_encode(['code' => 500, 'msg' => '用户id不能为空']);
}
2025-07-30 17:51:18 +08:00
$total = [
'msg' => 0,
'money' => 0,
'isFriend' => false,
'percentage' => '0.00%',
];
2025-07-29 17:04:00 +08:00
2025-07-30 17:51:18 +08:00
$data = TrafficPoolModel::alias('p')
->field(['p.id', 'p.identifier', 'p.wechatId',
'wa.nickname', 'wa.avatar', 'wa.gender', 'wa.phone','wa.alias'])
2025-07-29 17:04:00 +08:00
->join('wechat_account wa', 'p.identifier=wa.wechatId', 'left')
2025-07-30 17:51:18 +08:00
->order('p.id DESC')
2025-07-29 17:04:00 +08:00
->where(['p.id' => $userId])
->group('p.identifier')
->find();
2025-07-30 17:51:18 +08:00
$data['lastMsgTime'] = '';
//来源
$source = Db::name('traffic_source')->alias('ts')
->field(['wa.nickname', 'wa.avatar', 'wa.gender','wa.phone','wa.wechatId','wa.alias',
'ts.createTime',
'wf.id as friendId','wf.wechatAccountId'])
->join('wechat_account wa', 'ts.sourceId=wa.wechatId', 'left')
->join(['s2_wechat_friend' => 'wf'], 'wa.wechatId=wf.ownerWechatId', 'left')
->where(['ts.companyId' => $companyId,'ts.identifier' => $data['identifier'],'wf.wechatId' => $data['wechatId']])
->order('ts.createTime DESC')
->select();
$wechatFriendId = [];
if (!empty($source)) {
$total['isFriend'] = true;
foreach ($source as &$v) {
$wechatFriendId[] = $v['friendId'];
//最后消息
$v['createTime'] = date('Y-m-d H:i:s', $v['createTime']);
$lastMsgTime = Db::table('s2_wechat_message')
->where(['wechatFriendId' => $v['friendId'],'wechatAccountId' => $v['wechatAccountId']])
->value('wechatTime');
$v['lastMsgTime'] = !empty($lastMsgTime) ? date('Y-m-d H:i:s', $lastMsgTime) : '';
//设备信息
$device = Db::name('device_wechat_login')->alias('dwl')
->join('device d','d.id=dwl.deviceId')
->where(['dwl.wechatId' => $v['wechatId']])
->field('d.id,d.memo,d.imei,d.brand,d.extra,d.alive')
->order('dwl.id DESC')
->find();
$extra = json_decode($device['extra'],true);
unset($device['extra']);
$device['address'] = !empty($extra['address']) ? $extra['address'] : '';
$v['device'] = $device;
}
unset($v);
}
$data['source'] = $source;
2025-07-29 17:04:00 +08:00
2025-07-30 17:51:18 +08:00
//流量池
2025-07-29 17:04:00 +08:00
$package = Db::name('traffic_source_package_item')->alias('tspi')
->join('traffic_source_package p', 'tspi.packageId=p.id AND tspi.companyId=p.companyId')
2025-07-30 17:51:18 +08:00
->where(['tspi.companyId' => $companyId, 'tspi.identifier' => $data['identifier']])
2025-07-29 17:04:00 +08:00
->column('p.name');
$package2 = Db::name('traffic_source_package_item')->alias('tspi')
->join('traffic_source_package p', 'tspi.packageId=p.id')
2025-07-30 17:51:18 +08:00
->where(['tspi.companyId' => $companyId, 'tspi.identifier' => $data['identifier']])
2025-07-29 17:04:00 +08:00
->column('p.name');
$packages = array_merge($package, $package2);
2025-07-30 17:51:18 +08:00
$data['packages'] = $packages;
if (!empty($wechatFriendId)){
//消息统计
$msgTotal = Db::table('s2_wechat_message')
->whereIn('wechatFriendId', $wechatFriendId)
->count();
$total['msg'] = $msgTotal;
//金额计算
$money = Db::table('s2_wechat_message')
->whereIn('wechatFriendId', $wechatFriendId)
->where(['isSend' => 1,'msgType' => 419430449])
->select();
if (!empty($money)){
foreach ($money as $v){
$content = json_decode($v['content'],true);
if ($content['paysubtype'] == 1){
$number = number_format(str_replace("", "", $content['feedesc']), 2);
$floatValue = floatval($number);
$total['money'] += $floatValue;
}
2025-07-29 17:04:00 +08:00
}
}
}
2025-07-30 17:51:18 +08:00
$taskNum = Db::name('task_customer')->alias('tc')
->join('customer_acquisition_task t','tc.task_id=t.id')
->where(['t.companyId' => $companyId,'t.deleteTime' => 0])
->whereIn('tc.phone',[$data['phone'], $data['wechatId'], $data['alias']])
->count();
$passNum = Db::name('task_customer')->alias('tc')
->join('customer_acquisition_task t','tc.task_id=t.id')
->where(['t.companyId' => $companyId,'t.deleteTime' => 0,'tc.status' => 4])
->whereIn('tc.phone',[$data['phone'], $data['wechatId'], $data['alias']])
->count();
if (!empty($taskNum) && !empty($passNum)){
$percentage = number_format(($taskNum / $passNum) * 100, 2);
$total['percentage'] = $percentage;
}
2025-08-06 18:00:50 +08:00
2025-07-30 17:51:18 +08:00
2025-07-23 16:00:01 +08:00
2025-07-30 17:51:18 +08:00
$data['total'] = $total;
$data['rmm'] = [
'r' => 0,
'f' => 0,
'm' => 0,
];
return ResponseHelper::success($data);
2025-07-29 17:04:00 +08:00
}
2025-07-23 16:00:01 +08:00
/**
* 用户旅程
* @return false|string
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function getUserJourney()
{
2025-07-29 17:04:00 +08:00
$page = $this->request->param('page', 1);
$pageSize = $this->request->param('pageSize', 10);
$userId = $this->request->param('userId', '');
if (empty($userId)) {
2025-07-23 16:00:01 +08:00
return json_encode(['code' => 500, 'msg' => '用户id不能为空']);
}
$query = Db::name('user_portrait')
->field('id,type,trafficPoolId,remark,count,createTime,updateTime')
->where(['trafficPoolId' => $userId]);
$total = $query->count();
$list = $query->order('createTime desc')
2025-07-29 17:04:00 +08:00
->page($page, $pageSize)
2025-07-23 16:00:01 +08:00
->select();
2025-07-29 17:04:00 +08:00
foreach ($list as $k => $v) {
$list[$k]['createTime'] = date('Y-m-d H:i:s', $v['createTime']);
$list[$k]['updateTime'] = date('Y-m-d H:i:s', $v['updateTime']);
2025-07-23 16:00:01 +08:00
}
2025-07-29 17:04:00 +08:00
return ResponseHelper::success(['list' => $list, 'total' => $total]);
}
2025-07-23 16:00:01 +08:00
2025-07-29 17:04:00 +08:00
public function getUserTags()
{
$userId = $this->request->param('userId', '');
2025-08-06 18:00:50 +08:00
$companyId = $this->getUserInfo('companyId');
2025-07-29 17:04:00 +08:00
if (empty($userId)) {
return json_encode(['code' => 500, 'msg' => '用户id不能为空']);
}
$data = Db::name('traffic_pool')->alias('tp')
2025-08-06 18:00:50 +08:00
->join('wechat_friendship f', 'tp.wechatId=f.wechatId AND f.companyId='.$companyId, 'left')
->join(['s2_wechat_friend' => 'wf'], 'f.wechatId=wf.wechatId', 'left')
2025-07-29 17:04:00 +08:00
->where(['tp.id' => $userId])
->order('tp.createTime desc')
->column('wf.id,wf.labels,wf.siteLabels');
2025-08-06 18:00:50 +08:00
if (empty($data)) {
return ResponseHelper::success(['wechat' => [], 'siteLabels' => []]);
}
2025-07-29 17:04:00 +08:00
$tags = [];
$siteLabels = [];
foreach ($data as $k => $v) {
$tag = json_decode($v['labels'], true);
$tag2 = json_decode($v['siteLabels'], true);
if (!empty($tag)) {
$tags = array_merge($tags, $tag);
}
if (!empty($tag2)) {
$siteLabels = array_merge($siteLabels, $tag2);
}
}
$tags = array_unique($tags);
$tags = array_values($tags);
$siteLabels = array_unique($siteLabels);
$siteLabels = array_values($siteLabels);
return ResponseHelper::success(['wechat' => $tags, 'siteLabels' => $siteLabels]);
2025-07-23 16:00:01 +08:00
}
2025-08-06 18:00:50 +08:00
/* public function editUserTags()
{
$userId = $this->request->param('userId', '');
if (empty($userId)) {
return json_encode(['code' => 500, 'msg' => '用户id不能为空']);
}
$tags = $this->request->param('tags', []);
$tags = $this->request->param('tags', []);
$isWechat = $this->request->param('isWechat', false);
$companyId = $this->getUserInfo('companyId');
$friend = Db::name('traffic_pool')->alias('tp')
->join('wechat_friendship f', 'tp.wechatId=f.wechatId AND f.companyId='.$companyId, 'left')
->join(['s2_wechat_friend' => 'wf'], 'f.wechatId=wf.wechatId', 'left')
->where(['tp.id' => $userId])
->order('tp.createTime desc')
->column('wf.id,wf.accountId,wf.labels,wf.siteLabels');
if (empty($data)) {
return ResponseHelper::error('该用户不存在');
}
}*/
2025-07-29 17:04:00 +08:00
}