argument('time'); $today = Carbon::make($time); $i = 1; for ($i; Carbon::today()->diffInDays($today) > 0; $i++) { $today = $today->addDay(); $this->aa($today); } } // 测试 public function bb(Carbon $yestoday) { $today = Carbon::make($yestoday)->addDay(); Log::info($yestoday . '---' . $today . '---'); } private function aa($yestoday) { // $today = Carbon::make($yestoday)->addDay(); //押金充值记录 $deposit_orders = DepositOrder::query() ->where('pay_status', DepositOrder::PAY_STATUS_OK) ->where('pay_time', '>', $yestoday) ->where('pay_time', '<', $today) ->groupBy(['area_id']) ->select('area_id', DB::raw('SUM(money) as money_all ,COUNT(id) as count_id')) ->get() ->toArray(); $deposit_orders_arr = []; if (!empty($deposit_orders)) { foreach ($deposit_orders as $v1) { // 为了防止合并时 重新索引 将key重新定义 $deposit_orders_arr['area_' . $v1['area_id']] = $v1; } } //押金退还记录 $deposit_refunds = DepositRefund::query() ->where('pay_status', DepositRefund::PAY_STATUS_OK) ->where('pay_time', '>', $yestoday) ->where('pay_time', '<', $today) ->groupBy(['area_id']) ->select('area_id', DB::raw('SUM(money) as money_refund_all')) ->get() ->toArray(); // print_r($deposit_refunds); $deposit_refund_arr = []; if (!empty($deposit_refunds)) { foreach ($deposit_refunds as $v2) { // 为了防止合并时 重新索引 将key重新定义 $deposit_refund_arr['area_' . $v2['area_id']] = $v2; } } //合并 $arr = array_merge_recursive($deposit_orders_arr, $deposit_refund_arr); $insert = []; $total_money_all = 0; $total_count_id = 0; $total_money_refund_all = 0; foreach ($arr as $ar) { $deposit_statistics = []; // 合并之后 会存在相同的area_id 组成数组 所以进行判断 if (is_array($ar['area_id'])) { $key = $ar['area_id'][0]; // $ar['total_deposit'] = $this->total_deposit($key); unset($ar['area_id']); $deposit_statistics['body'] = json_encode($ar); $deposit_statistics['area_id'] = $key; $deposit_statistics['name'] = Statistic::$statisticsMaps[Statistic::SLUG_DEPOSIT_STATIC]; $deposit_statistics['slug'] = Statistic::SLUG_DEPOSIT_STATIC; $deposit_statistics['date'] = $yestoday; $insert[] = $deposit_statistics; } else { $key = $ar['area_id']; // $ar['total_deposit'] = $this->total_deposit($key); unset($ar['area_id']); $deposit_statistics['body'] = json_encode($ar); $deposit_statistics['area_id'] = $key; $deposit_statistics['name'] = Statistic::$statisticsMaps[Statistic::SLUG_DEPOSIT_STATIC]; $deposit_statistics['slug'] = Statistic::SLUG_DEPOSIT_STATIC; $deposit_statistics['date'] = $yestoday; $insert[] = $deposit_statistics; } $total_money_all += $ar['money_all'] ?? 0; $total_count_id += $ar['count_id'] ?? 0; $total_money_refund_all += $ar['money_refund_all'] ?? 0; } //计算系统总押金 $all['money_all'] = $total_money_all ?? 0; $all['count_id'] = $total_count_id ?? 0; $all['money_refund_all'] = $total_money_refund_all ?? 0; // $deposit_statistics['all']['total_deposit'] = $this->total_deposit("all"); $insertAll['body'] = json_encode($all); $insertAll['area_id'] = config('statistic.all'); $insertAll['name'] = Statistic::$statisticsMaps[Statistic::SLUG_DEPOSIT_STATIC]; $insertAll['slug'] = Statistic::SLUG_DEPOSIT_STATIC; $insertAll['date'] = $yestoday; array_push($insert, $insertAll); // print_r($deposit_statistics); //存储 $statistic = new Statistic(); $statistic->insert($insert); Log::info($yestoday . '---' . $today . '押金统计已执行'); } /** * * 统计每日总押金(数据大 会死掉) * */ // private function total_deposit($area_id){ // $total_deposit = DepositOrder::query() // ->where('pay_status',DepositRefund::PAY_STATUS_OK) // ->where('is_refund',DepositOrder::REFUND_NO); // if($area_id=='all'){ // $total_deposit = $total_deposit // ->sum('money'); // }else{ // $total_deposit = $total_deposit // ->where('area_id',$area_id) // ->sum('money'); // } // return $total_deposit; // } }