diff --git a/app/controller/api/dataview/Product.php b/app/controller/api/dataview/Product.php index a6d60fbc..7f124945 100644 --- a/app/controller/api/dataview/Product.php +++ b/app/controller/api/dataview/Product.php @@ -53,14 +53,26 @@ class Product extends BaseController $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) + ->where(function ($query){ + if ($this->streetCode != '') { + $query->where('m.street_id', $this->streetCode); + } else { + $query->where('m.area_id', $this->streetCode); + } + }) ->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) + ->where(function ($query){ + if ($this->streetCode != '') { + $query->where('m.street_id', $this->streetCode); + } else { + $query->where('m.area_id', $this->streetCode); + } + }) ->count(); // 上周商品总数 @@ -69,7 +81,13 @@ class Product extends BaseController $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) + ->where(function ($query){ + if ($this->streetCode != '') { + $query->where('m.street_id', $this->streetCode); + } else { + $query->where('m.area_id', $this->streetCode); + } + }) ->count(); // Db::name('store_product')->whereTime('create_time', '<=', $onWeekAgo)->count(); @@ -77,13 +95,18 @@ class Product extends BaseController $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) + ->where(function ($query){ + if ($this->streetCode != '') { + $query->where('m.street_id', $this->streetCode); + } else { + $query->where('m.area_id', $this->streetCode); + } + }) ->count(); // Db::name('store_product')->whereTime('create_time', '<=', time())->count(); // 计算商品总数周环比增长率 - $weeklyProductTotalGrowthRate = bcdiv(bcsub($thisWeekProductCount, $lastWeekProductCount), $lastWeekProductCount, 2); - + $weeklyProductTotalGrowthRate = $this->getRate($lastWeekProductCount, $thisWeekProductCount,4); return compact('todayProductCount', 'yestertodayProductCount', 'weeklyProductTotalGrowthRate'); } @@ -94,14 +117,26 @@ class Product extends BaseController $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) + ->where(function ($query){ + if ($this->streetCode != '') { + $query->where('m.street_id', $this->streetCode); + } else { + $query->where('m.area_id', $this->streetCode); + } + }) ->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) + ->where(function ($query){ + if ($this->streetCode != '') { + $query->where('m.street_id', $this->streetCode); + } else { + $query->where('m.area_id', $this->streetCode); + } + }) ->count(); // Db::name('store_product')->whereDay('create_time', 'yesterday')->count(); @@ -115,7 +150,13 @@ class Product extends BaseController $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) + ->where(function ($query){ + if ($this->streetCode != '') { + $query->where('m.street_id', $this->streetCode); + } else { + $query->where('m.area_id', $this->streetCode); + } + }) ->count(); // Db::name('store_product') // ->whereTime('create_time', 'between', [$previous_week_start, $previous_week_end]) @@ -129,14 +170,20 @@ class Product extends BaseController $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) + ->where(function ($query){ + if ($this->streetCode != '') { + $query->where('m.street_id', $this->streetCode); + } else { + $query->where('m.area_id', $this->streetCode); + } + }) ->count(); // Db::name('store_product') // ->whereTime('create_time', 'between', [$current_week_start, $current_week_end]) // ->count(); // 计算新增商品数的周环比增长率 - $weeklyNewProductTotalGrowthRate = bcdiv(bcsub($currWeekNewProductCount, $preWeekNewProductCount), $preWeekNewProductCount, 2); + $weeklyNewProductTotalGrowthRate = $this->getRate($preWeekNewProductCount, $currWeekNewProductCount, 4); return compact('todayNewProductCount', 'yestertodayNewProductCount', 'weeklyNewProductTotalGrowthRate'); } @@ -144,20 +191,38 @@ class Product extends BaseController { // 今日累计店铺总数 $todayMerchantCount = Db::name('merchant') - ->where('area_id', $this->areaCode) + ->where(function ($query){ + if ($this->streetCode != '') { + $query->where('street_id', $this->streetCode); + } else { + $query->where('area_id', $this->streetCode); + } + }) ->whereTime('create_time', '<=', time()) ->count(); // 昨日累计店铺总数 $yestertodayMerchantCount = Db::name('merchant') - ->where('area_id', $this->areaCode) + ->where(function ($query){ + if ($this->streetCode != '') { + $query->where('street_id', $this->streetCode); + } else { + $query->where('area_id', $this->streetCode); + } + }) ->whereTime('create_time', '<=', strtotime(date('Y-m-d', time()))-1) ->count(); // 上周累计店铺总数 $onWeekAgo = strtotime('-7 days'); $lastWeekMerchantCount = Db::name('merchant') - ->where('area_id', $this->areaCode) + ->where(function ($query){ + if ($this->streetCode != '') { + $query->where('street_id', $this->streetCode); + } else { + $query->where('area_id', $this->streetCode); + } + }) ->whereTime('create_time', '<=', $onWeekAgo) ->count(); @@ -168,7 +233,7 @@ class Product extends BaseController ->count(); // 计算商品总数周环比增长率 - $weeklyMerchantGrowthRate = bcdiv(bcsub($lastWeekMerchantCount, $thisWeekMerchantCount), $lastWeekMerchantCount, 2); + $weeklyMerchantGrowthRate = $this->getRate($lastWeekMerchantCount, $thisWeekMerchantCount, 4); return compact('todayMerchantCount', 'yestertodayMerchantCount', 'weeklyMerchantGrowthRate'); }