Files
water-back/app/Admin/Controllers/Platform/DashboardController.php
2023-01-11 11:00:43 +08:00

291 lines
9.6 KiB
PHP

<?php
namespace App\Admin\Controllers\Platform;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;
use Encore\Admin\Widgets\InfoBox;
use Illuminate\Routing\Controller;
use Modules\Mall\Models\Order;
use Modules\Mall\Models\OrderItem;
use Modules\User\Models\User;
use Modules\User\Models\UserChannel;
use Modules\User\Models\UserStock;
class DashboardController extends Controller
{
public function index(Content $content): Content
{
$this->content = $content->title('数据看板')->description('Description...');
$this->getVipUserData();
$this->getUserStockData();
// $this->getChannelData();
return $this->content;
}
/**
* Notes: 获取用户数据
*
* @Author: 玄尘
* @Date : 2021/11/17 11:24
*/
public function getVipUserData(): Content
{
$this->content->row($this->setDivider('会员统计'));
$users = [
'all' => [
'name' => '总会员量',
'color' => 'blue',
'count' => User::query()
->whereHas('identities', function ($q) {
$q->where('id', '>', 1);
})->count()
],
'mo' => [
'name' => '月卡会员量',
'color' => 'red',
'count' => User::query()
->whereHas('identities', function ($q) {
$q->where('id', 2);
})->count()
],
'jk' => [
'name' => '季卡会员量',
'color' => 'red',
'count' => User::query()
->whereHas('identities', function ($q) {
$q->where('id', 3);
})->count()
],
'nk' => [
'name' => '年卡会员量',
'color' => 'yellow',
'count' => User::query()
->whereHas('identities', function ($q) {
$q->where('id', 4);
})->count(),
],
'onlone' => [
'name' => '在线会员数',
'color' => 'red',
'count' => User::query()
->whereHas('identities', function ($q) {
$q->where('id', '>', 1);
})
->where('status', User::STATUS_INIT)
->count(),
],
'refund' => [
'name' => '退费会员数',
'color' => 'maroon',
'count' => User::query()
->whereHas('identities', function ($q) {
$q->where('id', '>', 1);
})
->where('status', User::STATUS_REFUND)
->count(),
],
];
$this->content->row(function (Row $row) use ($users) {
foreach ($users as $user) {
$row->column(2, function (Column $column) use ($user) {
$column->append(new InfoBox(
$user['name'],
'users',
$user['color'],
'/admin/platform/vips',
$user['count'],
));
});
}
});
return $this->content;
}
/**
* Notes: 获取渠道数据
*
* @Author: 玄尘
* @Date: 2022/10/14 13:55
*/
public function getChannelData()
{
$this->content->row($this->setDivider('渠道数据'));
$channels = UserChannel::query()->get();
$users = [
'all' => [
'name' => '总会员量',
'color' => 'blue',
'count' => User::query()
->whereHas('identities', function ($q) {
$q->where('id', '>', 2);
})
],
'jk' => [
'name' => '季卡会员量',
'color' => 'red',
'count' => User::query()
->whereHas('identities', function ($q) {
$q->where('id', 3);
})
],
'nk' => [
'name' => '年卡会员量',
'color' => 'yellow',
'count' => User::query()
->whereHas('identities', function ($q) {
$q->where('id', 4);
}),
],
'cs' => [
'name' => '创始会员量',
'color' => 'aqua',
'count' => User::query()
->whereHas('identities', function ($q) {
$q->where('id', 5);
}),
],
'hh' => [
'name' => '合伙人量',
'color' => 'navy',
'count' => User::query()
->whereHas('identities', function ($q) {
$q->where('id', 6);
}),
],
// 'onlone' => [
// 'name' => '在线会员数',
// 'color' => 'red',
// 'count' => User::query()
// ->whereHas('identities', function ($q) {
// $q->where('id', '>', 2);
// })
// ->where('status', User::STATUS_INIT),
// ],
'refund' => [
'name' => '退费会员数',
'color' => 'maroon',
'count' => User::query()
->whereHas('identities', function ($q) {
$q->where('id', '>', 2);
})
->where('status', User::STATUS_REFUND),
],
];
foreach ($channels as $channel) {
$this->channel_id = $channel->id;
$this->content->row(function (Row $row) use ($channel, $users) {
foreach ($users as $user) {
$row->column(2, function (Column $column) use ($channel, $user) {
$column->append(new InfoBox(
$channel->name.'-'.$user['name'],
'users',
$user['color'],
'/admin/platform/vips?channel_id='.$channel->id,
(clone $user['count'])->where('channel_id', $channel->id)->count(),
));
});
}
});
}
return $this->content;
}
/**
* Notes: 用户水数量
*
* @Author: 玄尘
* @Date: 2022/9/1 13:35
* @return mixed
*/
public function getUserStockData()
{
$this->content->row($this->setDivider('会员库存数'));
$all = UserStock::query()->sum('stock');
$holds = UserStock::query()->sum('hold');
$sy = bcsub($all, $holds);
$users = [
'all' => [
'name' => '累计总箱数',
'color' => 'blue',
'count' => $all
],
'stock' => [
'name' => '累计提货数',
'color' => 'green',
'count' => $holds
],
'sy' => [
'name' => '累计剩余',
'color' => 'green',
'count' => $sy
],
'online' => [
'name' => '线上发货',
'color' => 'green',
'count' => OrderItem::query()
->whereHas('order', function ($q) {
$q->where('type', Order::TYPE_SAMPLE)
->whereIn('state', [
Order::STATUS_PAID,
Order::STATUS_DELIVERED,
Order::STATUS_SIGNED,
])->where('channel', Order::CHANNEL_USER);
})
->sum('qty')
],
'offline' => [
'name' => '线下发货',
'color' => 'green',
'count' => OrderItem::query()
->whereHas('order', function ($q) {
$q->where('type', Order::TYPE_SAMPLE)
->whereIn('state', [
Order::STATUS_PAID,
Order::STATUS_DELIVERED,
Order::STATUS_SIGNED,
])->where('channel', Order::CHANNEL_SYSTEM);
})
->sum('qty')
],
];
$this->content->row(function (Row $row) use ($users) {
foreach ($users as $user) {
$row->column(2, function (Column $column) use ($user) {
$column->append(new InfoBox(
$user['name'],
'goods',
$user['color'],
'/admin/users/stocks',
$user['count'],
));
});
}
});
return $this->content;
}
public function setDivider($title)
{
return <<<HTML
<div style="height: 20px; border-bottom: 1px solid #eee; text-align: center;margin-top: 20px;margin-bottom: 20px;">
<span style="font-size: 18px; padding: 0 10px;">
{$title}
</span>
</div>
HTML;
}
}