You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
2.9 KiB

<?php
namespace App\Services\SupplyDemand;
use App\Models\User;
use App\Models\SupplyDemand;
class Profiles
{
public static function rank($name) {
foreach (['高研班', '攀峰班', 'OPC', '初创班', '人才培训'] as $i => $term) {
if (mb_stripos((string) $name, $term) !== false) return $i;
}
return 5;
}
public static function user(?User $user, $private = false) {
if (!$user) return null;
$user->loadMissing(['courseSigns.course.typeDetail']);
$sign = $user->courseSigns->filter(function ($s) use ($user) { return (string) $s->user_id === (string) $user->id && (int) $s->status === 1 && !$s->trashed() && $s->course; })
->sort(function ($a, $b) {
$rank = self::rank(optional($a->course->typeDetail)->name) <=> self::rank(optional($b->course->typeDetail)->name);
return $rank ?: (strcmp((string) $b->created_at, (string) $a->created_at) ?: ($b->id <=> $a->id));
})->first();
$data = ['id' => $user->id, 'name' => $user->name ?: $user->nickname, 'nickname' => $user->nickname,
'headimgurl' => $user->headimgurl, 'company_name' => $user->company_name,
'course_name' => $sign ? $sign->course->name : '', 'class_name' => $sign ? ($sign->course->class ?: $sign->course->name) : ''];
if ($private) $data['mobile'] = $user->getRawOriginal('mobile');
return $data;
}
public static function publisher(SupplyDemand $d, $private = false) {
$profile = self::user($d->user, $private);
if ($profile) {
$profile['name'] = $profile['name'] ?: $d->publisher_name;
$profile['company_name'] = $profile['company_name'] ?: $d->publisher_company;
return $profile;
}
$profile = ['id' => 0, 'name' => $d->publisher_name ?: '平台代发', 'company_name' => $d->publisher_company,
'headimgurl' => '', 'course_name' => '', 'class_name' => ''];
if ($private) $profile['mobile'] = $d->publisher_mobile;
return $profile;
}
public static function demand(SupplyDemand $d, $private = false, $viewerId = null) {
$d->loadMissing('user');
$out = $d->attributesToArray();
unset($out['publisher_mobile'], $out['created_by_admin_id'], $out['import_key']);
$out['publisher'] = self::publisher($d, $private);
$out['user'] = $out['publisher'];
$out['is_managed'] = (bool) $d->is_proxy && !$d->user_id;
if (!$private && ((bool) $d->is_proxy || ((int) $viewerId !== (int) $d->user_id && (int) $d->public_way !== 1))) {
$canShow = !$d->is_proxy && (int) $d->public_way === 2 && $d->messages()->where('user_id', $viewerId)->exists();
if (!$canShow) foreach (['mobile', 'wechat', 'email'] as $key) $out[$key] = null;
}
if (!$private) unset($out['binding_note']);
return $out;
}
}