BookAttr.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. $this->sendOrderSevenDayAttr();
  50. }
  51. }
  52. private function sendOrderDayAttr($day){
  53. $res = DB::table('send_orders')
  54. ->where('created_at','>=',$day)
  55. ->where('created_at','<=',$day.' 23:59:59')
  56. ->select('book_id','distribution_channel_id',DB::raw('count(*) as counts'))
  57. ->groupBy('book_id')
  58. ->groupBy('distribution_channel_id')
  59. ->get();
  60. $data = [];
  61. foreach ($res as $v){
  62. if($v->book_id){
  63. $book_total_send_order_stats = DB::table('book_total_send_order_stats')->where('bid',$v->book_id)->first();
  64. if($book_total_send_order_stats){
  65. DB::table('book_total_send_order_stats')->where('bid',$v->book_id)->increment('num',$v->counts,['updated_at'=>date('Y-m-d H:i:s')]);
  66. }else{
  67. DB::table('book_total_send_order_stats')->insert([
  68. 'bid'=>$v->book_id,
  69. 'num'=>$v->counts,
  70. 'created_at'=>date('Y-m-d H:i:s'),
  71. 'updated_at'=>date('Y-m-d H:i:s')
  72. ]);
  73. }
  74. $channel_book_total_send_order_stats =DB::table('channel_book_total_send_order_stats')
  75. ->where('bid',$v->book_id)
  76. ->where('distribution_channel_id',$v->distribution_channel_id)
  77. ->first();
  78. if($channel_book_total_send_order_stats){
  79. DB::table('channel_book_total_send_order_stats')
  80. ->where('bid',$v->book_id)
  81. ->where('distribution_channel_id',$v->distribution_channel_id)
  82. ->increment('num',$v->counts,['updated_at'=>date('Y-m-d H:i:s')]);
  83. }else{
  84. DB::table('channel_book_total_send_order_stats')->insert([
  85. 'bid'=>$v->book_id,
  86. 'num'=>$v->counts,
  87. 'distribution_channel_id'=>$v->distribution_channel_id,
  88. 'created_at'=>date('Y-m-d H:i:s'),
  89. 'updated_at'=>date('Y-m-d H:i:s')
  90. ]);
  91. }
  92. $data[] = [
  93. 'bid'=>$v->book_id,
  94. 'num'=>$v->counts,
  95. 'distribution_channel_id'=>$v->distribution_channel_id,
  96. 'day'=>$day,
  97. 'created_at'=>date('Y-m-d H:i:s'),
  98. 'updated_at'=>date('Y-m-d H:i:s')
  99. ] ;
  100. }
  101. }
  102. DB::table('book_send_order_stats')->insert($data);
  103. }
  104. private function sendOrderSevenDayAttr(){
  105. $result = DB::table('book_total_send_order_stats')->select('id','bid')->get();
  106. foreach ($result as $value) {
  107. $num = DB::table('book_send_order_stats')->where('bid',$value->bid)->where('day','>=',date('Y-m-d', strtotime('-7 day')))->sum('num');
  108. DB::table('book_total_send_order_stats')->where('id',$value->id)->update(['seven_day_num'=>$num]);
  109. }
  110. }
  111. private function tempReadRecord(){
  112. $data = [];
  113. $book_chapter_seq = [];
  114. $j = 1;
  115. for ($i = 10000;$i<=24139688;$i++){
  116. $read_bids = Redis::hgetall('book_read:' . $i);
  117. if(!$read_bids)
  118. continue;
  119. foreach ($read_bids as $key=>$v){
  120. if($key == 'last_read' || $key == 'send_order_id'){
  121. continue;
  122. }
  123. if(!isset($book_chapter_seq[$key])){
  124. $book_chapter_seq[$key] = $this->chapters($key);
  125. }
  126. $record = explode('_', $v);
  127. $cid = 0;
  128. if(isset($record[0])){
  129. $cid = $record[0];
  130. }
  131. $seq = 0;
  132. if(isset($book_chapter_seq[$key][$cid])){
  133. $seq = $book_chapter_seq[$key][$cid];
  134. }
  135. $data[] = [
  136. 'bid'=>$key,
  137. 'uid'=>$i,
  138. 'cid'=>$cid,
  139. 'seq'=>$seq,
  140. 'created_at'=>date('Y-m-d H:i:s'),
  141. 'updated_at'=>date('Y-m-d H:i:s')
  142. ];
  143. $j++;
  144. if($j%1000 == 0){
  145. DB::table('read_record_temp')->insert($data);
  146. $data = [];
  147. }
  148. }
  149. }
  150. DB::table('read_record_temp')->insert($data);
  151. }
  152. private function chapters($bid){
  153. return Chapter::where('bid',$bid)->select('id','sequence')->get()->pluck('sequence','id')->all();
  154. }
  155. }