123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- <?php
- namespace App\Repositories\Eloquent;
- use App\Models\BrowseRecord;
- use App\Models\NewMediaAccount;
- use App\Models\User;
- use App\Repositories\Contracts\DashboardInterface;
- class DashboardFacadeRepository extends BaseRepository implements DashboardInterface
- {
- /**
- * 获取指定时间段内的注册数据
- *
- * @param string $start 开始时间
- * @param string $end 结束时间
- * @param string $dateType 日期类型 0:按照起始时间:1日 日 当日+往前6天 2周 当前周+往前6周 3月 起始时间所在月,往前+5个月的数据
- * @return array 返回包含总数据、计数数据和按日期分组的数据数组
- */
- public function getPeriodRegisiter($start, $end, $dateType = 0)
- {
- //从配置中获取x轴字段,默认为['社心小程序', '新媒体']
- $xField = config('dashboard.UG.xField', ['社心小程序', '新媒体']);
- $ret = [];
- $mpCount = 0;
- $nmCount = 0;
- if ($dateType == 0) {
- //查询指定时间段内的浏览记录
- $result = BrowseRecord::whereBetween('created_at', [$start, $end])->get();
- //初始化数据结构,为每个日期创建一个数据项,并将x_field关联的数据初始化为0
- while (strtotime($start) <= strtotime($end)) {
- $ret[$start] = [
- 'x_data' => $start,
- 'y_field' => $xField,
- 'y_data' => array_fill(0, count($xField), 0),
- ];
- $startTime = date('Y-m-d', strtotime("+1 day", strtotime($start)));
- }
- //遍历查询结果,统计每个日期的社心小程序浏览量
- foreach ($result as $item) {
- $mpCount++;
- $ret[date('Y-m-d', strtotime($item->created_at))]['y_data'][array_search('社心小程序', $xField)]++;
- }
-
- } elseif ($dateType == 1) {
- $endTime = date('Y-m-d');
- $startTime = date('Y-m-d', strtotime("-6 days", strtotime($endTime)));
- //查询指定时间段内的浏览记录
- $result = BrowseRecord::whereBetween('created_at', [$startTime, $endTime])->get();
- //初始化数据结构,为每个日期创建一个数据项,并将x_field关联的数据初始化为0
- while (strtotime($startTime) <= strtotime($endTime)) {
- $ret[$startTime] = [
- 'x_data' => $startTime,
- 'y_field' => $xField,
- 'y_data' => array_fill(0, count($xField), 0),
- ];
- $startTime = date('Y-m-d', strtotime("+1 day", strtotime($startTime)));
- }
- //遍历查询结果,统计每个日期的社心小程序浏览量
- foreach ($result as $item) {
- $mpCount++;
- $ret[date('Y-m-d', strtotime($item->created_at))]['y_data'][array_search('社心小程序', $xField)]++;
- }
- } elseif ($dateType == 2){
- //当前周+往前6周
- $week = 0;
- while($week <7) {
- // 获取当前日期所在周的周日
- $endTime = date('Y-m-d', strtotime('sunday -'.$week.' week', strtotime($end)));
- // 获取六个自然周前的周一
- $startTime = date('Y-m-d', strtotime('monday -'.($week+1).' week', strtotime($end)));
- $result = BrowseRecord::whereBetween('created_at', [$startTime, $endTime])->get();
- $ret[] = [
- 'x_data' => $startTime.'~'.$endTime,
- 'y_field' => $xField,
- 'y_data' => [$result->count(), 0],
- ];
- $mpCount+= $result->count();
- $week++;
- }
- }elseif ($dateType == 3){
- //当前周+往前6周
- $month = 0;
- while($month <6) {
- $endTime = date('Y-m-t', strtotime(' -'.$month.' month', strtotime($end)));
- $startTime = date('Y-m-1', strtotime(' -'.$month.' month', strtotime($end)));
- $result = BrowseRecord::whereBetween('created_at', [$startTime, $endTime])->get();
- $ret[] = [
- 'x_data' => date('Y-m', strtotime($startTime)),
- 'y_field' => $xField,
- 'y_data' => [$result->count(), 0],
- ];
- $mpCount+= $result->count();
- $month++;
- }
- }
-
- //返回数据包括:总数据、计数数据和按日期分组的数据
- return [
- 'total' => [['label' => '社心小程序', 'value' => User::count()], ['label' => '新媒体', 'value' => NewMediaAccount::sum('fans_num')]],
- 'count' => [['label' => '社心小程序', 'value' => $mpCount], ['label' => '新媒体', 'value' => 0]],
- 'data' => array_values($ret),
- ];
- }
- }
|