海南旅游SAAS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
2.6 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. <?php
  2. namespace App\AdminAgent\Metrics\Examples;
  3. use App\Common\DataTime;
  4. use App\Models\User;
  5. use Carbon\Carbon;
  6. use Dcat\Admin\Admin;
  7. use Dcat\Admin\Widgets\Metrics\Line;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Support\Arr;
  10. use Illuminate\Support\Facades\DB;
  11. class NewUsers extends Line
  12. {
  13. /**
  14. * 初始化卡片内容
  15. *
  16. * @return void
  17. */
  18. protected function init()
  19. {
  20. parent::init();
  21. $this->title('新增用户');
  22. $this->dropdown([
  23. //'day' => '本日',
  24. 'week' => '本周',
  25. 'month' => '本月',
  26. //'year' => '今年',
  27. ]);
  28. }
  29. /**
  30. * 处理请求
  31. *
  32. * @param Request $request
  33. *
  34. * @return mixed|void
  35. */
  36. public function handle(Request $request)
  37. {
  38. $count = User::query()->where('agent_id', Admin::user()->id);
  39. $user = User::query()
  40. ->select('*')
  41. ->addSelect(DB::raw("COUNT(id) as count_id,CONCAT(YEAR(created_at),'-',MONTH(created_at),'-',DAY(created_at)) AS statistics_time"));
  42. switch ($request->get('option')) {
  43. case 'day':
  44. $count->whereDate('created_at', Carbon::today());
  45. $user->whereDate('created_at', Carbon::today());
  46. break;
  47. case 'week':
  48. $time = DataTime::beginAndEnd('week');
  49. $count->whereBetween('created_at', $time);
  50. $user->whereBetween('created_at', $time);
  51. break;
  52. case 'month':
  53. $count->whereMonth('created_at', Carbon::now()->month);
  54. $user->whereMonth('created_at', Carbon::now()->month);
  55. break;
  56. case 'year':
  57. $count->whereYear('created_at', Carbon::now()->year);
  58. $user->whereYear('created_at', Carbon::now()->year);
  59. break;
  60. default:
  61. $time = DataTime::beginAndEnd('week');
  62. $count->whereBetween('created_at', $time);
  63. $user->whereBetween('created_at', $time);
  64. }
  65. $user = $user->where('agent_id', Admin::user()->id)
  66. ->limit(30)
  67. ->groupBy('statistics_time')
  68. ->get()
  69. ->toArray();
  70. $count = $count->count();
  71. $this->withContent($count);
  72. // 图表数据
  73. $this->chartLabels(Arr::pluck($user, 'statistics_time'));
  74. $this->withChart(Arr::pluck($user, 'count_id'));
  75. }
  76. /**
  77. * 设置图表数据.
  78. *
  79. * @param array $data
  80. *
  81. * @return $this
  82. */
  83. public function withChart(array $data)
  84. {
  85. return $this->chart([
  86. 'series' => [
  87. [
  88. 'name' => $this->title,
  89. 'data' => $data,
  90. ],
  91. ],
  92. ]);
  93. }
  94. /**
  95. * 设置卡片内容.
  96. *
  97. * @param string $content
  98. *
  99. * @return $this
  100. */
  101. public function withContent($content)
  102. {
  103. return $this->content(
  104. <<<HTML
  105. <div class="d-flex justify-content-between align-items-center mt-1" style="margin-bottom: 2px">
  106. <h2 class="ml-1 font-lg-1">{$content}</h2>
  107. <span class="mb-0 mr-1 text-80">{$this->title}</span>
  108. </div>
  109. HTML
  110. );
  111. }
  112. }