Consume.php 3.6 KB

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