UserOInfo.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. namespace App\Module\User\Services;
  3. use App\Module\User\Dto\UserOInfoDto;
  4. use App\Module\User\Dto\UserPublicDto;
  5. use App\Module\User\Models\UserInfo;
  6. use App\Module\User\Unit;
  7. use App\Module\User\Unit\UserOInfo as UserOInfoUnit;
  8. use App\Module\User\Unit\UserPublic;
  9. use Dcore\Db\Arr;
  10. use Dcore\Exception\LogicException;
  11. class UserOInfo
  12. {
  13. static $ttl = 3600;
  14. /**
  15. * 读取单个用户O信息
  16. *
  17. * @param int $id 用户ID
  18. * @return UserOInfoDto|null 用户订单信息DTO或null
  19. */
  20. static public function oinfo($id)
  21. {
  22. $cache = self::infoCache($id);
  23. if($cache === null){
  24. /**
  25. * @var \App\Module\User\Models\UserInfo $user
  26. */
  27. $user = \App\Module\User\Models\UserInfo::query()->where('user_id', $id)->with([
  28. 'merchant',
  29. 'user_phone'
  30. ])->first();
  31. if($user){
  32. // 使用DTO而不是直接使用UserOInfo
  33. $userDto = UserOInfoDto::fromUserInfo($user);
  34. \Illuminate\Support\Facades\Cache::put(self::OInfoKey($user->user_id), $userDto, 60);
  35. return $userDto;
  36. }
  37. return null;
  38. }
  39. // 如果缓存中的是旧的UserOInfo对象,转换为DTO
  40. if ($cache instanceof Unit\UserOInfo) {
  41. $cache = UserOInfoDto::fromUserOInfo($cache);
  42. }
  43. return $cache;
  44. }
  45. /**
  46. * 数据库对象转换为DTO
  47. *
  48. * @param UserInfo $info 用户详细信息模型
  49. * @return UserOInfoDto 用户订单信息DTO
  50. * @deprecated 使用 UserOInfoDto::fromUserInfo() 代替
  51. */
  52. static protected function userInfo2Oinfo(UserInfo $info)
  53. {
  54. return UserOInfoDto::fromUserInfo($info);
  55. }
  56. /**
  57. * 获取用户订单信息
  58. *
  59. * @param array $ids 用户ID数组
  60. * @return array 用户订单信息DTO数组
  61. */
  62. static public function oinfos($ids = [])
  63. {
  64. if(empty($ids)){
  65. return [];
  66. }
  67. $ids2 = [];
  68. foreach ($ids as $id) {
  69. if(empty($id)){
  70. continue;
  71. }
  72. if(\App\Module\Sys\User::isSysUid($id)){
  73. continue;
  74. }
  75. $ids2[$id] = $id;
  76. }
  77. $res = self::infoCaches($ids2);
  78. if ($ids2) {
  79. /**
  80. * @var \App\Module\User\Models\UserInfo[] $users
  81. */
  82. $users = \App\Module\User\Models\UserInfo::query()->whereIn('user_id', $ids2)->with([
  83. 'merchant',
  84. 'user_phone'
  85. ])->get();
  86. foreach ($users as $user) {
  87. // 使用DTO而不是直接使用UserOInfo
  88. $userDto = UserOInfoDto::fromUserInfo($user);
  89. $res[$userDto->userId] = $userDto;
  90. \Illuminate\Support\Facades\Cache::put(self::OInfoKey($user->user_id), $userDto, self::$ttl);
  91. unset($ids2[$userDto->userId]);
  92. }
  93. }
  94. if (count($ids2)) {
  95. foreach ($ids2 as $id) {
  96. // 使用DTO而不是直接使用UserPublic
  97. $userDto = UserOInfoDto::createDeleted($id);
  98. $res[$userDto->userId] = $userDto;
  99. \Illuminate\Support\Facades\Cache::put(self::OInfoKey($id), $userDto, self::$ttl);
  100. unset($ids2[$userDto->userId]);
  101. }
  102. }
  103. return $res;
  104. }
  105. protected static function add()
  106. {
  107. }
  108. /**
  109. * 获取用户O信息
  110. *
  111. * @param int $id 用户ID
  112. * @return UserOInfoDto|UserPublicDto|null 用户订单信息DTO或null
  113. */
  114. static public function infoCache($id)
  115. {
  116. $c = \Illuminate\Support\Facades\Cache::get(self::OInfoKey($id));
  117. if ($c) {
  118. // 如果缓存中的是旧的UserOInfo对象,转换为DTO
  119. if ($c instanceof Unit\UserOInfo) {
  120. $c = UserOInfoDto::fromUserOInfo($c);
  121. }
  122. // 如果缓存中的是旧的UserPublic对象,转换为DTO
  123. else if ($c instanceof UserPublic) {
  124. $c = UserPublicDto::fromUserPublic($c);
  125. }
  126. return $c;
  127. }
  128. return null;
  129. }
  130. /**
  131. * 批量获取用户O信息
  132. *
  133. * @param array $ids 用户ID数组
  134. * @return array 用户订单信息DTO数组
  135. */
  136. static public function infoCaches(&$ids)
  137. {
  138. $res = [];
  139. foreach ($ids as $id) {
  140. $c = \Illuminate\Support\Facades\Cache::get(self::OInfoKey($id));
  141. if ($c) {
  142. // 如果缓存中的是旧的UserOInfo对象,转换为DTO
  143. if ($c instanceof Unit\UserOInfo) {
  144. $c = UserOInfoDto::fromUserOInfo($c);
  145. }
  146. // 如果缓存中的是旧的UserPublic对象,转换为DTO
  147. else if ($c instanceof UserPublic) {
  148. $c = UserPublicDto::fromUserPublic($c);
  149. }
  150. $res[$id] = $c;
  151. unset($ids[$id]);
  152. }
  153. }
  154. return $res;
  155. }
  156. /**
  157. * 用户公共数据缓存key
  158. *
  159. * @param $uid
  160. * @return string
  161. */
  162. static private function OInfoKey($uid)
  163. {
  164. return 'lan_usero4info_' . $uid;
  165. }
  166. }