From f8d5f15c16317526978fc38699eaf20cae0032c6 Mon Sep 17 00:00:00 2001 From: Edward Yang Date: Wed, 3 Aug 2022 19:18:18 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=90=8E=E5=8F=B0dashboard?= =?UTF-8?q?=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Admin/Http/Controllers/HomeController.php | 10 ++- beike/Helpers.php | 22 +++++ beike/Repositories/CustomerRepo.php | 45 +++++++--- beike/Repositories/DashboardRepo.php | 83 +++++++++++++++++++ beike/Repositories/OrderRepo.php | 31 ++++++- 5 files changed, 174 insertions(+), 17 deletions(-) create mode 100644 beike/Repositories/DashboardRepo.php diff --git a/beike/Admin/Http/Controllers/HomeController.php b/beike/Admin/Http/Controllers/HomeController.php index 2eaaa36e..d0df0cd6 100644 --- a/beike/Admin/Http/Controllers/HomeController.php +++ b/beike/Admin/Http/Controllers/HomeController.php @@ -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); } } diff --git a/beike/Helpers.php b/beike/Helpers.php index 90aa75bd..beea327b 100644 --- a/beike/Helpers.php +++ b/beike/Helpers.php @@ -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序列化结果 */ diff --git a/beike/Repositories/CustomerRepo.php b/beike/Repositories/CustomerRepo.php index 8052340d..335f671e 100644 --- a/beike/Repositories/CustomerRepo.php +++ b/beike/Repositories/CustomerRepo.php @@ -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) diff --git a/beike/Repositories/DashboardRepo.php b/beike/Repositories/DashboardRepo.php new file mode 100644 index 00000000..92f5f046 --- /dev/null +++ b/beike/Repositories/DashboardRepo.php @@ -0,0 +1,83 @@ + + * @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() + { + + } +} diff --git a/beike/Repositories/OrderRepo.php b/beike/Repositories/OrderRepo.php index b9b96321..1ca431bf 100644 --- a/beike/Repositories/OrderRepo.php +++ b/beike/Repositories/OrderRepo.php @@ -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; } - /** * 通过订单号获取订单 *