SiteUser.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. namespace App\Modules\User\Services;
  3. use App\Modules\Channel\Models\Channel;
  4. use App\Modules\Subscribe\Models\BookOrder;
  5. use App\Modules\Subscribe\Models\YearOrder;
  6. use App\Modules\User\Models\User;
  7. use DB;
  8. /**
  9. * 站点用户
  10. */
  11. class SiteUser
  12. {
  13. private $users;
  14. private $openid;
  15. private $channel_id;
  16. private $user;
  17. public function __construct($openid)
  18. {
  19. $this->openid = $openid;
  20. $this->users = [];
  21. }
  22. /**
  23. * 获取user
  24. */
  25. public function getChannelUser()
  26. {
  27. $funcs = [
  28. 'getUserFromLinkData', 'getUsersOnlyChannel',
  29. 'getUserFromYearOrder', 'getUserFromChapterOrderBeforeThreeDays',
  30. 'getUserFromChapterOrderLastThreeDays', 'getUserFromLast'
  31. ];
  32. foreach ($funcs as $func) {
  33. if (!$this->channel_id) {
  34. $this->$func();
  35. }
  36. }
  37. if ($this->channel_id) {
  38. return $this->user;
  39. } else {
  40. return [0, 0];
  41. }
  42. }
  43. /**
  44. * 已绑定关系
  45. */
  46. private function getUserFromLinkData()
  47. {
  48. $friend_link_uid_bind = DB::table('friend_link_uid_bind')->where('openid', $this->openid)->orderBy('id', 'desc')->first();
  49. if ($friend_link_uid_bind) {
  50. $user = User::where('id', $friend_link_uid_bind->uid)->select('id', 'distribution_channel_id')->first();
  51. if ($user) {
  52. $this->channel_id = $user->distribution_channel_id;
  53. $this->user = [$user->id, $this->channel_id];
  54. }
  55. }
  56. }
  57. /**
  58. * 唯一渠道
  59. */
  60. private function getUsersOnlyChannel()
  61. {
  62. $users = User::where('openid', $this->openid)->select('id', 'distribution_channel_id')->get()->all();
  63. $inner_channels = Channel::join('channel_users', 'channel_users.id', '=', 'distribution_channels.channel_user_id')
  64. ->select('distribution_channels.id')
  65. ->whereIn('channel_users.id', explode(',', redisEnv('PROMOTION_GROUP_CHANNEL_USER_ID')))
  66. ->get()
  67. ->pluck('id')
  68. ->toArray();
  69. foreach ($users as $user) {
  70. if (in_array($user->distribution_channel_id, $inner_channels)) {
  71. $this->users[] = $user;
  72. }
  73. }
  74. if (!$this->users) {
  75. $this->users = $users ?? [];
  76. }
  77. if (count($this->users) == 1) {
  78. $this->setUserLink($this->users[0]->id);
  79. $this->channel_id = $this->users[0]->distribution_channel_id;
  80. }
  81. }
  82. /**
  83. * 有包年的,按最新的包年记录渠道
  84. */
  85. private function getUserFromYearOrder()
  86. {
  87. $users = $this->users;
  88. $year_order = YearOrder::whereIn('uid', collect($users)->pluck('id')->all())->orderBy('end_time', 'desc')->first();
  89. if ($year_order) {
  90. //筛选包年结束时间最后的UID
  91. foreach ($users as $item) {
  92. if ($item->id == $year_order->uid) {
  93. $this->setUserLink($item->id);
  94. $this->channel_id = $item->distribution_channel_id;
  95. }
  96. }
  97. }
  98. }
  99. /**
  100. * 有订阅记录的3天的历史纪录,按最多的订阅渠道
  101. */
  102. private function getUserFromChapterOrderBeforeThreeDays()
  103. {
  104. $users_select = ['id' => 0, 'count' => 0, 'user' => null, 'create_time' => 0];
  105. foreach ($this->users as $user) {
  106. $table_prefix = ($user->id) % 512;
  107. $chapter_count = DB::connection('chapter_order_mysql')
  108. ->table('chapter_orders' . $table_prefix)
  109. ->where('created_at', '>=', date('Y-m-d H:i:s', strtotime('-3 day')))
  110. ->where('uid', $user->id)
  111. ->count('id');
  112. $book_order = BookOrder::where('uid', $user->id)
  113. ->where('created_at', '>=', date('Y-m-d H:i:s', strtotime('-3 day')))
  114. ->first();
  115. if ($book_order) {
  116. $chapter_count += 30;
  117. }
  118. if ($chapter_count > $users_select['count']) {
  119. $users_select['id'] = $user->id;
  120. $users_select['count'] = $chapter_count;
  121. $users_select['user'] = $user;
  122. }
  123. }
  124. if ($users_select['id'] > 0) {
  125. $this->setUserLink($users_select['id']);
  126. $this->channel_id = $users_select['user']->distribution_channel_id;
  127. }
  128. }
  129. /**
  130. * 有订阅记录的最近3天,按最新的订阅渠道
  131. */
  132. private function getUserFromChapterOrderLastThreeDays()
  133. {
  134. $users_select = ['id' => 0, 'count' => 0, 'user' => null, 'create_time' => 0];
  135. foreach ($this->users as $user) {
  136. $table_prefix = ($user->id) % 512;
  137. $chapter_order = DB::connection('chapter_order_mysql')
  138. ->table('chapter_orders' . $table_prefix)
  139. ->where('uid', $user->id)
  140. ->orderBy('created_at', 'desc')
  141. ->first();
  142. $book_order = BookOrder::where('uid', $user->id)
  143. ->first();
  144. if ($chapter_order) {
  145. if (strtotime($chapter_order->created_at) > $users_select['create_time']) {
  146. $users_select['id'] = $user->id;
  147. $users_select['create_time'] = strtotime($chapter_order->created_at);
  148. $users_select['user'] = $user;
  149. }
  150. }
  151. if ($book_order) {
  152. if (strtotime($book_order->created_at) > $users_select['create_time']) {
  153. $users_select['id'] = $user->id;
  154. $users_select['create_time'] = strtotime($book_order->created_at);
  155. $users_select['user'] = $user;
  156. }
  157. }
  158. }
  159. if ($users_select['id'] > 0) {
  160. $this->setUserLink($users_select['id']);
  161. $this->channel_id = $users_select['user']->distribution_channel_id;
  162. }
  163. }
  164. /**
  165. * 最后选择默认第一个渠道
  166. */
  167. private function getUserFromLast()
  168. {
  169. if ($this->users) {
  170. $this->setUserLink($this->users[0]->id);
  171. $this->channel_id = $this->users[0]->distribution_channel_id;
  172. }
  173. }
  174. private function setUserLink($uid)
  175. {
  176. $friend_link_uid_bind = ['uid' => $uid, 'openid' => $this->openid, 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s')];
  177. DB::table('friend_link_uid_bind')->insert($friend_link_uid_bind);
  178. $this->user = [$uid, $this->channel_id];
  179. }
  180. }