SiteUser.php 6.3 KB

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