ChapterOrderTotal.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use DB;
  5. use Redis;
  6. use Log;
  7. class ChapterOrderTotal extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'book:cot {--type=}';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = '更新章节订购统计';
  21. /**
  22. * Create a new command instance.
  23. *
  24. * @return void
  25. */
  26. public function __construct()
  27. {
  28. parent::__construct();
  29. }
  30. /**
  31. * Execute the console command.
  32. *
  33. * @return mixed
  34. */
  35. public function handle()
  36. {
  37. $option = $this->option('type');
  38. if(!$option){
  39. $this->sub1();
  40. }
  41. if($option == 'cp'){
  42. $this->cpChargeAndRewardV2();
  43. }
  44. if($option == 'order'){
  45. $this->orderDayStat();
  46. }
  47. }
  48. private function sub1(){
  49. $start = date('Y-m-d',time()-86400);
  50. $end = date('Y-m-d');
  51. $sql = 'call cp_data_temp("'.$start.'","'.$end.'");';
  52. DB::connection('chapter_order_mysql')->select($sql);
  53. $bids = DB::table('book_configs')->select('bid')->get();
  54. $data = [];
  55. $day = date('Y--m-d',time()-86400);
  56. foreach ($bids as $book){
  57. $fee = Redis::hget('wap:chapterandbookorder:bid:'.$book->bid,$day);
  58. if(!$fee) $fee = 0;
  59. $data[] = ['bid'=>$book->bid,'day'=>$day,'fee'=>$fee,'created_at'=>date('Y-m-d H:i:s'),'updated_at'=>date('Y-m-d H:i:s')];
  60. if(count($data) == 100){
  61. DB::table('book_order_statistical')->insert($data);
  62. $data = [];
  63. }
  64. Redis::hdel('wap:chapterandbookorder:bid:'.$book->bid,$day);
  65. }
  66. DB::table('book_order_statistical')->insert($data);
  67. $end2 = $start.' 23:59:59';
  68. $sql2 = 'call chapter_orders("'.$start.'","'.$end2.'");';
  69. DB::connection('chapter_order_mysql')->select($sql2);
  70. }
  71. private function cpChargeAndReward(){
  72. $date = date('Y-m-d',time()-86400);
  73. $start_date = $date;
  74. $end_date = date('Y-m-d');
  75. $sql_format = "select bid,date(created_at) as date,sum(charge_balance) as sum_charge_balance ,sum(reward_balance) as sum_reward_balance from book_orders where created_at >'%s' AND created_at <'%s' GROUP by bid,date(created_at)";
  76. echo sprintf($sql_format,$start_date,$end_date);
  77. $book = DB::select(sprintf($sql_format,$start_date,$end_date));
  78. foreach ($book as $v){
  79. DB::table('book_order_statistical')->where('bid',$v->bid)->where('day',$v->date)->increment('charge_balance',(int)$v->sum_charge_balance);
  80. DB::table('book_order_statistical')->where('bid',$v->bid)->where('day',$v->date)->increment('reward_balance',(int)$v->sum_reward_balance);
  81. }
  82. $sql = "select bid,date,sum(sum_charge_balance) as sum_charge_balance, sum(sum_reward_balance) as sum_reward_balance from sub_bak5 where date='%s' GROUP by bid,date";
  83. $chapter = DB::connection('chapter_order_mysql')->select(sprintf($sql,$date));
  84. foreach ($chapter as $v){
  85. DB::table('book_order_statistical')->where('bid',$v->bid)->where('day',$v->date)->increment('charge_balance',(int)$v->sum_charge_balance);
  86. DB::table('book_order_statistical')->where('bid',$v->bid)->where('day',$v->date)->increment('reward_balance',(int)$v->sum_reward_balance);
  87. }
  88. }
  89. private function cpChargeAndRewardV2(){
  90. $date = date('Y-m-d',time()-86400);
  91. $max_bid_info = DB::table('books')->select('id')->orderBy('id','desc')->first();
  92. $max_bid = $max_bid_info->id;
  93. $start_date = $date;
  94. $end_date = date('Y-m-d');
  95. $sql_format = "select bid,date(created_at) as date,sum(charge_balance) as sum_charge_balance ,sum(reward_balance) as sum_reward_balance from book_orders where created_at >'%s' AND created_at <'%s' GROUP by bid,date(created_at)";
  96. $book = DB::select(sprintf($sql_format,$start_date,$end_date));
  97. foreach ($book as $v){
  98. DB::table('book_order_statistical')->where('bid',$v->bid)->where('day',$v->date)->increment('charge_balance',(int)$v->sum_charge_balance);
  99. DB::table('book_order_statistical')->where('bid',$v->bid)->where('day',$v->date)->increment('reward_balance',(int)$v->sum_reward_balance);
  100. }
  101. $sql = "select bid,date,sum(sum_charge_balance) as sum_charge_balance, sum(sum_reward_balance) as sum_reward_balance from sub_bak5 where date='%s' and bid=%s";
  102. for($i =0;$i <= $max_bid ;$i++){
  103. $temp = DB::connection('chapter_order_mysql')->select(sprintf($sql,$date,$i));
  104. $temp = $temp[0];
  105. if($temp->bid){
  106. DB::table('book_order_statistical')->where('bid',$i)->where('day',$date)->increment('charge_balance',(int)$temp->sum_charge_balance);
  107. DB::table('book_order_statistical')->where('bid',$i)->where('day',$date)->increment('reward_balance',(int)$temp->sum_reward_balance);
  108. }
  109. }
  110. }
  111. private function orderDayStat(){
  112. DB::table('order_day_stats')->select('id', 'distribution_channel_id', 'date')->where('date',date('Y-m-d',time()-86400))->orderBy('id')->chunk(1000, function ($res) {
  113. foreach ($res as $v) {
  114. $chapter_sql = "select sum(sum_fee) as sum_fee,sum(sum_charge_balance) as sum_charge_balance,sum(sum_reward_balance) as sum_reward_balance from sub_bak5 where distribution_channel_id=%s and date='%s'";
  115. $chapter_data = DB::connection('chapter_order_mysql')->select(sprintf($chapter_sql,$v->distribution_channel_id,$v->date));
  116. $update_sql = "update send_orders_stats set sum_fee=sum_fee+".(int)$chapter_data[0]->sum_fee.',sum_charge_balance=sum_charge_balance+'.(int)$chapter_data[0]->sum_charge_balance.',sum_reward_balance=sum_reward_balance+'.(int)$chapter_data[0]->sum_reward_balance.' where id='.$v->id;
  117. DB::update($update_sql);
  118. $end_date = date('Y-m-d',strtotime($v->date)+86400);
  119. $book_sql = "select sum(fee) as sum_fee,sum(charge_balance) as sum_charge_balance,sum(reward_balance) as sum_reward_balance from book_orders where distribution_channel_id={$v->distribution_channel_id} and created_at >='{$v->date}' and created_at < '{$end_date}'";
  120. $chapter_data = DB::select($book_sql);
  121. $update_sql = "update send_orders_stats set sum_fee=sum_fee+".(int)$chapter_data[0]->sum_fee.',sum_charge_balance=sum_charge_balance+'.(int)$chapter_data[0]->sum_charge_balance.',sum_reward_balance=sum_reward_balance+'.(int)$chapter_data[0]->sum_reward_balance.' where id='.$v->id;
  122. DB::update($update_sql);
  123. }
  124. });
  125. }
  126. }