229 lines
8.5 KiB
PHP
229 lines
8.5 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
|
||
|
|
namespace App\Services;
|
||
|
|
|
||
|
|
use App\Services\SaasMatchService;
|
||
|
|
use App\Models\AccessRecord;
|
||
|
|
use App\Models\MerchantAccount;
|
||
|
|
use App\Models\Match\Profile;
|
||
|
|
use App\Models\Match\SBlacklist;
|
||
|
|
use Illuminate\Support\Facades\Log;
|
||
|
|
use Illuminate\Support\Facades\Redis;
|
||
|
|
use App\Models\Match\MerchantUser;
|
||
|
|
use App\Models\IdentityAuthorization;
|
||
|
|
|
||
|
|
class RecommendService
|
||
|
|
{
|
||
|
|
const WORKER_USER_ID_LIST = [
|
||
|
|
1, 625, 898, 61850, 42343, 48594, 49091, 49819, 647, 58701, 60889, 43020, 60744,
|
||
|
|
81685, 85172, 76429, 82400, 59078, 85037, 8163, 75247, 79675, 86159, 94076, 3480,
|
||
|
|
8771, 49408, 52958, 58290, 61848, 70533, 70535, 70537, 70538, 74993, 86029, 93259,
|
||
|
|
97623, 97733, 11800
|
||
|
|
];
|
||
|
|
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 产生毫秒级的时间
|
||
|
|
* @return float
|
||
|
|
*/
|
||
|
|
private function micro_time_float(): float
|
||
|
|
{
|
||
|
|
list($microsecond, $second) = explode(" ", microtime());
|
||
|
|
return ((float)$microsecond + (float)$second);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 系统黑名单sBlackList表变动数据插入队列
|
||
|
|
* @param $tableArray
|
||
|
|
* @return bool
|
||
|
|
*/
|
||
|
|
private function tableSystemBlacklist(&$tableArray)
|
||
|
|
{
|
||
|
|
foreach ($tableArray as $tableString) {
|
||
|
|
list($tableName, $table_id) = explode('_', $tableString);
|
||
|
|
$tableData = json_decode(Redis::get($tableString), true);
|
||
|
|
if (!empty($tableData['user_id'])) {
|
||
|
|
if (empty($tableData['deleted_at'])) { //拉出黑名单动作
|
||
|
|
Log::notice('[tableSystemBlacklist] 用户被拉进黑名单 user_id = ' . $tableData['user_id']);
|
||
|
|
Redis::lpush("ai:hidden:ids", $tableData['user_id']);
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
Log::notice('[tableSystemBlacklist] 黑名单处理完成');
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 初始化所有商家信息
|
||
|
|
*/
|
||
|
|
|
||
|
|
public function initAllMerchant(){
|
||
|
|
//获取服务商数据
|
||
|
|
$merchantAccount = MerchantAccount::pluck('id');
|
||
|
|
$system_black_ids = SBlacklist::whereNull('deleted_at')->pluck('user_id');
|
||
|
|
foreach ( $merchantAccount as $v){
|
||
|
|
$this->initMerchantUser($v);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 单用户匹配商家内匹配
|
||
|
|
* @param $merchantId
|
||
|
|
* @param $user_id
|
||
|
|
* @param $saasUserId
|
||
|
|
* @return array
|
||
|
|
*/
|
||
|
|
public function singleUserMatch($merchantId, $user_id, $saasUserId){
|
||
|
|
//判断商家用户是否相符
|
||
|
|
if($user_id){
|
||
|
|
$merchantUser = MerchantUser::where("user_id",$user_id)->select("id","user_id")->first();
|
||
|
|
$user_id = isset($merchantUser['user_id'])?$merchantUser['user_id']:null;
|
||
|
|
$saasUserId = isset($merchantUser["id"])?$merchantUser['id']:null;
|
||
|
|
}elseif($saasUserId){
|
||
|
|
$user_id = MerchantUser::where("id",$saasUserId)->pluck("user_id")->first();
|
||
|
|
}
|
||
|
|
$checkUser = IdentityAuthorization::where("m_id",$merchantId)->where('m_user_id',$saasUserId)->first();
|
||
|
|
|
||
|
|
if(!$checkUser){
|
||
|
|
return [];
|
||
|
|
}
|
||
|
|
$this->initMerchantUser($merchantId);
|
||
|
|
$user = Profile::where('user_id',$user_id) ->select([
|
||
|
|
"sex","user_id","belief","birthday","age","stature","province","city","resident_province",
|
||
|
|
"resident_city","degree","graduate_school","income","mate_conditon","weight","state"
|
||
|
|
])->first();
|
||
|
|
if($user){
|
||
|
|
if($user['sex'] == 1){
|
||
|
|
$match_user = json_decode(Redis::get("teacher:".$merchantId.":female"));
|
||
|
|
}elseif($user['sex'] == 2){
|
||
|
|
$match_user = json_decode(Redis::get("teacher:".$merchantId.":male"));
|
||
|
|
}else{
|
||
|
|
$match_user = [];
|
||
|
|
}
|
||
|
|
$items = collect();
|
||
|
|
$total_user = 0;
|
||
|
|
foreach ($match_user as $sender_user) {
|
||
|
|
$sassService = new SaasMatchService();
|
||
|
|
$result = $sassService->calculateUserScore($user, $sender_user);
|
||
|
|
$result['sender'] = $sender_user;
|
||
|
|
$items->push($result);
|
||
|
|
$total_user = $total_user + 1;
|
||
|
|
}
|
||
|
|
//排序
|
||
|
|
$items = $items->sortByDesc(function ($item) { //按分数排序
|
||
|
|
return $item['total_score'];
|
||
|
|
})->values()->take(1000);
|
||
|
|
$merchant_match['self']['total_user'] = $total_user;
|
||
|
|
$merchant_match['self']['match_user'] = $items;
|
||
|
|
return $merchant_match;
|
||
|
|
}
|
||
|
|
return [];
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 计算同商家自有用户匹配
|
||
|
|
* @param string $merchantId
|
||
|
|
*/
|
||
|
|
public function merchantUserMatch(string $merchantId){
|
||
|
|
$this->initMerchantUser($merchantId);
|
||
|
|
$merchant_match = [];
|
||
|
|
//从redis读取服务商用户数据
|
||
|
|
$merchantMale = json_decode(Redis::get("teacher:".$merchantId.":male"));
|
||
|
|
$merchantFemale = json_decode(Redis::get("teacher:".$merchantId.":female"));
|
||
|
|
$result = null;
|
||
|
|
//用户条件数据
|
||
|
|
$items = collect();
|
||
|
|
foreach ($merchantMale as $male){
|
||
|
|
$total_user = 0;
|
||
|
|
foreach ($merchantFemale as $female) {
|
||
|
|
$sassService = new SaasMatchService();
|
||
|
|
$result = $sassService->calculateUserScore($male, $female);
|
||
|
|
//$result['user'] = $male;
|
||
|
|
$result['sender'] = $female;
|
||
|
|
$items->push($result);
|
||
|
|
$total_user = $total_user + 1;
|
||
|
|
}
|
||
|
|
//排序
|
||
|
|
$items = $items->sortByDesc(function ($item) { //按分数排序
|
||
|
|
return $item['total_score'];
|
||
|
|
})->values()->take(30);
|
||
|
|
$merchant_match['self']['male']['total_user'] = $total_user;
|
||
|
|
$merchant_match['self']['male']['match_user'] = $items;
|
||
|
|
unset($items);
|
||
|
|
//unset($total_user);
|
||
|
|
$items = collect();
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
foreach ($merchantFemale as $female){
|
||
|
|
$total_user = 0;
|
||
|
|
foreach ($merchantMale as $male){
|
||
|
|
$sassService = new SaasMatchService();
|
||
|
|
$result = $sassService->calculateUserScore($female,$male);
|
||
|
|
//$result['user'] = $female ;
|
||
|
|
$result['sender'] = $male;
|
||
|
|
$items->push($result);
|
||
|
|
$total_user = $total_user + 1;
|
||
|
|
}
|
||
|
|
//排序
|
||
|
|
$items = $items->sortByDesc(function ($item) { //按分数排序
|
||
|
|
return $item['total_score'];
|
||
|
|
})->values()->take(30);
|
||
|
|
$merchant_match['self']['female']['total_user'] = $total_user;
|
||
|
|
$merchant_match['self']['female']['match_user'] = $items;
|
||
|
|
unset($items);
|
||
|
|
//unset($total_user);
|
||
|
|
$items = collect();
|
||
|
|
}
|
||
|
|
return $merchant_match;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 计算同联盟用户匹配
|
||
|
|
*/
|
||
|
|
public function unionUserMatch(){
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
/**
|
||
|
|
* 根据商家id初始化用户数据并存入redis
|
||
|
|
* @param string $merchantId
|
||
|
|
*/
|
||
|
|
private function initMerchantUser(string $merchantId){
|
||
|
|
$merchant_user_id_list = IdentityAuthorization::where("m_id",$merchantId)->pluck('m_user_id');
|
||
|
|
$male = [];
|
||
|
|
$female = [];
|
||
|
|
foreach ($merchant_user_id_list as $merchant_user_id){
|
||
|
|
//根据open_id获取用户id;
|
||
|
|
$user_id = MerchantUser::where('id',$merchant_user_id)->pluck("user_id")->first();
|
||
|
|
if($user_id) {
|
||
|
|
$user = Profile::with(['member' => function ($query) {
|
||
|
|
$query->select("id", "industry", "industry_sub", "app_avatar", "nickname");
|
||
|
|
}])
|
||
|
|
->with(['saasUser' => function ($query) {
|
||
|
|
$query->select("id", "user_id", "mobile", "pic", "sex", "nickname");
|
||
|
|
}])
|
||
|
|
->where('user_id', $user_id)
|
||
|
|
->wherehas('member', function ($sql) {
|
||
|
|
$sql->where('hidden_profile', 'NONE')->where('sex', '>', 0);
|
||
|
|
})
|
||
|
|
->select([
|
||
|
|
"sex", "user_id", "belief", "birthday", "age", "stature", "province", "city", "resident_province",
|
||
|
|
"resident_city", "degree", "graduate_school", "income", "mate_conditon", "weight", "state"
|
||
|
|
])->first();
|
||
|
|
if ($user && $user['sex'] == 1) {
|
||
|
|
$male[] = $user;
|
||
|
|
} elseif ($user && $user['sex'] == 2) {
|
||
|
|
$female[] = $user;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
Redis::set("teacher:".$merchantId.":male",json_encode($male));
|
||
|
|
Redis::set("teacher:".$merchantId.":female",json_encode($female));
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|