103 lines
3.6 KiB
PHP
103 lines
3.6 KiB
PHP
<?php
|
||
|
||
namespace app\dao;
|
||
|
||
use app\model\User;
|
||
use app\model\UserPhone;
|
||
use app\model\UserPhoneLog;
|
||
|
||
/**
|
||
* 手机号在线时长日志
|
||
*/
|
||
class UserPhoneLogDao
|
||
{
|
||
/**
|
||
* 获取手机号 今天在线时长 单位秒
|
||
*/
|
||
public static function getOnlineTimeByPhoneToday($phone)
|
||
{
|
||
$today = date('Y-m-d');
|
||
$OnlineTime = UserPhoneLog::where('phone', $phone)->where('created_at', '>=', $today . " 00:00:00")->sum('time');
|
||
return $OnlineTime;
|
||
}
|
||
/**
|
||
* 自动处理time
|
||
* phone 手机号
|
||
* status — 是否在线 1: 在线,0: 不在线
|
||
* currentTimestamp 考虑到运行性能差别 使用传递过来的时间戳
|
||
*/
|
||
public static function setOnlineTimeByPhone($phone, $status, $currentTimestamp)
|
||
{
|
||
//这里因为和api的返回是反的所以需要这样
|
||
$status = $status == 0 ? 1 : 0;
|
||
$today = date('Y-m-d');
|
||
// $UserPhoneLog=UserPhoneLog::where('phone',$phone)->where('created_at', '>=', $today." 00:00:00")->limit(2)->orderByDesc('id')->get();
|
||
$UserPhoneLog = UserPhoneLog::where('phone', $phone)->where('created_at', '>=', $today . " 00:00:00")->orderBy('id', 'desc')->first();
|
||
if (empty($UserPhoneLog)) {
|
||
$new_UserPhoneLog = new UserPhoneLog();
|
||
$new_UserPhoneLog->status = $status;
|
||
$new_UserPhoneLog->phone = $phone;
|
||
$new_UserPhoneLog->time = 0;
|
||
$new_UserPhoneLog->save();
|
||
return;
|
||
}
|
||
if ($UserPhoneLog->status == 1 && $status == 1) {
|
||
$new_UserPhoneLog = new UserPhoneLog();
|
||
$new_UserPhoneLog->status = $status;
|
||
$new_UserPhoneLog->phone = $phone;
|
||
$new_UserPhoneLog->time = $currentTimestamp - strtotime($UserPhoneLog->created_at);
|
||
$new_UserPhoneLog->save();
|
||
echo ($currentTimestamp - strtotime($UserPhoneLog->created_at));
|
||
} else {
|
||
$new_UserPhoneLog = new UserPhoneLog();
|
||
$new_UserPhoneLog->status = $status;
|
||
$new_UserPhoneLog->phone = $phone;
|
||
$new_UserPhoneLog->time = 0;
|
||
$new_UserPhoneLog->save();
|
||
}
|
||
}
|
||
/**
|
||
* 根据用户id查询 是否有手机号在线时间大于6小时
|
||
* 有返回 true 没用返回false
|
||
*/
|
||
public static function getOnlineTimeByPhoneTodayId($user_id)
|
||
{
|
||
$user_phones = UserPhone::where('user_id', $user_id)->get();
|
||
foreach ($user_phones as $v) {
|
||
if (self::getOnlineTimeByPhoneToday($v->phone) > 60 * 60 * 6) {
|
||
return true;
|
||
}
|
||
}
|
||
return false;
|
||
}
|
||
/**
|
||
* 获取今天在线时长超过6小时的用户列表
|
||
*/
|
||
public static function getOnlineTimeByPhone6HoserUserId()
|
||
{
|
||
$today = date('Y-m-d');
|
||
// 获取当天每个手机号的在线时长总和
|
||
$OnlineTime = UserPhoneLog::where('created_at', '>=', $today . " 00:00:00")
|
||
->groupBy('phone')
|
||
->selectRaw('phone, sum(time) as total_time')
|
||
->get();
|
||
|
||
$phoneMax6hoser_phone = [];
|
||
foreach ($OnlineTime as $v) {
|
||
if ($v->total_time > 60 * 60 * 6) { // 如果在线时长超过6小时
|
||
$phoneMax6hoser_phone[] = $v->phone; // 仅存储手机号
|
||
}
|
||
}
|
||
|
||
// 获取这些手机号对应的用户ID
|
||
$user_ids = UserPhone::whereIn('phone', $phoneMax6hoser_phone)
|
||
->groupBy('user_id')
|
||
->pluck('user_id'); // 使用pluck提取user_id字段
|
||
|
||
// 输出用户ID
|
||
// var_dump(json_encode($user_ids));
|
||
|
||
return $user_ids; // 返回用户ID列表
|
||
}
|
||
}
|