GenerateOrderDayStat.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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\SendOrder\Services\SendOrderForceDayStatService;
  10. use App\Modules\Trade\Models\OrderDayStat;
  11. use App\Modules\Trade\Services\OrderDayStatService;
  12. use App\Modules\Channel\Services\ChannelService;
  13. use App\Modules\Trade\Services\OrderStatService;
  14. use Log;
  15. use Illuminate\Console\Command;
  16. use DB;
  17. class GenerateOrderDayStat extends Command
  18. {
  19. /**
  20. * 执行命令 php artisan generate_order_day_stat
  21. *
  22. * The name and signature of the console command.
  23. *
  24. * @var string
  25. */
  26. protected $signature = 'generate_order_day_stat';
  27. /**
  28. * The console command description.
  29. *
  30. * @var string
  31. */
  32. protected $description = '渠道订单日统计数据生成';
  33. /**
  34. * Execute the console command.
  35. *
  36. * @return mixed
  37. */
  38. public function handle()
  39. {
  40. print_r("======渠道订单日统计数据生成 【任务执行开始】=====".date("y-m-d H:i:s"."\n"));
  41. Log::info("======渠道订单日统计数据生成 【任务执行开始】=====".date("y-m-d H:i:s"."\n"));
  42. $channels = ChannelService::getAllChannels();
  43. $date = date('Y-m-d',strtotime('-1 day'));
  44. $end_time = date('Y-m-d H:i:s',strtotime($date) + 86400-1);
  45. if(count($channels))
  46. {
  47. $min_uid = DB::table('users')->where('created_at','>=',$date)->min('id');
  48. $max_uid = DB::table('users')->where('created_at','>=',$date)->where('created_at','<=',$end_time)->max('id');
  49. $once_num = 5000;
  50. $offset = 0;
  51. //实际注册用户数
  52. $start_uid = $min_uid;
  53. $real_reg_data = [];
  54. while (true)
  55. {
  56. $offset += $once_num;
  57. $end_user = DB::table('users')->select('id')->where('created_at','>=',$date)->where('created_at','<=',$end_time)->skip($offset)->limit(1)->first();
  58. $end_uid = $end_user ? $end_user->id : $max_uid;
  59. $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");
  60. foreach ($reg_data as $_reg_data)
  61. {
  62. @$real_reg_data[$_reg_data->distribution_channel_id] += (int)$_reg_data->num;
  63. }
  64. if($end_uid == $max_uid) break;
  65. $start_uid = $end_uid;
  66. }
  67. $channels->each(function($channel) use($date,$real_reg_data){
  68. $begin_time = strtotime($date)+86400;
  69. $real_reg_num = isset($real_reg_data[$channel->id]) ? $real_reg_data[$channel->id] : 0;
  70. if(strtotime($channel->created_at) <= $begin_time) OrderDayStatService::add($channel->id,$channel->channel_user_id, $date,$channel->nickname,$real_reg_num);
  71. });
  72. print_r("------订单汇总表更新start---");
  73. Log::info("======订单汇总表更新 【任务执行开始】=====".date("y-m-d H:i:s"."\n"));
  74. $channels->each(function($channel) use($date){
  75. OrderStatService::add($channel->id, $date);
  76. });
  77. Log::info("======订单汇总表更新 【任务执行结束】=====".date("y-m-d H:i:s"."\n"));
  78. print_r("-----订单汇总表更新end----");
  79. //更新订阅
  80. $this->generate_temp_table($date,$end_time);
  81. $subscribes = $this->getChannelSubscribeNumAndAmount($date);
  82. $channels->each(function($channel) use($subscribes,$date){
  83. $subscribe_num=0;
  84. $subscribe_amount=0;
  85. if(array_key_exists($channel->id,$subscribes)){
  86. $subscribe_num = $subscribes[$channel->id]['user_num'];
  87. $subscribe_amount = $subscribes[$channel->id]['sum_fee'];
  88. }
  89. OrderDayStat::where('date',$date)
  90. ->where('distribution_channel_id',$channel->id)
  91. ->update(['subscribe_user_num'=>$subscribe_num,'subscribe_amount'=>$subscribe_amount]);
  92. });
  93. /*$gender_recharges = OrderDayStatService::getRechargeByGender($date,$end_time);
  94. foreach ($gender_recharges as $key=>$gender_recharge) {
  95. $order_day_stats = OrderDayStat::where('distribution_channel_id',$key)->where('date',$date)->first();
  96. if($order_day_stats){
  97. $male_channel_recharge=$female_channel_recharge =0;
  98. $gender_total = 0;
  99. if(isset($gender_recharge[1])) {
  100. $male_channel_recharge = $gender_recharge[1];
  101. $gender_total += $male_channel_recharge;
  102. }
  103. if(isset($gender_recharge[2])) {
  104. $female_channel_recharge = $gender_recharge[2];
  105. $gender_total += $female_channel_recharge;
  106. }
  107. $left=$order_day_stats->total_recharge_amount -$gender_total;
  108. $male_channel_recharge +=$left*($male_channel_recharge/$gender_total);
  109. $female_channel_recharge +=$left*($female_channel_recharge/$gender_total);
  110. $order_day_stats->male_channel_recharge=$male_channel_recharge;
  111. $order_day_stats->female_channel_recharge=$female_channel_recharge;
  112. $order_day_stats->save();
  113. }
  114. }*/
  115. //$date = date('Y-m-d',strtotime('-'.$i.' day'));
  116. /*sleep(5);
  117. $end_order_stats = DB::table('send_orders_force_day_stats')
  118. ->join('books','send_orders_force_day_stats.bid','=','books.id')
  119. ->join('book_categories','book_categories.id','=','books.category_id')
  120. ->where('send_orders_force_day_stats.date',$date)
  121. ->select([DB::raw('sum(send_orders_force_day_stats.recharge_amount) as total_recharge'),'pid','send_orders_force_day_stats.distribution_channel_id'])
  122. ->groupBy('send_orders_force_day_stats.distribution_channel_id')
  123. ->groupBy('book_categories.pid')
  124. ->get();
  125. $data_format = [];
  126. if($end_order_stats) {
  127. foreach ($end_order_stats as $key=>$end_order_stat) {
  128. if(array_key_exists($end_order_stat->distribution_channel_id,$data_format)) {
  129. $data_format[$end_order_stat->distribution_channel_id][$end_order_stat->pid]=$end_order_stat->total_recharge;
  130. }else{
  131. $data_format[$end_order_stat->distribution_channel_id]=array();
  132. $data_format[$end_order_stat->distribution_channel_id][$end_order_stat->pid]=$end_order_stat->total_recharge;
  133. }
  134. }
  135. foreach ($data_format as $k=>$each){
  136. $male_recharge = $female_recharge = 0;
  137. $total_recharge = 0;
  138. foreach ($each as $kk=>$v){
  139. if($kk==1) {
  140. $male_recharge = $v;
  141. $total_recharge +=$v;
  142. }
  143. if($kk==2) {
  144. $female_recharge =$v;
  145. $total_recharge +=$v;
  146. }
  147. }
  148. $order_day_stats = OrderDayStat::where('distribution_channel_id',$k)->where('date',$date)->first();
  149. if($order_day_stats) {
  150. $left = $order_day_stats->total_recharge_amount-$total_recharge;
  151. if($male_recharge>0 || $female_recharge>0) {
  152. $male_total = $male_recharge+($male_recharge/$total_recharge)*$left;
  153. $female_total = $female_recharge+($female_recharge/$total_recharge)*$left;
  154. }else{
  155. $male_total = ($order_day_stats->total_recharge_amount)*0.2;
  156. $female_total = ($order_day_stats->total_recharge_amount)*0.8;
  157. }
  158. $order_day_stats->male_channel_recharge = $male_total;
  159. $order_day_stats->female_channel_recharge = $female_total;
  160. $order_day_stats->save();
  161. }
  162. }
  163. }
  164. $order_day_statss = OrderDayStat::whereNull('male_channel_recharge')->whereNull('female_channel_recharge')->where('date',$date)->get();
  165. foreach($order_day_statss as $order_day_stats) {
  166. $order_day_stats->male_channel_recharge = ($order_day_stats->total_recharge_amount)*0.2;
  167. $order_day_stats->female_channel_recharge = ($order_day_stats->total_recharge_amount)*0.8;
  168. $order_day_stats->save();
  169. }
  170. $end_order_stats = DB::table('send_orders_force_day_stats')
  171. ->join('books','send_orders_force_day_stats.bid','=','books.id')
  172. ->join('book_categories','book_categories.id','=','books.category_id')
  173. ->where('send_orders_force_day_stats.date',$date)
  174. ->select([DB::raw('sum(send_orders_force_day_stats.recharge_amount) as total_recharge'),'pid'])
  175. ->groupBy('book_categories.pid')
  176. ->get();
  177. $male_recharge=$female_recharge=0;
  178. foreach ($end_order_stats as $end_order_stat){
  179. if($end_order_stat->pid==1) $male_recharge = $end_order_stat->total_recharge;
  180. if($end_order_stat->pid==2) $female_recharge = $end_order_stat->total_recharge;
  181. }
  182. $insert_data = compact('male_recharge','female_recharge');
  183. $insert_data['date'] = $date;
  184. $insert_data['created_at']=date('Y-m-d H:i:s');
  185. $insert_data['updated_at']=date('Y-m-d H:i:s');
  186. DB::table('order_gender_stats')->insert($insert_data);*/
  187. }
  188. Log::info("======渠道订单日统计数据生成 【任务执行结束】=====".date("y-m-d H:i:s"."\n"));
  189. print_r("======渠道订单日统计数据生成 【任务执行结束】=====".date("y-m-d H:i:s"."\n"));
  190. }
  191. protected function generate_temp_table($date,$end_time){
  192. DB::connection('chapter_order_mysql')->update("truncate table temp_chapter_order");
  193. DB::connection('chapter_order_mysql')->update("call temp_chapter_order_update('{$date}','{$end_time}')");
  194. DB::table('book_orders')
  195. ->select(['uid','distribution_channel_id',DB::raw("date(created_at) as date"),DB::raw("sum(fee) as fee")])
  196. ->whereBetween('created_at',[$date,$end_time])
  197. ->groupBy('uid')
  198. ->orderBy('id')
  199. ->chunk(5000,function($book_orders){
  200. $book_orders=json_decode(json_encode($book_orders),true);
  201. DB::connection('chapter_order_mysql')->table('temp_chapter_order')->insert($book_orders);
  202. });
  203. }
  204. protected function getChannelSubscribeNumAndAmount($date){
  205. $orders = DB::connection('chapter_order_mysql')
  206. ->table('temp_chapter_order')
  207. ->select(DB::raw("distribution_channel_id,count(distinct uid) as user_num,sum(fee) as sum_fee"))
  208. ->where('date',$date)
  209. ->groupBy('distribution_channel_id')
  210. ->get();
  211. $res=[];
  212. foreach ($orders as $order){
  213. $res[$order->distribution_channel_id] = json_decode(json_encode($order),true);
  214. }
  215. return $res;
  216. }
  217. }