'integer', 'user_key' => 'string', 'user_id' => 'integer', 'mapping_time' => 'datetime', 'status' => 'integer', 'is_active' => 'integer', 'last_activity_check' => 'datetime', 'active_days_count' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime', ]; /** * 状态常量 */ const STATUS_INVALID = 0; // 无效 const STATUS_VALID = 1; // 有效 /** * 活跃状态常量 */ const ACTIVE_NO = 0; // 不活跃 const ACTIVE_YES = 1; // 活跃 /** * 状态映射 */ public static $statusMap = [ self::STATUS_INVALID => '无效', self::STATUS_VALID => '有效', ]; /** * 活跃状态映射 */ public static $activeMap = [ self::ACTIVE_NO => '不活跃', self::ACTIVE_YES => '活跃', ]; /** * 根据URS用户ID获取农场用户ID * * @param int $ursUserId URS用户ID * @return int|null 农场用户ID,如果未找到返回null */ public static function getFarmUserIdByUrsUserId(int $ursUserId): int { $mapping = self::where('urs_user_id', $ursUserId) ->where('status', self::STATUS_VALID) ->first(); return(integer)( $mapping ? $mapping->user_id : 0); } /** * 根据农场用户ID获取URS用户ID * * @param int $userId 农场用户ID * @return int|null URS用户ID,如果未找到返回null */ public static function getUrsUserIdByFarmUserId(int $userId): ?int { $mapping = self::where('user_id', $userId) ->where('status', self::STATUS_VALID) ->first(); return $mapping ? $mapping->urs_user_id : null; } /** * 批量获取URS用户ID对应的农场用户ID * * @param array $ursUserIds URS用户ID数组 * @return array 映射关系数组 [urs_user_id => user_id] */ public static function getFarmUserIdsByUrsUserIds(array $ursUserIds): array { $mappings = self::whereIn('urs_user_id', $ursUserIds) ->where('status', self::STATUS_VALID) ->pluck('user_id', 'urs_user_id') ->toArray(); return $mappings; } /** * 批量获取农场用户ID对应的URS用户ID * * @param array $userIds 农场用户ID数组 * @return array 映射关系数组 [user_id => urs_user_id] */ public static function getUrsUserIdsByFarmUserIds(array $userIds): array { $mappings = self::whereIn('user_id', $userIds) ->where('status', self::STATUS_VALID) ->pluck('urs_user_id', 'user_id') ->toArray(); return $mappings; } /** * 创建或更新用户映射关系 * * @param int $ursUserId URS用户ID * @param int $userId 农场用户ID * @return self */ public static function createOrUpdateMapping(int $ursUserId, int $userId): self { return self::updateOrCreate( ['urs_user_id' => $ursUserId], [ 'user_id' => $userId, 'mapping_time' => now(), 'status' => self::STATUS_VALID, ] ); } /** * 检查URS用户是否已进入农场 * * @param int $ursUserId URS用户ID * @return bool */ public static function hasEnteredFarm(int $ursUserId): bool { return self::where('urs_user_id', $ursUserId) ->where('status', self::STATUS_VALID) ->exists(); } /** * 获取已进入农场的URS用户ID列表 * * @param array $ursUserIds URS用户ID数组 * @return array 已进入农场的URS用户ID数组 */ public static function getEnteredFarmUrsUserIds(array $ursUserIds): array { return self::whereIn('urs_user_id', $ursUserIds) ->where('status', self::STATUS_VALID) ->pluck('urs_user_id') ->toArray(); } /** * 获取关联的农场用户 */ public function user(): BelongsTo { return $this->belongsTo(\App\Module\User\Models\User::class, 'user_id', 'id'); } /** * 检查用户是否活跃 * * @return bool */ public function isActive(): bool { return $this->is_active === self::ACTIVE_YES; } /** * 获取活跃用户列表 * * @param array $ursUserIds URS用户ID数组 * @return array 活跃的URS用户ID数组 */ public static function getActiveUrsUserIds(array $ursUserIds): array { return self::whereIn('urs_user_id', $ursUserIds) ->where('status', self::STATUS_VALID) ->where('is_active', self::ACTIVE_YES) ->pluck('urs_user_id') ->toArray(); } /** * 获取活跃用户统计 * * @return array */ public static function getActiveUserStats(): array { $total = self::where('status', self::STATUS_VALID)->count(); $active = self::where('status', self::STATUS_VALID) ->where('is_active', self::ACTIVE_YES) ->count(); return [ 'total_users' => $total, 'active_users' => $active, 'inactive_users' => $total - $active, 'active_percentage' => $total > 0 ? round($active * 100 / $total, 2) : 0, ]; } /** * 批量更新用户活跃状态 * * @param array $activeData 活跃数据 [urs_user_id => ['is_active' => 1, 'active_days_count' => 5]] * @return int 更新的记录数 */ public static function batchUpdateActiveStatus(array $activeData): int { $updated = 0; foreach ($activeData as $ursUserId => $data) { $result = self::where('urs_user_id', $ursUserId) ->where('status', self::STATUS_VALID) ->update([ 'is_active' => $data['is_active'], 'last_activity_check' => now(), 'active_days_count' => $data['active_days_count'] ?? 0, ]); $updated += $result; } return $updated; } /** * 获取需要检查活跃状态的用户 * * @param int $limit 限制数量 * @return \Illuminate\Database\Eloquent\Collection */ public static function getUsersNeedActivityCheck(int $limit = 1000): \Illuminate\Database\Eloquent\Collection { return self::where('status', self::STATUS_VALID) ->where(function($query) { $query->whereNull('last_activity_check') ->orWhere('last_activity_check', '<', now()->subDay()); }) ->with(['user', 'user.info']) // 预加载用户和用户信息关联 ->limit($limit) ->get(); } /** * 设置用户凭证 */ public function setUserKey(?string $userKey): void { $this->user_key = $userKey; } /** * 获取用户凭证 */ public function getUserKey(): ?string { return $this->user_key; } /** * 检查是否有用户凭证 */ public function hasUserKey(): bool { return !empty($this->user_key); } /** * 根据userKey查找用户映射 */ public static function findByUserKey(string $userKey): ?self { return self::where('user_key', $userKey) ->where('status', self::STATUS_VALID) ->first(); } /** * 根据userKey获取农场用户ID */ public static function getFarmUserIdByUserKey(string $userKey): ?int { $mapping = self::findByUserKey($userKey); return $mapping ? $mapping->user_id : null; } /** * 根据userKey获取URS用户ID */ public static function getUrsUserIdByUserKey(string $userKey): ?int { $mapping = self::findByUserKey($userKey); return $mapping ? $mapping->urs_user_id : null; } /** * 批量根据userKey获取映射关系 */ public static function getMappingsByUserKeys(array $userKeys): array { return self::whereIn('user_key', $userKeys) ->where('status', self::STATUS_VALID) ->get() ->keyBy('user_key') ->toArray(); } }