1, 'company_area' => 1, 'company_industry' => 1, 'company_type' => 1, 'is_vip' => ['普通学员', 'VIP学员'], 'is_schoolmate' => ['否', '是'], ]; public static function hasCompleteBirthday(?string $birthday): bool { if (empty($birthday) || !preg_match('/^\d{4}-\d{2}-\d{2}$/', $birthday)) { return false; } [$year, $month, $day] = array_map('intval', explode('-', $birthday)); return checkdate($month, $day, $year); } public static function isBirthdayToday(?string $birthday): bool { if (!self::hasCompleteBirthday($birthday)) { return false; } return substr($birthday, 5, 5) === date('m-d'); } public function getMobileAttribute($value) { // 如果url中包含admin字符串,则所有的手机号显示中间4位星号代替 if (strpos(request()->url(), 'admin') !== false && env('APP_ENV') == 'local') { return substr_replace($value, '****', 3, 4); } return $value; } 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] ?? '否'; } /** * 获取是否校友企业(数字:0否1是) * 查询学员对应的企业下的用户,是否包含校友 */ public function getIsCompanySchoolmateAttribute($value) { // 如果学员没有关联企业,返回否 if (empty($this->company_id)) { return 0; } // 查询该企业下是否有校友(is_schoolmate = 1) $hasSchoolmate = self::where('company_id', $this->company_id) ->where('is_schoolmate', 1) ->exists(); return $hasSchoolmate ? 1 : 0; } /** * 获取是否校友企业(文本:是/否) */ public function getIsCompanySchoolmateTextAttribute($value) { return $this->is_company_schoolmate == 1 ? '是' : '否'; } 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 function company() { return $this->hasOne(Company::class, 'id', 'company_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; } /** * 批量将用户更新为校友 * @param array $userIds 用户ID数组 * @param string $courseStartDate 课程开课时间(用于设置 schoolmate_time) * @return int 更新的用户数量 */ public static function batchUpdateToSchoolmate(array $userIds, string $courseStartDate) { if (empty($userIds)) { return 0; } // 将课程开课时间转换为 datetime 格式 $schoolmateTime = Carbon::parse($courseStartDate)->format('Y-m-d H:i:s'); // 批量更新:只更新还不是校友的学员;从 非校友→校友 时使用课程开课时间作为 schoolmate_time return self::whereIn('id', $userIds) ->whereRaw('COALESCE(is_schoolmate, 0) != 1') ->update([ 'is_schoolmate' => 1, 'schoolmate_time' => $schoolmateTime ]); } }