DataController.php 4.3 KB

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