UrsUserMapping.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. <?php
  2. namespace App\Module\UrsPromotion\Models;
  3. use UCore\ModelCore;
  4. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  5. /**
  6. * URS用户与农场用户关系映射模型
  7. *
  8. * field start
  9. * @property int $id 主键ID
  10. * @property int $urs_user_id URS用户ID
  11. * @property string $user_key URS用户凭证(userKey)
  12. * @property int $user_id 农场用户ID
  13. * @property \Carbon\Carbon $mapping_time 映射建立时间(用户进入农场时间)
  14. * @property int $status 状态:1有效,0无效
  15. * @property \Carbon\Carbon $created_at 创建时间
  16. * @property \Carbon\Carbon $updated_at 更新时间
  17. * field end
  18. */
  19. class UrsUserMapping extends ModelCore
  20. {
  21. /**
  22. * 数据表名
  23. */
  24. protected $table = 'urs_promotion_user_mappings';
  25. /**
  26. * 可批量赋值的属性
  27. */
  28. protected $fillable = [
  29. 'urs_user_id',
  30. 'user_key',
  31. 'user_id',
  32. 'mapping_time',
  33. 'status',
  34. 'is_active',
  35. 'last_activity_check',
  36. 'active_days_count',
  37. ];
  38. /**
  39. * 属性类型转换
  40. */
  41. protected $casts = [
  42. 'urs_user_id' => 'integer',
  43. 'user_key' => 'string',
  44. 'user_id' => 'integer',
  45. 'mapping_time' => 'datetime',
  46. 'status' => 'integer',
  47. 'is_active' => 'integer',
  48. 'last_activity_check' => 'datetime',
  49. 'active_days_count' => 'integer',
  50. 'created_at' => 'datetime',
  51. 'updated_at' => 'datetime',
  52. ];
  53. /**
  54. * 状态常量
  55. */
  56. const STATUS_INVALID = 0; // 无效
  57. const STATUS_VALID = 1; // 有效
  58. /**
  59. * 活跃状态常量
  60. */
  61. const ACTIVE_NO = 0; // 不活跃
  62. const ACTIVE_YES = 1; // 活跃
  63. /**
  64. * 状态映射
  65. */
  66. public static $statusMap = [
  67. self::STATUS_INVALID => '无效',
  68. self::STATUS_VALID => '有效',
  69. ];
  70. /**
  71. * 活跃状态映射
  72. */
  73. public static $activeMap = [
  74. self::ACTIVE_NO => '不活跃',
  75. self::ACTIVE_YES => '活跃',
  76. ];
  77. /**
  78. * 根据URS用户ID获取农场用户ID
  79. *
  80. * @param int $ursUserId URS用户ID
  81. * @return int|null 农场用户ID,如果未找到返回null
  82. */
  83. public static function getFarmUserIdByUrsUserId(int $ursUserId): int
  84. {
  85. $mapping = self::where('urs_user_id', $ursUserId)
  86. ->where('status', self::STATUS_VALID)
  87. ->first();
  88. return(integer)( $mapping ? $mapping->user_id : 0);
  89. }
  90. /**
  91. * 根据农场用户ID获取URS用户ID
  92. *
  93. * @param int $userId 农场用户ID
  94. * @return int|null URS用户ID,如果未找到返回null
  95. */
  96. public static function getUrsUserIdByFarmUserId(int $userId): ?int
  97. {
  98. $mapping = self::where('user_id', $userId)
  99. ->where('status', self::STATUS_VALID)
  100. ->first();
  101. return $mapping ? $mapping->urs_user_id : null;
  102. }
  103. /**
  104. * 批量获取URS用户ID对应的农场用户ID
  105. *
  106. * @param array $ursUserIds URS用户ID数组
  107. * @return array 映射关系数组 [urs_user_id => user_id]
  108. */
  109. public static function getFarmUserIdsByUrsUserIds(array $ursUserIds): array
  110. {
  111. $mappings = self::whereIn('urs_user_id', $ursUserIds)
  112. ->where('status', self::STATUS_VALID)
  113. ->pluck('user_id', 'urs_user_id')
  114. ->toArray();
  115. return $mappings;
  116. }
  117. /**
  118. * 批量获取农场用户ID对应的URS用户ID
  119. *
  120. * @param array $userIds 农场用户ID数组
  121. * @return array 映射关系数组 [user_id => urs_user_id]
  122. */
  123. public static function getUrsUserIdsByFarmUserIds(array $userIds): array
  124. {
  125. $mappings = self::whereIn('user_id', $userIds)
  126. ->where('status', self::STATUS_VALID)
  127. ->pluck('urs_user_id', 'user_id')
  128. ->toArray();
  129. return $mappings;
  130. }
  131. /**
  132. * 创建或更新用户映射关系
  133. *
  134. * @param int $ursUserId URS用户ID
  135. * @param int $userId 农场用户ID
  136. * @return self
  137. */
  138. public static function createOrUpdateMapping(int $ursUserId, int $userId): self
  139. {
  140. return self::updateOrCreate(
  141. ['urs_user_id' => $ursUserId],
  142. [
  143. 'user_id' => $userId,
  144. 'mapping_time' => now(),
  145. 'status' => self::STATUS_VALID,
  146. ]
  147. );
  148. }
  149. /**
  150. * 检查URS用户是否已进入农场
  151. *
  152. * @param int $ursUserId URS用户ID
  153. * @return bool
  154. */
  155. public static function hasEnteredFarm(int $ursUserId): bool
  156. {
  157. return self::where('urs_user_id', $ursUserId)
  158. ->where('status', self::STATUS_VALID)
  159. ->exists();
  160. }
  161. /**
  162. * 获取已进入农场的URS用户ID列表
  163. *
  164. * @param array $ursUserIds URS用户ID数组
  165. * @return array 已进入农场的URS用户ID数组
  166. */
  167. public static function getEnteredFarmUrsUserIds(array $ursUserIds): array
  168. {
  169. return self::whereIn('urs_user_id', $ursUserIds)
  170. ->where('status', self::STATUS_VALID)
  171. ->pluck('urs_user_id')
  172. ->toArray();
  173. }
  174. /**
  175. * 获取关联的农场用户
  176. */
  177. public function user(): BelongsTo
  178. {
  179. return $this->belongsTo(\App\Module\User\Models\User::class, 'user_id', 'id');
  180. }
  181. /**
  182. * 检查用户是否活跃
  183. *
  184. * @return bool
  185. */
  186. public function isActive(): bool
  187. {
  188. return $this->is_active === self::ACTIVE_YES;
  189. }
  190. /**
  191. * 获取活跃用户列表
  192. *
  193. * @param array $ursUserIds URS用户ID数组
  194. * @return array 活跃的URS用户ID数组
  195. */
  196. public static function getActiveUrsUserIds(array $ursUserIds): array
  197. {
  198. return self::whereIn('urs_user_id', $ursUserIds)
  199. ->where('status', self::STATUS_VALID)
  200. ->where('is_active', self::ACTIVE_YES)
  201. ->pluck('urs_user_id')
  202. ->toArray();
  203. }
  204. /**
  205. * 获取活跃用户统计
  206. *
  207. * @return array
  208. */
  209. public static function getActiveUserStats(): array
  210. {
  211. $total = self::where('status', self::STATUS_VALID)->count();
  212. $active = self::where('status', self::STATUS_VALID)
  213. ->where('is_active', self::ACTIVE_YES)
  214. ->count();
  215. return [
  216. 'total_users' => $total,
  217. 'active_users' => $active,
  218. 'inactive_users' => $total - $active,
  219. 'active_percentage' => $total > 0 ? round($active * 100 / $total, 2) : 0,
  220. ];
  221. }
  222. /**
  223. * 批量更新用户活跃状态
  224. *
  225. * @param array $activeData 活跃数据 [urs_user_id => ['is_active' => 1, 'active_days_count' => 5]]
  226. * @return int 更新的记录数
  227. */
  228. public static function batchUpdateActiveStatus(array $activeData): int
  229. {
  230. $updated = 0;
  231. foreach ($activeData as $ursUserId => $data) {
  232. $result = self::where('urs_user_id', $ursUserId)
  233. ->where('status', self::STATUS_VALID)
  234. ->update([
  235. 'is_active' => $data['is_active'],
  236. 'last_activity_check' => now(),
  237. 'active_days_count' => $data['active_days_count'] ?? 0,
  238. ]);
  239. $updated += $result;
  240. }
  241. return $updated;
  242. }
  243. /**
  244. * 获取需要检查活跃状态的用户
  245. *
  246. * @param int $limit 限制数量
  247. * @return \Illuminate\Database\Eloquent\Collection
  248. */
  249. public static function getUsersNeedActivityCheck(int $limit = 1000): \Illuminate\Database\Eloquent\Collection
  250. {
  251. return self::where('status', self::STATUS_VALID)
  252. ->where(function($query) {
  253. $query->whereNull('last_activity_check')
  254. ->orWhere('last_activity_check', '<', now()->subDay());
  255. })
  256. ->with('user')
  257. ->limit($limit)
  258. ->get();
  259. }
  260. /**
  261. * 设置用户凭证
  262. */
  263. public function setUserKey(?string $userKey): void
  264. {
  265. $this->user_key = $userKey;
  266. }
  267. /**
  268. * 获取用户凭证
  269. */
  270. public function getUserKey(): ?string
  271. {
  272. return $this->user_key;
  273. }
  274. /**
  275. * 检查是否有用户凭证
  276. */
  277. public function hasUserKey(): bool
  278. {
  279. return !empty($this->user_key);
  280. }
  281. /**
  282. * 根据userKey查找用户映射
  283. */
  284. public static function findByUserKey(string $userKey): ?self
  285. {
  286. return self::where('user_key', $userKey)
  287. ->where('status', self::STATUS_VALID)
  288. ->first();
  289. }
  290. /**
  291. * 根据userKey获取农场用户ID
  292. */
  293. public static function getFarmUserIdByUserKey(string $userKey): ?int
  294. {
  295. $mapping = self::findByUserKey($userKey);
  296. return $mapping ? $mapping->user_id : null;
  297. }
  298. /**
  299. * 根据userKey获取URS用户ID
  300. */
  301. public static function getUrsUserIdByUserKey(string $userKey): ?int
  302. {
  303. $mapping = self::findByUserKey($userKey);
  304. return $mapping ? $mapping->urs_user_id : null;
  305. }
  306. /**
  307. * 批量根据userKey获取映射关系
  308. */
  309. public static function getMappingsByUserKeys(array $userKeys): array
  310. {
  311. return self::whereIn('user_key', $userKeys)
  312. ->where('status', self::STATUS_VALID)
  313. ->get()
  314. ->keyBy('user_key')
  315. ->toArray();
  316. }
  317. }