123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- <?php
- namespace App\Console\Commands;
- use App\Modules\Finance\Services\FinanceService;
- use Log;
- use Illuminate\Console\Command;
- use DB;
- use Redis;
- use App\Modules\Book\Services\BookSubscribleChapterService;
- use App\Modules\Sys\Services\SysConfigService;
- class ForceUserActive extends Command
- {
-
- protected $signature = 'force_user_active {start} {end}';
-
- protected $description = '每日强关用户行为';
-
- public function handle()
- {
-
- print_r("======每日强关用户行为生成 【任务执行开始】=====".date("y-m-d H:i:s"."\n"));
- Log::info("======每日强关用户行为生成 【任务执行开始】=====".date("y-m-d H:i:s"."\n"));
-
- DB::table('force_users_date_actives')->truncate();
- $_start = new \DateTime($this->argument('start'));
- $_end = new \DateTime($this->argument('end'));
- if(!$_start || !$_end) dd('param empty!exit');
- foreach (new \DatePeriod($_start, new \DateInterval('P1D'), $_end) as $d)
- {
- $date = $d->format('Y-m-d');
- $start = $date;
- $end = date('Y-m-d', strtotime($date) + 86400);
- print_r("======执行日期:{$date}=====");
- $offset = 0;
- $limit = 13000;
- $data = [];
- $_m_data = [];
- while (true) {
- 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"));
- $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");
- print_r("users_count:".count($users));
- if (count($users) == 0) break;
- foreach ($users as $user) {
- $is_unsub_in_one_day = false;
- $is_pay_in_one_day = false;
- $is_pay_in_two_days = false;
- $is_pay_in_three_days = false;
- $is_unsub_in_two_days = false;
- $is_unsub_in_three_days = false;
- $order_sum_in_one_day = 0;
- $order_sum_in_two_day = 0;
- $order_sum_in_three_day = 0;
- $order_sum_in_one_month = 0;
- $order_sum_in_two_month = 0;
- $order_sum_in_three_month = 0;
- $order_sum_in_six_month = 0;
- $user_orders = DB::table('orders')->where('uid', $user->uid)->where('status', 'PAID')->get();
- $sub_time = strtotime($user->created_at);
- if (count($user_orders)) {
- $first_order = $user_orders ? $user_orders[0] : null;
- if (strtotime($first_order->created_at) < $sub_time + 86400)
- {
- @$data[$user->appid]['pay_user_num_in_one_day']++;
- $is_pay_in_one_day = true;
- }
- if (strtotime($first_order->created_at) < $sub_time + 86400 * 2)
- {
- $is_pay_in_two_days = true;
- }
- if (strtotime($first_order->created_at) < $sub_time + 86400 * 3)
- {
- @$data[$user->appid]['pay_user_num_in_three_days']++;
- $is_pay_in_three_days = true;
- }
- if (strtotime($first_order->created_at) < $sub_time + 86400 * 30)
- {
- @$data[$user->appid]['pay_user_num_in_one_month']++;
- }
- foreach ($user_orders as $_order)
- {
- $price = $_order->price;
- if(strtotime($_order->created_at) - $sub_time <= 86400) $order_sum_in_one_day += $price;
- if(strtotime($_order->created_at) - $sub_time <= 86400*3) $order_sum_in_three_day += $price;
- if(strtotime($_order->created_at) - $sub_time <= 86400*30) $order_sum_in_one_month += $price;
- if(strtotime($_order->created_at) - $sub_time <= 86400*60) $order_sum_in_two_month += $price;
- if(strtotime($_order->created_at) - $sub_time <= 86400*90) $order_sum_in_three_month += $price;
- if(strtotime($_order->created_at) - $sub_time <= 86400*180) $order_sum_in_six_month += $price;
- }
- }
- @$data[$user->appid]['order_sum_in_one_day'] += $order_sum_in_one_day;
-
- @$data[$user->appid]['order_sum_in_three_day'] += $order_sum_in_three_day;
- @$data[$user->appid]['order_sum_in_one_month'] += $order_sum_in_one_month;
- @$data[$user->appid]['fans_num']++;
- if ($user->unsubscribe_time && strtotime($user->unsubscribe_time) < strtotime($user->created_at) + 86400)
- {
- @$data[$user->appid]['unsub_user_num_in_one_day']++;
- $is_unsub_in_one_day = true;
- }
- if ($user->unsubscribe_time && strtotime($user->unsubscribe_time) < strtotime($user->created_at) + 86400*2)
- {
- @$data[$user->appid]['unsub_user_num_in_two_days']++;
- $is_unsub_in_two_days = true;
- }
- if ($user->unsubscribe_time && strtotime($user->unsubscribe_time) < strtotime($user->created_at) + 86400*3)
- {
- @$data[$user->appid]['unsub_user_num_in_three_days']++;
- $is_unsub_in_three_days = true;
- }
- }
- $offset = $offset + $limit;
- }
- foreach ($data as $appid => $item) {
- $_data = [];
- $_data['appid'] = $appid;
- $_data['date'] = $date;
- $_data['fans_num'] = isset($item['fans_num']) ? $item['fans_num'] : 0;
- $_data['pay_user_num_in_one_day'] = isset($item['pay_user_num_in_one_day']) ? $item['pay_user_num_in_one_day'] : 0;
- $_data['read_last_free_user_num'] = isset($item['read_last_free_user_num']) ? $item['read_last_free_user_num'] : 0;
- $_data['unsub_user_num_in_one_day'] = isset($item['unsub_user_num_in_one_day']) ? $item['unsub_user_num_in_one_day'] : 0;
- $_data['unsub_in_one_day_read_last_free_user_num'] = isset($item['unsub_in_one_day_read_last_free_user_num']) ? $item['unsub_in_one_day_read_last_free_user_num'] : 0;
- $_data['unsub_user_num_in_two_days'] = isset($item['unsub_user_num_in_two_days']) ? $item['unsub_user_num_in_two_days'] : 0;
- $_data['unsub_in_two_day_read_last_free_user_num'] = isset($item['unsub_in_two_day_read_last_free_user_num']) ? $item['unsub_in_two_day_read_last_free_user_num'] : 0;
- $_data['unsub_user_num_in_three_days'] = isset($item['unsub_user_num_in_three_days']) ? $item['unsub_user_num_in_three_days'] : 0;
- $_data['unsub_in_three_day_read_last_free_user_num'] = isset($item['unsub_in_three_day_read_last_free_user_num']) ? $item['unsub_in_three_day_read_last_free_user_num'] : 0;
- $_data['pay_user_num_in_three_days'] = isset($item['pay_user_num_in_three_days']) ? $item['pay_user_num_in_three_days'] : 0;
- $_data['pay_user_num_in_one_month'] = isset($item['pay_user_num_in_one_month']) ? $item['pay_user_num_in_one_month'] : 0;
- $_data['order_sum_in_one_day'] = $item['order_sum_in_one_day'];
- $_data['order_sum_in_three_day'] = $item['order_sum_in_three_day'];
- $_data['order_sum_in_one_month'] = $item['order_sum_in_one_month'];
- $_data['created_at'] = $_data['updated_at'] = date('Y-m-d H:i:s');
- $_m_data[] = $_data;
- }
- DB::table('force_users_date_actives')->insert($_m_data);
- }
-
- DB::update("update force_users_date_actives,distribution_channels,official_accounts,companies,channel_users set force_users_date_actives.distribution_channel_id = distribution_channels.id,force_users_date_actives.distribution_channel_name=companies.name,force_users_date_actives.gzh_name = official_accounts.nickname where force_users_date_actives.appid = official_accounts.appid and official_accounts.distribution_channel_id = distribution_channels.id and companies.id = channel_users.company_id and distribution_channels.channel_user_id = channel_users.id");
-
- SysConfigService::closeForceActiveSwitch();
- Log::info("======每日强关用户行为生成 【任务执行结束】=====" . date("y-m-d H:i:s" . "\n"));
- print_r("======每日强关用户行为生成 【任务执行结束】=====" . date("y-m-d H:i:s" . "\n"));
- }
- }
|