2024-01-26 09:39:36 +08:00

304 lines
12 KiB
PHP
Executable File

<?php
namespace app\controller\api\dataview;
use app\common\repositories\store\product\ProductRepository;
use app\common\repositories\store\StoreCategoryRepository as repository;
use app\common\repositories\user\UserVisitRepository;
use crmeb\basic\BaseController;
use think\App;
use think\exception\ValidateException;
use think\facade\Cache;
use think\facade\Db;
class Product extends BaseController
{
/**
* @var repository
*/
protected $repository;
public $areaCode; // 区县地区码
public $streetCode; // 镇街道地区码
public function __construct(App $app, repository $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(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(function ($query){
if ($this->streetCode != '') {
$query->where('m.street_id', $this->streetCode);
} else {
$query->where('m.area_id', $this->streetCode);
}
})
->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(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();
// 本周商品总数
$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(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 = $this->getRate($lastWeekProductCount, $thisWeekProductCount,4);
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(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(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();
// 上周新商品数
$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(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])
// ->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(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 = $this->getRate($preWeekNewProductCount, $currWeekNewProductCount, 4);
return compact('todayNewProductCount', 'yestertodayNewProductCount', 'weeklyNewProductTotalGrowthRate');
}
private function countMerchant()
{
// 今日累计店铺总数
$todayMerchantCount = Db::name('merchant')
->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(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(function ($query){
if ($this->streetCode != '') {
$query->where('street_id', $this->streetCode);
} else {
$query->where('area_id', $this->streetCode);
}
})
->whereTime('create_time', '<=', $onWeekAgo)
->count();
// 本周店铺累计总数
$thisWeekMerchantCount = Db::name('merchant')
->where('area_id', $this->areaCode)
->whereTime('create_time', '<=', time())
->count();
// 计算商品总数周环比增长率
$weeklyMerchantGrowthRate = $this->getRate($lastWeekMerchantCount, $thisWeekMerchantCount, 4);
return compact('todayMerchantCount', 'yestertodayMerchantCount', 'weeklyMerchantGrowthRate');
}
// 实时浏览量
public function viewCount()
{
$res = Cache::store('file')->remember(self::class . '@viewCount', function () {
$today = $this->mainGroup('today');
$yesterday = $this->mainGroup('yesterday');
$lastWeek = $this->mainGroup(date('Y-m-d', strtotime('- 7day')));
$lastWeekRate = $this->getRate($lastWeek, $today, 4);
return compact('today', 'yesterday', 'lastWeekRate');
}, 2000 + random_int(600, 1200));
return app('json')->success($res);
}
protected function mainGroup($date)
{
$userVisitRepository = app()->make(UserVisitRepository::class);
$visitNum = (float)$userVisitRepository->dateVisitNum($date);
return $visitNum;
}
/**
* @param $last
* @param $today
* @param int $scale
* @return int|string|null
* @author xaboy
* @day 2020/6/25
*/
protected function getRate($last, $today, $scale = 2)
{
if ($last == $today)
return 0;
else if ($last == 0)
return $today;
else if ($today == 0)
return -$last;
else
return (float)bcdiv(bcsub($today, $last, $scale), $last, $scale);
}
// 商品分类列表
public function productCategoryList()
{
return app('json')->success($this->repository->getFormatList($this->request->merId()));
}
// 商品列表统计标题
public function getStatusFilter(ProductRepository $repository)
{
return app('json')->success($repository->getFilter(null,'商品',0));
}
// 商品列表
public function productList(ProductRepository $repository)
{
[$page, $limit] = $this->getPage();
$where = $this->request->params([['type', 1]]);
$where['is_gift_bag'] = 0;
$_where = $repository->switchType($where['type'], null,0);
// unset($_where['product_type']);
unset($_where['star']);
$where = array_merge($where, $_where);
return app('json')->success($repository->getAdminList(null, $where, $page, $limit));
}
}