checkToken(); if ($token[ 'code' ] < 0) { echo $this->response($token); exit; } } /** * 统计数据总和 */ public function getStatTotal() { $store_id = $this->params[ 'store_id' ] ?? 0; $start_time = $this->params[ 'start_time' ] ?? strtotime(date('Y-m-d', time())); $end_time = $this->params[ 'end_time' ] ?? time(); if ($start_time > $end_time) { $start_time = $this->params[ 'end_time' ]; $end_time = $this->params[ 'start_time' ]; } $stat_model = new StatModel(); $data = $stat_model->getShopStatSum($this->site_id, $start_time, $end_time, $store_id); return $this->response($data); } /** * 获取天统计趋势数据 */ public function getStatData() { $store_id = $this->params[ 'store_id' ] ?? 0; $start_time = $this->params[ 'start_time' ] ?? strtotime(date('Y-m-d', strtotime('-6 day'))); $end_time = $this->params[ 'end_time' ] ?? time(); if ($start_time > $end_time) { $start_time = $this->params[ 'end_time' ]; $end_time = $this->params[ 'start_time' ]; } $stat_model = new StatModel(); $fields = $stat_model->getStatField(); $fields[] = 'expected_earnings_total_money'; $stat_list = $stat_model->getShopStatList($this->site_id, $start_time, $end_time, $store_id)[ 'data' ]; $stat_list = array_map(function($item) { $item[ 'day_time' ] = date('Y-m-d', $item[ 'day_time' ]); return $item; }, $stat_list); $stat_list = array_column($stat_list, null, 'day_time'); $day = ceil(( $end_time - $start_time ) / 86400); foreach ($fields as $field) { $value = []; $time = []; for ($i = 0; $i < $day; $i++) { $date = date('Y-m-d', $start_time + $i * 86400); $time[] = $date; $value[] = isset($stat_list[ $date ]) ? $stat_list[ $date ][ $field ] : 0; } $data[ $field ] = $value; $data[ 'time' ] = $time; } return $this->response($this->success($data)); } /** * 获取小时统计趋势数据 */ public function getStatHourData() { $store_id = $this->params[ 'store_id' ] ?? 0; $time = $this->params[ 'start_time' ] ?? time(); $carbon = Carbon::createFromTimestamp($time); $stat_model = new StatModel(); $fields = $stat_model->getStatHourField(); $fields[] = 'expected_earnings_total_money'; $stat_list = $stat_model->getShopStatHourList($this->site_id, $carbon->year, $carbon->month, $carbon->day, $store_id)[ 'data' ]; $data = []; $empty = array_map(function() { return 0; }, range(0, 23, 1)); if (!empty($stat_list)) { $stat_list = array_column($stat_list, null, 'hour'); foreach ($fields as $field) { $value = []; for ($i = 0; $i < 24; $i++) { $value[ $i ] = isset($stat_list[ $i ]) ? $stat_list[ $i ][ $field ] : 0; } $data[ $field ] = $value; } } else { foreach ($fields as $field) { $data[ $field ] = $empty; } } $data[ 'time' ] = array_map(function($value) { return $value . '时'; }, range(0, 23, 1)); return $this->response($this->success($data)); } }