Consume.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. $res = 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. })->where('type', 2)
  73. ->sum('change');
  74. return abs($res);
  75. }
  76. /**
  77. * @param int $percent
  78. *
  79. * @return $this
  80. */
  81. public function up($percent)
  82. {
  83. return $this->footer(
  84. "<i class=\"feather icon-trending-up text-success\"></i> {$percent}% 增加"
  85. );
  86. }
  87. /**
  88. * @param int $percent
  89. *
  90. * @return $this
  91. */
  92. public function down($percent)
  93. {
  94. return $this->footer(
  95. "<i class=\"feather icon-trending-down text-danger\"></i> {$percent}% 减少"
  96. );
  97. }
  98. /**
  99. * 设置卡片底部内容.
  100. *
  101. * @param string|Renderable|\Closure $footer
  102. *
  103. * @return $this
  104. */
  105. public function footer($footer)
  106. {
  107. $this->footer = $footer;
  108. return $this;
  109. }
  110. /**
  111. * 渲染卡片内容.
  112. *
  113. * @return string
  114. */
  115. public function renderContent()
  116. {
  117. $content = parent::renderContent();
  118. return <<<HTML
  119. <div class="d-flex justify-content-between align-items-center mt-1" style="margin-bottom: 2px">
  120. <h2 class="ml-1 font-lg-1">{$content}</h2>
  121. </div>
  122. <div class="ml-1 mt-1 font-weight-bold text-80">
  123. {$this->renderFooter()}
  124. </div>
  125. HTML;
  126. }
  127. /**
  128. * 渲染卡片底部内容.
  129. *
  130. * @return string
  131. */
  132. public function renderFooter()
  133. {
  134. return $this->toString($this->footer);
  135. }
  136. }