Consume.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace App\Admin\Metrics;
  3. use App\Models\User;
  4. use App\Models\UserConsumeRecord;
  5. use Carbon\Carbon;
  6. use Dcat\Admin\Widgets\Metrics\Card;
  7. use Illuminate\Contracts\Support\Renderable;
  8. use Illuminate\Http\Request;
  9. class Consume extends Card
  10. {
  11. /**
  12. * 卡片底部内容.
  13. *
  14. * @var string|Renderable|\Closure
  15. */
  16. protected $footer;
  17. /**
  18. * 初始化卡片.
  19. */
  20. protected function init()
  21. {
  22. parent::init();
  23. $this->title('消费金币');
  24. $this->height(300);
  25. $this->dropdown([
  26. '1' => '当日新增消费金币',
  27. '7' => '7天新增消费金币',
  28. '30' => '当月新增消费金币',
  29. '-1' => '总消费金币',
  30. ]);
  31. }
  32. /**
  33. * 处理请求.
  34. *
  35. * @param Request $request
  36. *
  37. * @return void
  38. */
  39. public function handle(Request $request)
  40. {
  41. switch ($request->get('option')) {
  42. case '-1':
  43. $this->content($this->getConsume());
  44. break;
  45. case '7':
  46. $startAt = Carbon::now()->subDays(7)->toDateString().' 23:59:59';
  47. $endAt = Carbon::now()->toDateString().' 23:59:59';
  48. $today = $this->getConsume($startAt, $endAt);
  49. $this->content($today);
  50. break;
  51. case '30':
  52. $startAt = Carbon::now()->subDays(30)->toDateString().' 23:59:59';
  53. $endAt = Carbon::now()->toDateString().' 23:59:59';
  54. $today = $this->getConsume($startAt, $endAt);
  55. $this->content($today);
  56. break;
  57. case '1':
  58. default:
  59. $startAt = Carbon::now()->subDay()->toDateString().' 23:59:59';
  60. $endAt = Carbon::now()->toDateString().' 23:59:59';
  61. $today = $this->getConsume($startAt, $endAt);
  62. $this->content($today);
  63. break;
  64. }
  65. }
  66. private function getConsume($startAt = '', $endAt = ''): int
  67. {
  68. return UserConsumeRecord::when($startAt, function ($query) use ($startAt) {
  69. $query->where('created_at', '>', $startAt);
  70. })->when($endAt, function ($query) use ($endAt) {
  71. $query->where('created_at', '<=', $endAt);
  72. })->sum('change');
  73. }
  74. /**
  75. * @param int $percent
  76. *
  77. * @return $this
  78. */
  79. public function up($percent)
  80. {
  81. return $this->footer(
  82. "<i class=\"feather icon-trending-up text-success\"></i> {$percent}% 增加"
  83. );
  84. }
  85. /**
  86. * @param int $percent
  87. *
  88. * @return $this
  89. */
  90. public function down($percent)
  91. {
  92. return $this->footer(
  93. "<i class=\"feather icon-trending-down text-danger\"></i> {$percent}% 减少"
  94. );
  95. }
  96. /**
  97. * 设置卡片底部内容.
  98. *
  99. * @param string|Renderable|\Closure $footer
  100. *
  101. * @return $this
  102. */
  103. public function footer($footer)
  104. {
  105. $this->footer = $footer;
  106. return $this;
  107. }
  108. /**
  109. * 渲染卡片内容.
  110. *
  111. * @return string
  112. */
  113. public function renderContent()
  114. {
  115. $content = parent::renderContent();
  116. return <<<HTML
  117. <div class="d-flex justify-content-between align-items-center mt-1" style="margin-bottom: 2px">
  118. <h2 class="ml-1 font-lg-1">{$content}</h2>
  119. </div>
  120. <div class="ml-1 mt-1 font-weight-bold text-80">
  121. {$this->renderFooter()}
  122. </div>
  123. HTML;
  124. }
  125. /**
  126. * 渲染卡片底部内容.
  127. *
  128. * @return string
  129. */
  130. public function renderFooter()
  131. {
  132. return $this->toString($this->footer);
  133. }
  134. }