123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323 |
- <?php
- namespace App\Http\Controllers\Api\mini;
- use App\Models\Device;
- use App\Models\Order;
- use App\Models\OrderDevice;
- use App\Models\Project;
- use Carbon\Carbon;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Log;
- class DataController extends BaseController
- {
- protected $order;
- protected $order_device;
- protected $order_ids = [];
- protected $project_ids = [];
- protected $start_at;
- protected $end_at;
- public function __construct()
- {
- $this->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()
- {
- $max_date = Carbon::now()->toDateString();
- // $order = $this->order->orderBy('created_at', 'desc')->first();
- // $min_date = $order ? substr($order->created_at, 0, 10) : Carbon::now()->subYear()->toDateString();
- $min_date = Carbon::now()->subYears(10)->toDateString();
- $date = substr($max_date, 0, 7);
- $start_date = $max_date;
- $end_date = Carbon::now()->addMonth()->toDateString();
- $start_date = substr($start_date, 0, 7);
- $end_date = substr($end_date, 0, 7);
- return $this->success(['data' => compact('date', 'min_date', 'max_date', 'start_date', 'end_date')]);
- }
- public function getStartAt($date)
- {
- return strlen($date) <= 7 ? $date . '-01 00:00:00' : $date . ' 00:00:00';
- }
- public function getEndAt($date)
- {
- return strlen($date) <= 7 ? Carbon::createFromTimeString($date . '-01 00:00:00')->addMonth(1)->toDateTimeString() : Carbon::createFromTimeString($date . ' 00:00:00')->addDay(1)->toDateTimeString();
- }
- public function getOrders(Request $request)
- {
- $order_ids = $this->getOrderIds($request);
- $start_at = $this->getStartAt($request->input('start_date'));
- $end_at = $this->getEndAt($request->input('end_date'));
- return $this->order->whereIn('id', $order_ids)->where([
- ['type', '=', 1],
- ['created_at', '>=', $start_at],
- ['created_at', '<', $end_at]
- ])->get();
- }
- public function getOrderIds(Request $request)
- {
- $ids = $request->input('project_ids');
- if(!$ids || count($ids) < 0) return false;
- $this->project_ids = $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 && $item_ids->count() > 0) {
- $order_devices = $order_devices->whereIn($key_items[$key], $item_ids);
- }
- }
- }
- return $order_devices->pluck('order_id')->unique();
- }
- public function getStat(Request $request)
- {
- if(!$request->input('project_ids')) return $this->error(['msg' => '']);
- if($request->input('chart_type') == 'pie') return $this->getPieData($request);
- else if($request->input('chart_type') == 'radar') return $this->getRadarData($request);
- $orders = $this->getOrders($request);
- if(!$orders) return $this->error(['msg' => '']);
- $names = [];
- $values = [];
- // year|month
- $type = $request->input('type') ? $request->input('type') : 'year';
- $cnt = 0;
- $projects = Project::whereIn('id', $this->project_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] + ($item->money / 100) : $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 ? ($item->money / 100) : 0;
- }
- $cnt = $cnt + 1;
- }
- }
- $legends = $projects->pluck('name');
- return $this->success(['data' => compact('values', 'names', 'legends')]);
- }
- public function getRadarData(Request $request)
- {
- $orders = $this->getOrders($request);
- if(!$orders) return $this->error(['msg' => '']);
- $data = [];
- $projects = Project::whereIn('id', $request->input('project_ids'))->get();
- $indicator = [
- ['name' => '累计租赁花费', 'max' => 0],
- ['name' => '累计租赁数量', 'max' => 0],
- ['name' => '平均月消费', 'max' => 0],
- ['name' => '租赁次数', 'max' => 0],
- ['name' => '租赁天数', 'max' => 0]
- ];
- $legends = [];
- foreach($projects as $key => $project) {
- $tmp_orders = $orders->where('project_id', $project->id);
- $total_money = $tmp_orders->sum('money') / 100;
- $total_num = OrderDevice::whereIn('order_id', $orders->where('project_id', $project->id)->pluck('id'))->count();
- $months = $this->getMonths($tmp_orders);
- $mean_month = $total_money / $months;
- $rent_times = $tmp_orders->count();
- $rent_days = $this->getRentDays($tmp_orders);
- $indicator[0]['max'] = max($indicator[0]['max'], $total_money);
- $indicator[1]['max'] = max($indicator[1]['max'], $total_num);
- $indicator[2]['max'] = max($indicator[2]['max'], $mean_month);
- $indicator[3]['max'] = max($indicator[3]['max'], $rent_times);
- $indicator[4]['max'] = max($indicator[4]['max'], $rent_days);
- array_push($data, [
- 'name' => $project->name,
- 'value' => [$total_money, $total_num, $mean_month, $rent_times, $rent_days]
- ]);
- array_push($legends, $project->name);
- }
- return $this->success(['data' => compact('data', 'legends', 'indicator')]);
- }
- public function getMonths($orders)
- {
- $min_at = $orders->min('created_at');
- $max_at = $orders->max('updated_at');
- if($max_at && $max_at) $months = $min_at->diffInMonths($max_at);
- else $months = 1;
- return $months ? $months : 1;
- }
- public function getRentDays($orders)
- {
- $order_ids = $orders->pluck('id');
- $order_devices = OrderDevice::whereId('order_id', $order_ids)->get();
- $days = 0;
- foreach($order_devices as $order_device) {
- if($order_device->start_date && $order_device->end_date) {
- $days = $days + Carbon::createFromTimeString($order_device->start_date . ' 00:00:00')->diffInDays($order_device->end_date);
- }
- }
- return $days;
- }
- public function getPieData(Request $request)
- {
- $order_ids = $this->getOrderIds($request);
- if(!$order_ids) return $this->error(['msg' => '']);
- $total = OrderDevice::whereIn('order_id', $order_ids)->count();
- $devices = Device::all();
- $data = [];
- foreach($devices as $device) {
- $tmp = OrderDevice::whereIn('order_id', $order_ids)->where('device_id', $device->id)->count();
- $percent = $total > 0 ? round(($tmp / $total * 10000)) / 100 : '0';
- array_push($data, ['name' => $device->name, 'value' => $percent]);
- }
- $legends = $devices->pluck('name');
- return $this->success(['data' => compact('data', 'legends')]);
- }
- public function getDetailData(Request $request)
- {
- $order_ids = $this->getOrderIds($request);
- if(!$order_ids) return $this->error(['msg' => '']);
- $order_devices = OrderDevice::whereIn('order_id', $order_ids)->orderBy('created_at')->get();
- $devices = Device::all();
- $dates = [];
- // year|month
- $type = $request->input('type') ? $request->input('type') : 'year';
- $columns = [];
- $date = '';
- $values = [
- ['date' => '日期', 'total' => '总金额', 'data' => $devices->pluck('name')]
- ];
- foreach($order_devices as $item) {
- if($this->inDate($dates, $item->created_at, $type)) {
- foreach($devices as $key => $device) {
- $money = $device->id == $item->device_id ? ($item->price * $item->quantity) / 100 : 0;
- $columns[$key] = $columns[$key] + $money;
- }
- } else {
- $date = $type == 'year' ? substr($item->created_at, 0, 7) : substr($item->created_at, 0, 10);
- array_push($dates, $date);
- if(count($columns) > 0) {
- array_push($values, [
- 'date' => $date,
- 'total' => collect($columns)->sum() / 100,
- 'data' => $columns
- ]);
- }
- $columns = [];
- foreach($devices as $device) {
- $money = $device->id == $item->device_id ? ($item->price * $item->quantity) / 100 : 0;
- array_push($columns, $money);
- }
- }
- }
- if(count($columns) > 0) {
- array_push($values, [
- 'date' => $date,
- 'total' => collect($columns)->sum(),
- 'data' => $columns
- ]);
- }
- return $this->success(['data' => $values]);
- }
- public function getTotalInfo(Request $request)
- {
- $start_at = $this->getStartAt($request->input('date'));
- $end_at = $this->getStartAt($request->input('date'));
- $total_money = $this->order->whereIn('project_id', $request->input('project_ids'))->sum('money') / 100;
- $month_money = $this->order->whereIn('project_id', $request->input('project_ids'))->where([
- ['created_at', '>=', $start_at],
- ['created_at', '<', $end_at]
- ])->sum('money') / 100;
- return $this->success(['data' => compact('total_money', 'month_money')]);
- }
- 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');
- }
- }
|