order = new Order(); $this->order_device = new OrderDevice(); } public function getYearsAndMonths(Request $request) { $years = [ ['text' => '所有年', 'value' => ''] ]; $this_year = Carbon::now()->year; for($i = 5; $i >= 0; $i--) { array_push($years, [ 'text' => $this_year - $i, 'value' => $this_year - $i ]); } $months = []; for($i = 1; $i < 13; ++$i) { array_push($months, [ 'text' => $i . '月', 'value' => $i ]); } $month = Carbon::now()->month; $now = Carbon::now()->toDateString(); return $this->success(['data' => [ ['key' => 'year', 'values' => $years, 'defaultIndex' => count($years) - 1], ['key' => 'month', 'values' => $months, 'defaultIndex' => $month] ], 'now' => $now]); } public function getDateInfo() { $now = Carbon::now(); $date = $now->year . '-' . $now->month; $min_date = Carbon::now()->subYears(1)->getTimestamp() * 1000; return $this->success(['data' => compact('date', 'min_date')]); } public function getStat(Request $request) { $date = $request->input('date') ? $request->input('date') : '2020-1'; $date_data = $this->parseDate($date); $date = Carbon::createFromDate($date_data['year'], $date_data['month'], $date_data['day'])->toDateTimeString(); $ids = $request->input('project_ids'); $order_devices = $this->order_device->whereIn('project_id', $ids); $in_items = ['device_ids', 'device_name_ids', 'spec_ids', 'rent_type_ids']; $key_items = ['device_id', 'device_name_id', 'spec_id', 'rent_type_id']; foreach ($in_items as $key => $item) { if($request->input($item)) { $item_ids = collect($request->input($item))->filter(function($id) { return $id; }); if($item_ids->count() > 0) { $order_devices = $order_devices->whereIn($key_items[$key], $item_ids); } } } $order_ids = $order_devices->pluck('order_id')->unique(); $orders = $this->order->whereIn('id', $order_ids)->get(); $names = []; $values = []; // year|month $type = $request->input('type') ? $request->input('type') : 'year'; $cnt = 0; $projects = Project::whereIn('id', $ids)->get(); foreach($orders as $item) { if($this->inDate($names, $item->created_at, $type)) { foreach ($projects as $key => $project) { $values[$key][$cnt-1] = $project->id == $item->project_id ? $values[$key][$cnt-1] + 1 : $values[$key][$cnt-1]; } } else { $names[$cnt] = $type == 'year' ? substr($item->created_at, 0, 7) : substr($item->created_at, 0, 10); foreach ($projects as $key => $project) { $values[$key][$cnt] = $project->id == $item->project_id ? 1 : 0; } $cnt = $cnt + 1; } } $legends = $projects->pluck('name'); $total_project = $orders->sum('money') / 100; $tmp = Carbon::createFromTimeString($date); if($tmp->month == 12) { $next_month = Carbon::createFromDate($tmp->year + 1, 1, 1)->toDateTimeString(); } else { $next_month = Carbon::createFromDate($tmp->year, $tmp->month + 1, 1)->toDateTimeString(); } $total_month = $orders->where('created_at', '<', $next_month)->sum('money') / 100; return $this->success(['data' => compact('values', 'names', 'legends', 'total_project', 'total_month')]); } public function inDate($dates, $created_at, $type) { $date = $type == 'year' ? substr($created_at, 0, 7) : substr($created_at, 0, 10); return in_array($date, $dates); } public function parseDate($date) { $now = Carbon::now(); if(!$date) return ['year' => $now->year, 'month' => $now->month, 'day' => $now->day]; $items = explode('-', $date); $year = $now->year; $month = $now->month; $day = $now->day; if(isset($items[0])) { $year = (int)$items[0]; $year = $year > 2010 && $year < 2050 ? $year : $now->year; } if(isset($items[1])) { $month = (int)$items[1]; $month = $month > 0 && $month < 13 ? $month : $now->month; } if(isset($item[2])) { $day = (int)$items[2]; $day = $day > 0 && $day < 32 ? $date : 1; } return compact('year', 'month', 'day'); } }