Recharge.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. namespace App\Admin\Metrics;
  3. use App\Models\UserRechargeRecord;
  4. use App\Models\UserVipRecord;
  5. use Carbon\Carbon;
  6. use Dcat\Admin\Admin;
  7. use Dcat\Admin\Widgets\Metrics\Round;
  8. use Illuminate\Database\Query\Builder;
  9. use Illuminate\Http\Request;
  10. class Recharge extends Round
  11. {
  12. protected $labels = ['VIP充值金额', '金币充值金额'];
  13. private $vipRecharge = 0;
  14. private $goldRecharge = 0;
  15. private $totalRecharge = 0;
  16. /**
  17. * 初始化卡片内容
  18. */
  19. protected function init()
  20. {
  21. parent::init();
  22. $color = Admin::color();
  23. $colors = [$color->primary(), $color->alpha('blue2', 0.5)];
  24. $this->height(300);
  25. $this->chartHeight(300);
  26. $this->chartRadialBarSize(1);
  27. $this->title('VIP充值/金币充值金额');
  28. $this->chartLabels($this->labels);
  29. // 设置图表颜色
  30. $this->chartColors($colors);
  31. $this->dropdown([
  32. '1' => '当日新增充值金额',
  33. '7' => '7天新增充值金额',
  34. '30' => '当月新增充值金额',
  35. '-1' => '总充值金额',
  36. ]);
  37. }
  38. public function handle(Request $request)
  39. {
  40. switch ($request->get('option')) {
  41. case '-1':
  42. $this->getVipRecharge();
  43. $this->getGoldRecharge();
  44. break;
  45. case '7':
  46. $startAt = Carbon::now()->subDays(7)->toDateString().' 23:59:59';
  47. $endAt = Carbon::now()->toDateString().' 23:59:59';
  48. $this->getVipRecharge($startAt,$endAt);
  49. $this->getGoldRecharge($startAt,$endAt);
  50. break;
  51. case '30':
  52. $startAt = Carbon::now()->subDays(30)->toDateString().' 23:59:59';
  53. $endAt = Carbon::now()->toDateString().' 23:59:59';
  54. $this->getVipRecharge($startAt,$endAt);
  55. $this->getGoldRecharge($startAt,$endAt);
  56. break;
  57. case '1':
  58. default:
  59. $startAt = Carbon::now()->toDateString().' 00:00:0';
  60. $endAt = Carbon::now()->toDateString().' 23:59:59';
  61. $this->getVipRecharge($startAt,$endAt);
  62. $this->getGoldRecharge($startAt,$endAt);
  63. break;
  64. }
  65. $this->totalRecharge = $this->vipRecharge + $this->goldRecharge;
  66. $this->withContent();
  67. // 图表数据
  68. $vipPercent = $this->totalRecharge ? $this->vipRecharge / $this->totalRecharge : 0;
  69. $goldPercent = $this->totalRecharge ? $this->goldRecharge / $this->totalRecharge : 0;
  70. $data = [
  71. \round($vipPercent*100, 2),
  72. \round($goldPercent*100, 2)
  73. ];
  74. $this->withChart($data);
  75. $this->chartTotal('总充值金额', $this->totalRecharge);
  76. }
  77. private function getVipRecharge($statAt = '', $endAt = '')
  78. {
  79. $this->vipRecharge = UserVipRecord::withSum('pay','order_fee')
  80. ->whereHas('pay', function ($query) use($statAt, $endAt){
  81. /* @var Builder $query*/
  82. $query->where('status', 1)
  83. ->when($statAt, function ($query) use($statAt){
  84. /* @var Builder $query*/
  85. $query->where('created_at', '>', $statAt);
  86. }) ->when($statAt, function ($query) use($endAt){
  87. /* @var Builder $query*/
  88. $query->where('created_at', '<=', $endAt);
  89. });
  90. })
  91. ->get()
  92. ->sum('pay_sum_order_fee');
  93. }
  94. private function getGoldRecharge($statAt = '', $endAt = '')
  95. {
  96. $this->goldRecharge = UserRechargeRecord::withSum('pay','order_fee')
  97. ->whereHas('pay', function ($query) use($statAt, $endAt){
  98. /* @var Builder $query*/
  99. $query->where('status', 1)
  100. ->when($statAt, function ($query) use($statAt){
  101. /* @var Builder $query*/
  102. $query->where('created_at', '>', $statAt);
  103. }) ->when($statAt, function ($query) use($endAt){
  104. /* @var Builder $query*/
  105. $query->where('created_at', '<=', $endAt);
  106. });
  107. })
  108. ->get()
  109. ->sum('pay_sum_order_fee');
  110. }
  111. /**
  112. * 设置图表数据.
  113. *
  114. *
  115. * @return $this
  116. */
  117. public function withChart($data): Recharge
  118. {
  119. return $this->chart([
  120. 'series' => $data
  121. ]);
  122. }
  123. /**
  124. * 设置卡片头部内容.
  125. *
  126. * @return $this
  127. */
  128. protected function withContent(): Recharge
  129. {
  130. $blue = Admin::color()->alpha('blue2', 0.5);
  131. $style = 'margin-bottom: 8px';
  132. $labelWidth = 120;
  133. return $this->content(
  134. <<<HTML
  135. <div class="d-flex pl-1 pr-1 pt-1" style="{$style}">
  136. <div style="width: {$labelWidth}px">
  137. <i class="fa fa-circle text-success"></i> 总充值金额
  138. </div>
  139. <div>{$this->totalRecharge}</div>
  140. </div>
  141. <div class="d-flex pl-1 pr-1" style="{$style}">
  142. <div style="width: {$labelWidth}px">
  143. <i class="fa fa-circle text-primary"></i> {$this->labels[0]}
  144. </div>
  145. <div>{$this->vipRecharge}</div>
  146. </div>
  147. <div class="d-flex pl-1 pr-1" style="{$style}">
  148. <div style="width: {$labelWidth}px">
  149. <i class="fa fa-circle" style="color: $blue"></i> {$this->labels[1]}
  150. </div>
  151. <div>{$this->goldRecharge}</div>
  152. </div>
  153. HTML
  154. );
  155. }
  156. }