UrsUserMappingService.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. namespace App\Module\UrsPromotion\Services;
  3. use App\Module\UrsPromotion\Dtos\UrsUserMappingDto;
  4. use App\Module\UrsPromotion\Models\UrsUserMapping;
  5. use Illuminate\Support\Facades\DB;
  6. use Illuminate\Support\Facades\Log;
  7. /**
  8. * URS用户映射服务
  9. *
  10. * 管理URS用户ID与农场用户ID的映射关系
  11. */
  12. class UrsUserMappingService
  13. {
  14. /**
  15. * 创建用户映射关系(用户进入农场时调用)
  16. *
  17. * @param int $ursUserId URS用户ID
  18. * @param int $farmUserId 农场用户ID
  19. * @return UrsUserMappingDto
  20. * @throws \Exception
  21. */
  22. public static function createMapping(int $ursUserId, int $farmUserId): UrsUserMappingDto
  23. {
  24. try {
  25. DB::beginTransaction();
  26. // 检查是否已存在映射
  27. $existingMapping = UrsUserMapping::where('urs_user_id', $ursUserId)->first();
  28. if ($existingMapping) {
  29. if ($existingMapping->user_id === $farmUserId) {
  30. // 相同映射,直接返回
  31. DB::rollBack();
  32. return UrsUserMappingDto::fromModel($existingMapping);
  33. } else {
  34. // 不同映射,抛出异常
  35. throw new \Exception("URS用户ID {$ursUserId} 已映射到农场用户ID {$existingMapping->user_id}");
  36. }
  37. }
  38. // 检查农场用户ID是否已被映射
  39. $existingFarmMapping = UrsUserMapping::where('user_id', $farmUserId)->first();
  40. if ($existingFarmMapping) {
  41. throw new \Exception("农场用户ID {$farmUserId} 已映射到URS用户ID {$existingFarmMapping->urs_user_id}");
  42. }
  43. // 创建新映射
  44. $mapping = UrsUserMapping::create([
  45. 'urs_user_id' => $ursUserId,
  46. 'user_id' => $farmUserId,
  47. 'mapping_time' => now(),
  48. 'status' => UrsUserMapping::STATUS_VALID,
  49. ]);
  50. DB::commit();
  51. Log::info('URS用户映射创建成功', [
  52. 'urs_user_id' => $ursUserId,
  53. 'farm_user_id' => $farmUserId,
  54. 'mapping_id' => $mapping->id
  55. ]);
  56. return UrsUserMappingDto::fromModel($mapping);
  57. } catch (\Exception $e) {
  58. DB::rollBack();
  59. Log::error('URS用户映射创建失败', [
  60. 'urs_user_id' => $ursUserId,
  61. 'farm_user_id' => $farmUserId,
  62. 'error' => $e->getMessage()
  63. ]);
  64. throw $e;
  65. }
  66. }
  67. /**
  68. * 根据URS用户ID获取农场用户ID
  69. *
  70. * @param int $ursUserId URS用户ID
  71. * @return int|null 农场用户ID,如果未找到返回null
  72. */
  73. public static function getFarmUserId(int $ursUserId): ?int
  74. {
  75. return UrsUserMapping::getFarmUserIdByUrsUserId($ursUserId);
  76. }
  77. /**
  78. * 根据农场用户ID获取URS用户ID
  79. *
  80. * @param int $farmUserId 农场用户ID
  81. * @return int|null URS用户ID,如果未找到返回null
  82. */
  83. public static function getUrsUserId(int $farmUserId): ?int
  84. {
  85. return UrsUserMapping::getUrsUserIdByFarmUserId($farmUserId);
  86. }
  87. /**
  88. * 批量获取URS用户ID对应的农场用户ID
  89. *
  90. * @param array $ursUserIds URS用户ID数组
  91. * @return array 映射关系数组 [urs_user_id => farm_user_id]
  92. */
  93. public static function batchGetFarmUserIds(array $ursUserIds): array
  94. {
  95. return UrsUserMapping::getFarmUserIdsByUrsUserIds($ursUserIds);
  96. }
  97. /**
  98. * 批量获取农场用户ID对应的URS用户ID
  99. *
  100. * @param array $farmUserIds 农场用户ID数组
  101. * @return array 映射关系数组 [farm_user_id => urs_user_id]
  102. */
  103. public static function batchGetUrsUserIds(array $farmUserIds): array
  104. {
  105. return UrsUserMapping::getUrsUserIdsByFarmUserIds($farmUserIds);
  106. }
  107. /**
  108. * 检查URS用户是否已进入农场
  109. *
  110. * @param int $ursUserId URS用户ID
  111. * @return bool
  112. */
  113. public static function hasEnteredFarm(int $ursUserId): bool
  114. {
  115. return UrsUserMapping::hasEnteredFarm($ursUserId);
  116. }
  117. /**
  118. * 获取已进入农场的URS用户ID列表
  119. *
  120. * @param array $ursUserIds URS用户ID数组
  121. * @return array 已进入农场的URS用户ID数组
  122. */
  123. public static function getEnteredFarmUsers(array $ursUserIds): array
  124. {
  125. return UrsUserMapping::getEnteredFarmUrsUserIds($ursUserIds);
  126. }
  127. /**
  128. * 禁用用户映射关系
  129. *
  130. * @param int $ursUserId URS用户ID
  131. * @return bool
  132. */
  133. public static function disableMapping(int $ursUserId): bool
  134. {
  135. try {
  136. $mapping = UrsUserMapping::where('urs_user_id', $ursUserId)->first();
  137. if (!$mapping) {
  138. return false;
  139. }
  140. $mapping->status = UrsUserMapping::STATUS_INVALID;
  141. $mapping->save();
  142. Log::info('URS用户映射已禁用', [
  143. 'urs_user_id' => $ursUserId,
  144. 'mapping_id' => $mapping->id
  145. ]);
  146. return true;
  147. } catch (\Exception $e) {
  148. Log::error('URS用户映射禁用失败', [
  149. 'urs_user_id' => $ursUserId,
  150. 'error' => $e->getMessage()
  151. ]);
  152. return false;
  153. }
  154. }
  155. /**
  156. * 启用用户映射关系
  157. *
  158. * @param int $ursUserId URS用户ID
  159. * @return bool
  160. */
  161. public static function enableMapping(int $ursUserId): bool
  162. {
  163. try {
  164. $mapping = UrsUserMapping::where('urs_user_id', $ursUserId)->first();
  165. if (!$mapping) {
  166. return false;
  167. }
  168. $mapping->status = UrsUserMapping::STATUS_VALID;
  169. $mapping->save();
  170. Log::info('URS用户映射已启用', [
  171. 'urs_user_id' => $ursUserId,
  172. 'mapping_id' => $mapping->id
  173. ]);
  174. return true;
  175. } catch (\Exception $e) {
  176. Log::error('URS用户映射启用失败', [
  177. 'urs_user_id' => $ursUserId,
  178. 'error' => $e->getMessage()
  179. ]);
  180. return false;
  181. }
  182. }
  183. /**
  184. * 获取映射统计信息
  185. *
  186. * @return array
  187. */
  188. public static function getMappingStats(): array
  189. {
  190. return [
  191. 'total_mappings' => UrsUserMapping::count(),
  192. 'valid_mappings' => UrsUserMapping::where('status', UrsUserMapping::STATUS_VALID)->count(),
  193. 'invalid_mappings' => UrsUserMapping::where('status', UrsUserMapping::STATUS_INVALID)->count(),
  194. 'today_mappings' => UrsUserMapping::whereDate('created_at', today())->count(),
  195. ];
  196. }
  197. /**
  198. * 获取用户映射详情
  199. *
  200. * @param int $ursUserId URS用户ID
  201. * @return UrsUserMappingDto|null
  202. */
  203. public static function getMappingDetail(int $ursUserId): ?UrsUserMappingDto
  204. {
  205. $mapping = UrsUserMapping::where('urs_user_id', $ursUserId)->first();
  206. if (!$mapping) {
  207. return null;
  208. }
  209. return UrsUserMappingDto::fromModel($mapping);
  210. }
  211. }