Commit 9f7989c8 authored by gxx's avatar gxx

完成活跃用户、流失用户查询操作

parent cc169ef1
...@@ -13,5 +13,7 @@ interface StatisticsServiceContract ...@@ -13,5 +13,7 @@ interface StatisticsServiceContract
{ {
public function UserIncrease(); public function UserIncrease();
public function ActiveUser();
public function OutflowUser();
} }
\ No newline at end of file
...@@ -48,4 +48,20 @@ class StatisticsController extends Controller ...@@ -48,4 +48,20 @@ class StatisticsController extends Controller
return $date; return $date;
} }
// 获取用户信息
public function ActiveUser()
{
$data = $this->mStatistics->ActiveUser();
return $data;
}
public function OutflowUser()
{
$outflowUsers = $this->mStatistics->OutflowUser();
return $outflowUsers;
}
} }
...@@ -10,7 +10,9 @@ namespace App\Services; ...@@ -10,7 +10,9 @@ namespace App\Services;
use App\Contracts\StatisticsServiceContract; use App\Contracts\StatisticsServiceContract;
use App\Models\Task;
use App\Models\User; use App\Models\User;
use App\Models\UserInfo;
use Carbon\Carbon; use Carbon\Carbon;
class StatisticsService implements StatisticsServiceContract class StatisticsService implements StatisticsServiceContract
...@@ -18,10 +20,16 @@ class StatisticsService implements StatisticsServiceContract ...@@ -18,10 +20,16 @@ class StatisticsService implements StatisticsServiceContract
public function UserIncrease() public function UserIncrease()
{ {
$dtD = Carbon::now()->subDay(); $startTime = strtotime(date('Ymd'))-86400;
$dtM = Carbon::now()->subMonth(); $start = date('Y-m-d H:i:s', $startTime) ; // 开始时间 00:00:00 开始计算
$dtW = Carbon::now()->subWeek();
$dayUsers = User::where('created_at','>',$dtD)->get();
$dtM = date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m")-1,1,date("Y")));
$dtW = date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),date("d")-date("w")+1-7,date("Y")));
$dayUsers = User::where('created_at','>',$start)->where(function ($query){
$query->where('created_at','<',date('Y-m-d H:i:s', strtotime(date('Ymd'))));
})->get();
$monthUsers = User::where('created_at','>',$dtM)->get(); $monthUsers = User::where('created_at','>',$dtM)->get();
$weekUsers = User::where('created_at','>',$dtW)->get(); $weekUsers = User::where('created_at','>',$dtW)->get();
$users = User::all()->count(); $users = User::all()->count();
...@@ -46,4 +54,122 @@ class StatisticsService implements StatisticsServiceContract ...@@ -46,4 +54,122 @@ class StatisticsService implements StatisticsServiceContract
return $date; return $date;
} }
// 活跃用户
public function ActiveUser()
{
// 返回方式 日、月、周
$startTime = strtotime(date('Ymd'))-86400; // 昨天开始时间
$start = date('Y-m-d H:i:s', $startTime) ; // 昨天开始时间 00:00:00 开始计算
$monthStartTime = date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m")-1,1,date("Y")));
$weekStartTime =date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),date("d")-date("w")+1-7,date("Y"))) ;
// 查询昨天登录用户
$yesterdayLoginUsers = UserInfo::where('last_login_time','>',$start)->where(function ($query){
$query->where('last_login_time','<',date('Y-m-d H:i:s', strtotime(date('Ymd'))));
})->get();
// 查询上一个月登录用户
$LastMonthLoginUsers = UserInfo::where('last_login_time','>',$monthStartTime)
->where(function ($query) {
$query->where('last_login_time', '<', date("Y-m-d H:i:s",mktime(23,59,59,date("m") ,0,date("Y"))));
})->get();
// 查询上周登录用户
$LastWeekLoginUsers = UserInfo::where('last_login_time','>', $weekStartTime)
->where(function ($query) {
$query->where('last_login_time', '<', date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("d")-date("w")+7-7,date("Y"))));
})->get();
$yesterdayActiveUser=0;
if (!$yesterdayLoginUsers->isEmpty()){
// 昨日活跃用户 判断条件是否执行了一次或者多次 创建任务者
foreach ($yesterdayLoginUsers as $yesterdayLoginUser){
$todayTask = Task::where('creator_id',$yesterdayLoginUser['user_id'])
->where(function ($query){
$query->where('created_at','>',date('Y-m-d H:i:s', strtotime(date('Ymd'))-86400));
})->get();
if(!$todayTask->isEmpty()){
$yesterdayActiveUser+=1;
}
}
}
$LastMonthActiveUser=0;
if (!$LastMonthLoginUsers->isEmpty()){
// 上一个月活跃用户 判断条件是否执行了一次或者多次 创建任务者
foreach ($LastMonthLoginUsers as $LastMonthLoginUser){
$monthTask = Task::where('creator_id',$LastMonthLoginUser['user_id'])
->where(function ($query){
$query->where('created_at','>',date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m")-1,1,date("Y"))))
->orWhere(function ($q){
$q->where('created_at','<',date("Y-m-d H:i:s",mktime(23,59,59,date("m") ,0,date("Y"))));
});
})->get();
if(!$monthTask->isEmpty()){
if(count($monthTask) >= 1){
$LastMonthActiveUser+=1;
}
}
}
}
$LastWeekActiveUser=0;
if (!$LastWeekLoginUsers->isEmpty()){
// 上周活跃用户 判断条件是否执行了一次或者多次 创建任务者
foreach ($LastWeekLoginUsers as $LastWeekLoginUser){
$weekTask = Task::where('creator_id',$LastWeekLoginUser['user_id'])
->where(function ($query){
$query->where('created_at','>',date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),date("d")-date("w")+1-7,date("Y"))))
->orWhere(function ($q){
$q->where('created_at','<',date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("d")-date("w")+7-7,date("Y"))));
});
})->get();
if(!$weekTask->isEmpty()){
//dd(count($weekTask));
if(count($weekTask) >= 1){
$LastWeekActiveUser+=1;
}
}
}
}
$date = array($yesterdayActiveUser,$LastMonthActiveUser,$LastWeekActiveUser);
return $date;
}
// 流失用户
public function OutflowUser()
{
$userLogin = UserInfo::all();
$outflowUsers=0;
if(!$userLogin->isEmpty()){
foreach ($userLogin as $user){
$timeDifference = strtotime(date('Ymd')) - strtotime($user['last_login_time']);
$days = floor($timeDifference/84600);
if ($days > 7){
$outflowUsers+=1;
}
}
};
return $outflowUsers;
}
} }
\ No newline at end of file
...@@ -72,7 +72,7 @@ ...@@ -72,7 +72,7 @@
{{--<div style="width: 728px;height: 200px;background-color: #f0f0f0;border: 1px solid #D2D6DE;float: right;">--}} {{--<div style="width: 728px;height: 200px;background-color: #f0f0f0;border: 1px solid #D2D6DE;float: right;">--}}
<div style="width: 815px;height: 200px;background-color: #f0f0f0;border: 1px solid #D2D6DE;float: right;"> <div style="width: 808px;height: 200px;background-color: #f0f0f0;border: 1px solid #D2D6DE;float: right;margin-top: 70px">
<p style="margin-left: 265px;"> 新增用户占总用户百分比数据图</p> <p style="margin-left: 265px;"> 新增用户占总用户百分比数据图</p>
......
...@@ -82,7 +82,8 @@ Route::group(['prefix'=> 'project'],function (){ ...@@ -82,7 +82,8 @@ Route::group(['prefix'=> 'project'],function (){
// 统计 // 统计
Route::group(['prefix'=> 'statistics'],function (){ Route::group(['prefix'=> 'statistics'],function (){
Route::get('user_increase','StatisticsController@UserIncrease')->name('statistics.UserIncrease'); Route::get('user_increase','StatisticsController@UserIncrease')->name('statistics.UserIncrease');
Route::get('active_user','StatisticsController@ActiveUser')->name('statistics.ActiveUser');
Route::get('outflow_user','StatisticsController@OutflowUser')->name('statistics.OutflowUser');
}); });
...@@ -90,8 +91,6 @@ Route::group(['prefix'=> 'statistics'],function (){ ...@@ -90,8 +91,6 @@ Route::group(['prefix'=> 'statistics'],function (){
// 权限 // 权限
Route::group(['prefix'=> 'auth'],function (){ Route::group(['prefix'=> 'auth'],function (){
Route::get('getAll','AuthController@getAll')->name('auth.getAll'); Route::get('getAll','AuthController@getAll')->name('auth.getAll');
...@@ -103,18 +102,13 @@ Route::group(['prefix'=> 'auth'],function (){ ...@@ -103,18 +102,13 @@ Route::group(['prefix'=> 'auth'],function (){
Route::get('table',function (){
return view('admin/usertables');
});
Route::get('table',function (){
return view('admin/usertables');
});
Auth::routes(); Auth::routes();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment