ForceUserActiveDetail.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: tandunzhao
  5. * Date: 2017/11/20
  6. * Time: 下午5:26
  7. */
  8. namespace App\Console\Commands;
  9. use App\Modules\Finance\Services\FinanceService;
  10. use Log;
  11. use Illuminate\Console\Command;
  12. use DB;
  13. use Redis;
  14. use App\Modules\Book\Services\BookSubscribleChapterService;
  15. class ForceUserActiveDetail extends Command
  16. {
  17. /**
  18. * 执行命令 php artisan force_user_active
  19. *
  20. * The name and signature of the console command.
  21. *
  22. * @var string
  23. */
  24. protected $signature = 'force_user_active_detail';
  25. /**
  26. * The console command description.
  27. *
  28. * @var string
  29. */
  30. protected $description = '每日强关用户行为详细';
  31. /**
  32. * Execute the console command.
  33. *
  34. * @return mixed
  35. */
  36. public function handle()
  37. {
  38. print_r("======每日强关用户行为详细生成 【任务执行开始】=====".date("y-m-d H:i:s"."\n"));
  39. Log::info("======每日强关用户行为详细生成 【任务执行开始】=====".date("y-m-d H:i:s"."\n"));
  40. $_start = new \DateTime('2018-03-16');
  41. $_end = new \DateTime('2018-03-17');
  42. foreach (new \DatePeriod($_start, new \DateInterval('P1D'), $_end) as $d)
  43. {
  44. $date = $d->format('Y-m-d');
  45. $start = $date;
  46. $end = date('Y-m-d', strtotime($date) + 86400);
  47. $offset = 0;
  48. $limit = 10000;
  49. $data = [];
  50. $_m_data = [];
  51. while (true) {
  52. print_r("select send_order_id,uid,appid,unsubscribe_time,distribution_channel_id,created_at from force_subscribe_users where created_at > '{$start}' and created_at < '{$end}' limit $offset,$limit" . date("y-m-d H:i:s" . "\n"));
  53. $users = DB::select("select send_order_id,uid,appid,unsubscribe_time,distribution_channel_id,created_at from force_subscribe_users where created_at > '{$start}' and created_at < '{$end}' limit $offset,$limit");
  54. print_r("users_count:".count($users));
  55. if (count($users) == 0) break;
  56. foreach ($users as $user) {
  57. $is_unsub_in_one_day = false;
  58. $is_pay_in_one_day = false;
  59. $_read_data = [
  60. 'date' => $date,
  61. 'appid' => $user->appid,
  62. 'uid' => $user->uid,
  63. 'sub_time' => strtotime($user->created_at),
  64. 'unsub_time' => strtotime($user->unsubscribe_time),
  65. ];
  66. $order = DB::table('orders')->where('uid', $user->uid)->where('status', 'PAID')->first();
  67. if ($order) {
  68. if (strtotime($order->created_at) < strtotime($user->created_at) + 86400)//24小时内
  69. {
  70. $_read_data['recharge_time_in_one_day'] = strtotime($order->created_at);
  71. $is_pay_in_one_day = true;
  72. }
  73. }
  74. $one_day_time = strtotime($user->created_at) + 86400;
  75. if ($user->unsubscribe_time && strtotime($user->unsubscribe_time) < strtotime($user->created_at) + 86400)//24小时内取注
  76. {
  77. $is_unsub_in_one_day = true;
  78. $_read_data['is_unsub_in_one_day'] = 1;
  79. }
  80. $last_read_info = Redis::hget('book_base:' . $user->uid, 'last_read');
  81. $send_order = DB::table('send_orders')->where('id', $user->send_order_id)->first();
  82. if ($send_order && $send_order->book_id && $last_read_info) {
  83. $book = Redis::hget('book_read:' . $user->uid, $send_order->book_id);
  84. $book_info = explode('_', $book);
  85. $cid = $book_info[0];
  86. $vip_chapter = DB::table('chapters')->select(['sequence'])->where('bid', $send_order->book_id)->where('is_vip', 1)->orderby('sequence')->first();
  87. $read_last_free = DB::table('read_records')->where('bid', $send_order->book_id)->where('uid', $user->uid)->where('created_at', '<=', date('Y-m-d H:i:s', $one_day_time))->where('seq', $vip_chapter->sequence - 1)->first();
  88. $last_read_array = explode('_', $last_read_info);
  89. $last_read_time = $last_read_array[4];
  90. //$_read_data['last_read_time'] = $last_read_time;
  91. $_read_data['last_free_read_time'] = $read_last_free ? strtotime($read_last_free->created_at) : 0;
  92. $book_config = DB::table('book_configs')->select(['force_subscribe_chapter_seq','charge_type'])->where('bid', $send_order->book_id)->first();
  93. //订阅
  94. if($book_config->charge_type == 'CHAPTER') {
  95. $first_sub = DB::connection('chapter_order_mysql')->table('chapter_orders' . $user->uid % 512)->where('uid', $user->uid)->where('created_at', '<=', date('Y-m-d H:i:s', $one_day_time))->where('bid', $send_order->book_id)->orderBy('id', 'asc')->first();
  96. $_read_data['first_sub_time'] = $first_sub ? strtotime($first_sub->created_at) : 0;
  97. }
  98. if($book_config->charge_type == 'BOOK') {
  99. $_read_last_free = DB::table('read_records')->where('bid', $send_order->book_id)->where('uid', $user->uid)->where('seq', $vip_chapter->sequence - 1)->first();
  100. $_read_data['first_sub_time'] = $_read_last_free ? strtotime($_read_last_free->created_at) : 0;
  101. }
  102. }
  103. //第一次点击返回时间
  104. $back_info = DB::table('back_actives')->where('uid',$user->uid)->first();
  105. if($back_info)
  106. {
  107. $_read_data['first_click_back_time'] = strtotime($back_info->created_at);
  108. }
  109. $_read_data['created_at'] = date("Y-m-d H:i:s");
  110. DB::table('force_users_date_read')->insert($_read_data);
  111. }
  112. $offset = $offset + $limit;
  113. }
  114. Log::info("======每日强关用户行为详细生成 【任务执行结束】=====" . date("y-m-d H:i:s" . "\n"));
  115. print_r("======每日强关用户行为详细生成 【任务执行结束】=====" . date("y-m-d H:i:s" . "\n"));
  116. }
  117. }
  118. }