1], [['status',], 'default', 'value' => -1], [['date_start', 'date_end', 'fields'], 'trim'], // [['mch_per',], 'default', 'value' => false], ]; } public function init() { parent::init(); // TODO: Change the autogenerated stub } protected function get_all_data() { //以下随时间查询改变 $order_query = Order::find()->alias('o')->where(['o.is_recycle' => 0, 'o.is_delete' => 0]) ->andWhere(['not', ['o.cancel_status' => 1]]); if(\Yii::$app->mallId){ $order_query->andWhere(['o.mall_id' => \Yii::$app->mallId]); } //插件分类查询 if ($this->sign == 'all') { } else if ($this->sign == 'mall') { $order_query->andWhere(['o.sign' => '']); } else { $order_query->andWhere(['o.sign' => $this->sign]); } $wait_query = clone $order_query; $data_arr['wait_send_num'] = $wait_query->andWhere(['is_send' => 0]) ->andWhere(['or', ['o.is_pay' => 1], ['o.pay_type' => 2]]) ->andWhere(['o.cancel_status' => 0, 'o.sale_status' => 0]) ->count(); $refund_query = OrderRefund::find()->where([ 'status' => 1, 'is_delete' => 0 ]); if(\Yii::$app->mallId){ $refund_query->andWhere(['mall_id' => \Yii::$app->mallId,]); } $data_arr['wait_refund_num'] = $refund_query->count(); return $data_arr; } //经营概况 public function data_search() { if (!$this->validate()) { throw new ApiException(implode(",",$this->getFirstErrors())); } $arr_list = []; //订单数据 $arr_list['total_data'] = $this->today_order_data(); //wait数据 $arr_list['wait_data'] = $this->get_all_data(); return $arr_list; } /** * 销量排行榜 */ public function sales_top() { $this->goods_order = 'num DESC'; $goods_query = Order::find()->alias('o') ->where([ 'o.is_recycle' => 0, 'o.is_delete' => 0])->andWhere(['not', ['o.cancel_status' => 1]]) ->leftJoin(['od' => OrderDetail::tableName()], 'od.order_id = o.id') ->leftJoin(['g' => Goods::tableName()], 'g.id = od.goods_id') ->leftJoin(['gw' => GoodsWarehouse::tableName()], 'g.goods_warehouse_id = gw.id'); if(\Yii::$app->mallId){ $goods_query->andWhere(['g.mall_id' => \Yii::$app->mallId,]); } //排序 $goods_query->orderBy('num DESC,g.goods_warehouse_id'); $goods_query->select("COALESCE(SUM(od.`num`),0) AS `num`,gw.brand,gw.name") ->groupBy('g.goods_warehouse_id'); $goods_query_2 = clone $goods_query; $week_top_list = $goods_query ->andWhere(['>=', 'od.created_at', strtotime(date('Y-m-d',strtotime('-7 day')) . ' 00:00:00')]) ->andWhere(['<=', 'od.created_at', strtotime(date('Y-m-d') . ' 23:59:59')]) ->limit(10) ->asArray() ->all(); $mouth_top_list = $goods_query_2 ->andWhere(['>=', 'od.created_at', strtotime(date('Y-m-d',strtotime('-30 day')) . ' 00:00:00')]) ->andWhere(['<=', 'od.created_at', strtotime(date('Y-m-d') . ' 23:59:59')]) ->limit(10) ->asArray() ->all(); return [ 'week_top_list' => $week_top_list, 'mouth_top_list' => $mouth_top_list ]; } public function table_search() { if (!$this->validate()) { throw new ApiException(implode(",",$this->getFirstErrors())); } $query = $this->table_where(); $query->select("FROM_UNIXTIME(`o`.`created_at`, '%Y-%m-%d') AS `time`, COALESCE(COUNT(DISTINCT `o`.`id`),0) AS `order_num`,SUM(`o`.`total_pay_price`) AS `total_pay_price`"); //时间查询 if ($this->date_start) { $query->andWhere(['>=', 'o.created_at', strtotime($this->date_start . ' 00:00:00')]); } if ($this->date_end) { $query->andWhere(['<=', 'o.created_at', strtotime($this->date_end . ' 23:59:59')]); } $list = $query->groupBy('time') ->orderBy('time')->asArray()->all(); $day = floor((strtotime($this->date_end) - strtotime($this->date_start)) / 86400) + 1; for ($i = 0; $i < $day; $i++) { $date = date('Y-m-d', strtotime("-$i day",strtotime($this->date_end))); $bool = false; foreach ($list as $item) { if ($date == $item['time']) { $bool = true; $arr[$i]['created_at'] = $item['time']; $arr[$i]['order_num'] = $item['order_num']; $arr[$i]['total_pay_price'] = $item['total_pay_price']; } } if (!$bool) { $arr[$i]['created_at'] = $date; $arr[$i]['order_num'] = '0'; $arr[$i]['total_pay_price'] = '0.00'; } } $list = !empty($arr) ? array_reverse($arr) : []; return $list; } protected function table_where() { $orderQuery = OrderDetail::find()->alias('od')->where(['is_delete' => 0]) ->select(['od.order_id', 'SUM(`od`.`num`) num'])->groupBy('od.order_id'); //时间查询 if ($this->date_start) { $orderQuery->andWhere(['>=', 'od.created_at', strtotime($this->date_start . ' 00:00:00')]); } if ($this->date_end) { $orderQuery->andWhere(['<=', 'od.created_at', strtotime($this->date_end . ' 23:59:59')]); } $query = Order::find()->alias('o') ->where(['o.is_recycle' => 0, 'o.is_pay' => 1]) ->andWhere(['o.is_delete' => 0])->andWhere(['not', ['o.cancel_status' => 1]]) ->leftJoin(['d' => $orderQuery], 'd.order_id = o.id'); // ->leftJoin(['d' => OrderDetail::tableName()], 'd.order_id = o.id'); if(\Yii::$app->mallId){ $query->andWhere(['o.mall_id' => \Yii::$app->mallId]); } //插件分类查询 if ($this->sign == 'all') { } else if ($this->sign == 'mall') { $query->andWhere(['o.sign' => '']); } else { $query->andWhere(['o.sign' => $this->sign]); } return $query; } protected function goods_where() { $query = Order::find()->alias('o') ->where(['o.is_recycle' => 0, 'o.is_delete' => 0])->andWhere(['not', ['o.cancel_status' => 1]]) ->leftJoin(['od' => OrderDetail::tableName()], 'od.order_id = o.id and od.is_refund = 0')//过滤退款 ->leftJoin(['g' => Goods::tableName()], 'g.id = od.goods_id'); //时间查询 if ($this->date_start) { $query->andWhere(['>=', 'od.created_at', $this->date_start . ' 00:00:00']); } if ($this->date_end) { $query->andWhere(['<=', 'od.created_at', $this->date_end . ' 23:59:59']); } if(\Yii::$app->mallId){ $query->andWhere(['g.mall_id' => \Yii::$app->mallId, ]); } //排序 $query->orderBy((!empty($this->goods_order) ? $this->goods_order : 'total_price DESC') . ',g.goods_warehouse_id'); return $query; } public function today_order_data() { $this->date_start = date('Y-m-d',time()); $this->date_end = date('Y-m-d',time()); $order_query = $this->table_where(); $order_query->select("COALESCE(COUNT(DISTINCT `o`.`id`),0) AS `order_num`, COALESCE(SUM(`o`.`total_pay_price`),0) AS `total_pay_price`"); //比较昨日 $list_1 = [ 'order_num' => 0, 'total_pay_price' => 0, ]; $order_query_1 = clone $order_query; $order_query_1->andWhere(['>=', 'o.created_at', strtotime(date('Y-m-d', strtotime('-1 day')) . ' 00:00:00')]); $order_query_1->andWhere(['<=', 'o.created_at', strtotime(date('Y-m-d', strtotime('-1 day')) . ' 23:59:59')]); $list_1 = $order_query_1->asArray()->one(); //比较上周的这一日 $list_2 = [ 'order_num' => 0, 'total_pay_price' => 0, ]; $order_query_2 = clone $order_query; $order_query_2->andWhere(['>=', 'o.created_at', strtotime(date('Y-m-d', strtotime('-7 day')) . ' 00:00:00')]); $order_query_2->andWhere(['<=', 'o.created_at', strtotime(date('Y-m-d', strtotime('-7 day')) . ' 23:59:59')]); $list_2 = $order_query_2->asArray()->one(); //时间查询 if ($this->date_start) { $order_query->andWhere(['>=', 'o.created_at', strtotime($this->date_start . ' 00:00:00')]); } if ($this->date_end) { $order_query->andWhere(['<=', 'o.created_at', strtotime($this->date_end . ' 23:59:59')]); } $list = $order_query->asArray()->one(); //------------------------------------------------------------------------ $user_query = User::find(); //比较昨日 $user_num_1 = 0; $user_query_1 = clone $user_query; $user_query_1->andWhere(['>=', 'created_at', strtotime(date('Y-m-d', strtotime('-1 day')) . ' 00:00:00')]); $user_query_1->andWhere(['<=', 'created_at', strtotime(date('Y-m-d', strtotime('-1 day')) . ' 23:59:59')]); $user_num_1 = $user_query_1->count(); //比较上周的这一日 $user_num_2 = 0; $user_query_2 = clone $user_query; $user_query_2->andWhere(['>=', 'created_at', strtotime(date('Y-m-d', strtotime('-7 day')) . ' 00:00:00')]); $user_query_2->andWhere(['<=', 'created_at', strtotime(date('Y-m-d', strtotime('-7 day')) . ' 23:59:59')]); $user_num_2 = $user_query_2->count(); if ($this->date_start) { $user_query->andWhere(['>=', 'created_at', strtotime($this->date_start . ' 00:00:00')]); } if ($this->date_end) { $user_query->andWhere(['<=', 'created_at', strtotime($this->date_end . ' 23:59:59')]); } $user_num = $user_query->count(); //------------------------------------------------------------------------ $yestoday = []; $yestoday['order_num_status'] = empty($list_1) || $list['order_num'] > $list_1['order_num'] ? 'up' : ($list['order_num'] < $list_1['order_num'] ? 'down' : 'equal'); $yestoday['order_num_abs'] = abs($list['order_num']-$list_1['order_num']); $yestoday['total_pay_price_status'] = empty($list_1) || $list['total_pay_price'] > $list_1['total_pay_price'] ? 'up' : ($list['total_pay_price'] < $list_1['total_pay_price'] ? 'down' : 'equal'); $yestoday['total_pay_price_abs'] = abs($list['total_pay_price']-$list_1['total_pay_price']); $yestoday['user_num_status'] = $user_num > $user_num_1 ? 'up' : ($user_num < $user_num_1 ? 'down' : 'equal'); $yestoday['user_num_abs'] = abs($user_num-$user_num_1); $lastweek = []; $lastweek['order_num_status'] = empty($list_2) || $list['order_num'] > $list_2['order_num'] ? 'up' : ($list['order_num'] < $list_2['order_num'] ? 'down' : 'equal'); $lastweek['order_num_abs'] = abs($list['order_num']-$list_2['order_num']); $lastweek['total_pay_price_status'] = empty($list_2) || $list['total_pay_price'] > $list_2['total_pay_price'] ? 'up' : ($list['total_pay_price'] < $list_2['total_pay_price'] ? 'down' : 'equal'); $lastweek['total_pay_price_abs'] = abs(bcsub($list['total_pay_price'],$list_2['total_pay_price'],2)); $lastweek['user_num_status'] = $user_num > $user_num_2 ? 'up' : ($user_num < $user_num_2 ? 'down' : 'equal'); $lastweek['user_num_abs'] = abs($user_num-$user_num_2); $list['user_num'] = $user_num; return [ 'today' => $list, 'yestoday' => $yestoday, 'lastweek' => $lastweek ]; } }