StatisticsProfitCommand.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\Bike;
  4. use App\Models\Statistic;
  5. use App\Models\WalletLog;
  6. use App\Utils\RedisKeys;
  7. use Carbon\Carbon;
  8. use Illuminate\Console\Command;
  9. use Illuminate\Support\Facades\DB;
  10. use Illuminate\Support\Facades\Log;
  11. class StatisticsProfitCommand extends Command
  12. {
  13. /**
  14. * The name and signature of the console command.
  15. *
  16. * @var string
  17. */
  18. protected $signature = 'statistics:profit';
  19. /**
  20. * The console command description.
  21. *
  22. * @var string
  23. */
  24. protected $description = '每日收益统计';
  25. /**
  26. * Create a new command instance.
  27. *
  28. * @return void
  29. */
  30. public function __construct()
  31. {
  32. parent::__construct();
  33. }
  34. /**
  35. * Execute the console command.
  36. *
  37. * @return mixed
  38. */
  39. public function handle()
  40. {
  41. //
  42. $key = RedisKeys::PROFIT_STATISTICS_KEY;
  43. app()->redis->incr($key);
  44. $res = app()->redis->get($key);
  45. app()->redis->Expire($key, RedisKeys::STATISTICS_KEY_TIME);
  46. if ($res == 1) {
  47. $yestoday = Carbon::yesterday();
  48. $today = Carbon::today();
  49. $wallets = WalletLog::query()
  50. ->where('created_at', '>', $yestoday)
  51. ->where('created_at', '<', $today)
  52. ->whereIn('type', WalletLog::$subType)
  53. ->groupBy(['area_id'])
  54. ->select('area_id', DB::raw('SUM(money) as money_all'))
  55. ->get()
  56. ->toArray();
  57. // print_r($wallets);
  58. $profit_arr = [];
  59. $profit_total = 0;
  60. foreach ($wallets as $k => &$v) {
  61. $put_bikes = Bike::query()->where('put_status', Bike::PUT_STATUS_YES)->where('put_area_id', $v['area_id'])->count();
  62. $body = ['profit' => abs($v['money_all']), 'put_bikes' => $put_bikes];
  63. $v['body'] = json_encode($body);
  64. $v['name'] = Statistic::$statisticsMaps[Statistic::SLUG_PROFIT_STATIC];
  65. $v['slug'] = Statistic::SLUG_PROFIT_STATIC;
  66. $v['date'] = $yestoday;
  67. // $profit_arr[$v['area_id']] = $v['money_all'];
  68. $profit_total += $v['money_all'];
  69. unset($v['money_all']);
  70. }
  71. $profit_arr['area_id'] = config('statistic.all'); // 9999 表示总和
  72. $all_put_bikes = Bike::query()->where('put_status', Bike::PUT_STATUS_YES)->count();
  73. $all_body = ['profit' => abs($profit_total), 'put_bikes' => $all_put_bikes];
  74. $profit_arr['body'] = json_encode($all_body);
  75. $profit_arr['name'] = Statistic::$statisticsMaps[Statistic::SLUG_PROFIT_STATIC];
  76. $profit_arr['slug'] = Statistic::SLUG_PROFIT_STATIC;
  77. $profit_arr['date'] = $yestoday;
  78. array_push($wallets, $profit_arr);
  79. // print_r($profit_arr);
  80. $statistic = new Statistic();
  81. $statistic->insert($wallets);
  82. } else {
  83. return;
  84. }
  85. Log::info($yestoday . '收益统计已执行');
  86. }
  87. }