SiteUser.php 6.3 KB

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