| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357 |
- <?php
- namespace App\Module\UrsPromotion\Models;
- use UCore\ModelCore;
- use Illuminate\Database\Eloquent\Relations\BelongsTo;
- /**
- * URS用户与农场用户关系映射模型
- *
- * field start
- * @property int $id 主键ID
- * @property int $urs_user_id URS用户ID
- * @property string $user_key URS用户凭证(userKey)
- * @property int $user_id 农场用户ID
- * @property \Carbon\Carbon $mapping_time 映射建立时间(用户进入农场时间)
- * @property int $status 状态:1有效,0无效
- * @property int $is_active 是否活跃:1活跃,0不活跃
- * @property \Carbon\Carbon $last_activity_check 最后活跃检查时间
- * @property int $active_days_count 活跃天数统计
- * @property \Carbon\Carbon $created_at 创建时间
- * @property \Carbon\Carbon $updated_at 更新时间
- * field end
- */
- class UrsUserMapping extends ModelCore
- {
- /**
- * 数据表名
- */
- protected $table = 'urs_promotion_user_mappings';
- /**
- * 可批量赋值的属性
- */
- protected $fillable = [
- 'urs_user_id',
- 'user_key',
- 'user_id',
- 'mapping_time',
- 'status',
- 'is_active',
- 'last_activity_check',
- 'active_days_count',
- ];
- /**
- * 属性类型转换
- */
- protected $casts = [
- 'urs_user_id' => '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();
- }
- }
|