123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- <?php
- namespace App\Admin\Metrics;
- use App\Models\UserRechargeRecord;
- use App\Models\UserVipRecord;
- use Carbon\Carbon;
- use Dcat\Admin\Admin;
- use Dcat\Admin\Widgets\Metrics\Round;
- use Illuminate\Database\Query\Builder;
- use Illuminate\Http\Request;
- class Recharge extends Round
- {
- protected $labels = ['VIP充值金额', '金币充值金额'];
- private $vipRecharge = 0;
- private $goldRecharge = 0;
- private $totalRecharge = 0;
- /**
- * 初始化卡片内容.
- */
- protected function init()
- {
- parent::init();
- $color = Admin::color();
- $colors = [$color->primary(), $color->alpha('blue2', 0.5)];
- $this->height(300);
- $this->chartHeight(300);
- $this->chartRadialBarSize(1);
- $this->title('VIP充值/金币充值金额');
- $this->chartLabels($this->labels);
- // 设置图表颜色
- $this->chartColors($colors);
- $this->dropdown([
- '1' => '当日新增充值金额',
- '7' => '7天新增充值金额',
- '30' => '当月新增充值金额',
- '-1' => '总充值金额',
- ]);
- }
- public function handle(Request $request)
- {
- switch ($request->get('option')) {
- case '-1':
- $this->getVipRecharge();
- $this->getGoldRecharge();
- break;
- case '7':
- $startAt = Carbon::now()->subDays(7)->toDateString() . ' 23:59:59';
- $endAt = Carbon::now()->toDateString() . ' 23:59:59';
- $this->getVipRecharge($startAt, $endAt);
- $this->getGoldRecharge($startAt, $endAt);
- break;
- case '30':
- $startAt = Carbon::now()->subDays(30)->toDateString() . ' 23:59:59';
- $endAt = Carbon::now()->toDateString() . ' 23:59:59';
- $this->getVipRecharge($startAt, $endAt);
- $this->getGoldRecharge($startAt, $endAt);
- break;
- case '1':
- default:
- $startAt = Carbon::now()->toDateString() . ' 00:00:0';
- $endAt = Carbon::now()->toDateString() . ' 23:59:59';
- $this->getVipRecharge($startAt, $endAt);
- $this->getGoldRecharge($startAt, $endAt);
- break;
- }
- $this->totalRecharge = $this->vipRecharge + $this->goldRecharge;
- $this->withContent();
- // 图表数据
- $vipPercent = $this->totalRecharge ? $this->vipRecharge / $this->totalRecharge : 0;
- $goldPercent = $this->totalRecharge ? $this->goldRecharge / $this->totalRecharge : 0;
- $data = [
- \round($vipPercent * 100, 2),
- \round($goldPercent * 100, 2),
- ];
- $this->withChart($data);
- $this->chartTotal('总充值金额', $this->totalRecharge);
- }
- private function getVipRecharge($statAt = '', $endAt = '')
- {
- $this->vipRecharge = UserVipRecord::withSum('pay', 'order_fee')
- ->whereHas('pay', function ($query) use ($statAt, $endAt) {
- /* @var Builder $query */
- $query->where('status', 1)
- ->when($statAt, function ($query) use ($statAt) {
- /* @var Builder $query */
- $query->where('created_at', '>', $statAt);
- })->when($statAt, function ($query) use ($endAt) {
- /* @var Builder $query */
- $query->where('created_at', '<=', $endAt);
- });
- })
- ->get()
- ->sum('pay_sum_order_fee');
- }
- private function getGoldRecharge($statAt = '', $endAt = '')
- {
- $this->goldRecharge = UserRechargeRecord::withSum('pay', 'order_fee')
- ->whereHas('pay', function ($query) use ($statAt, $endAt) {
- /* @var Builder $query */
- $query->where('status', 1)
- ->when($statAt, function ($query) use ($statAt) {
- /* @var Builder $query */
- $query->where('created_at', '>', $statAt);
- })->when($statAt, function ($query) use ($endAt) {
- /* @var Builder $query */
- $query->where('created_at', '<=', $endAt);
- });
- })
- ->get()
- ->sum('pay_sum_order_fee');
- }
- /**
- * 设置图表数据.
- *
- * @return $this
- */
- public function withChart($data): Recharge
- {
- return $this->chart([
- 'series' => $data,
- ]);
- }
- /**
- * 设置卡片头部内容.
- *
- * @return $this
- */
- protected function withContent(): Recharge
- {
- $blue = Admin::color()->alpha('blue2', 0.5);
- $style = 'margin-bottom: 8px';
- $labelWidth = 120;
- return $this->content(
- <<<HTML
- <div class="d-flex pl-1 pr-1 pt-1" style="{$style}">
- <div style="width: {$labelWidth}px">
- <i class="fa fa-circle text-success"></i> 总充值金额
- </div>
- <div>{$this->totalRecharge}</div>
- </div>
- <div class="d-flex pl-1 pr-1" style="{$style}">
- <div style="width: {$labelWidth}px">
- <i class="fa fa-circle text-primary"></i> {$this->labels[0]}
- </div>
- <div>{$this->vipRecharge}</div>
- </div>
- <div class="d-flex pl-1 pr-1" style="{$style}">
- <div style="width: {$labelWidth}px">
- <i class="fa fa-circle" style="color: $blue"></i> {$this->labels[1]}
- </div>
- <div>{$this->goldRecharge}</div>
- </div>
- HTML
- );
- }
- }
|