request->isAjax()) { $startTime = $this->parseTime($this->request->get('start_time')); $endTime = $this->parseTime($this->request->get('end_time'), true); $timeWhere = ['createtime', 'between', [$startTime, $endTime]]; $summary = [ 'agent_total' => Agent::where('status', 'normal')->count(), 'agent_pending' => Agent::where('status', 'pending')->count(), 'order_count' => RegionalOrderModel::where($timeWhere[0], $timeWhere[1], $timeWhere[2])->count(), 'order_money' => round(RegionalOrderModel::where($timeWhere[0], $timeWhere[1], $timeWhere[2])->sum('amount'), 2), 'commission_total' => round(RewardModel::where($timeWhere[0], $timeWhere[1], $timeWhere[2])->sum('commission'), 2), 'commission_settled' => round(RewardModel::where($timeWhere[0], $timeWhere[1], $timeWhere[2])->where('status', 1)->sum('commission'), 2), 'commission_waiting' => round(RewardModel::where($timeWhere[0], $timeWhere[1], $timeWhere[2])->where('status', 0)->sum('commission'), 2), ]; $topAreas = Db::name('shopro_regional_agent_order') ->alias('o') ->join('shopro_regional_agent a', 'o.agent_id = a.id') ->where('o.createtime', 'between', [$startTime, $endTime]) ->field('a.area_name, a.area_level, COUNT(o.id) as order_count, SUM(o.amount) as order_money') ->group('o.agent_id') ->order('order_money desc') ->limit(10) ->select(); $dailyTrend = Db::name('shopro_regional_agent_order') ->where('createtime', 'between', [$startTime, $endTime]) ->field(Db::raw("FROM_UNIXTIME(createtime, '%Y-%m-%d') as day, COUNT(id) as order_count, SUM(amount) as order_money")) ->group('day') ->order('day asc') ->select(); $this->success('区域业绩看板', null, [ 'summary' => $summary, 'top_areas' => $topAreas, 'daily_trend' => $dailyTrend, ]); } return $this->view->fetch(); } protected function parseTime($value, $endOfDay = false) { if (empty($value)) { return $endOfDay ? strtotime(date('Y-m-d 23:59:59')) : strtotime(date('Y-m-d 00:00:00', strtotime('-29 days'))); } $time = is_numeric($value) ? intval($value) : strtotime($value); if ($endOfDay && !is_numeric($value) && strpos($value, ':') === false) { $time = strtotime(date('Y-m-d 23:59:59', $time)); } return $time; } }