SiteUser.php 6.4 KB

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