StatisticsNissaya.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. *
  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_channel = Channel::where('type','nissaya')->select('uid')->get();
  42. $this->info('channel:'.count($nissaya_channel));
  43. $maxDay = 360;
  44. $file = "public/statistics/nissaya-monthly.csv";
  45. Storage::disk('local')->put($file, "");
  46. #按月获取数据
  47. $firstDay = Sentence::whereIn('channel_uid',$nissaya_channel)
  48. ->orderBy('created_at')
  49. ->select('created_at')
  50. ->first();
  51. $firstDay = strtotime($firstDay->created_at);
  52. $firstMonth = Carbon::create(date("Y-m",$firstDay));
  53. $now = Carbon::now();
  54. $current = $firstMonth;
  55. $sumStrlen = 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. $strlen = Sentence::whereIn('channel_uid',$nissaya_channel)
  62. ->whereDate('created_at','>=',$start)
  63. ->whereDate('created_at','<=',$end)
  64. ->sum('strlen');
  65. $sumStrlen += $strlen;
  66. $editor = Sentence::whereIn('channel_uid',$nissaya_channel)
  67. ->whereDate('created_at','>=',$start)
  68. ->whereDate('created_at','<=',$end)
  69. ->groupBy('editor_uid')
  70. ->select('editor_uid')->get();
  71. $info = $date.','.$strlen.','.$sumStrlen.','.count($editor);
  72. $this->info($info);
  73. Storage::disk('local')->append($file, $info);
  74. $current->addMonth(1);
  75. }
  76. return 0;
  77. }
  78. }