DataController.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace App\Http\Controllers\Api\mini;
  3. use App\Models\Order;
  4. use App\Models\OrderDevice;
  5. use App\Models\Project;
  6. use Carbon\Carbon;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Support\Facades\Log;
  9. class DataController extends BaseController
  10. {
  11. protected $order;
  12. protected $order_device;
  13. public function __construct()
  14. {
  15. $this->order = new Order();
  16. $this->order_device = new OrderDevice();
  17. }
  18. public function getYearsAndMonths(Request $request)
  19. {
  20. $years = [
  21. ['text' => '所有年', 'value' => '']
  22. ];
  23. $this_year = Carbon::now()->year;
  24. for($i = 5; $i >= 0; $i--) {
  25. array_push($years, [
  26. 'text' => $this_year - $i,
  27. 'value' => $this_year - $i
  28. ]);
  29. }
  30. $months = [];
  31. for($i = 1; $i < 13; ++$i) {
  32. array_push($months, [
  33. 'text' => $i . '月',
  34. 'value' => $i
  35. ]);
  36. }
  37. $month = Carbon::now()->month;
  38. $now = Carbon::now()->toDateString();
  39. return $this->success(['data' => [
  40. ['key' => 'year', 'values' => $years, 'defaultIndex' => count($years) - 1],
  41. ['key' => 'month', 'values' => $months, 'defaultIndex' => $month]
  42. ], 'now' => $now]);
  43. }
  44. public function getDateInfo()
  45. {
  46. $now = Carbon::now();
  47. $date = $now->year . '-' . $now->month;
  48. $min_date = Carbon::now()->subYears(1)->getTimestamp() * 1000;
  49. return $this->success(['data' => compact('date', 'min_date')]);
  50. }
  51. public function getStat(Request $request)
  52. {
  53. $date = $request->input('date') ? $request->input('date') : '2020-1';
  54. $date_data = $this->parseDate($date);
  55. $date = Carbon::createFromDate($date_data['year'], $date_data['month'], $date_data['day'])->toDateTimeString();
  56. $ids = $request->input('project_ids');
  57. $order_devices = $this->order_device->whereIn('project_id', $ids);
  58. $in_items = ['device_ids', 'device_name_ids', 'spec_ids', 'rent_type_ids'];
  59. $key_items = ['device_id', 'device_name_id', 'spec_id', 'rent_type_id'];
  60. foreach ($in_items as $key => $item) {
  61. if($request->input($item)) {
  62. $item_ids = collect($request->input($item))->filter(function($id) {
  63. return $id;
  64. });
  65. if($item_ids->count() > 0) {
  66. $order_devices = $order_devices->whereIn($key_items[$key], $item_ids);
  67. }
  68. }
  69. }
  70. $order_ids = $order_devices->pluck('order_id')->unique();
  71. $orders = $this->order->whereIn('id', $order_ids)->get();
  72. $names = [];
  73. $values = [];
  74. // year|month
  75. $type = $request->input('type') ? $request->input('type') : 'year';
  76. $cnt = 0;
  77. $projects = Project::whereIn('id', $ids)->get();
  78. foreach($orders as $item) {
  79. if($this->inDate($names, $item->created_at, $type)) {
  80. foreach ($projects as $key => $project) {
  81. $values[$key][$cnt-1] = $project->id == $item->project_id ? $values[$key][$cnt-1] + 1 : $values[$key][$cnt-1];
  82. }
  83. } else {
  84. $names[$cnt] = $type == 'year' ? substr($item->created_at, 0, 7) : substr($item->created_at, 0, 10);
  85. foreach ($projects as $key => $project) {
  86. $values[$key][$cnt] = $project->id == $item->project_id ? 1 : 0;
  87. }
  88. $cnt = $cnt + 1;
  89. }
  90. }
  91. $legends = $projects->pluck('name');
  92. $total_project = $orders->sum('money') / 100;
  93. $tmp = Carbon::createFromTimeString($date);
  94. if($tmp->month == 12) {
  95. $next_month = Carbon::createFromDate($tmp->year + 1, 1, 1)->toDateTimeString();
  96. } else {
  97. $next_month = Carbon::createFromDate($tmp->year, $tmp->month + 1, 1)->toDateTimeString();
  98. }
  99. $total_month = $orders->where('created_at', '<', $next_month)->sum('money') / 100;
  100. return $this->success(['data' => compact('values', 'names', 'legends', 'total_project', 'total_month')]);
  101. }
  102. public function inDate($dates, $created_at, $type)
  103. {
  104. $date = $type == 'year' ? substr($created_at, 0, 7) : substr($created_at, 0, 10);
  105. return in_array($date, $dates);
  106. }
  107. public function parseDate($date)
  108. {
  109. $now = Carbon::now();
  110. if(!$date) return ['year' => $now->year, 'month' => $now->month, 'day' => $now->day];
  111. $items = explode('-', $date);
  112. $year = $now->year;
  113. $month = $now->month;
  114. $day = $now->day;
  115. if(isset($items[0])) {
  116. $year = (int)$items[0];
  117. $year = $year > 2010 && $year < 2050 ? $year : $now->year;
  118. }
  119. if(isset($items[1])) {
  120. $month = (int)$items[1];
  121. $month = $month > 0 && $month < 13 ? $month : $now->month;
  122. }
  123. if(isset($item[2])) {
  124. $day = (int)$items[2];
  125. $day = $day > 0 && $day < 32 ? $date : 1;
  126. }
  127. return compact('year', 'month', 'day');
  128. }
  129. }