SiteUser.php 6.4 KB

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