170 lines
7.1 KiB
PHP
170 lines
7.1 KiB
PHP
![]() |
<?php
|
||
|
|
||
|
namespace app\controller\api\dataview;
|
||
|
|
||
|
use app\common\repositories\BaseRepository;
|
||
|
use crmeb\basic\BaseController;
|
||
|
use think\App;
|
||
|
use think\exception\ValidateException;
|
||
|
use think\facade\Db;
|
||
|
class Product extends BaseController
|
||
|
{
|
||
|
/**
|
||
|
* @var repository
|
||
|
*/
|
||
|
protected $repository;
|
||
|
|
||
|
public $areaCode; // 区县地区码
|
||
|
|
||
|
public $streetCode; // 镇街道地区码
|
||
|
|
||
|
public function __construct(App $app, BaseRepository $repository)
|
||
|
{
|
||
|
parent::__construct($app);
|
||
|
$this->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');
|
||
|
}
|
||
|
|
||
|
}
|