ActiveUser.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Carbon\Carbon;
  5. use DB;
  6. use Redis;
  7. class ActiveUser extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'au';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = 'Command description';
  21. /**
  22. * Create a new command instance.
  23. *
  24. * @return void
  25. */
  26. public function __construct()
  27. {
  28. parent::__construct();
  29. }
  30. /**
  31. * Execute the console command.
  32. *
  33. * @return mixed
  34. */
  35. public function handle()
  36. {
  37. $appids = $this->getAppids();
  38. if(!$appids) return ;
  39. foreach ($appids as $appid) {
  40. $this->getUser($appid);
  41. }
  42. }
  43. //获取appid
  44. private function getAppids():array
  45. {
  46. //return ['wxb9fe767d99513216'];
  47. $data = [];
  48. $appids = DB::table('media_push_gzhs')->where('is_enabled',1)->select('appid')->get();
  49. if(!$appids) return $data;
  50. foreach ($appids as $appid_info){
  51. $data[] = $appid_info->appid;
  52. }
  53. return $data;
  54. }
  55. private function getUser($appid)
  56. {
  57. $count = DB::table('force_subscribe_users')->where('appid', $appid)->where('is_subscribed', 1)->count();
  58. //echo '$count is:' . $count . PHP_EOL;
  59. //echo ' ceil($count/1000) is:' . ceil($count / 1000) . PHP_EOL;
  60. for ($i = 0, $c = ceil($count / 1000); $i < $c; $i++) {
  61. //echo 'now is the ' . $i . 'times' . PHP_EOL;
  62. $result = DB::table('force_subscribe_users')
  63. ->select('uid', 'distribution_channel_id', 'appid', 'openid', 'subscribe_time', 'last_interactive_time')
  64. ->orderBy('created_at', 'desc')
  65. ->offset($i * 1000)->limit(1000)->get();
  66. if (!$result)
  67. break;
  68. //echo 'foreach start' . PHP_EOL;
  69. foreach ($result as $v) {
  70. $temp_time = 0;
  71. //签到时间
  72. $sign = DB::table('smart_push_user_sign')->where('uid', $v->uid)->where('day', Carbon::yesterday()->format('Y-m-d'))->select('sign_time')->first();
  73. if ($sign) $temp_time = $sign->sign_time;
  74. //阅读时间
  75. $read_record = Redis::hget('book_read:' . $v->uid, 'last_read');
  76. if ($read_record) {
  77. $arr = explode('_', $read_record);
  78. $read_time = isset($arr[2]) ? $arr[2] : 0;
  79. $temp_time = $read_time > $temp_time ? $read_time : $temp_time;
  80. }
  81. //交互时间
  82. $last_interactive_time = Carbon::createFromFormat('Y-m-d H:i:s', $v->last_interactive_time)->timestamp;
  83. if ($last_interactive_time > Carbon::yesterday()->timestamp) {
  84. $temp_time = $last_interactive_time > $temp_time ? $last_interactive_time : $temp_time;
  85. }
  86. if (!$temp_time || $temp_time < Carbon::yesterday()->timestamp) continue;
  87. $hour = Carbon::createFromTimestamp($temp_time)->hour;
  88. if ($hour >= 22 || $hour <= 6) {
  89. $hour = 10;
  90. }
  91. $result = $this->updateOrCreateMediaPushUser($v->uid, $v->openid, $v->distribution_channel_id, $v->appid, $hour, $v->subscribe_time);
  92. $pay_info = DB::table('orders')->where('uid', $v->uid)->where('status', 'PAID')->orderBy('id')->first();
  93. $now = Carbon::now();
  94. $days_num_in_month = $now->daysInMonth;
  95. //这个月剩余天数
  96. $last_days = $days_num_in_month - $now->day + 1;
  97. if (Carbon::createFromFormat('Y-m-d H:i:s', $v->subscribe_time)->format('Y-m-d') == Carbon::yesterday()->format('Y-m-d')) {
  98. //关注
  99. if ($pay_info) {
  100. //echo 'paid $uid is: ' . $v->uid . '----------------------------------------' . PHP_EOL;
  101. //已经付费
  102. $o = DB::table('media_push_user_details')->where('media_push_user_id', $result[0])->where('created_at', '>=', date('Y-m-d 00:00:00'))->count();
  103. //echo '$o is ' . $o . ',----------------' . PHP_EOL;
  104. if (!$o) {
  105. $days = $this->selectDay($last_days, 4, Carbon::createFromFormat('Y-m-d H:i:s', $v->last_interactive_time)->addDay(2));
  106. $this->updateOrCreateMediaPushUserDetail($v->uid, $result[0], $days);
  107. }
  108. } else {
  109. //echo 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' . PHP_EOL;
  110. //echo 'no pay $uid is: ' . $v->uid . '----------------------------------------' . PHP_EOL;
  111. //未付费
  112. if ($last_days > 8) {
  113. $num = ceil($last_days / 2);
  114. } else {
  115. $num = $last_days;
  116. }
  117. $o = DB::table('media_push_user_details')->where('media_push_user_id', $result[0])->where('created_at', '>=', date('Y-m-d 00:00:00'))->count();
  118. if (!$o) {
  119. $days = $this->selectDay($num, 4, Carbon::createFromFormat('Y-m-d H:i:s', $v->last_interactive_time)->addDay(2));
  120. $this->updateOrCreateMediaPushUserDetail($v->uid, $result[0], $days);
  121. }
  122. }
  123. } else {
  124. //非当日强关
  125. if ($pay_info && date('Y-m-d', strtotime($pay_info->created_at)) == date('Y-m-d', time() - 86400)) {
  126. //echo 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,$uid is :' . $v->uid . PHP_EOL;
  127. //充值了
  128. $old = DB::table('media_push_user_details')->where('uid', $v->uid)->where('created_at', '>=', date('Y-m-d 00:00:00'))->count();
  129. $all = DB::table('media_push_user_details')->where('uid', $v->uid)->where('created_at', '>=', date('Y-m-01 00:00:00'))->count();
  130. $one = DB::table('media_push_user_details')->where('uid', $v->uid)->where('created_at', '>=', date('Y-m-01 00:00:00'))->first();
  131. //echo sprintf('$old is: %s,$all is: %s,%s',$old,$all,PHP_EOL);
  132. //print_r($one);
  133. if ($all && $old) {
  134. $days = $this->selectDay($last_days, $old, $now);
  135. foreach ($days as $k => $vd) {
  136. DB::table('media_push_user_details')
  137. ->where('media_push_user_id', $one->media_push_user_id)
  138. ->where('sort_no', $all - $old + $k + 1)
  139. ->update(['updated_at' => date('Y-m-d H:i:s'), 'send_time' => $vd]);
  140. }
  141. }
  142. } else {
  143. if ($last_days > 8) {
  144. $num = ceil($last_days / 2);
  145. } else {
  146. $num = $last_days;
  147. }
  148. //echo 'ddddddddddddddddddddddddddddddddddddddd' . PHP_EOL;
  149. $days = $this->selectDay($num, 4, $now);
  150. $this->updateOrCreateMediaPushUserDetail($v->uid, $result[0], $days, 'old');
  151. }
  152. }
  153. }
  154. }
  155. }
  156. private function updateOrCreateMediaPushUser($uid, $openid, $distribution_channel_id, $appid, $active_hour, $sub_time)
  157. {
  158. $old = DB::table('media_push_users')->where('uid', $uid)->where('openid', $openid)->select('id')->first();
  159. $updated_at = Carbon::now()->format('Y-m-d H:i:s');
  160. $created_at = $updated_at;
  161. if ($old) {
  162. $id = $old->id;
  163. $type = 'update';
  164. DB::table('media_push_users')->where('id', $old->id)->update(compact('active_hour', 'updated_at'));
  165. } else {
  166. $id = DB::table('media_push_users')->insertGetId(
  167. compact('uid', 'openid', 'distribution_channel_id', 'appid', 'active_hour', 'sub_time', 'created_at', 'updated_at')
  168. );
  169. $type = 'create';
  170. }
  171. return [$id, $type];
  172. }
  173. private function updateOrCreateMediaPushUserDetail($uid, $media_push_user_id, $days, $type = 'new')
  174. {
  175. if (!$days) return;
  176. $data = [];
  177. if ($type == 'new') {
  178. foreach ($days as $k => $d) {
  179. $data[] = ['media_push_user_id' => $media_push_user_id, 'uid' => $uid, 'is_send' => 0, 'sort_no' => $k + 1, 'send_time' => $d, 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()];
  180. }
  181. DB::table('media_push_user_details')->insert($data);
  182. }
  183. if ($type == 'old') {
  184. $o = DB::table('media_push_user_details')->where('uid', $uid)->where('created_at', '>=', date('Y-m-01 00:00:00'))->count();
  185. if (!$o) {
  186. foreach ($days as $k => $d) {
  187. $data[] = ['media_push_user_id' => $media_push_user_id, 'uid' => $uid, 'is_send' => 0, 'sort_no' => $k + 1, 'send_time' => $d, 'created_at' => Carbon::now(), 'updated_at' => Carbon::now()];
  188. }
  189. DB::table('media_push_user_details')->insert($data);
  190. }
  191. }
  192. if ($type == 'update') {
  193. }
  194. }
  195. /**
  196. * @param int $uid
  197. * @param bool $is_charge 是否充值
  198. * @return array
  199. */
  200. private function getFourDate(int $uid, bool $is_charge, int $count = 4)
  201. {
  202. $now = Carbon::now();
  203. $days_num_in_month = $now->daysInMonth;
  204. //这个月剩余天数
  205. $last_days = $days_num_in_month - $now->day + 1;
  206. $num = 10;
  207. return $this->selectDay($num, $count, $now);
  208. }
  209. private function selectDay(int $alldays, int $selectCount = 4, $start_day):array
  210. {
  211. $start_day = $start_day->format('Y-m-d');
  212. //echo '$alldays is :' . $alldays . '--$selectCount is :' . $selectCount . '--$start_day is: ' . $start_day . PHP_EOL;
  213. $days = [];
  214. $month = Carbon::now()->month;
  215. for ($i = 0; $i < $alldays; $i++) {
  216. $temp_timestamp = strtotime($start_day) + $i * 86400;
  217. $temp = date('Y-m-d', $temp_timestamp);
  218. if (date('n', $temp_timestamp) != $month) {
  219. break;
  220. }
  221. $days[] = $temp;
  222. }
  223. if (empty($days) || count($days) <= $selectCount) return $days;
  224. $result = [];
  225. $c = 0;
  226. $saturdays = [];
  227. foreach ($days as $key => $v) {
  228. if (date('N', strtotime($v)) == 6) {
  229. $saturdays[] = $key;
  230. array_push($result, $v);
  231. if (++$c == $selectCount) return $result;
  232. }
  233. }
  234. $rate = (count($days) - 1) / $selectCount;
  235. $temp_count = 0;
  236. $temp_count2 = 0;
  237. for ($k = 0, $d = count($days); $k < $d; $k++) {
  238. $temp_count2++;
  239. $temp = $k + $rate * ($k);
  240. $key = round($temp);
  241. $key = $key < 0 ? 0 : $key;
  242. if (!isset($days[$key]) || count($result) >= $selectCount) {
  243. break;
  244. }
  245. if (isset($saturdays[$temp_count])) {
  246. if (abs($saturdays[$temp_count] - $temp) >= $rate * 0.8) {
  247. $result[] = $days[$key];
  248. if ($key >= $saturdays[$temp_count]) {
  249. $temp_count++;
  250. }
  251. } else {
  252. continue;
  253. }
  254. } else {
  255. $result[] = $days[$key];
  256. if (isset($saturdays[$temp_count]) && $key >= $saturdays[$temp_count]) {
  257. $temp_count++;
  258. }
  259. }
  260. }
  261. usort($result, function ($a, $b) {
  262. return strtotime($a) >= strtotime($b);
  263. });
  264. $result_count = count($result);
  265. if ($result_count < $selectCount) {
  266. $start = $days[0];
  267. $end = $days[count($days) - 1];
  268. $last = [];
  269. for ($i = 0; $i <= $result_count; $i++) {
  270. if ($i == 0) {
  271. $last[] = strtotime($result[$i]) - strtotime($start);
  272. continue;
  273. }
  274. if ($i == $result_count) {
  275. $last[] = strtotime($end) - strtotime($result[$i - 1]);
  276. continue;
  277. }
  278. $last[] = strtotime($result[$i]) - strtotime($result[$i - 1]);
  279. }
  280. if ($last) {
  281. $max = max($last);
  282. $max_key = [];
  283. foreach ($last as $lk => $l) {
  284. if ($max == $l) {
  285. $max_key[] = $lk;
  286. }
  287. }
  288. $other_key = array_random($max_key);
  289. if ($other_key > $result_count) {
  290. array_push($result, date('Y-m-d', strtotime($result[$result_count - 1]) + $max / 2));
  291. } else if ($other_key == 0) {
  292. array_push($result, date('Y-m-d', strtotime($start) + $max / 2));
  293. } else {
  294. array_push($result, date('Y-m-d', strtotime($result[$other_key - 1]) + $max / 2));
  295. }
  296. }
  297. usort($result, function ($a, $b) {
  298. return strtotime($a) >= strtotime($b);
  299. });
  300. }
  301. return $result;
  302. }
  303. private function test()
  304. {
  305. $result = $this->selectDay(4, 4, Carbon::now());
  306. echo "---------------------------------------------------\r\n";
  307. if ($result) {
  308. foreach ($result as $v) {
  309. echo $v . PHP_EOL;
  310. }
  311. }
  312. }
  313. }