UrsUserMapping.php 9.5 KB

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