SiteUser.php 6.3 KB

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