love_php/app/Http/Controllers/Admin/BigDataController.php
2026-04-02 09:20:51 +08:00

186 lines
8.6 KiB
PHP

<?php
namespace App\Http\Controllers\Admin;
use App\Models\RecommendLinking;
use App\Repositories\Eloquent\SmsRepository as Sms;
use App\Facades\WechatService;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Response\ResponseJson;
use App\Models\RecommendLinkingNew;
use App\Models\User;
use App\Models\LinkingProgress;
use Illuminate\Support\Facades\Redis;
use App\Utils\Http;
class BigDataController extends Controller
{
/**
* 数据匹配统计
*
* @return void
*/
public function bigDataStat()
{
try{
$redis = Redis::connection('big_data');
//可以被推荐的单身男
$male_ids_key = 'ai:send:male:ids';
$male_ids = json_decode($redis->get($male_ids_key), true)?:[];
$male_count = count($male_ids);
//可以被推荐的单身男
$female_ids_key = 'ai:send:female:ids';
$female_ids = json_decode($redis->get($female_ids_key), true)?:[];
$female_count = count($female_ids);
//已有数据匹配的单身用户数
$female_ids_key = 'ai:recv:female:ids';
$male_ids_key = 'ai:recv:male:ids';
$single_female_ids = json_decode($redis->get($female_ids_key), true)?:[];
$single_male_ids = json_decode($redis->get($male_ids_key), true)?:[];
$single_female_count = count($single_female_ids);
$single_male_count = count($single_male_ids);
//已有数据匹配的介绍人数用户数
$female_ma_ids_key = "ai:recv:female_marriage:ids";
$male_ma_ids_key = "ai:recv:male_marriage:ids";
$marriage_female_ids = json_decode($redis->get($female_ma_ids_key), true)?:[];
$marriage_male_ids = json_decode($redis->get($male_ma_ids_key), true)?:[];
$marriage_female_count = count($marriage_female_ids);
$marriage_male_count = count($marriage_male_ids);
$marriage_count = $marriage_female_count + $marriage_male_count;
return $this->success('ok', compact('male_count', 'female_count', 'single_male_count', 'single_female_count', 'marriage_count', 'marriage_female_count', 'marriage_male_count'));
}catch(\Exeception $e){
$this->getError($e);
return $this->failure("服务器开小差");
}
}
/**
* 用户大数据匹配列表
*
* @param [type] $user_id 用户id
*/
public function userBigData($user_id)
{
try{
$is_recommend = request()->input('is_recommend', 1);
if ($is_recommend) {
$users = RecommendLinkingNew::with('otherUser:id,nickname,sex,app_avatar,photo,belief,is_real_approved,is_photo_audited', 'otherUser.profileCourtship:user_id,province,city,resident_province,resident_city,birthday,stature')->where('id_users_left', $user_id)->where('is_recommend', $is_recommend)->orderBy('score', 'desc')->orderBy('id','desc')->paginate();
}else{
// $users = RecommendLinking::with('otherUser:id,nickname,sex,app_avatar,photo,belief,is_real_approved', 'otherUser.profileCourtship:user_id,province,city,resident_province,resident_city,birthday,stature')->where('id_users_left', $user_id)->orderBy('score', 'desc')->orderBy('id','desc')->paginate();
$users = RecommendLinking::suffix($user_id % 10)->with('otherUser:id,nickname,sex,app_avatar,photo,belief,is_real_approved,is_photo_audited', 'otherUser.profileCourtship:user_id,province,city,resident_province,resident_city,birthday,stature')->where('id_users_left', $user_id)->orderBy('score', 'desc')->orderBy('id','desc')->paginate();
}
foreach ($users as $user) {
if (empty($user->otherUser)) continue;
$user->otherUser->age = $user->otherUser->profileCourtship?\CommonUtilsService::getAge($user->otherUser->profileCourtship->birthday):0;
$user->otherUser->avatar;
// unset($user->otherUser->app_avatar,$user->otherUser->photo, $user->id_users_left, $user->id_users_right);
}
$user = User::where('id', $user_id)->with('profileCourtship:user_id,province,city,resident_province,resident_city,birthday,stature,max_age,min_age,max_height,min_height')->select('id',"nickname",'sex','app_avatar','photo','belief','is_real_approved','type','is_photo_audited')->first();
if(!$user){
return $this->failure('用户不存在');
}
if ($user->type =='single' && $user->profileCourtship){
$user->profileCourtship->age = \CommonUtilsService::getAge($user->profileCourtship->birthday);
$user->profileCourtship->min_age = \CommonUtilsService::getAge($user->profileCourtship->min_age);
$user->profileCourtship->max_age = \CommonUtilsService::getAge($user->profileCourtship->max_age);
}elseif($user->type !='single' && $user->profileMarriage){
$profile_courtship = [];
$profile_courtship['age'] = \CommonUtilsService::getAge($user->profileMarriage->birthday);
$profile_courtship['min_age'] = 0;
$profile_courtship['max_age'] = 0;
$profile_courtship['province'] = $user->profileMarriage->province;
$profile_courtship['city'] = $user->profileMarriage->city;
$profile_courtship['resident_province'] = $user->profileMarriage->resident_province;
$profile_courtship['resident_city'] = $user->profileMarriage->resident_city;
$user->profile_courtship = $profile_courtship;
}else{
$profile_courtship['age'] = 0;
$profile_courtship['min_age'] = 0;
$profile_courtship['max_age'] = 0;
$profile_courtship['province'] = null;
$profile_courtship['city'] = null;
$profile_courtship['resident_province'] = null;
$profile_courtship['resident_city'] = null;
$user->profile_courtship = $profile_courtship;
}
// unset($user->app_avatar,$user->photo);
return $this->success('ok', compact('user', 'users'));
}catch(\Exeception $e){
$this->getError($e);
return $this->failure("服务器开小差");
}
}
/**
* 重新计算单个用户匹配信息
*
* @param [type] $user_id 用户id
* @return void
*/
public function recalUserBigData($user_id)
{
try{
//检查用户信息
$user = User::where('id', $user_id)->first();
if (empty($user)) return $this->failure('用户id错误');
if (empty($user->sex) || empty($user->belief)) return $this->failure('用户缺少基本资料');
if (empty($user->profileCourtship) || empty($user->profileCourtship->birthday)|| empty($user->profileCourtship->province) || empty($user->profileCourtship->city)) return $this->failure('用户缺少基本资料');
//todo 请求sync计算匹配
$url = 'http://sync.ufutx.com/api/ai/getRecommendUser';
$data = ['process_type'=>'part', 'user_id_list'=>[$user_id]];
$result = Http::http($url, $data, 'POST');
return $this->success('ok', json_decode($result));
}catch(\Exeception $e){
$this->getError($e);
return $this->failure("服务器开小差");
}
}
/**
* 重新计算所有用户匹配信息
*
* @param [type] $user_id 用户id
* @return void
*/
public function recalUsersBigData()
{
try{
//todo 请求sync计算匹配
$url = 'http://sync.ufutx.com/api/ai/getRecommendUser';
$data = ['process_type'=>'all'];
$result = Http::http($url, $data, 'POST');
return $this->success('ok', $result);
}catch(\Exeception $e){
$this->getError($e);
return $this->failure("服务器开小差");
}
}
/**
* 用户单次计算纪录
*
* @param [type] $user_id
* @return void
*/
public function userProgresses($user_id=0)
{
try{
$logs = LinkingProgress::where('user_id', $user_id)->orderBy('id', 'desc')->paginate();
return $this->success('ok', $logs);
}catch(\Exception $e){
$this->getError($e);
return $this->failure("服务器开小差");
}
}
public function getOfficialAccountsUserOpenId(){
$result = WechatService::getOfficialAccountsUserOpenId();
return $this->success('ok', $result);
}
}