代码优化
This commit is contained in:
@@ -19,92 +19,6 @@ use think\Db;
|
||||
*/
|
||||
class GetWechatsOnDevicesV1Controller extends BaseController
|
||||
{
|
||||
/**
|
||||
* 计算今日可添加好友数量
|
||||
*
|
||||
* @param string $wechatId
|
||||
* @return int
|
||||
*/
|
||||
protected function getCanAddFriendCount(string $wechatId): int
|
||||
{
|
||||
$weight = (string)WechatCustomerModel::where(
|
||||
[
|
||||
'wechatId' => $wechatId,
|
||||
'companyId' => $this->getUserInfo('companyId')
|
||||
]
|
||||
)
|
||||
->value('weight');
|
||||
|
||||
return json_decode($weight)->addLimit ?? 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算今日新增好友数量
|
||||
*
|
||||
* @param string $ownerWechatId
|
||||
* @return int
|
||||
*/
|
||||
protected function getTodayNewFriendCount(string $ownerWechatId): int
|
||||
{
|
||||
return WechatFriendShipModel::where(compact('ownerWechatId'))
|
||||
->whereBetween('createTime',
|
||||
[
|
||||
strtotime(date('Y-m-d 00:00:00')),
|
||||
strtotime(date('Y-m-d 23:59:59'))
|
||||
]
|
||||
)
|
||||
->count('*');
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO 获取微信加友状态
|
||||
*
|
||||
* @param string $wechatId
|
||||
* @return int
|
||||
*/
|
||||
protected function getWechatAddFriendStatus(string $wechatId): int
|
||||
{
|
||||
return Db::name('device_wechat_login')->where(['wechatId' => $wechatId])->order('id DESC')->value('alive');
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取微信好友数量
|
||||
*
|
||||
* @param string $ownerWechatId
|
||||
* @return int
|
||||
*/
|
||||
protected function getFriendsCount(string $ownerWechatId): int
|
||||
{
|
||||
return WechatFriendShipModel::where(
|
||||
[
|
||||
'ownerWechatId' => $ownerWechatId,
|
||||
//'companyId' => $this->getUserInfo('companyId'),
|
||||
]
|
||||
)
|
||||
->count();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取微信所属设备
|
||||
*
|
||||
* @param string $wechatId
|
||||
* @return string
|
||||
*/
|
||||
protected function getWhereOnDevice(string $wechatId): string
|
||||
{
|
||||
return (string)DeviceModel::alias('d')
|
||||
->where(
|
||||
[
|
||||
'l.wechatId' => $wechatId,
|
||||
'l.alive' => DeviceWechatLoginModel::ALIVE_WECHAT_ACTIVE,
|
||||
'l.companyId' => $this->getUserInfo('companyId')
|
||||
]
|
||||
)
|
||||
->join('device_wechat_login l', 'd.id = l.deviceId AND l.companyId = '. $this->getUserInfo('companyId'))
|
||||
->order('l.id desc')
|
||||
->value('d.memo');
|
||||
}
|
||||
|
||||
/**
|
||||
* 主操盘手获取项目下所有设备的id
|
||||
*
|
||||
@@ -172,22 +86,6 @@ class GetWechatsOnDevicesV1Controller extends BaseController
|
||||
->column('wechatId');
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO 获取设备最新活跃时间
|
||||
*
|
||||
* @param string $wechatId
|
||||
* @return string
|
||||
*/
|
||||
protected function getLatestActiveTime(string $wechatId): string
|
||||
{
|
||||
$wechatAccountId = Db::table('s2_wechat_account')->where(['wechatId' => $wechatId])->value('id');
|
||||
if (empty($wechatAccountId)){
|
||||
return '-';
|
||||
}
|
||||
$time = Db::table('s2_wechat_message')->where('wechatAccountId',$wechatAccountId)->order('id DESC')->value('wechatTime');
|
||||
return !empty($time) ? date('Y-m-d H:i:s', $time) : '-';
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建查询条件
|
||||
*
|
||||
@@ -256,15 +154,26 @@ class GetWechatsOnDevicesV1Controller extends BaseController
|
||||
protected function makeResultedSet(\think\Paginator $result): array
|
||||
{
|
||||
$resultSets = [];
|
||||
$items = $result->items();
|
||||
|
||||
foreach ($result->items() as $item) {
|
||||
if (empty($items)) {
|
||||
return $resultSets;
|
||||
}
|
||||
|
||||
$wechatIds = array_values(array_unique(array_map(function ($item) {
|
||||
return $item->wechatId ?? ($item['wechatId'] ?? '');
|
||||
}, $items)));
|
||||
|
||||
$metrics = $this->collectWechatMetrics($wechatIds);
|
||||
|
||||
foreach ($items as $item) {
|
||||
$sections = $item->toArray() + [
|
||||
'times' => $this->getCanAddFriendCount($item->wechatId),
|
||||
'addedCount' => $this->getTodayNewFriendCount($item->wechatId),
|
||||
'wechatStatus' => $this->getWechatAddFriendStatus($item->wechatId),
|
||||
'totalFriend' => $this->getFriendsCount($item->wechatId),
|
||||
'deviceMemo' => $this->getWhereOnDevice($item->wechatId),
|
||||
'activeTime' => $this->getLatestActiveTime($item->wechatId),
|
||||
'times' => $metrics['addLimit'][$item->wechatId] ?? 0,
|
||||
'addedCount' => $metrics['todayAdded'][$item->wechatId] ?? 0,
|
||||
'wechatStatus' => $metrics['wechatStatus'][$item->wechatId] ?? 0,
|
||||
'totalFriend' => $metrics['totalFriend'][$item->wechatId] ?? 0,
|
||||
'deviceMemo' => $metrics['deviceMemo'][$item->wechatId] ?? '',
|
||||
'activeTime' => $metrics['activeTime'][$item->wechatId] ?? '-',
|
||||
];
|
||||
|
||||
array_push($resultSets, $sections);
|
||||
@@ -273,6 +182,109 @@ class GetWechatsOnDevicesV1Controller extends BaseController
|
||||
return $resultSets;
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量收集微信账号的统计信息
|
||||
* @param array $wechatIds
|
||||
* @return array
|
||||
*/
|
||||
protected function collectWechatMetrics(array $wechatIds): array
|
||||
{
|
||||
$metrics = [
|
||||
'addLimit' => [],
|
||||
'todayAdded' => [],
|
||||
'totalFriend' => [],
|
||||
'wechatStatus' => [],
|
||||
'deviceMemo' => [],
|
||||
'activeTime' => [],
|
||||
];
|
||||
|
||||
if (empty($wechatIds)) {
|
||||
return $metrics;
|
||||
}
|
||||
|
||||
$companyId = $this->getUserInfo('companyId');
|
||||
|
||||
// 可添加好友额度
|
||||
$weightRows = WechatCustomerModel::where('companyId', $companyId)
|
||||
->whereIn('wechatId', $wechatIds)
|
||||
->column('weight', 'wechatId');
|
||||
foreach ($weightRows as $wechatId => $weight) {
|
||||
$decoded = json_decode($weight, true);
|
||||
$metrics['addLimit'][$wechatId] = $decoded['addLimit'] ?? 0;
|
||||
}
|
||||
|
||||
// 今日新增好友
|
||||
$start = strtotime(date('Y-m-d 00:00:00'));
|
||||
$end = strtotime(date('Y-m-d 23:59:59'));
|
||||
$todayRows = WechatFriendShipModel::whereIn('ownerWechatId', $wechatIds)
|
||||
->whereBetween('createTime', [$start, $end])
|
||||
->field('ownerWechatId, COUNT(*) as total')
|
||||
->group('ownerWechatId')
|
||||
->select();
|
||||
foreach ($todayRows as $row) {
|
||||
$wechatId = is_array($row) ? ($row['ownerWechatId'] ?? '') : ($row->ownerWechatId ?? '');
|
||||
if ($wechatId) {
|
||||
$metrics['todayAdded'][$wechatId] = (int)(is_array($row) ? ($row['total'] ?? 0) : ($row->total ?? 0));
|
||||
}
|
||||
}
|
||||
|
||||
// 总好友
|
||||
$friendRows = WechatFriendShipModel::whereIn('ownerWechatId', $wechatIds)
|
||||
->field('ownerWechatId, COUNT(*) as total')
|
||||
->group('ownerWechatId')
|
||||
->select();
|
||||
foreach ($friendRows as $row) {
|
||||
$wechatId = is_array($row) ? ($row['ownerWechatId'] ?? '') : ($row->ownerWechatId ?? '');
|
||||
if ($wechatId) {
|
||||
$metrics['totalFriend'][$wechatId] = (int)(is_array($row) ? ($row['total'] ?? 0) : ($row->total ?? 0));
|
||||
}
|
||||
}
|
||||
|
||||
// 设备状态与备注
|
||||
$loginRows = Db::name('device_wechat_login')
|
||||
->alias('l')
|
||||
->leftJoin('device d', 'd.id = l.deviceId')
|
||||
->field('l.wechatId,l.alive,d.memo')
|
||||
->where('l.companyId', $companyId)
|
||||
->whereIn('l.wechatId', $wechatIds)
|
||||
->order('l.id', 'desc')
|
||||
->select();
|
||||
foreach ($loginRows as $row) {
|
||||
$wechatId = is_array($row) ? ($row['wechatId'] ?? '') : ($row->wechatId ?? '');
|
||||
if (empty($wechatId) || isset($metrics['wechatStatus'][$wechatId])) {
|
||||
continue;
|
||||
}
|
||||
$metrics['wechatStatus'][$wechatId] = (int)(is_array($row) ? ($row['alive'] ?? 0) : ($row->alive ?? 0));
|
||||
$metrics['deviceMemo'][$wechatId] = is_array($row) ? ($row['memo'] ?? '') : ($row->memo ?? '');
|
||||
}
|
||||
|
||||
// 活跃时间
|
||||
$accountMap = Db::table('s2_wechat_account')
|
||||
->whereIn('wechatId', $wechatIds)
|
||||
->column('id', 'wechatId');
|
||||
if (!empty($accountMap)) {
|
||||
$accountRows = Db::table('s2_wechat_message')
|
||||
->whereIn('wechatAccountId', array_values($accountMap))
|
||||
->field('wechatAccountId, MAX(wechatTime) as lastTime')
|
||||
->group('wechatAccountId')
|
||||
->select();
|
||||
$accountLastTime = [];
|
||||
foreach ($accountRows as $row) {
|
||||
$accountId = is_array($row) ? ($row['wechatAccountId'] ?? 0) : ($row->wechatAccountId ?? 0);
|
||||
if ($accountId) {
|
||||
$accountLastTime[$accountId] = (int)(is_array($row) ? ($row['lastTime'] ?? 0) : ($row->lastTime ?? 0));
|
||||
}
|
||||
}
|
||||
foreach ($accountMap as $wechatId => $accountId) {
|
||||
if (isset($accountLastTime[$accountId]) && $accountLastTime[$accountId] > 0) {
|
||||
$metrics['activeTime'][$wechatId] = date('Y-m-d H:i:s', $accountLastTime[$accountId]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $metrics;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取在线微信账号列表
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user