TimeStatisticsDepositOrderCommand.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\DepositOrder;
  4. use App\Models\DepositRefund;
  5. use App\Models\Statistic;
  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 TimeStatisticsDepositOrderCommand extends Command
  12. {
  13. /**
  14. * The name and signature of the console command.
  15. *
  16. * @var string
  17. */
  18. protected $signature = 'time_statistics:deposit_orders
  19. {time=2020-03-16 00:00:00}';
  20. /**
  21. * The console command description.
  22. *
  23. * @var string
  24. */
  25. protected $description = '押金订单每日统计';
  26. /**
  27. * Create a new command instance.
  28. *
  29. * @return void
  30. */
  31. public function __construct()
  32. {
  33. parent::__construct();
  34. }
  35. /**
  36. * Execute the console command.
  37. *
  38. * @return mixed
  39. */
  40. public function handle()
  41. {
  42. $time = $this->argument('time');
  43. $today = Carbon::make($time);
  44. $i = 1;
  45. for ($i; Carbon::today()->diffInDays($today) > 0; $i++) {
  46. $today = $today->addDay();
  47. $this->aa($today);
  48. }
  49. }
  50. // 测试
  51. public function bb(Carbon $yestoday)
  52. {
  53. $today = Carbon::make($yestoday)->addDay();
  54. Log::info($yestoday . '---' . $today . '---');
  55. }
  56. private function aa($yestoday)
  57. {
  58. //
  59. $today = Carbon::make($yestoday)->addDay();
  60. //押金充值记录
  61. $deposit_orders = DepositOrder::query()
  62. ->where('pay_status', DepositOrder::PAY_STATUS_OK)
  63. ->where('pay_time', '>', $yestoday)
  64. ->where('pay_time', '<', $today)
  65. ->groupBy(['area_id'])
  66. ->select('area_id', DB::raw('SUM(money) as money_all ,COUNT(id) as count_id'))
  67. ->get()
  68. ->toArray();
  69. $deposit_orders_arr = [];
  70. if (!empty($deposit_orders)) {
  71. foreach ($deposit_orders as $v1) {
  72. // 为了防止合并时 重新索引 将key重新定义
  73. $deposit_orders_arr['area_' . $v1['area_id']] = $v1;
  74. }
  75. }
  76. //押金退还记录
  77. $deposit_refunds = DepositRefund::query()
  78. ->where('pay_status', DepositRefund::PAY_STATUS_OK)
  79. ->where('pay_time', '>', $yestoday)
  80. ->where('pay_time', '<', $today)
  81. ->groupBy(['area_id'])
  82. ->select('area_id', DB::raw('SUM(money) as money_refund_all'))
  83. ->get()
  84. ->toArray();
  85. // print_r($deposit_refunds);
  86. $deposit_refund_arr = [];
  87. if (!empty($deposit_refunds)) {
  88. foreach ($deposit_refunds as $v2) {
  89. // 为了防止合并时 重新索引 将key重新定义
  90. $deposit_refund_arr['area_' . $v2['area_id']] = $v2;
  91. }
  92. }
  93. //合并
  94. $arr = array_merge_recursive($deposit_orders_arr, $deposit_refund_arr);
  95. $insert = [];
  96. $total_money_all = 0;
  97. $total_count_id = 0;
  98. $total_money_refund_all = 0;
  99. foreach ($arr as $ar) {
  100. $deposit_statistics = [];
  101. // 合并之后 会存在相同的area_id 组成数组 所以进行判断
  102. if (is_array($ar['area_id'])) {
  103. $key = $ar['area_id'][0];
  104. // $ar['total_deposit'] = $this->total_deposit($key);
  105. unset($ar['area_id']);
  106. $deposit_statistics['body'] = json_encode($ar);
  107. $deposit_statistics['area_id'] = $key;
  108. $deposit_statistics['name'] = Statistic::$statisticsMaps[Statistic::SLUG_DEPOSIT_STATIC];
  109. $deposit_statistics['slug'] = Statistic::SLUG_DEPOSIT_STATIC;
  110. $deposit_statistics['date'] = $yestoday;
  111. $insert[] = $deposit_statistics;
  112. } else {
  113. $key = $ar['area_id'];
  114. // $ar['total_deposit'] = $this->total_deposit($key);
  115. unset($ar['area_id']);
  116. $deposit_statistics['body'] = json_encode($ar);
  117. $deposit_statistics['area_id'] = $key;
  118. $deposit_statistics['name'] = Statistic::$statisticsMaps[Statistic::SLUG_DEPOSIT_STATIC];
  119. $deposit_statistics['slug'] = Statistic::SLUG_DEPOSIT_STATIC;
  120. $deposit_statistics['date'] = $yestoday;
  121. $insert[] = $deposit_statistics;
  122. }
  123. $total_money_all += $ar['money_all'] ?? 0;
  124. $total_count_id += $ar['count_id'] ?? 0;
  125. $total_money_refund_all += $ar['money_refund_all'] ?? 0;
  126. }
  127. //计算系统总押金
  128. $all['money_all'] = $total_money_all ?? 0;
  129. $all['count_id'] = $total_count_id ?? 0;
  130. $all['money_refund_all'] = $total_money_refund_all ?? 0;
  131. // $deposit_statistics['all']['total_deposit'] = $this->total_deposit("all");
  132. $insertAll['body'] = json_encode($all);
  133. $insertAll['area_id'] = config('statistic.all');
  134. $insertAll['name'] = Statistic::$statisticsMaps[Statistic::SLUG_DEPOSIT_STATIC];
  135. $insertAll['slug'] = Statistic::SLUG_DEPOSIT_STATIC;
  136. $insertAll['date'] = $yestoday;
  137. array_push($insert, $insertAll);
  138. // print_r($deposit_statistics);
  139. //存储
  140. $statistic = new Statistic();
  141. $statistic->insert($insert);
  142. Log::info($yestoday . '---' . $today . '押金统计已执行');
  143. }
  144. /**
  145. *
  146. * 统计每日总押金(数据大 会死掉)
  147. * */
  148. // private function total_deposit($area_id){
  149. // $total_deposit = DepositOrder::query()
  150. // ->where('pay_status',DepositRefund::PAY_STATUS_OK)
  151. // ->where('is_refund',DepositOrder::REFUND_NO);
  152. // if($area_id=='all'){
  153. // $total_deposit = $total_deposit
  154. // ->sum('money');
  155. // }else{
  156. // $total_deposit = $total_deposit
  157. // ->where('area_id',$area_id)
  158. // ->sum('money');
  159. // }
  160. // return $total_deposit;
  161. // }
  162. }