GenerateOrderDayStat.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: songdb
  5. * Date: 2017/12/26
  6. * Time: 下午5:26
  7. */
  8. namespace App\Console\Commands\Trade;
  9. use App\Modules\Trade\Services\OrderDayStatService;
  10. use App\Modules\Channel\Services\ChannelService;
  11. use Log;
  12. use Illuminate\Console\Command;
  13. use DB;
  14. class GenerateOrderDayStat extends Command
  15. {
  16. /**
  17. * 执行命令 php artisan generate_order_day_stat
  18. *
  19. * The name and signature of the console command.
  20. *
  21. * @var string
  22. */
  23. protected $signature = 'generate_order_day_stat';
  24. /**
  25. * The console command description.
  26. *
  27. * @var string
  28. */
  29. protected $description = '渠道订单日统计数据生成';
  30. /**
  31. * Execute the console command.
  32. *
  33. * @return mixed
  34. */
  35. public function handle()
  36. {
  37. print_r("======渠道订单日统计数据生成 【任务执行开始】=====".date("y-m-d H:i:s"."\n"));
  38. Log::info("======渠道订单日统计数据生成 【任务执行开始】=====".date("y-m-d H:i:s"."\n"));
  39. $channels = ChannelService::getAllChannels();
  40. $date = date('Y-m-d',strtotime('-1 day'));
  41. $end_time = date('Y-m-d H:i:s',strtotime($date) + 86400-1);
  42. if(count($channels))
  43. {
  44. $min_uid = DB::table('users')->where('created_at','>=',$date)->min('id');
  45. $max_uid = DB::table('users')->where('created_at','>=',$date)->where('created_at','<=',$end_time)->max('id');
  46. $once_num = 5000;
  47. $offset = 0;
  48. //实际注册用户数
  49. $start_uid = $min_uid;
  50. $real_reg_data = [];
  51. while (true)
  52. {
  53. $offset += $once_num;
  54. $end_user = DB::table('users')->select('id')->where('created_at','>=',$date)->where('created_at','<=',$end_time)->skip($offset)->limit(1)->first();
  55. $end_uid = $end_user ? $end_user->id : $max_uid;
  56. $reg_data = DB::select("select distribution_channel_id,count(1) num from users u where id >= {$start_uid} and id <= {$end_uid} and not exists (select id from users where openid = u.openid and id < {$start_uid} limit 1) group by distribution_channel_id");
  57. foreach ($reg_data as $_reg_data)
  58. {
  59. @$real_reg_data[$_reg_data->distribution_channel_id] += (int)$_reg_data->num;
  60. }
  61. if($end_uid == $max_uid) break;
  62. $start_uid = $end_uid;
  63. }
  64. $channels->each(function($channel) use($date,$real_reg_data){
  65. $begin_time = strtotime($date)+86400;
  66. $real_reg_num = isset($real_reg_data[$channel->id]) ? $real_reg_data[$channel->id] : 0;
  67. if(strtotime($channel->created_at) <= $begin_time) OrderDayStatService::add($channel->id,$channel->channel_user_id, $date,$channel->nickname,$real_reg_num);
  68. });
  69. }
  70. Log::info("======渠道订单日统计数据生成 【任务执行结束】=====".date("y-m-d H:i:s"."\n"));
  71. print_r("======渠道订单日统计数据生成 【任务执行结束】=====".date("y-m-d H:i:s"."\n"));
  72. }
  73. }