StatisticsDict.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\UserOperationLog;
  4. use App\Tools\Tools;
  5. use Carbon\Carbon;
  6. use Illuminate\Console\Command;
  7. use Illuminate\Support\Facades\Storage;
  8. class StatisticsDict extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'statistics:dict';
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = '统计 字典 每月查询';
  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 (Tools::isStop()) {
  39. return 0;
  40. }
  41. $file = 'public/statistics/lookup-monthly.csv';
  42. Storage::disk('local')->put($file, '');
  43. // 按月获取数据
  44. $firstDay = UserOperationLog::where('op_type', 'dict_lookup')
  45. ->orderBy('created_at')
  46. ->select('created_at')
  47. ->first();
  48. $firstDay = strtotime($firstDay->created_at);
  49. $firstMonth = Carbon::create(date('Y-m', $firstDay));
  50. $now = Carbon::now();
  51. $current = $firstMonth;
  52. $sumCount = 0;
  53. while ($current <= $now) {
  54. // code...
  55. $start = Carbon::create($current)->startOfMonth();
  56. $end = Carbon::create($current)->endOfMonth();
  57. $date = $current->format('Y-m');
  58. $count = UserOperationLog::where('op_type', 'dict_lookup')
  59. ->whereDate('created_at', '>=', $start)
  60. ->whereDate('created_at', '<=', $end)
  61. ->count();
  62. $sumCount += $count;
  63. $editor = UserOperationLog::where('op_type', 'dict_lookup')
  64. ->whereDate('created_at', '>=', $start)
  65. ->whereDate('created_at', '<=', $end)
  66. ->groupBy('user_id')
  67. ->select('user_id')->get();
  68. $info = $date.','.$count.','.$sumCount.','.count($editor);
  69. $this->info($info);
  70. Storage::disk('local')->append($file, $info);
  71. $current->addMonth(1);
  72. }
  73. return 0;
  74. }
  75. }