'integer', 'company_id' => 'integer', 'status' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime', ]; /** * 关联用户 */ public function user() { return $this->belongsTo(User::class, 'user_id'); } /** * 关联公司 */ public function company() { return $this->belongsTo(Company::class, 'company_id'); } /** * 获取有效的映射 */ public function scopeActive($query) { return $query->where('status', 1); } /** * 根据系统代码查找 */ public function scopeBySystem($query, string $systemCode) { return $query->where('system_code', $systemCode); } /** * 根据公司ID查找 */ public function scopeByCompany($query, int $companyId) { return $query->where('company_id', $companyId); } /** * 根据外部系统用户ID查找 */ public function scopeBySystemUserId($query, string $systemUserId, string $systemCode = 'fdesk') { return $query->where('system_code', $systemCode) ->where('system_user_id', $systemUserId); } /** * 获取用户在指定系统的映射 */ public function scopeUserMapping($query, int $userId, int $companyId, string $systemCode = 'fdesk') { return $query->where('user_id', $userId) ->where('company_id', $companyId) ->where('system_code', $systemCode); } }