流量池更新优化

This commit is contained in:
wong
2026-02-02 14:51:25 +08:00
parent 519a9b1d1b
commit dd4b43c10e
10 changed files with 457 additions and 179 deletions

View File

@@ -3,6 +3,7 @@
namespace app\common\model;
use think\Model;
use think\Db;
/**
* 流量来源表模型类
@@ -130,6 +131,200 @@ class TrafficPoolSource extends Model
->order('createTime DESC')
->select();
}
/**
* 获取流量的所有来源(带群归属信息)
* @param int $poolCompanyId
* @return array
*/
public static function getSourcesWithOwners(int $poolCompanyId): array
{
$sources = self::where('poolCompanyId', $poolCompanyId)
->order('createTime DESC')
->select()
->toArray();
if (empty($sources)) {
return [];
}
// 收集所有群ID
$chatroomIds = [];
foreach ($sources as $source) {
if (!empty($source['sourceChatroomId'])) {
$chatroomIds[] = $source['sourceChatroomId'];
}
}
// 查询群信息和归属客服
$chatroomOwners = [];
if (!empty($chatroomIds)) {
$chatroomOwners = self::getChatroomOwners($chatroomIds);
}
// 组装数据(按来源类型和关键标识去重)
$result = [];
$seenChatroomIds = []; // 用于群成员来源去重
$seenFriendIds = []; // 用于好友添加来源去重
foreach ($sources as $source) {
// 群成员来源去重:同一个群只保留一条记录
if ($source['sourceType'] == self::SOURCE_TYPE_GROUP_MEMBER && !empty($source['sourceChatroomId'])) {
$chatroomId = $source['sourceChatroomId'];
if (isset($seenChatroomIds[$chatroomId])) {
continue; // 跳过重复的群
}
$seenChatroomIds[$chatroomId] = true;
}
// 好友添加来源去重同一个好友按sourceWechatId或sourceName只保留一条记录
if ($source['sourceType'] == self::SOURCE_TYPE_FRIEND_ADD) {
$friendKey = $source['sourceWechatId'] ?: ($source['sourceName'] ?: '');
if (!empty($friendKey) && isset($seenFriendIds[$friendKey])) {
continue; // 跳过重复的好友来源
}
if (!empty($friendKey)) {
$seenFriendIds[$friendKey] = true;
}
}
$sourceData = $source;
// 格式化时间(兼容时间戳和日期字符串)
if (!empty($source['createTime'])) {
if (is_numeric($source['createTime'])) {
$sourceData['createTimeFormatted'] = date('Y-m-d H:i:s', (int)$source['createTime']);
} else {
$sourceData['createTimeFormatted'] = $source['createTime'];
}
} else {
$sourceData['createTimeFormatted'] = null;
}
// 添加来源类型名称
$sourceData['sourceTypeName'] = self::SOURCE_TYPE_NAMES[$source['sourceType']] ?? '未知来源';
// 如果是群成员来源添加群归属信息和群ID展示
if ($source['sourceType'] == self::SOURCE_TYPE_GROUP_MEMBER && !empty($source['sourceChatroomId'])) {
$chatroomId = $source['sourceChatroomId'];
$sourceData['chatroomOwners'] = $chatroomOwners[$chatroomId] ?? [];
$sourceData['chatroomInfo'] = self::getChatroomInfo($chatroomId);
// 添加群ID用于展示
$sourceData['displayId'] = $chatroomId;
} elseif ($source['sourceType'] == self::SOURCE_TYPE_FRIEND_ADD) {
// 好友添加来源添加好友微信ID用于展示
$sourceData['chatroomOwners'] = [];
$sourceData['chatroomInfo'] = null;
$sourceData['displayId'] = $source['sourceWechatId'] ?: '';
} else {
$sourceData['chatroomOwners'] = [];
$sourceData['chatroomInfo'] = null;
$sourceData['displayId'] = $source['sourceId'] ?: '';
}
$result[] = $sourceData;
}
return $result;
}
/**
* 获取群的归属客服信息(支持多个客服)
* @param array $chatroomIds 群聊ID数组
* @return array [chatroomId => [owner1, owner2, ...]]
*/
protected static function getChatroomOwners(array $chatroomIds): array
{
if (empty($chatroomIds)) {
return [];
}
// 查询群和归属账号信息
$chatrooms = Db::table(['s2_wechat_chatroom' => 'wc'])
->leftJoin(['s2_wechat_account' => 'wa'], 'wa.wechatId = wc.wechatAccountWechatId')
->whereIn('wc.chatroomId', $chatroomIds)
->where('wc.isDeleted', 0)
->field([
'wc.chatroomId',
'wc.nickname as chatroomName',
'wc.chatroomAvatar',
'wc.wechatAccountWechatId as ownerWechatId',
'wc.wechatAccountNickname as ownerNickname',
'wc.wechatAccountAvatar as ownerAvatar',
'wc.wechatAccountAlias as ownerAlias',
'wa.id as accountId',
'wa.nickName as accountNickname',
])
->select();
// 按 chatroomId 分组,一个群可能有多条记录(多个客服管理)
$result = [];
foreach ($chatrooms as $chatroom) {
$chatroomId = $chatroom['chatroomId'];
if (!isset($result[$chatroomId])) {
$result[$chatroomId] = [];
}
// 避免重复添加相同的客服
$ownerWechatId = $chatroom['ownerWechatId'];
$exists = false;
foreach ($result[$chatroomId] as $existing) {
if ($existing['ownerWechatId'] === $ownerWechatId) {
$exists = true;
break;
}
}
if (!$exists && !empty($ownerWechatId)) {
$result[$chatroomId][] = [
'ownerWechatId' => $ownerWechatId,
'ownerNickname' => $chatroom['ownerNickname'] ?: $chatroom['accountNickname'] ?: '',
'ownerAvatar' => $chatroom['ownerAvatar'] ?: '',
'ownerAlias' => $chatroom['ownerAlias'] ?: '',
'accountId' => $chatroom['accountId'],
];
}
}
return $result;
}
/**
* 获取群信息
* @param string $chatroomId 群聊ID
* @return array|null
*/
protected static function getChatroomInfo(string $chatroomId): ?array
{
$chatroom = Db::table(['s2_wechat_chatroom' => 'wc'])
->where('wc.chatroomId', $chatroomId)
->where('wc.isDeleted', 0)
->field([
'wc.id',
'wc.chatroomId',
'wc.nickname as chatroomName',
'wc.chatroomAvatar',
'wc.createTime',
])
->find();
if (!$chatroom) {
return null;
}
// 格式化创建时间(兼容时间戳和日期字符串)
if (!empty($chatroom['createTime'])) {
if (is_numeric($chatroom['createTime'])) {
$chatroom['createTimeFormatted'] = date('Y-m-d H:i:s', (int)$chatroom['createTime']);
} else {
$chatroom['createTimeFormatted'] = $chatroom['createTime'];
}
} else {
$chatroom['createTimeFormatted'] = null;
}
return $chatroom;
}
}