【操盘手】 流量分发功能提交

This commit is contained in:
wong
2025-06-04 11:38:43 +08:00
parent 1f2a1dab00
commit 296464bff9
9 changed files with 199 additions and 86 deletions

View File

@@ -649,24 +649,24 @@ class WebSocketController extends BaseController
// 消息拼接 msgType(1:文本 3:图片 43:视频 47:动图表情包gif、其他表情包 49:小程序/其他:图文、文件)
// 当前type 为文本、图片、动图表情包的时候content为string, 其他情况为对象 {type: 'file/link/...', url: '', title: '', thunmbPath: '', desc: ''}
$result = [
"cmdType" => "CmdSendMessage",
"content" => $dataArray['content'],
"msgSubType" => 0,
"msgType" => $dataArray['msgType'],
"seq" => time(),
"wechatAccountId" => $dataArray['wechatAccountId'],
"wechatChatroomId" => 0,
"wechatFriendId" => $dataArray['wechatFriendId'],
];
$result = [
"cmdType" => "CmdSendMessage",
"content" => $dataArray['content'],
"msgSubType" => 0,
"msgType" => $dataArray['msgType'],
"seq" => time(),
"wechatAccountId" => $dataArray['wechatAccountId'],
"wechatChatroomId" => 0,
"wechatFriendId" => $dataArray['wechatFriendId'],
];
$result = json_encode($result);
$this->client->send($result);
$message = $this->client->receive();
$message = json_decode($message, 1);
//关闭WS链接
$this->client->close();
//Log::write('WS个人消息发送');
$result = json_encode($result);
$this->client->send($result);
$message = $this->client->receive();
$message = json_decode($message, 1);
//关闭WS链接
$this->client->close();
//Log::write('WS个人消息发送');
return $message;
}

View File

