From 69a69bef4264805e7eab12b4c7cff8b67711f4f5 Mon Sep 17 00:00:00 2001 From: chenbo <709206448@qq.com> Date: Wed, 6 Dec 2023 16:25:11 +0800 Subject: [PATCH] =?UTF-8?q?add=20=E6=95=B0=E6=8D=AE=E4=B9=8B=E7=9C=BC-?= =?UTF-8?q?=E5=95=86=E5=93=81=E5=92=8C=E5=BA=97=E9=93=BA=E7=BB=9F=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controller/api/dataview/Product.php | 170 ++++++++++++++++++++++++ app/controller/api/dataview/User.php | 2 +- route/api.php | 1 + 3 files changed, 172 insertions(+), 1 deletion(-) create mode 100644 app/controller/api/dataview/Product.php diff --git a/app/controller/api/dataview/Product.php b/app/controller/api/dataview/Product.php new file mode 100644 index 00000000..e8c2281f --- /dev/null +++ b/app/controller/api/dataview/Product.php @@ -0,0 +1,170 @@ +repository = $repository; + $this->areaCode = $this->request->param('areaCode', ''); + $this->streetCode = $this->request->param('streetCode', ''); + + if ($this->areaCode == '' && $this->streetCode == '') { + throw new ValidateException('请选择地区'); + } + } + + // 今昨 商品和店铺统计信息 + public function productCount() + { + + $totalProductCounInfo = $this->countTotalProduct(); + + $newProductCountInfo = $this->countNewProduct(); + + $merchantCountInfo = $this->countMerchant(); + + return \app('json')->success(compact('totalProductCounInfo', 'newProductCountInfo', 'merchantCountInfo')); + } + + private function countTotalProduct() { + // 今日商品总数 截止到今日商品总数 + $todayProductCount = Db::name('store_product')->alias('p')->field('count(p.product_id) as think_count') + ->join('merchant m', 'm.mer_id = p.mer_id') + ->whereTime('p.create_time', '<=', time()) + ->where('m.area_id', $this->areaCode) + ->count(); + + // 昨日商品总数 + $yestertodayProductCount = Db::name('store_product')->alias('p')->field('count(p.product_id) as think_count') + ->join('merchant m', 'm.mer_id = p.mer_id') + ->whereTime('p.create_time', '<=', strtotime(date('Y-m-d', time()))-1) + ->where('m.area_id', $this->areaCode) + ->count(); + + // 上周商品总数 + $onWeekAgo = strtotime('-7 days'); + // 查询截止到上周的商品总数 + $lastWeekProductCount = Db::name('store_product')->alias('p')->field('count(p.product_id) as think_count') + ->join('merchant m', 'm.mer_id = p.mer_id') + ->whereTime('p.create_time', '<=', $onWeekAgo) + ->where('m.area_id', $this->areaCode) + ->count(); + // Db::name('store_product')->whereTime('create_time', '<=', $onWeekAgo)->count(); + + // 本周商品总数 + $thisWeekProductCount = Db::name('store_product')->alias('p')->field('count(p.product_id) as think_count') + ->join('merchant m', 'm.mer_id = p.mer_id') + ->whereTime('p.create_time', '<=', time()) + ->where('m.area_id', $this->areaCode) + ->count(); +// Db::name('store_product')->whereTime('create_time', '<=', time())->count(); + + // 计算商品总数周环比增长率 + $weeklyProductTotalGrowthRate = bcdiv(bcsub($thisWeekProductCount, $lastWeekProductCount), $lastWeekProductCount, 2); + + + return compact('todayProductCount', 'yestertodayProductCount', 'weeklyProductTotalGrowthRate'); + } + + private function countNewProduct() + { + // 今日新商品数 + $todayNewProductCount = Db::name('store_product')->alias('p')->field('count(p.product_id) as think_count') + ->join('merchant m', 'm.mer_id = p.mer_id') + ->whereDay('p.create_time', 'today') + ->where('m.area_id', $this->areaCode) + ->count(); +// Db::name('store_product')->whereDay('create_time', 'today')->count(); + // 昨日新商品数 + $yestertodayNewProductCount = Db::name('store_product')->alias('p')->field('count(p.product_id) as think_count') + ->join('merchant m', 'm.mer_id = p.mer_id') + ->whereDay('p.create_time', 'yesterday') + ->where('m.area_id', $this->areaCode) + ->count(); +// Db::name('store_product')->whereDay('create_time', 'yesterday')->count(); + + + // 上周新商品数 + $today = date('Y-m-d'); + // 获取上一个自然周的开始,结束日期 + $previous_week_start = strtotime(date('Y-m-d', strtotime('previous week', strtotime($today)))); + $previous_week_end = strtotime('+7 days', strtotime(date('Y-m-d', strtotime('previous week', strtotime($today))))) - 1; + // 查询上一个自然周 周期内新增商品数 + $preWeekNewProductCount = Db::name('store_product')->alias('p')->field('count(p.product_id) as think_count') + ->join('merchant m', 'm.mer_id = p.mer_id') + ->whereTime('p.create_time', 'between', [$previous_week_start, $previous_week_end]) + ->where('m.area_id', $this->areaCode) + ->count(); +// Db::name('store_product') +// ->whereTime('create_time', 'between', [$previous_week_start, $previous_week_end]) +// ->count(); + + // 本周新商品数 + // 获取本自然周的起始日期 + $current_week_start = strtotime(date('Y-m-d', strtotime('this week', strtotime($today)))); + $current_week_end = time(); + // 查询本自然周 周期内新增商品数 + $currWeekNewProductCount = Db::name('store_product')->alias('p')->field('count(p.product_id) as think_count') + ->join('merchant m', 'm.mer_id = p.mer_id') + ->whereTime('p.create_time', 'between', [$current_week_start, $current_week_end]) + ->where('m.area_id', $this->areaCode) + ->count(); +// Db::name('store_product') +// ->whereTime('create_time', 'between', [$current_week_start, $current_week_end]) +// ->count(); + + // 计算新增商品数的周环比增长率 + $weeklyNewProductTotalGrowthRate = bcdiv(bcsub($currWeekNewProductCount, $preWeekNewProductCount), $preWeekNewProductCount, 2); + return compact('todayNewProductCount', 'yestertodayNewProductCount', 'weeklyNewProductTotalGrowthRate'); + } + + private function countMerchant() + { + // 今日累计店铺总数 + $todayMerchantCount = Db::name('merchant') + ->where('area_id', $this->areaCode) + ->whereTime('create_time', '<=', time()) + ->count(); + + // 昨日累计店铺总数 + $yestertodayMerchantCount = Db::name('merchant') + ->where('area_id', $this->areaCode) + ->whereTime('create_time', '<=', strtotime(date('Y-m-d', time()))-1) + ->count(); + + // 上周累计店铺总数 + $onWeekAgo = strtotime('-7 days'); + $lastWeekMerchantCount = Db::name('merchant') + ->where('area_id', $this->areaCode) + ->whereTime('create_time', '<=', $onWeekAgo) + ->count(); + + // 本周店铺累计总数 + $thisWeekMerchantCount = Db::name('merchant') + ->where('area_id', $this->areaCode) + ->whereTime('create_time', '<=', time()) + ->count(); + + // 计算商品总数周环比增长率 + $weeklyMerchantGrowthRate = bcdiv(bcsub($lastWeekMerchantCount, $thisWeekMerchantCount), $lastWeekMerchantCount, 2); + return compact('todayMerchantCount', 'yestertodayMerchantCount', 'weeklyMerchantGrowthRate'); + } + +} \ No newline at end of file diff --git a/app/controller/api/dataview/User.php b/app/controller/api/dataview/User.php index c93c3f0a..363603bf 100644 --- a/app/controller/api/dataview/User.php +++ b/app/controller/api/dataview/User.php @@ -34,7 +34,7 @@ class User extends BaseController } } - public function userMerchantCount(UserRepository $repository, UserVisitRepository $visitRepository) + public function userMerchantCount() { // 近5日 平台用户量统计 $userCountlist = []; diff --git a/route/api.php b/route/api.php index 2076893e..662d01c0 100644 --- a/route/api.php +++ b/route/api.php @@ -735,6 +735,7 @@ Route::group('api/', function () { Route::get('order_statistics', 'Order/orderStatistics'); Route::get('sales_ranking', 'Order/salesRanking'); Route::get('curr_day_order_amount', 'Order/currDayOrderAmount'); + Route::get('product_count', 'Product/productCount'); /**---------------------数据之眼可视化大屏api-------------------- end */