SiteUser.php 6.3 KB

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