'integer', 'report_to' => 'integer', 'sex' => 'integer', 'status' => 'integer', 'user_type' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime', ]; protected $hidden = [ 'password', ]; /** * 关联公司 */ public function company() { return $this->belongsTo(Company::class, 'company_id'); } /** * 上级用户 */ public function parent() { return $this->belongsTo(User::class, 'report_to'); } /** * 下级用户 */ public function children() { return $this->hasMany(User::class, 'report_to'); } /** * 用户飞书应用绑定 */ public function feishuApps() { return $this->hasMany(UserFeishuApp::class, 'user_id'); } /** * 最后登录的飞书应用 */ public function lastFeishuApp() { return $this->belongsTo(CompanyFeishuApp::class, 'feishu_app_id'); } /** * 获取用户在当前公司的默认飞书应用 */ public function getDefaultFeishuApp() { return $this->hasOne(UserFeishuApp::class, 'user_id') ->where('company_id', $this->company_id) ->where('is_default', 1); } /** * 获取用户在指定飞书应用下的绑定信息 */ public function feishuAppBinding(int $feishuAppId) { return $this->hasOne(UserFeishuApp::class, 'user_id') ->where('company_id', $this->company_id) ->where('feishu_app_id', $feishuAppId); } /** * 获取用户在指定飞书应用下的 feishu_user_id */ public function getFeishuUserId(int $feishuAppId): ?string { $binding = $this->feishuAppBinding($feishuAppId)->first(); return $binding ? $binding->feishu_user_id : null; } /** * 获取用户在最后登录应用下的 feishu_user_id */ public function getLastFeishuUserId(): ?string { if (!$this->feishu_app_id) { return null; } return $this->getFeishuUserId($this->feishu_app_id); } /** * 获取启用的用户 */ public function scopeActive($query) { return $query->where('status', 1); } /** * 根据公司ID查找 */ public function scopeByCompany($query, int $companyId) { return $query->where('company_id', $companyId); } /** * 根据飞书用户ID查找 * @deprecated 请通过 UserFeishuApp 模型查询 */ public function scopeByFeishuUserId($query, ?string $feishuUserId) { if ($feishuUserId === null) { return $query; } return $query->where('feishu_user_id', $feishuUserId); } /** * 根据飞书 UnionID 查找 * union_id 用于跨应用识别用户,保留此方法 */ public function scopeByFeishuUnionId($query, ?string $feishuUnionId) { if ($feishuUnionId === null) { return $query; } return $query->where('union_id', $feishuUnionId); } /** * 根据OpenID查找 * @deprecated 请通过 UserFeishuApp 模型查询 */ public function scopeByFeishuOpenId($query, ?string $openId) { if ($openId === null) { return $query; } return $query->where('feishu_open_id', $openId); } /** * 搜索用户 */ public function scopeSearch($query, ?string $keyword) { if (!$keyword) { return $query; } return $query->where(function ($q) use ($keyword) { $q->where('name', 'like', "%{$keyword}%") ->orWhere('email', 'like', "%{$keyword}%") ->orWhere('phone', 'like', "%{$keyword}%") ->orWhere('sn', 'like', "%{$keyword}%"); }); } }