StatisticsNissaya.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Models\Channel;
  5. use App\Models\Sentence;
  6. use Illuminate\Support\Facades\Storage;
  7. use Carbon\Carbon;
  8. class StatisticsNissaya extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. * php artisan statistics:nissaya
  13. * @var string
  14. */
  15. protected $signature = 'statistics:nissaya';
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = '统计nissaya 每月录入进度';
  22. /**
  23. * Create a new command instance.
  24. *
  25. * @return void
  26. */
  27. public function __construct()
  28. {
  29. parent::__construct();
  30. }
  31. /**
  32. * Execute the console command.
  33. *
  34. * @return int
  35. */
  36. public function handle()
  37. {
  38. if(\App\Tools\Tools::isStop()){
  39. return 0;
  40. }
  41. $nissaya_channels = Channel::where('type','nissaya')->select('uid')->get();
  42. $this->info('channel:'.count($nissaya_channels));
  43. $file = "public/statistics/nissaya-monthly.csv";
  44. Storage::disk('local')->put($file, "");
  45. #按月获取数据
  46. $firstDay = Sentence::whereIn('channel_uid',$nissaya_channels)
  47. ->orderBy('created_at')
  48. ->select('created_at')
  49. ->first();
  50. $firstDay = strtotime($firstDay->created_at);
  51. $firstMonth = Carbon::create(date("Y-m",$firstDay));
  52. $now = Carbon::now();
  53. $current = $firstMonth;
  54. $sumStrlen = 0;
  55. $sumCount = 0;
  56. while ($current <= $now) {
  57. # code...
  58. $start = Carbon::create($current)->startOfMonth();
  59. $end = Carbon::create($current)->endOfMonth();
  60. $date = $current->format('Y-m');
  61. $table = Sentence::whereIn('channel_uid',$nissaya_channels)
  62. ->whereDate('created_at','>=',$start)
  63. ->whereDate('created_at','<=',$end);
  64. $strlen = $table->sum('strlen');
  65. $sumStrlen += $strlen;
  66. $count = $table->count();
  67. $sumCount += $count;
  68. $editor = Sentence::whereIn('channel_uid',$nissaya_channels)
  69. ->whereDate('created_at','>=',$start)
  70. ->whereDate('created_at','<=',$end)
  71. ->groupBy('editor_uid')
  72. ->select('editor_uid')->get();
  73. $info = "{$date},{$strlen},{$sumStrlen},{$count},{$sumCount},".count($editor);
  74. $this->info($info);
  75. Storage::disk('local')->append($file, $info);
  76. $current->addMonth(1);
  77. }
  78. $this->info('path='.$file);
  79. return 0;
  80. }
  81. }