ChapterOrderTotal.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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->cpChargeAndReward();
  43. //$this->cpData();
  44. }
  45. if($option == 'order'){
  46. $this->orderDayStat();
  47. //$this->orderDayStatAll();
  48. }
  49. }
  50. public function sub1(){
  51. $start = date('Y-m-d',time()-86400);
  52. $end = date('Y-m-d');
  53. $sql = 'call cp_data_temp("'.$start.'","'.$end.'");';
  54. DB::connection('chapter_order_mysql')->select($sql);
  55. $bids = DB::table('book_configs')->select('bid')->get();
  56. $data = [];
  57. $day = date('Y--m-d',time()-86400);
  58. foreach ($bids as $book){
  59. $fee = Redis::hget('wap:chapterandbookorder:bid:'.$book->bid,$day);
  60. if(!$fee) $fee = 0;
  61. $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')];
  62. Redis::hdel('wap:chapterandbookorder:bid:'.$book->bid,$day);
  63. }
  64. DB::table('book_order_statistical')->insert($data);
  65. $end2 = $start.' 23:59:59';
  66. $sql2 = 'call chapter_orders("'.$start.'","'.$end2.'");';
  67. DB::connection('chapter_order_mysql')->select($sql2);
  68. }
  69. public function cpData(){
  70. $sql = "select bid,date,sum(sum_charge_balance) as sum_charge_balance, sum(sum_reward_balance) as sum_reward_balance from sub_bak5 GROUP by bid,date";
  71. $chapter = DB::connection('chapter_order_mysql')->select($sql);
  72. Log::info($chapter);
  73. foreach ($chapter as $v){
  74. DB::table('book_order_statistical')->where('bid',$v->bid)->where('day',$v->date)->update(['charge_balance'=>(int)$v->sum_charge_balance,'reward_balance'=>(int)$v->sum_reward_balance]);
  75. $v = null;
  76. }
  77. $sql = "select bid,date(created_at) as date,sum(charge_balance) as sum_charge_balance ,sum(reward_balance) as sum_reward_balance from book_orders GROUP by bid,date(created_at)";
  78. $book = DB::select($sql);
  79. foreach ($book as $v){
  80. DB::table('book_order_statistical')->where('bid',$v->bid)->where('day',$v->date)->increment('charge_balance',(int)$v->sum_charge_balance);
  81. DB::table('book_order_statistical')->where('bid',$v->bid)->where('day',$v->date)->increment('reward_balance',(int)$v->sum_reward_balance);
  82. }
  83. }
  84. public function cpChargeAndReward(){
  85. $date = date('Y-m-d',time()-86400);
  86. $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 date(created_at)='%s' GROUP by bid,date(created_at)";
  87. echo sprintf($sql_format,$date);
  88. $book = DB::select(sprintf($sql_format,$date));
  89. foreach ($book as $v){
  90. DB::table('book_order_statistical')->where('bid',$v->bid)->where('day',$v->date)->increment('charge_balance',(int)$v->sum_charge_balance);
  91. DB::table('book_order_statistical')->where('bid',$v->bid)->where('day',$v->date)->increment('reward_balance',(int)$v->sum_reward_balance);
  92. }
  93. $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";
  94. $chapter = DB::connection('chapter_order_mysql')->select(sprintf($sql,$date));
  95. foreach ($chapter as $v){
  96. DB::table('book_order_statistical')->where('bid',$v->bid)->where('day',$v->date)->increment('charge_balance',(int)$v->sum_charge_balance);
  97. DB::table('book_order_statistical')->where('bid',$v->bid)->where('day',$v->date)->increment('reward_balance',(int)$v->sum_reward_balance);
  98. }
  99. }
  100. public function orderDayStatAll(){
  101. DB::table('order_day_stats')->select('id', 'distribution_channel_id', 'date')->orderBy('id')->chunk(1000, function ($res) {
  102. foreach ($res as $v) {
  103. $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'";
  104. $chapter_data = DB::connection('chapter_order_mysql')->select(sprintf($chapter_sql,$v->distribution_channel_id,$v->date));
  105. DB::table('send_orders_stats')->where('id',$v->id)->update([
  106. 'fee'=>(int)$chapter_data[0]->sum_fee,
  107. 'charge_balance'=>(int)$chapter_data[0]->sum_charge_balance,
  108. 'reward_balance'=>(int)$chapter_data[0]->sum_reward_balance,
  109. ]);
  110. $end_date = date('Y-m-d',strtotime($v->date)+86400);
  111. $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=%s and created_at >='{$v->date}' and created_at < '{$end_date}'";
  112. $chapter_data = DB::select($book_sql);
  113. $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;
  114. DB::update($update_sql);
  115. }
  116. });
  117. }
  118. public function orderDayStat(){
  119. 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) {
  120. foreach ($res as $v) {
  121. $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'";
  122. $chapter_data = DB::connection('chapter_order_mysql')->select(sprintf($chapter_sql,$v->distribution_channel_id,$v->date));
  123. $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;
  124. DB::update($update_sql);
  125. $end_date = date('Y-m-d',strtotime($v->date)+86400);
  126. $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}'";
  127. $chapter_data = DB::select($book_sql);
  128. $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;
  129. DB::update($update_sql);
  130. }
  131. });
  132. }
  133. }