UrsUserMapping.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. 'is_active' => self::ACTIVE_YES, // 新用户默认为活跃状态
  150. 'last_activity_check' => now(), // 设置初始检查时间
  151. 'active_days_count' => 1, // 新用户默认活跃天数为1
  152. ]
  153. );
  154. }
  155. /**
  156. * 检查URS用户是否已进入农场
  157. *
  158. * @param int $ursUserId URS用户ID
  159. * @return bool
  160. */
  161. public static function hasEnteredFarm(int $ursUserId): bool
  162. {
  163. return self::where('urs_user_id', $ursUserId)
  164. ->where('status', self::STATUS_VALID)
  165. ->exists();
  166. }
  167. /**
  168. * 获取已进入农场的URS用户ID列表
  169. *
  170. * @param array $ursUserIds URS用户ID数组
  171. * @return array 已进入农场的URS用户ID数组
  172. */
  173. public static function getEnteredFarmUrsUserIds(array $ursUserIds): array
  174. {
  175. return self::whereIn('urs_user_id', $ursUserIds)
  176. ->where('status', self::STATUS_VALID)
  177. ->pluck('urs_user_id')
  178. ->toArray();
  179. }
  180. /**
  181. * 获取关联的农场用户
  182. */
  183. public function user(): BelongsTo
  184. {
  185. return $this->belongsTo(\App\Module\User\Models\User::class, 'user_id', 'id');
  186. }
  187. /**
  188. * 检查用户是否活跃
  189. *
  190. * @return bool
  191. */
  192. public function isActive(): bool
  193. {
  194. return $this->is_active === self::ACTIVE_YES;
  195. }
  196. /**
  197. * 获取活跃用户列表
  198. *
  199. * @param array $ursUserIds URS用户ID数组
  200. * @return array 活跃的URS用户ID数组
  201. */
  202. public static function getActiveUrsUserIds(array $ursUserIds): array
  203. {
  204. return self::whereIn('urs_user_id', $ursUserIds)
  205. ->where('status', self::STATUS_VALID)
  206. ->where('is_active', self::ACTIVE_YES)
  207. ->pluck('urs_user_id')
  208. ->toArray();
  209. }
  210. /**
  211. * 获取活跃用户统计
  212. *
  213. * @return array
  214. */
  215. public static function getActiveUserStats(): array
  216. {
  217. $total = self::where('status', self::STATUS_VALID)->count();
  218. $active = self::where('status', self::STATUS_VALID)
  219. ->where('is_active', self::ACTIVE_YES)
  220. ->count();
  221. return [
  222. 'total_users' => $total,
  223. 'active_users' => $active,
  224. 'inactive_users' => $total - $active,
  225. 'active_percentage' => $total > 0 ? round($active * 100 / $total, 2) : 0,
  226. ];
  227. }
  228. /**
  229. * 批量更新用户活跃状态
  230. *
  231. * @param array $activeData 活跃数据 [urs_user_id => ['is_active' => 1, 'active_days_count' => 5]]
  232. * @return int 更新的记录数
  233. */
  234. public static function batchUpdateActiveStatus(array $activeData): int
  235. {
  236. $updated = 0;
  237. foreach ($activeData as $ursUserId => $data) {
  238. $result = self::where('urs_user_id', $ursUserId)
  239. ->where('status', self::STATUS_VALID)
  240. ->update([
  241. 'is_active' => $data['is_active'],
  242. 'last_activity_check' => now(),
  243. 'active_days_count' => $data['active_days_count'] ?? 0,
  244. ]);
  245. $updated += $result;
  246. }
  247. return $updated;
  248. }
  249. /**
  250. * 获取需要检查活跃状态的用户
  251. *
  252. * @param int $limit 限制数量
  253. * @return \Illuminate\Database\Eloquent\Collection
  254. */
  255. public static function getUsersNeedActivityCheck(int $limit = 1000): \Illuminate\Database\Eloquent\Collection
  256. {
  257. return self::where('status', self::STATUS_VALID)
  258. ->where(function($query) {
  259. $query->whereNull('last_activity_check')
  260. ->orWhere('last_activity_check', '<', now()->subDay());
  261. })
  262. ->with(['user', 'user.info']) // 预加载用户和用户信息关联
  263. ->limit($limit)
  264. ->get();
  265. }
  266. /**
  267. * 设置用户凭证
  268. */
  269. public function setUserKey(?string $userKey): void
  270. {
  271. $this->user_key = $userKey;
  272. }
  273. /**
  274. * 获取用户凭证
  275. */
  276. public function getUserKey(): ?string
  277. {
  278. return $this->user_key;
  279. }
  280. /**
  281. * 检查是否有用户凭证
  282. */
  283. public function hasUserKey(): bool
  284. {
  285. return !empty($this->user_key);
  286. }
  287. /**
  288. * 根据userKey查找用户映射
  289. */
  290. public static function findByUserKey(string $userKey): ?self
  291. {
  292. return self::where('user_key', $userKey)
  293. ->where('status', self::STATUS_VALID)
  294. ->first();
  295. }
  296. /**
  297. * 根据userKey获取农场用户ID
  298. */
  299. public static function getFarmUserIdByUserKey(string $userKey): ?int
  300. {
  301. $mapping = self::findByUserKey($userKey);
  302. return $mapping ? $mapping->user_id : null;
  303. }
  304. /**
  305. * 根据userKey获取URS用户ID
  306. */
  307. public static function getUrsUserIdByUserKey(string $userKey): ?int
  308. {
  309. $mapping = self::findByUserKey($userKey);
  310. return $mapping ? $mapping->urs_user_id : null;
  311. }
  312. /**
  313. * 批量根据userKey获取映射关系
  314. */
  315. public static function getMappingsByUserKeys(array $userKeys): array
  316. {
  317. return self::whereIn('user_key', $userKeys)
  318. ->where('status', self::STATUS_VALID)
  319. ->get()
  320. ->keyBy('user_key')
  321. ->toArray();
  322. }
  323. }