2023-12-06 10:08:21 +08:00

90 lines
3.4 KiB
PHP

<?php
namespace app\controller\api\dataview;
use app\common\repositories\BaseRepository;
use app\common\repositories\user\UserRepository;
use app\common\repositories\user\UserVisitRepository;
use crmeb\basic\BaseController;
use think\App;
use think\facade\Cache;
use think\facade\Db;
use think\exception\ValidateException;
class User extends BaseController
{
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 userMerchantCount(UserRepository $repository, UserVisitRepository $visitRepository)
{
// 近5日 平台用户量统计
$userCountlist = [];
$merchatCountList = [];
// 4天前
$startBeforeDay5 = strtotime(date('Y-m-d', strtotime('-4 days')));
$endBeforeDay5 = $startBeforeDay5+ 86399;
$userCountlist[] = $this->getTimeRangeUserCount([$startBeforeDay5, $endBeforeDay5]);
// 3天前
$startBeforeDay4 = strtotime(date('Y-m-d', strtotime('-3 days')));
$endBeforeDay4 = $startBeforeDay4 + 86399;
$userCountlist[] = $this->getTimeRangeUserCount([$startBeforeDay4, $endBeforeDay4]);
// 2天前
$startBeforeDay3 = strtotime(date('Y-m-d', strtotime('-2 days')));
$endBeforeDay3 = $startBeforeDay3 + 86399;
$userCountlist[] = $this->getTimeRangeUserCount([$startBeforeDay3, $endBeforeDay3]);
// 1天前
$startBeforeDay2= strtotime(date('Y-m-d', strtotime('-1 days')));
$endBeforeDay2 = $startBeforeDay2+ 86399;
$userCountlist[] = $this->getTimeRangeUserCount([$startBeforeDay2, $endBeforeDay2]);
// 今天
$startCurrDay1 = strtotime(date('Y-m-d', time()));
$endCurrDay1 = $startCurrDay1+ 86399;
$userCountlist[] = $this->getTimeRangeUserCount([$startCurrDay1, $endCurrDay1]);
// 地方店铺数量统计
// 该地区下所有乡镇
$geoStreetList = Db::name('geo_street')->where('area_code',$this->areaCode)->select()->toArray();
// 遍历统计每个乡镇的店铺数
$merchantTotalCount = 0;
foreach ($geoStreetList as $street) {
$temp['street_name'] = $street['street_name'];
$temp['merchant_count'] = Db::name('merchant')->where('street_id', $street['street_code'])->count();
$merchantTotalCount += $temp['merchant_count'];
$merchatCountList[] = $temp;
unset($temp);
}
return app('json')->success(compact('merchantTotalCount' ,'userCountlist', 'merchatCountList'));
}
public function getTimeRangeUserCount($timeRange=[])
{
// 新增
$newUserCount = Db::name('user')
->whereTime('create_time', 'between', $timeRange)
->count();
// 访问
$viewUserCount = Db::name('user')
->whereTime('last_time', 'between', $timeRange)
->count();
// 累计
$totalUserCount = Db::name('user')->whereTime('create_time', '<', $timeRange[1])->count();
return compact('newUserCount', 'viewUserCount', 'totalUserCount');
}
}