OperationData.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Models\Operation;
  5. use App\Models\FundsRecord;
  6. use App\Models\Order;
  7. use App\Models\Wallet;
  8. use Carbon\Carbon;
  9. class OperationData extends Command
  10. {
  11. /**
  12. * 命令名称和签名
  13. *
  14. * @var string
  15. */
  16. protected $signature = 'operation:data';
  17. /**
  18. * 命令描述
  19. *
  20. * @var string
  21. */
  22. protected $description = '统计昨日的充值提现订单等数据(每天00:00 之后执行)';
  23. /**
  24. * 执行命令
  25. *
  26. * @return int
  27. */
  28. public function handle()
  29. {
  30. $this->info('开始执行统计充值提现订单等数据任务...');
  31. $this->addYesterdayOperationData();
  32. $this->info('结束执行统计充值提现订单等数据任务');
  33. }
  34. public function addYesterdayOperationData()
  35. {
  36. $yesterday = Carbon::yesterday()->format('Y-m-d');
  37. $data = [
  38. 'date' => $yesterday,
  39. 'recharge' => 0,
  40. 'user_total_money' => 0,
  41. ];
  42. $data['recharge'] = FundsRecord::whereIn('change_type', ['充值','人工充值','三方充值'])
  43. ->where('created_at', '>=', "{$yesterday} 00:00:00")
  44. ->where('created_at', '<=', "{$yesterday} 23:59:59")
  45. ->sum('amount');
  46. $data['withdraw'] = FundsRecord::whereIn('change_type', ['提现','三方提现'])
  47. ->where('created_at', '>=', "{$yesterday} 00:00:00")
  48. ->where('created_at', '<=', "{$yesterday} 23:59:59")
  49. ->sum('amount');
  50. $data['balance_difference'] = $data['recharge'] - $data['withdraw'];
  51. $data['total_price'] = Order::where('create_time', '>=', strtotime($yesterday.' 00:00:00'))
  52. ->where('create_time', '<=', strtotime($yesterday.' 23:59:59'))
  53. ->where('status', 1)
  54. ->where('pay_status', 1)
  55. ->where('return_status', 0)
  56. ->sum('amount');
  57. $data['user_total_money'] = Wallet::query()->sum('available_balance');
  58. Operation::updateOrCreate(['date' => $data['date']], $data);
  59. return true;
  60. }
  61. }