123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469 |
- <?php
- namespace App\Modules\SendOrder\Services;
- use App\Modules\SendOrder\Models\SendOrderStat;
- use DB;
- use Redis;
- class SendOrderStatisticsService
- {
- public static function get_query_date(){
- $today = date('Y-m-d H:i:s');
- $today_time = strtotime($today);
- $week_date = self::getWeek($today_time);
- \Log::info('$week_date');
- \Log::info($week_date);
- $today = date('Y-m-d');
- $start_date = $end_date = '';
-
-
-
-
- if($week_date['week_num'] ==1){
- $reduce_big_end_day = -7;
- }
-
- elseif($week_date['week_num'] ==0){
- $reduce_big_end_day = -6;
- }else{
- $reduce_big_end_day = 1-$week_date['week_num'];
- }
- $reduce_big_start_day = $reduce_big_end_day - 9;
- $reduce_small_start_day = $reduce_big_start_day;
- $reduce_small_end_day = $reduce_big_end_day - 3;
- \Log::info('$reduce_big_start_day:'.$reduce_big_start_day.' $reduce_big_end_day:'.$reduce_big_end_day.' $reduce_small_start_day:'.$reduce_small_start_day.' $reduce_small_end_day:'.$reduce_small_end_day);
-
- $small_start_day = date('Y-m-d',$today_time + 3600*24*$reduce_small_start_day);
- $small_end_day = date('Y-m-d',$today_time + 3600*24*$reduce_small_end_day);
- $big_start_day = date('Y-m-d',$today_time + 3600*24*$reduce_big_start_day);
- $big_end_day = date('Y-m-d',$today_time +3600*24*$reduce_big_end_day);
-
- $data = [
- 'start_day'=>$small_start_day,
- 'end_day'=>$small_end_day,
- 'small_start_day'=>$small_start_day.' 00:00:00',
- 'small_end_day'=>$small_end_day.' 23:59:59',
- 'big_start_day'=>$big_start_day.' 00:00:00',
- 'big_end_day'=>$big_end_day.' 23:59:59',
- ];
-
- \Log::info('get_query_date_end');
- \Log::info($data);
- return $data;
- }
-
-
- public static function getWeek($unixTime=''){
- $unixTime=is_numeric($unixTime)?$unixTime:time();
- $weekarray=array('日','一','二','三','四','五','六');
- $week_num = date('w',$unixTime);
- $data = [
- 'week_num'=>$week_num,
- 'week_name'=>'星期'.$weekarray[$week_num]
- ];
- return $data;
- }
-
-
- public static function get_total_charge_amount($distribution_channel_id,$start_date,$end_date){
- $data = DB::table('orders')
- ->select(DB::raw('sum(price) as total_charge_amount '))
- ->where('created_at','>=',$start_date)
- ->where('created_at','<',$end_date)
- ->where('status','PAID')
- ->where('distribution_channel_id',$distribution_channel_id)
- ->first();
- return isset($data->total_charge_amount) && $data->total_charge_amount ? $data->total_charge_amount:'0';
- }
-
-
- public static function get_charge_user_num($distribution_channel_id,$start_date,$end_date){
- $data = DB::table('orders')
- ->select(DB::raw('count(distinct uid) as user_num'))
- ->where('created_at','>=',$start_date)
- ->where('created_at','<',$end_date)
- ->where('status','PAID')
- ->where('distribution_channel_id',$distribution_channel_id)
- ->first();
- return isset($data->user_num) && $data->user_num ? $data->user_num:'0';
- }
-
- public static function get_suc_order_num($distribution_channel_id,$start_date,$end_date){
- $data = DB::table('orders')
- ->select(DB::raw('count(1) as order_num'))
- ->where('created_at','>=',$start_date)
- ->where('created_at','<',$end_date)
- ->where('status','PAID')
- ->where('distribution_channel_id',$distribution_channel_id)
- ->first();
- return isset($data->order_num) && $data->order_num ? $data->order_num:'0';
- }
-
- public static function get_real_cost_amount($distribution_channel_id,$start_date,$end_date){
- $data = DB::table('send_orders')
- ->select(DB::raw('sum(cost) as real_cost_amount'))
- ->where('send_time','>=',$start_date)
- ->where('send_time','<',$end_date)
- ->where('is_enable','1')
- ->where('distribution_channel_id',$distribution_channel_id)
- ->first();
- return isset($data->real_cost_amount) && $data->real_cost_amount ? $data->real_cost_amount:'0';
- }
-
- public static function get_real_send_order_num($distribution_channel_id,$start_date,$end_date){
- $data = DB::table('send_orders')
- ->select(DB::raw('count(1) as send_order_num'))
- ->where('send_time','>=',$start_date)
- ->where('send_time','<',$end_date)
- ->where('is_enable','1')
- ->where('distribution_channel_id',$distribution_channel_id)
- ->first();
- return isset($data->send_order_num) && $data->send_order_num ? $data->send_order_num:'0';
- }
-
- public static function get_force_subscribe_rate($new_subscribe_user_num,$register_user_num){
- if($register_user_num){
- return round($new_subscribe_user_num*100/$register_user_num,2);
- }else{
- return 0;
- }
- }
-
- public static function get_inner_send_order_num($distribution_channel_id,$start_date,$end_date){
- $inner_send_orders = DB::table('send_orders')
- ->select('send_orders.id',
- DB::raw('(select count(1) from force_subscribe_users where send_order_id=send_orders.id and is_subscribed=1 and distribution_channel_id=send_orders.distribution_channel_id) as subscribe_num'))
- ->where('send_time','>=',$start_date)
- ->where('send_time','<',$end_date)
- ->where('is_enable','1')
- ->where('distribution_channel_id',$distribution_channel_id)
- ->groupBy('send_orders.id')
- ->having('subscribe_num','<','20')
- ->get();
- \Log::info('$inner_send_orders');\Log::info($inner_send_orders);
- return count($inner_send_orders);
- }
-
-
- public static function get_inner_send_order_num_by_order_type($distribution_channel_id,$start_date,$end_date){
- $data = DB::table('send_orders')
- ->select(DB::raw('count(1) as send_order_num'))
- ->where('send_time','>=',$start_date)
- ->where('send_time','<',$end_date)
- ->where('promotion_type','INTERNAL')
- ->where('is_enable','1')
- ->where('distribution_channel_id',$distribution_channel_id)
- ->first();
- return isset($data->send_order_num) && $data->send_order_num ? $data->send_order_num:'0';
- }
-
- public static function get_new_subscribe_user_num($distribution_channel_id,$start_date,$end_date){
- $data = DB::table('send_orders')
- ->join('force_subscribe_users','force_subscribe_users.send_order_id','send_orders.id')
- ->select(DB::raw('count(1) as new_subscribe_user_num'))
- ->where('send_orders.send_time','>=',$start_date)
- ->where('send_orders.send_time','<',$end_date)
- ->where('send_orders.is_enable','1')
- ->where('send_orders.distribution_channel_id',$distribution_channel_id)
- ->first();
- return isset($data->new_subscribe_user_num) && $data->new_subscribe_user_num ? $data->new_subscribe_user_num:'0';
- }
-
-
-
- public static function get_new_subscribe_charge_user_num($distribution_channel_id,$start_date,$end_date,$big_start_date,$big_end_date){
- $data = DB::table('send_orders')
- ->join('force_subscribe_users','force_subscribe_users.send_order_id','send_orders.id')
- ->join('orders','orders.uid','force_subscribe_users.uid')
- ->select(DB::raw('count(distinct orders.uid) as new_subscribe_charge_user_num'))
- ->where('orders.created_at','>=',$big_start_date)
- ->where('orders.created_at','<',$big_end_date)
- ->where('orders.status','PAID')
- ->where('send_orders.send_time','>=',$start_date)
- ->where('send_orders.send_time','<',$end_date)
- ->where('send_orders.is_enable','1')
- ->where('send_orders.distribution_channel_id',$distribution_channel_id)
- ->first();
- return isset($data->new_subscribe_charge_user_num) && $data->new_subscribe_charge_user_num ? $data->new_subscribe_charge_user_num:'0';
-
- }
-
-
- public static function get_new_subscribe_charge_amount($distribution_channel_id,$start_date,$end_date,$big_start_date,$big_end_date){
- $data = DB::table('send_orders')
- ->join('force_subscribe_users','force_subscribe_users.send_order_id','send_orders.id')
- ->join('orders','orders.uid','force_subscribe_users.uid')
- ->select(DB::raw('ifnull(sum(price),0) as new_subscribe_charge_amount'))
- ->where('orders.created_at','>=',$big_start_date)
- ->where('orders.created_at','<',$big_end_date)
- ->where('orders.status','PAID')
- ->where('send_orders.send_time','>=',$start_date)
- ->where('send_orders.send_time','<',$end_date)
- ->where('send_orders.is_enable','1')
- ->where('send_orders.distribution_channel_id',$distribution_channel_id)
- ->first();
- return isset($data->new_subscribe_charge_amount) && $data->new_subscribe_charge_amount ? $data->new_subscribe_charge_amount:'0';
-
- }
-
- public static function get_new_subscribe_cost($real_cost_amount,$new_subscribe_user_num){
- if($new_subscribe_user_num){
- return round($real_cost_amount/$new_subscribe_user_num,2);
- }else{
- return 0;
- }
- }
-
- public static function get_new_subscribe_3day_out($new_subscribe_charge_amount,$new_subscribe_user_num){
- if($new_subscribe_user_num){
- return round($new_subscribe_charge_amount/$new_subscribe_user_num,2);
- }else{
- return 0;
- }
- }
-
- public static function get_new_subscribe_re_filling_rate($small_re_charge_user_num,$new_subscribe_charge_user_num){
- if($new_subscribe_charge_user_num){
- return round($small_re_charge_user_num*100/$new_subscribe_charge_user_num,2);
- }else{
- return 0;
- }
- }
-
-
- public static function get_new_subscribe_charge_rate($new_subscribe_charge_user_num,$new_subscribe_user_num){
- if($new_subscribe_user_num){
- return round($new_subscribe_charge_user_num*100/$new_subscribe_user_num,2);
- }else{
- return 0;
- }
- }
-
- public static function get_new_register_user_num($distribution_channel_id,$start_date,$end_date){
- $data = DB::table('send_orders')
- ->join('users','users.send_order_id','send_orders.id')
- ->select(DB::raw('count(1) as new_register_user_num'))
- ->where('send_orders.send_time','>=',$start_date)
- ->where('send_orders.send_time','<',$end_date)
- ->where('send_orders.is_enable','1')
- ->where('send_orders.distribution_channel_id',$distribution_channel_id)
- ->first();
- return isset($data->new_register_user_num) && $data->new_register_user_num ? $data->new_register_user_num:'0';
- }
-
-
- public static function get_new_subscribe_small_charge_user_num($distribution_channel_id,$start_date,$end_date){
- $data = DB::table('send_orders')
- ->join('force_subscribe_users','force_subscribe_users.send_order_id','send_orders.id')
- ->join('orders','force_subscribe_users.uid','orders.uid')
- ->select(DB::raw('count(distinct orders.uid) as new_subscribe_small_charge_user_num'))
- ->where('send_orders.send_time','>=',$start_date)
- ->where('send_orders.send_time','<',$end_date)
- ->where('send_orders.is_enable','1')
- ->where('orders.created_at','>=',$start_date)
- ->where('orders.created_at','<',$end_date)
- ->where('force_subscribe_users.created_at','>=',$start_date)
- ->where('force_subscribe_users.created_at','<',$end_date)
- ->where('orders.status','PAID')
- ->where('send_orders.distribution_channel_id',$distribution_channel_id)
- ->first();
- return isset($data->new_subscribe_small_charge_user_num) && $data->new_subscribe_small_charge_user_num ? $data->new_subscribe_small_charge_user_num:'0';
-
- }
-
- public static function get_new_subscribe_small_re_charge_user_num($distribution_channel_id,$start_date,$end_date,$big_start_date,$big_end_date){
- $data = DB::table('send_orders')
- ->join('force_subscribe_users','force_subscribe_users.send_order_id','send_orders.id')
- ->join('orders','force_subscribe_users.uid','orders.uid')
- ->select(DB::raw('count(distinct orders.id) as new_subscribe_small_re_charge_user_order_num'),'orders.uid')
- ->where('send_orders.send_time','>=',$start_date)
- ->where('send_orders.send_time','<',$end_date)
- ->where('send_orders.is_enable','1')
- ->where('orders.created_at','>=',$big_start_date)
- ->where('orders.created_at','<',$big_end_date)
- ->where('orders.status','PAID')
- ->where('send_orders.distribution_channel_id',$distribution_channel_id)
- ->groupBy('orders.uid')
- ->having('new_subscribe_small_re_charge_user_order_num','>',1)
- ->get();
- return count($data);
-
- }
-
-
- public static function get_top_send_order_num_books($distribution_channel_id,$start_date,$end_date){
- $data = DB::table('send_orders')
- ->select(DB::raw('count(1) as send_order_num'),'book_id','book_name')
- ->where('send_time','>=',$start_date)
- ->where('send_time','<',$end_date)
- ->where('is_enable','1')
- ->where('distribution_channel_id',$distribution_channel_id)
- ->groupBy('book_id')
- ->orderBy('send_order_num','desc')
- ->limit(3)
- ->get();
- if(!empty($data)){
- return $data->toArray();
- }
- return $data;
- }
-
-
- public static function get_top_charge_register_books($distribution_channel_id,$start_date,$end_date){
- $charge_register_books = DB::table('send_orders')
- ->select(
- DB::raw('ifnull((select sum(orders.price) from orders inner join users ON users.send_order_id=orders.send_order_id and users.id=orders.uid where orders.send_order_id=send_orders.id and status="PAID"),0) as recharge_amount'),
- DB::raw('ifnull((select count(1) from users where users.send_order_id=send_orders.id),1) as register_num'),
- 'book_id','book_name')
- ->where('send_orders.send_time','>=',$start_date)
- ->where('send_orders.send_time','<',$end_date)
- ->where('send_orders.is_enable','1')
- ->where('send_orders.distribution_channel_id',$distribution_channel_id)
- ->groupBy('book_id')
- ->having('register_num','>','100')
- ->orderBy(DB::raw('(recharge_amount/register_num)'),'desc')
- ->limit(3)
- ->get();
-
- if(!empty($charge_register_books)){
- foreach($charge_register_books as $key=>$charge_register_book){
- $charge_register_books[$key]->charge_register_number = (int)$charge_register_book->recharge_amount.'/'.$charge_register_book->register_num;
- if($charge_register_book->register_num > 0){
- $charge_register_books[$key]->charge_register = round($charge_register_book->recharge_amount/$charge_register_book->register_num,2);
- }else{
- $charge_register_books[$key]->charge_register = 0;
- }
- }
- }
- if(!empty($charge_register_books)){
- return $charge_register_books->toArray();
- }
- return $charge_register_books;
- }
-
-
- public static function getWeeklySendOrderStatistics($distribution_channel_id)
- {
- if(empty($distribution_channel_id)){
- return response()->error('PARAM_EMPTY');
- }
- \Log::info('getWeeklySendOrderStatistics_start:'.$distribution_channel_id);
- $data = [];
- try{
- $date = SendOrderStatisticsService::get_query_date();
- $data['date'] = [
- 'start_day'=>$date['start_day'],
- 'end_day'=>$date['end_day'],
- 'small_start_day'=>$date['small_start_day'],
- 'small_end_day'=>$date['small_end_day'],
- 'big_start_day'=>$date['big_start_day'],
- 'big_end_day'=>$date['big_end_day'],
- ];
-
-
- $weekly_report_key = 'weeklyReport:distribution_channel_id:'.$distribution_channel_id.':start_day:'.$date['start_day'].':end_day:'.$date['end_day'];
- $redis_data = Redis::get($weekly_report_key);
- if(!empty($redis_data)){
- \Log::info('redis_has_weekly_report:'.$weekly_report_key);
- $data = object_to_array(json_decode($redis_data));
- \Log::info($data);
- return $data;
- }
-
-
-
- \Log::info('get_total_charge_amount_start:'.$distribution_channel_id);
- $total_charge_amount = SendOrderStatisticsService::get_total_charge_amount($distribution_channel_id,$date['small_start_day'],$date['small_end_day']);
- \Log::info('get_charge_user_num_start:'.$distribution_channel_id);
- $charge_user_num = SendOrderStatisticsService::get_charge_user_num($distribution_channel_id,$date['small_start_day'],$date['small_end_day']);
- \Log::info('get_suc_order_num_start:'.$distribution_channel_id);
- $suc_order_num = SendOrderStatisticsService::get_suc_order_num($distribution_channel_id,$date['small_start_day'],$date['small_end_day']);
- \Log::info('get_real_cost_amount_start:'.$distribution_channel_id);
- $real_cost_amount = SendOrderStatisticsService::get_real_cost_amount($distribution_channel_id,$date['small_start_day'],$date['small_end_day']);
-
- $data['charge_statistics'] = [
- 'total_charge_amount' => $total_charge_amount,
- 'charge_user_num' => $charge_user_num,
- 'suc_order_num' => $suc_order_num,
- 'real_cost_amount' => $real_cost_amount,
- ];
-
-
- \Log::info('get_new_register_user_num_start:'.$distribution_channel_id);
- $new_register_user_num = SendOrderStatisticsService::get_new_register_user_num($distribution_channel_id,$date['small_start_day'],$date['small_end_day']);
- \Log::info('get_new_subscribe_user_num_start:'.$distribution_channel_id);
- $new_subscribe_user_num = SendOrderStatisticsService::get_new_subscribe_user_num($distribution_channel_id,$date['small_start_day'],$date['small_end_day']);
- \Log::info('get_force_subscribe_rate_start:'.$distribution_channel_id);
- $force_subscribe_rate = SendOrderStatisticsService::get_force_subscribe_rate($new_subscribe_user_num,$new_register_user_num);
- \Log::info('get_real_send_order_num_start:'.$distribution_channel_id);
- $real_send_order_num = SendOrderStatisticsService::get_real_send_order_num($distribution_channel_id,$date['small_start_day'],$date['small_end_day']);
- \Log::info('get_inner_send_order_num_start:'.$distribution_channel_id);
- $inner_send_order_num = SendOrderStatisticsService::get_inner_send_order_num($distribution_channel_id,$date['small_start_day'],$date['small_end_day']);
- \Log::info('get_new_subscribe_charge_user_num_start:'.$distribution_channel_id);
- $new_subscribe_charge_user_num = SendOrderStatisticsService::get_new_subscribe_charge_user_num($distribution_channel_id,$date['small_start_day'],$date['small_end_day'],$date['big_start_day'],$date['big_end_day']);
- \Log::info('get_new_subscribe_charge_amount_start:'.$distribution_channel_id);
- $new_subscribe_charge_amount = SendOrderStatisticsService::get_new_subscribe_charge_amount($distribution_channel_id,$date['small_start_day'],$date['small_end_day'],$date['big_start_day'],$date['big_end_day']);
- \Log::info('get_new_subscribe_cost_start:'.$distribution_channel_id);
- $new_subscribe_cost = SendOrderStatisticsService::get_new_subscribe_cost($real_cost_amount,$new_subscribe_user_num);
- \Log::info('get_new_subscribe_3day_out_start:'.$distribution_channel_id);
- $new_subscribe_3day_out = SendOrderStatisticsService::get_new_subscribe_3day_out($new_subscribe_charge_amount,$new_subscribe_user_num);
-
-
- \Log::info('get_new_subscribe_small_re_charge_user_num_start:'.$distribution_channel_id);
- $small_re_charge_user_num = SendOrderStatisticsService::get_new_subscribe_small_re_charge_user_num($distribution_channel_id,$date['small_start_day'],$date['small_end_day'],$date['big_start_day'],$date['big_end_day']);
- \Log::info('get_new_subscribe_re_filling_rate_start:'.$distribution_channel_id);
- $new_subscribe_re_filling_rate = SendOrderStatisticsService::get_new_subscribe_re_filling_rate($small_re_charge_user_num,$new_subscribe_charge_user_num);
- \Log::info('get_new_subscribe_charge_rate_start:'.$distribution_channel_id);
- $new_subscribe_charge_rate = SendOrderStatisticsService::get_new_subscribe_charge_rate($new_subscribe_charge_user_num,$new_subscribe_user_num);
-
- $data['send_order_statistics'] = [
- 'real_send_order_num' =>$real_send_order_num,
- 'force_subscribe_rate' => $force_subscribe_rate.'%',
- 'inner_send_order_num' => $inner_send_order_num,
- 'new_subscribe_user_num' => $new_subscribe_user_num,
- 'new_subscribe_charge_user_num' => $new_subscribe_charge_user_num,
- 'new_subscribe_charge_amount' => $new_subscribe_charge_amount,
- 'new_subscribe_cost' => $new_subscribe_cost,
- 'new_subscribe_3day_out' => $new_subscribe_3day_out,
- 'new_subscribe_re_charge_user_num' => $small_re_charge_user_num,
- 'new_subscribe_re_filling_rate' => $new_subscribe_re_filling_rate.'%',
- 'new_subscribe_charge_rate' => $new_subscribe_charge_rate.'%',
- 'charge_register_rate' => $charge_user_num.'/'.$new_register_user_num,
- ];
-
-
- \Log::info('get_top_send_order_num_books_start:'.$distribution_channel_id);
- $top_send_order_num_books = SendOrderStatisticsService::get_top_send_order_num_books($distribution_channel_id,$date['small_start_day'],$date['small_end_day']);
- \Log::info('get_top_charge_register_books_start:'.$distribution_channel_id);
- $top_charge_register_books = SendOrderStatisticsService::get_top_charge_register_books($distribution_channel_id,$date['small_start_day'],$date['small_end_day']);
-
- $data['book_statistics'] = [
- 'top_send_order_num_books' => object_to_array($top_send_order_num_books),
- 'top_charge_register_books' => object_to_array($top_charge_register_books)
- ];
- }catch(Exception $e){
- \Log::info('getWeeklySendOrderStatistics_ept:'.$e->getMessage());
- }
-
- Redis::set($weekly_report_key,json_encode($data));
-
- \Log::info('getWeeklySendOrderStatistics_end:'.$distribution_channel_id);
- \Log::info($data);
- return $data;
- }
-
- }
|