SiteUser.php 6.2 KB

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