V2boardStatistics.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 = CommissionLog::where('created_at', '>=', $startAt)
  50. ->where('created_at', '<', $endAt)
  51. ->where('get_amount', '>', 0);
  52. $commissionCount = $commissionBuilder->count();
  53. $commissionAmount = $commissionBuilder->sum('get_amount');
  54. $data = [
  55. 'order_count' => $orderCount,
  56. 'order_amount' => $orderAmount,
  57. 'commission_count' => $commissionCount,
  58. 'commission_amount' => $commissionAmount,
  59. 'record_type' => 'd',
  60. 'record_at' => $startAt
  61. ];
  62. $statistic = StatOrder::where('record_at', $startAt)
  63. ->where('record_type', 'd')
  64. ->first();
  65. if ($statistic) {
  66. $statistic->update($data);
  67. return;
  68. }
  69. StatOrder::create($data);
  70. }
  71. }