<?php namespace App\Http\Services; //后台用户相关信息服务 use App\Model\SupplierChannelModel; use Carbon\Carbon; use DateInterval; use DatePeriod; use DateTime; use Illuminate\Support\Facades\DB; class IndexService { //获取每日新增统计 public function getDailySupplierAddStatistics() { //获取最近10天的数据 $date = Carbon::now()->subDays(9)->getTimestamp(); $today = time(); $period = new DatePeriod( new DateTime(date('Y-m-d H:i:s', $date)), new DateInterval('P1D'), new DateTime(date('Y-m-d H:i:s', $today + 86400)) ); $days = []; foreach ($period as $key => $value) { $days[] = $value->format('Y-m-d'); } $userId = request()->user->userId; //先统计全局的 $data = DB::connection('web')->select("select FROM_UNIXTIME( create_time, '%Y-%m-%d' ) AS date, count(1) as count from lie_supplier_channel" . " where create_time >= ${date} and create_time <= ${today} and status = 2 group by date order by date asc;"); //再统计个人的 $userData = DB::connection('web')->select("select FROM_UNIXTIME( create_time, '%Y-%m-%d' ) AS date, count(1) as count from lie_supplier_channel" . " where create_time >= ${date} and create_time <= ${today} and status =2 and create_uid = ? group by date order by date asc;", [$userId]); //组装数据 $allResult = $userResult = []; foreach ($days as $k => $day) { foreach ($userData as $key => $item) { if ($item['date'] == $day) { $userResult[$k] = [ 'date' => $day, 'count' => $item['count'] ]; } else { if (!in_array($day, array_column($userResult, 'date'))) { $userResult[$k] = [ 'date' => $day, 'count' => 0 ]; } } } foreach ($data as $key => $item) { if ($item['date'] == $day) { $allResult[$k] = [ 'date' => $day, 'count' => $item['count'] ]; } else { if (!in_array($day, array_column($allResult, 'date'))) { $allResult[$k] = [ 'date' => $day, 'count' => 0 ]; } } } } if (empty($data) || empty($userData)) { foreach ($days as $key => $day) { $allResult[$key] = [ 'date' => $day, 'count' => 0, ]; } } return [ 'all' => $allResult, 'user' => $userResult, ]; } }