UserGroupService.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. namespace Modules\Audience\Services;
  3. use Illuminate\Support\Facades\DB;
  4. class UserGroupService
  5. {
  6. public static function getSQL($tags) {
  7. $now = time();
  8. $sql = DB::table('user_some_info as a');
  9. if(strlen($tags['attention_hour'] ?? '') || (1 == $tags['in_48_hour'] ?? 1) ||
  10. strlen($tags['interact_hour'] ?? '') || $tags['gzh_appids'] ?? []) {
  11. $sql->join('wechat_official_user_info as b', 'a.uid', '=', 'b.uid');
  12. if(strlen($tags['attention_hour'] ?? '')) {
  13. $filter = self::filterTime($tags['attention_hour'], 'hour');
  14. foreach ($filter as $f) {
  15. $sql->where([
  16. ['b.subscribe_time', $f[0], $f[1]]
  17. ]);
  18. }
  19. }
  20. if(1 == $tags['in_48_hour'] ?? 1) {
  21. $filter = self::filterTime('0-48', 'hour');
  22. foreach ($filter as $f) {
  23. $sql->where([
  24. ['b.subscribe_time', $f[0], $f[1]]
  25. ]);
  26. }
  27. }
  28. if(strlen($tags['interact_hour'] ?? '')) {
  29. $filter = self::filterTime($tags['interact_hour'], 'hour');
  30. foreach ($filter as $f) {
  31. $sql->where([
  32. ['b.active_at', $f[0], $f[1]]
  33. ]);
  34. }
  35. }
  36. if($tags['gzh_appids'] ?? []) {
  37. $sql->whereIn('b.gzh_appid', $tags['gzh_appids']);
  38. }
  39. }
  40. if(strlen($tags['last_watch_day'] ?? '')) {
  41. $filter = self::filterTime($tags['last_watch_day'], 'day');
  42. foreach ($filter as $f) {
  43. $sql->where('a.last_watch_at', $f[0], $f[1]);
  44. }
  45. }
  46. if(strlen($tags['register_day'] ?? '')) {
  47. $filter = self::filterTime($tags['register_day'], 'day');
  48. foreach ($filter as $f) {
  49. $sql->where('a.register_at', $f[0], $f[1]);
  50. }
  51. }
  52. if(strlen($tags['total_watch_day'] ?? '')) {
  53. $filter = self::filterNum($tags['total_watch_day']);
  54. foreach ($filter as $f) {
  55. $sql->where('a.total_watch_day', $f[0], $f[1]);
  56. }
  57. }
  58. if($tags['charge_type'] ?? ''){
  59. switch ($tags['charge_type']) {
  60. case 4:
  61. $sql->where('a.is_vip', 1)
  62. ->where('a.vip_end_at', '>=', $now);
  63. break;
  64. default:
  65. $sql->where('a.charge_type', $tags['charge_type']);
  66. break;
  67. }
  68. }
  69. if($tags['total_charge_money'] ?? '') {
  70. $filter = self::filterNum($tags['total_charge_money']);
  71. foreach ($filter as $f) {
  72. $sql->where('a.total_charge_money', $f[0], $f[1]);
  73. }
  74. }
  75. if($tags['avg_charge_money'] ?? '') {
  76. $filter = self::filterNum($tags['avg_charge_money']);
  77. foreach ($filter as $f) {
  78. $sql->where('a.avg_charge_money', $f[0], $f[1]);
  79. }
  80. }
  81. if($tags['charge_num'] ?? '') {
  82. $filter = self::filterNum($tags['charge_num']);
  83. foreach ($filter as $f) {
  84. $sql->where('a.charge_num', $f[0], $f[1]);
  85. }
  86. }
  87. if($tags['remain_coin'] ?? '') {
  88. $filter = self::filterNum($tags['remain_coin']);
  89. foreach ($filter as $f) {
  90. $sql->where('a.remain_coin', $f[0], $f[1]);
  91. }
  92. }
  93. if(strlen($tags['last_charge_day'] ?? '')) {
  94. $filter = self::filterTime($tags['last_charge_day']);
  95. foreach ($filter as $f) {
  96. $sql->where('a.last_charge_at', $f[0], $f[1]);
  97. }
  98. }
  99. if($tags['uid'] ?? 0) {
  100. $sql->where('b.uid', $tags['uid']);
  101. }
  102. return $sql;
  103. }
  104. public static function filterTime($time, $type='day') {
  105. $factor = 'hour' == $type ? 3600 : 24 * 3600;
  106. $arr = explode('-', $time);
  107. if(0 == $arr[1]) $arr[1] = PHP_INT_MAX;
  108. $now = time();
  109. return [
  110. ['>=', $arr[0] * $factor + $now],
  111. ['<=', $arr[1] * $factor + $now],
  112. ];
  113. }
  114. public static function filterNum($numStr) {
  115. $arr = explode('-', $numStr);
  116. if(0 == $arr[1]) $arr[1] = PHP_INT_MAX;
  117. return [
  118. ['>=', $arr[0]],
  119. ['<=', $arr[1]]
  120. ];
  121. }
  122. public static function dealPayVideo($uids, $labelInfo) {
  123. $videoIds = $labelInfo['video_charge'] ?? [];
  124. if($videoIds && count($uids)) {
  125. $maxUid = max($uids);
  126. $minUid = min($uids);
  127. $_uids = collect();
  128. $_uids = $_uids->merge(DB::table('orders')->where([
  129. ['status', '<>', 'UNPAID']
  130. ])->whereIn('video_id', $videoIds)
  131. ->when(count($uids) < 500, function ($query) use($uids){
  132. return $query->whereIn('uid', $uids);
  133. }, function ($query) use($minUid, $maxUid){
  134. return $query->where('uid', '>=', $minUid)
  135. ->where('uid', '<=', $maxUid);
  136. })
  137. ->distinct()
  138. ->select('uid')->get()->pluck('uid'))->unique();
  139. return collect($uids)->intersect($_uids->unique())->unique()->toArray();
  140. } else {
  141. return $uids;
  142. }
  143. }
  144. public static function dealHistoryReadBooks($uids, $labelInfo) {
  145. $videoIds = $labelInfo['video_watch'] ?? [];
  146. if($videoIds && count($uids)) {
  147. $maxUid = max($uids);
  148. $minUid = min($uids);
  149. $_uids = collect();
  150. foreach ($videoIds as $videoId) {
  151. $tableName = 'video_users_'. ($videoId % 8);
  152. $_uids = $_uids->merge(DB::table($tableName)
  153. ->where(['video_id' => $videoId])
  154. ->when(count($uids) < 500, function ($query) use($uids){
  155. return $query->whereIn('uid', $uids);
  156. }, function ($query) use($minUid, $maxUid){
  157. return $query->where('uid', '>=', $minUid)
  158. ->where('uid', '<=', $maxUid);
  159. })
  160. ->select('uid')->get()->pluck('uid'));
  161. }
  162. return collect($uids)->intersect($_uids->unique())->unique()->toArray();
  163. } else {
  164. return $uids;
  165. }
  166. }
  167. }