V2boardStatistics.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Models\Order;
  5. use App\Models\StatOrder;
  6. use App\Models\CommissionLog;
  7. class V2boardStatistics extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'v2board:statistics';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $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. ini_set('memory_limit', -1);
  38. $this->statOrder();
  39. }
  40. private function statOrder()
  41. {
  42. $endAt = strtotime(date('Y-m-d'));
  43. $startAt = strtotime('-1 day', $endAt);
  44. $orderBuilder = Order::where('paid_at', '>=', $startAt)
  45. ->where('paid_at', '<', $endAt)
  46. ->whereNotIn('status', [0, 2]);
  47. $orderCount = $orderBuilder->count();
  48. $orderAmount = $orderBuilder->sum('total_amount');
  49. $commissionBuilder = Order::where('created_at', '>=', $startAt)
  50. ->where('created_at', '<', $endAt);
  51. $commissionCount = $commissionBuilder->count();
  52. $commissionAmount = $commissionBuilder->sum('actual_commission_balance');
  53. $data = [
  54. 'order_count' => $orderCount,
  55. 'order_amount' => $orderAmount,
  56. 'commission_count' => $commissionCount,
  57. 'commission_amount' => $commissionAmount,
  58. 'record_type' => 'd',
  59. 'record_at' => $startAt
  60. ];
  61. $statistic = StatOrder::where('record_at', $startAt)
  62. ->where('record_type', 'd')
  63. ->first();
  64. if ($statistic) {
  65. $statistic->update($data);
  66. return;
  67. }
  68. StatOrder::create($data);
  69. }
  70. }