添加后台dashboard数据

This commit is contained in:
Edward Yang 2022-08-03 19:18:18 +08:00
parent 7d67dd0d5c
commit f8d5f15c16
5 changed files with 174 additions and 17 deletions

View File

@ -3,12 +3,18 @@
namespace Beike\Admin\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Beike\Repositories\DashboardRepo;
class HomeController extends Controller
{
public function index()
{
return view('admin::pages.home');
$data = [
'product' => DashboardRepo::getProductData(),
'order' => DashboardRepo::getOrderData(),
'customer' => DashboardRepo::getCustomerData(),
'total' => DashboardRepo::getTotalData(),
];
return view('admin::pages.home', $data);
}
}

View File

@ -280,6 +280,28 @@ function current_currency_code(): string
return 'CNY';
}
/**
* 数量格式化, 用于商品、订单统计
*
* @param $quantity
* @return mixed|string
*/
function quantity_format($quantity)
{
if ($quantity > 1000000000000) {
return round($quantity / 1000000000000, 1) . 'T';
} elseif ($quantity > 1000000000) {
return round($quantity / 1000000000, 1) . 'B';
} elseif ($quantity > 1000000) {
return round($quantity / 1000000, 1) . 'M';
} elseif ($quantity > 1000) {
return round($quantity / 1000, 1) . 'K';
} else {
return $quantity;
}
}
/**
* 返回json序列化结果
*/

View File

@ -77,26 +77,49 @@ class CustomerRepo
* @return LengthAwarePaginator
*/
public static function list($data): LengthAwarePaginator
{
$builder = self::getListBuilder($data);
return $builder->paginate(20)->withQueryString();
}
/**
* 获取筛选对象
*
* @param array $filters
* @return Builder
*/
public static function getListBuilder(array $filters = []): Builder
{
$builder = Customer::query()->with("customerGroup.description");
if (isset($data['name'])) {
$builder->where('customers.name', 'like', "%{$data['name']}%");
if (isset($filters['name'])) {
$builder->where('customers.name', 'like', "%{$filters['name']}%");
}
if (isset($data['email'])) {
$builder->where('customers.email', 'like', "%{$data['email']}%");
if (isset($filters['email'])) {
$builder->where('customers.email', 'like', "%{$filters['email']}%");
}
if (isset($data['status'])) {
$builder->where('customers.status', $data['status']);
if (isset($filters['status'])) {
$builder->where('customers.status', (int)$filters['status']);
}
if (isset($data['from'])) {
$builder->where('customers.from', $data['from']);
if (isset($filters['from'])) {
$builder->where('customers.from', $filters['from']);
}
if (isset($data['customer_group_id'])) {
$builder->where('customers.customer_group_id', $data['customer_group_id']);
if (isset($filters['customer_group_id'])) {
$builder->where('customers.customer_group_id', $filters['customer_group_id']);
}
return $builder->paginate(20)->withQueryString();
$start = $filters['start'] ?? null;
if ($start) {
$builder->where('customers.created_at', '>', $start);
}
$end = $filters['end'] ?? null;
if ($end) {
$builder->where('customers.created_at', '<', $end);
}
return $builder;
}
public static function restore($id)

View File

@ -0,0 +1,83 @@
<?php
/**
* DashboardRepo.php
*
* @copyright 2022 opencart.cn - All Rights Reserved
* @link http://www.guangdawangluo.com
* @author Edward Yang <yangjin@opencart.cn>
* @created 2022-08-03 18:16:53
* @modified 2022-08-03 18:16:53
*/
namespace Beike\Repositories;
use Beike\Models\Customer;
use Beike\Models\Product;
class DashboardRepo
{
/**
* 获取产品总数
*
* @return array
*/
public static function getProductData(): array
{
return [
'total' => quantity_format(Product::query()->count()),
'percentage' => 0,
];
}
/**
* 获取订单基础统计, 总数和今日昨日比较
*
* @return array
*/
public static function getOrderData(): array
{
$today = OrderRepo::getListBuilder(['start' => today(), 'end' => today()->addDay()])->count();
$yesterday = OrderRepo::getListBuilder(['start' => today()->subDay(), 'end' => today()])->count();
$difference = $today - $yesterday;
if ($difference && $yesterday) {
$percentage = round(($difference / $yesterday) * 100);
} else {
$percentage = 0;
}
return [
'total' => quantity_format(Product::query()->count()),
'percentage' => $percentage,
];
}
/**
* 获取客户基础统计, 总数和今日昨日比较
*
* @return array
*/
public static function getCustomerData(): array
{
$today = CustomerRepo::getListBuilder(['start' => today(), 'end' => today()->addDay()])->count();
$yesterday = CustomerRepo::getListBuilder(['start' => today()->subDay(), 'end' => today()])->count();
$difference = $today - $yesterday;
if ($difference && $yesterday) {
$percentage = round(($difference / $yesterday) * 100);
} else {
$percentage = 0;
}
return [
'total' => quantity_format(Customer::query()->count()),
'percentage' => $percentage,
];
}
public static function getTotalData()
{
}
}

View File

@ -39,25 +39,48 @@ class OrderRepo
*/
public static function getListByCustomer($customer): LengthAwarePaginator
{
$builder = self::getListBuilder($customer);
$builder = self::getListBuilder(['customer' => $customer]);
return $builder->paginate();
}
/**
* @param null $customer
* @param array $filters
* @return LengthAwarePaginator
*/
public static function filterOrders(array $filters = []): LengthAwarePaginator
{
$builder = self::getListBuilder($filters);
return $builder->paginate();
}
/**
* @param array $filters
* @return Builder
*/
private static function getListBuilder($customer = null): Builder
public static function getListBuilder(array $filters = []): Builder
{
$builder = Order::query()->orderByDesc('created_at');
$customer = $filters['customer'] ?? null;
if ($customer) {
$builder->where('customer_id', $customer->id);
}
$start = $filters['start'] ?? null;
if ($start) {
$builder->where('created_at', '>', $start);
}
$end = $filters['end'] ?? null;
if ($end) {
$builder->where('created_at', '<', $end);
}
return $builder;
}
/**
* 通过订单号获取订单
*