BookAttr.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Modules\Book\Models\Chapter;
  4. use Illuminate\Console\Command;
  5. use DB;
  6. use Redis;
  7. class BookAttr extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'book:attr {--start=} {--end=}';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = 'Command 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. //$this->tempReadRecord();
  38. //print_r($res);
  39. $start = $this->option('start');
  40. $end = $this->option('end');
  41. if($start && $end){
  42. while (strtotime($start)<=strtotime($end)){
  43. //echo $start.PHP_EOL;
  44. $this->sendOrderDayAttr($start);
  45. $start = date('Y-m-d',strtotime($start)+86400);
  46. }
  47. }else{
  48. $this->sendOrderDayAttr(date('Y-m-d',time()-86400));
  49. }
  50. }
  51. private function sendOrderDayAttr($day){
  52. $res = DB::table('send_orders')
  53. ->where('created_at','>=',$day)
  54. ->where('created_at','<=',$day.' 23:59:59')
  55. ->select('book_id','distribution_channel_id',DB::raw('count(*) as counts'))
  56. ->groupBy('book_id')
  57. ->groupBy('distribution_channel_id')
  58. ->get();
  59. $data = [];
  60. foreach ($res as $v){
  61. if($v->book_id){
  62. $data[] = [
  63. 'bid'=>$v->book_id,
  64. 'num'=>$v->counts,
  65. 'distribution_channel_id'=>$v->distribution_channel_id,
  66. 'day'=>$day,
  67. 'created_at'=>date('Y-m-d H:i:s'),
  68. 'updated_at'=>date('Y-m-d H:i:s')
  69. ] ;
  70. }
  71. }
  72. DB::table('book_send_order_stats')->insert($data);
  73. }
  74. private function tempReadRecord(){
  75. $data = [];
  76. $book_chapter_seq = [];
  77. $j = 1;
  78. for ($i = 10000;$i<=24139688;$i++){
  79. $read_bids = Redis::hgetall('book_read:' . $i);
  80. if(!$read_bids)
  81. continue;
  82. foreach ($read_bids as $key=>$v){
  83. if($key == 'last_read' || $key == 'send_order_id'){
  84. continue;
  85. }
  86. if(!isset($book_chapter_seq[$key])){
  87. $book_chapter_seq[$key] = $this->chapters($key);
  88. }
  89. $record = explode('_', $v);
  90. $cid = 0;
  91. if(isset($record[0])){
  92. $cid = $record[0];
  93. }
  94. $seq = 0;
  95. if(isset($book_chapter_seq[$key][$cid])){
  96. $seq = $book_chapter_seq[$key][$cid];
  97. }
  98. $data[] = [
  99. 'bid'=>$key,
  100. 'uid'=>$i,
  101. 'cid'=>$cid,
  102. 'seq'=>$seq,
  103. 'created_at'=>date('Y-m-d H:i:s'),
  104. 'updated_at'=>date('Y-m-d H:i:s')
  105. ];
  106. $j++;
  107. if($j%1000 == 0){
  108. DB::table('read_record_temp')->insert($data);
  109. $data = [];
  110. }
  111. }
  112. }
  113. DB::table('read_record_temp')->insert($data);
  114. }
  115. private function chapters($bid){
  116. return Chapter::where('bid',$bid)->select('id','sequence')->get()->pluck('sequence','id')->all();
  117. }
  118. }