GenerateCpSubDayStat.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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\Sub;
  9. use Log;
  10. use Illuminate\Console\Command;
  11. use DB;
  12. class GenerateCpSubDayStat extends Command
  13. {
  14. /**
  15. * 执行命令 php artisan generate_order_day_stat
  16. *
  17. * The name and signature of the console command.
  18. *
  19. * @var string
  20. */
  21. protected $signature = 'generate_cp_sub_day_stat';
  22. /**
  23. * The console command description.
  24. *
  25. * @var string
  26. */
  27. protected $description = 'CP日订阅统计数据生成';
  28. /**
  29. * Execute the console command.
  30. *
  31. * @return mixed
  32. */
  33. public function handle()
  34. {
  35. print_r("======CP日订阅统计数据生成 【任务执行开始】=====".date("y-m-d H:i:s"."\n"));
  36. Log::info("======CP日订阅统计数据生成 【任务执行开始】=====".date("y-m-d H:i:s"."\n"));
  37. $cps = [
  38. 'mianhua','guijj'
  39. ];
  40. $end_date = "2018-01-01";
  41. foreach ($cps as $cp)
  42. {
  43. $data = [];
  44. //取cp下所有bid
  45. $books = DB::select("select bid,books.name from books,book_configs where books.id = book_configs.bid and cp_source = '{$cp}' ");
  46. dump('books');dump($books);
  47. $book_data = [];
  48. foreach ($books as $book)
  49. {
  50. $book_data[$book->bid] = $book->name;
  51. $data[$book->bid]['book_name'] = $book->name;
  52. $data[$book->bid]['count'] = 0;
  53. $data[$book->bid]['charge_balance'] = 0;
  54. $data[$book->bid]['book_count'] = 0;
  55. $data[$book->bid]['book_charge_balance'] = 0;
  56. }
  57. dump('book_data');dump($book_data);
  58. $bids = implode(',',array_keys($book_data));
  59. dump('bids');dump($bids);
  60. //遍历所有订阅表查寻章节订购数、金额
  61. for($i=0;$i<512;$i++)
  62. {
  63. $table_datas = DB::connection('chapter_order_mysql')->select("select bid,count(1) count,sum(charge_balance) charge_balance from chapter_orders{$i} where created_at <'{$end_date}' and bid in ({$bids}) group by bid");
  64. foreach ($table_datas as $item)
  65. {
  66. $data[$item->bid]['count'] += $item->count;
  67. $data[$item->bid]['charge_balance'] += $item->charge_balance;
  68. }
  69. }
  70. $charge_book_data = DB::select("select bid,count(1) count,sum(charge_balance) charge_balance from book_orders where created_at <'{$end_date}' and bid in ({$bids}) group by bid");
  71. foreach ($charge_book_data as $item_book)
  72. {
  73. $data[$item_book->bid]['book_count'] += $item_book->count;
  74. $data[$item_book->bid]['book_charge_balance'] += $item_book->charge_balance;
  75. }
  76. foreach ($data as $bid=>$_item)
  77. {
  78. $_data = [
  79. 'date'=>'2017-12-12',
  80. 'cp_name'=>$cp,
  81. 'bid'=>$bid,
  82. 'book_name'=>$book_data[$bid],
  83. 'book_name'=>$book_data[$bid],
  84. 'chapter_sub_num'=>$_item['count'],
  85. 'chapter_sub_amount'=>$_item['charge_balance'],
  86. 'book_sub_num'=>$_item['book_count'],
  87. 'book_sub_amount'=>$_item['book_charge_balance']
  88. ];
  89. DB::table('cp_day_subs')->insert($_data);
  90. }
  91. }
  92. Log::info("======CP日订阅统计数据生成 【任务执行结束】=====".date("y-m-d H:i:s"."\n"));
  93. print_r("======CP日订阅统计数据生成 【任务执行结束】=====".date("y-m-d H:i:s"."\n"));
  94. }
  95. }