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.

226 lines
6.4 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace App\Models;
use DateTimeInterface;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Carbon;
use Laravel\Sanctum\HasApiTokens;
use OwenIt\Auditing\Contracts\Auditable;
use Illuminate\Database\Eloquent\SoftDeletes;
class User extends Authenticatable implements Auditable
{
use HasApiTokens, HasFactory, Notifiable;
use \OwenIt\Auditing\Auditable;
use SoftDeletes;
protected $fillable = [
'remember_token',
'created_at',
'updated_at',
'nickname',
'openid',
'country',
'province',
'city',
'headimgurl',
'username',
'password',
'name',
'sex',
'birthday',
'mobile',
'idcard',
'education',
'company_name',
'company_position',
'company_has_share',
'type',
'company_type',
'company_fund',
'company_area',
'company_address',
'company_industry',
'company_product',
'school',
'speciality',
'overseas_experience',
'sign_from',
'email',
'sales_volume',
'valuation',
'market_value',
'is_yuanhe',
'plate',
'introduce',
'honour',
'company_need_fund',
'company_other',
'remark',
'is_import',
'is_vip',
'is_schoolmate',
'appointment_total',
'letter',
'code',
'score',
'pid',
'company_introduce',
'company_date',
'deleted_at',
'no',
];
protected $appends = ['is_vip_text', 'is_schoolmate_text', 'appointment_total', 'name'];
// 更新时候覆盖更新的字段
public static $coverFields = [
'name', 'sex', 'mobile', 'company_has_share', 'company_name', 'school', 'speciality',
'birthday', 'mobile', 'idcard', 'education', 'company_date', 'sales_volume', 'valuation',
'market_value', 'is_yuanhe', 'username',
'company_position', 'company_need_fund', 'company_address', 'company_area',
'course_id', 'status', 'fee_status', 'is_vip'
];
public function getNameAttribute($value)
{
return $this->username;
}
public function getAppointmentTotalAttribute($value)
{
$now = date('Y-m-d H:i:s');
$courseAppointmentTotals = CourseAppointmentTotal::where('user_id', $this->id)
//->where('start_time', '<=', $now)
->where('end_time', '>=', $now)
->sum('total');
return $courseAppointmentTotals;
}
protected function serializeDate(DateTimeInterface $date)
{
return $date->format(Carbon::parse($date)->toDateTimeString());
}
public function getIsVipTextAttribute($value)
{
return self::$intToString['is_vip'][$this->is_vip] ?? '';
}
public function getIsSchoolmateTextAttribute($value)
{
return self::$intToString['is_schoolmate'][$this->is_schoolmate] ?? '';
}
/**
* 数据类型转换,数字转字符串。
* 值是id则是数据字典的顶级id或者是枚举型数组
* @var array[]
*/
public static $intToString = [
'company_position' => 1,
'company_area' => 1,
'company_industry' => 1,
'company_type' => 1,
'is_vip' => ['普通学员', 'VIP学员'],
'is_schoolmate' => ['否', '是'],
];
public function courses()
{
return $this->belongsToMany(Course::class, 'course_signs', 'user_id', 'course_id');
}
public function courseSigns()
{
return $this->hasMany(CourseSign::class, 'user_id', 'id');
}
public function companyAreaDetail()
{
return $this->hasOne(ParameterDetail::class, 'id', 'company_area');
}
public function companyPositionDetail()
{
return $this->hasOne(ParameterDetail::class, 'id', 'company_position');
}
public function companyIndustryDetail()
{
return $this->hasOne(ParameterDetail::class, 'id', 'company_industry');
}
public function companyTypeDetail()
{
return $this->hasOne(ParameterDetail::class, 'id', 'company_type');
}
public function appointments()
{
return $this->hasMany(Appointment::class, 'user_id', 'id');
}
/**
* 获取预约剩余次数
*/
public static function getHasAppointment($userId)
{
$user = self::find($userId);
// 已预约次数 todo::已使用的次数应该存在有效期统计
$useTotal = Appointment::where('user_id', $userId)
->where('status', 1)
->count();
return $user->appointment_total - $useTotal >= 0 ? $user->appointment_total - $useTotal : 0;
}
/**
* 更新用户编号
*/
public static function updateNo($userId)
{
// todo::编号可能回重复,还需要详细排查
$user = self::find($userId);
if (!empty($user->no)) {
return false;
}
// 获取最早一条审核通过的报名数据
$courseSigns = CourseSign::with('course')
->where('user_id', $userId)
->where('status', 1)
->orderBy('created_at', 'asc')
->first();
if (empty($courseSigns)) {
return false;
}
if (empty($courseSigns->course->start_date)) {
return false;
}
// 编号前缀
$prefix = date('Ymd', strtotime($courseSigns->course->start_date));
// 获取同一天开始的所有课程
$course = Course::where('start_date', $courseSigns->course->start_date)->orderBy('created_at', 'asc')->get();
// 获取同一天开始所有课程的报名信息
$courseSignsList = CourseSign::whereIn('id', function ($query) use ($course) {
$query->from('course_signs')
->where('status', 1)
->whereIn('course_id', $course->pluck('id'))
->selectRaw('MIN(id)')
->groupBy('user_id');
})->orderBy('created_at', 'asc')->get();
// 获取当前用户id在$courseSigns中第几位
$index = $courseSignsList->search(function ($item) use ($user) {
return $item->user_id == $user->id;
});
$no = $prefix . str_pad($index + 1, 3, '0', STR_PAD_LEFT);
$user->no = $no;
$user->save();
return $user->no;
}
}