@@ -188,7 +188,7 @@ class WorkbenchController extends Controller
$list = Workbench::where($where)
->with($with)
->field('id,name,type,status,autoStart,userId,createTime,updateTime')
->field('id,companyId,name,type,status,autoStart,userId,createTime,updateTime')
->order('id', 'desc')
->page($page, $limit)
->select()
@@ -260,12 +260,50 @@ class WorkbenchController extends Controller
$item->config = $item->trafficConfig;
$item->config->devices = json_decode($item->config->devices, true);
$item->config->pools = json_decode($item->config->pools, true);
$config_item = Db::name('workbench_traffic_config_item')->where(['workbenchId' => $item->id])->order('id DESC')->find();
$item->config->lastUpdated = !empty($config_item) ? date('Y-m-d H:i',$config_item['createTime']) : '--';
//统计
$labels = $item->config->pools;
$totalUsers = Db::table('s2_wechat_friend')->alias('wf')
->join(['s2_company_account' => 'sa'], 'sa.id = wf.accountId', 'left')
->join(['s2_wechat_account' => 'wa'], 'wa.id = wf.wechatAccountId', 'left')
->where([
['wf.isDeleted', '=', 0],
['sa.departmentId', '=', $item->companyId]
])
->whereIn('wa.currentDeviceId', $item->config->devices)
->field('wf.id,wf.wechatAccountId,wf.wechatId,wf.labels,sa.userName,wa.currentDeviceId as deviceId')
->where(function ($q) use ($labels) {
foreach ($labels as $label) {
$q->whereOrRaw("JSON_CONTAINS(wf.labels, '\"{$label}\"')");
}
})->count();
$totalAccounts = Db::table('s2_company_account')
->alias('a')
->where(['a.departmentId' => $item->companyId, 'a.status' => 0])
->whereNotLike('a.userName', '%_offline%')
->whereNotLike('a.userName', '%_delete%')
->group('a.id')
->count();
$todayStart = strtotime(date('Y-m-d 00:00:00'));
$todayEnd = strtotime(date('Y-m-d 23:59:59'));
$dailyAverage = Db::name('workbench_traffic_config_item')
->where('workbenchId', $item->id)
->whereTime('createTime', 'between', [$todayStart, $todayEnd])
->count();
if($dailyAverage > 0){
$dailyAverage = $dailyAverage / $totalAccounts;
}
$item->config->total = [
'dailyAverage' => 0,
'dailyAverage' => intval($dailyAverage),
'totalAccounts' => $totalAccounts,
'deviceCount' => count($item->config->devices),
'poolCount' => count($item->config->pools),
'dailyAverage' => $item->config->maxPerDay,
'totalUsers' => $item->config->maxPerDay * count($item->config->devices) * count($item->config->pools)
'totalUsers' => $totalUsers >> 0
];
}
unset($item->trafficConfig,$item->traffic_config);
@@ -1175,7 +1213,7 @@ class WorkbenchController extends Controller
foreach ($labels as $label) {
$friendCount = Db::table('s2_wechat_friend')
->whereIn('ownerWechatId',$wechatIds)
->where('labels', 'like', '%'.$label.'%')
->where('labels', 'like', '%"'.$label.'"%')
->count();
$newLabel[] = [
'label' => $label,

View File

@@ -10,6 +10,7 @@ use think\facade\Cache;
use app\cunkebao\model\Workbench;
use app\cunkebao\model\WorkbenchTrafficConfig;
use think\Db;
use app\api\controller\AutomaticAssign;
class WorkbenchTrafficDistributeJob
{
@@ -69,31 +70,83 @@ class WorkbenchTrafficDistributeJob
return;
}
// 获取账号userName不包含offline和delete
// 获取当天未超额的可用账号
$todayStart = strtotime(date('Y-m-d 00:00:00'));
$todayEnd = strtotime(date('Y-m-d 23:59:59'));
$accounts = Db::table('s2_company_account')
->where(['departmentId' => $workbench->companyId, 'status' => 0])
->whereNotLike('userName', '%_offline%')
->whereNotLike('userName', '%_delete%')
->field('id,userName,realName')
->alias('a')
->where(['a.departmentId' => $workbench->companyId, 'a.status' => 0])
->whereNotLike('a.userName', '%_offline%')
->whereNotLike('a.userName', '%_delete%')
->leftJoin('workbench_traffic_config_item wti', "wti.wechatAccountId = a.id AND wti.workbenchId = {$workbench->id} AND wti.createTime BETWEEN {$todayStart} AND {$todayEnd}")
->field('a.id,a.userName,a.realName,COUNT(wti.id) as todayCount')
->group('a.id')
->having('todayCount <= ' . $config['maxPerDay'])
->select();
$accountNum = count($accounts);
if ($accountNum < 2) {
Log::info("流量分发工作台 {$workbench->id} 账号少于3");
if ($accountNum < 1) {
Log::info("流量分发工作台 {$workbench->id} 可分配账号少于1");
return;
}
$automaticAssign = new AutomaticAssign();
do {
$friends = $this->getFriendsByLabels($workbench, $config, $page, $pageSize);
if (empty($friends) || count($friends) == 0) {
Log::info("流量分发工作台 {$workbench->id} 没有可分配的好友");
break;
}
$i = 0;
$accountNum = count($accounts);
foreach ($friends as $friend) {
if ($accountNum == 0) {
Log::info("流量分发工作台 {$workbench->id} 所有账号今日分配已满");
break 2;
}
if ($i >= $accountNum) {
$i = 0;
}
$account = $accounts[$i];
// 获取可分配好友
$friends = $this->getFriendsByLabels($workbench, $config, $page, $pageSize);
if (empty($friends) || count($friends) == 0) {
Log::info("流量分发工作台 {$workbench->id} 没有可分配的好友");
return;
}
// 如果该账号今天分配的记录数加上本次分配的记录数超过最大限制
if (($account['todayCount'] + $pageSize) >= $config['maxPerDay']) {
// 查询该客服账号当天分配记录数
$todayCount = Db::name('workbench_traffic_config_item')
->where('workbenchId', $workbench->id)
->where('wechatAccountId', $account['id'])
->whereBetween('createTime', [$todayStart, $todayEnd])
->count();
if ($todayCount >= $config['maxPerDay']) {
unset($accounts[$i]);
$accounts = array_values($accounts);
$accountNum = count($accounts);
$i++;
continue;
}
}
// TODO: 在这里实现分发逻辑
print_r($friends);
exit;
// 执行切换好友命令
$automaticAssign->allotWechatFriend([
'wechatFriendId' => $friend['id'],
'toAccountId' => $account['id']
], true);
// 写入分配记录表
Db::name('workbench_traffic_config_item')->insert([
'workbenchId' => $workbench->id,
'deviceId' => $friend['deviceId'],
'wechatFriendId' => $friend['id'],
'wechatAccountId' => $account['id'],
'createTime' => time()
]);
Log::info("流量分发工作台 {$workbench->id} 好友[{$friend['id']}]分配给客服[{$account['id']}] 成功");
$i++;
}
break;
$page++;
} while (true);
Log::info("流量分发工作台 {$workbench->id} 执行分发逻辑完成");
}
/**
* 检查是否在流量分发时间范围内
* @param WorkbenchAutoLike $config
@@ -146,7 +199,7 @@ class WorkbenchTrafficDistributeJob
$q->whereOrRaw("JSON_CONTAINS(wf.labels, '\"{$label}\"')");
}
});
$list = $query->page($page, $pageSize)->select();
$list = $query->page($page, $pageSize)->order('wf.id DESC')->select();
return $list;
}