91 lines
2.9 KiB
PHP
91 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\MessageLinkman;
|
|
use App\Models\User;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class MarriageService
|
|
{
|
|
/**
|
|
* 获取邀请用户的年龄分布数据
|
|
*/
|
|
public function getAgeDistributeData($user_id): array
|
|
{
|
|
if (!$user_id){
|
|
return [];
|
|
}
|
|
// $user_ids = User::query()->where('from_user_id',$user_id)->pluck('id');
|
|
$user_ids = MessageLinkman::query()->where('user_id',$user_id)->pluck('other_user_id');
|
|
|
|
$profile_courtship = DB::table('profile_courtships')->whereIn('user_id',$user_ids)
|
|
->whereNotNull('birthday')
|
|
->selectRaw('user_id')
|
|
->selectRaw('CONCAT(SUBSTRING(birthday,3,1),"0后") as year_after');
|
|
|
|
$profile_marriage = DB::table('profile_marriages')->whereIn('user_id',$user_ids)
|
|
->whereNotNull('birthday')
|
|
->selectRaw('user_id')
|
|
->selectRaw('CONCAT(SUBSTRING(birthday,3,1),"0后") as year_after');
|
|
|
|
$query = $profile_courtship->union($profile_marriage);
|
|
|
|
$data = DB::table(DB::raw("({$query->toSql()}) as a"))->mergeBindings($query)
|
|
->select('year_after')
|
|
->selectRaw('count(*) as count')
|
|
->orderByDesc('count')
|
|
->groupBy('year_after')
|
|
->get();
|
|
|
|
if (!$data){
|
|
return [];
|
|
}
|
|
|
|
$data = $data->toArray();
|
|
|
|
$total_count = array_sum(array_column($data,'count'));
|
|
|
|
foreach ($data as $key => $item) {
|
|
$ratio = round(($item->count / $total_count) * 100);
|
|
if ($ratio == 0) {
|
|
unset($data[$key]);
|
|
continue;
|
|
}
|
|
$item->ratio = $ratio / 100;
|
|
$item->ratio_text = $ratio . '%';
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
public function getFriendMaxYearAfter($user_id)
|
|
{
|
|
if (!$user_id){
|
|
return '';
|
|
}
|
|
// $user_ids = User::query()->where('from_user_id',$user_id)->pluck('id');
|
|
$user_ids = MessageLinkman::query()->where('user_id',$user_id)->pluck('other_user_id');
|
|
|
|
$profile_courtship = DB::table('profile_courtships')->whereIn('user_id',$user_ids)
|
|
->whereNotNull('birthday')
|
|
->selectRaw('user_id')
|
|
->selectRaw('CONCAT(SUBSTRING(birthday,3,1),"0后") as year_after');
|
|
|
|
$profile_marriage = DB::table('profile_marriages')->whereIn('user_id',$user_ids)
|
|
->whereNotNull('birthday')
|
|
->selectRaw('user_id')
|
|
->selectRaw('CONCAT(SUBSTRING(birthday,3,1),"0后") as year_after');
|
|
|
|
$query = $profile_courtship->union($profile_marriage);
|
|
|
|
$data = DB::table(DB::raw("({$query->toSql()}) as a"))->mergeBindings($query)
|
|
->select('year_after')
|
|
->selectRaw('count(*) as count')
|
|
->orderByDesc('count')
|
|
->groupBy('year_after')
|
|
->first();
|
|
|
|
return $data->year_after ?? '';
|
|
}
|
|
} |