UserGroupService.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. if($tags['video_watch'] ?? []) {
  103. $sql->whereRaw(sprintf("json_contains(a.video_watch, cast('[%s]' as json))",
  104. join(',', $tags['video_watch'])));
  105. }
  106. if($tags['video_charge'] ?? []) {
  107. $sql->whereRaw(sprintf("json_contains(a.video_charge, cast('[%s]' as json))",
  108. join(',', $tags['video_charge'])));
  109. }
  110. return $sql;
  111. }
  112. public static function filterTime($time, $type='day') {
  113. $factor = 'hour' == $type ? 3600 : 24 * 3600;
  114. $arr = explode('-', $time);
  115. if(0 == $arr[1]) $arr[1] = PHP_INT_MAX;
  116. $now = time();
  117. return [
  118. ['>=', $arr[0] * $factor + $now],
  119. ['<=', $arr[1] * $factor + $now],
  120. ];
  121. }
  122. public static function filterNum($numStr) {
  123. $arr = explode('-', $numStr);
  124. if(0 == $arr[1]) $arr[1] = PHP_INT_MAX;
  125. return [
  126. ['>=', $arr[0]],
  127. ['<=', $arr[1]]
  128. ];
  129. }
  130. }