whereBetweenTime('order.create_time', $sta_time, date('Y-m-d 23:59:59', strtotime($end_time))); $model = $model->whereBetweenTime('order.create_time', $sta_time, date('Y-m-d 23:59:59', strtotime($end_time))); } //获取所有的订单商品数量 $total_product_num = $tmodel->alias('o_product') ->join('order', 'order.order_id = o_product.order_id') ->where('order.pay_status', '=', OrderPayStatusEnum::SUCCESS) ->where('order.order_status', '<>', OrderStatusEnum::CANCELLED) ->sum("o_product.total_num"); $list = $model->alias('o_product') ->field([ 'o_product.product_id', 'SUM(total_num) AS total_sales_num' ])->join('order', 'order.order_id = o_product.order_id') ->where('order.pay_status', '=', OrderPayStatusEnum::SUCCESS) ->where('order.order_status', '<>', OrderStatusEnum::CANCELLED) ->group('o_product.product_id') ->having('total_sales_num>0') ->order(['total_sales_num' => 'DESC']) ->limit(20) ->select(); foreach ($list as &$item){ $detail = $pmodel->with(['image'])->where('product_id', '=', $item['product_id'])->find(); $item['image_path'] = isset($detail['image'])?$detail['image']['file_path']:''; $item['product_name'] = $detail?$detail['product_name']:''; $item['total_sales_num'] = $item["total_sales_num"]; $item['rate'] = empty($total_product_num)? 0 : floor($item["total_sales_num"]/$total_product_num*100); } return $list; } /** * 订单商品金额占比 */ public function getProductMoneyRanking($postData) { $tmodel = $pmodel = $model = new OrderProductModel(); //搜索时间段 if (isset($postData['create_time']) && $postData['create_time'] != '') { $sta_time = array_shift($postData['create_time']); $end_time = array_pop($postData['create_time']); $tmodel = $tmodel->whereBetweenTime('order.create_time', $sta_time, date('Y-m-d 23:59:59', strtotime($end_time))); $model = $model->whereBetweenTime('order.create_time', $sta_time, date('Y-m-d 23:59:59', strtotime($end_time))); } //获取所有的订单商品数量 $total_product_money = $tmodel->alias('o_product') ->join('order', 'order.order_id = o_product.order_id') ->where('order.pay_status', '=', OrderPayStatusEnum::SUCCESS) ->where('order.order_status', '<>', OrderStatusEnum::CANCELLED) ->sum("o_product.total_pay_price"); $list = $model->alias('o_product') ->field([ 'o_product.product_id', 'SUM(total_pay_price) AS total_sales_money' ])->join('order', 'order.order_id = o_product.order_id') ->where('order.pay_status', '=', OrderPayStatusEnum::SUCCESS) ->where('order.order_status', '<>', OrderStatusEnum::CANCELLED) ->group('o_product.product_id') ->having('total_sales_money>0') ->order(['total_sales_money' => 'DESC']) ->limit(20) ->select(); foreach ($list as &$item){ $detail = $pmodel->with(['image'])->where('product_id', '=', $item['product_id'])->find(); $item['image_path'] = isset($detail['image'])?$detail['image']['file_path']:''; $item['product_name'] = $detail?$detail['product_name']:''; $item['total_sales_money'] = $item["total_sales_money"]; $item['rate'] = empty($total_product_money)? 0 : floor($item["total_sales_money"]/$total_product_money*100); } return $list; } /** * 商品分类占比 */ public function getCategoryRanking($postData) { $post_data = $postData; $model = new OrderProductModel(); //搜索时间段 if (!empty($postData['create_time'])) { $sta_time = array_shift($postData['create_time']); $end_time = array_pop($postData['create_time']); $model = $model->whereBetweenTime('order.create_time', $sta_time, date('Y-m-d 23:59:59', strtotime($end_time))); } //获取所有的订单商品数量 $total_product_num = $model->alias('o_product') ->join('order', 'order.order_id = o_product.order_id') ->where('order.pay_status', '=', OrderPayStatusEnum::SUCCESS) ->where('order.order_status', '<>', OrderStatusEnum::CANCELLED) ->sum("o_product.total_num"); //获取1级商品分类 $list = CategoryModel::getFirstCategory(); if(!empty($list)){ foreach ($list as &$item){ //获取当前分类的购买商品数量 $total_sales_num = OrderProductModel::getSalesNumByCategory($item["category_id"],$post_data); $item['rate'] = empty($total_product_num)? 0 : floor($total_sales_num/$total_product_num*100); $item['total_sales_num'] = $total_sales_num; $item['total_sales_money'] = OrderProductModel::getSalesMoneyByCategory($item["category_id"],$post_data); } } $array = $list->toArray(); usort($array, function($a, $b) { return $b['rate'] - $a['rate']; }); return $array; } /** * 会员等级占比 */ public function getGradeRanking($postData) { $post_data = $postData; $model = new OrderProductModel(); //搜索时间段 if (isset($postData['create_time']) && $postData['create_time'] != '') { $sta_time = array_shift($postData['create_time']); $end_time = array_pop($postData['create_time']); $model = $model->whereBetweenTime('order.create_time', $sta_time, date('Y-m-d 23:59:59', strtotime($end_time))); } //获取所有的订单商品数量 $total_product_num = $model->alias('o_product') ->join('order', 'order.order_id = o_product.order_id') ->where('order.pay_status', '=', OrderPayStatusEnum::SUCCESS) ->where('order.order_status', '<>', OrderStatusEnum::CANCELLED) ->sum("o_product.total_num"); //获取1级商品分类 $list = GradeModel::getUsableList(); if(!empty($list)){ foreach ($list as &$item){ //获取当前分类的购买商品数量 $total_sales_num = OrderProductModel::getSalesNumByGrade($item["grade_id"],$post_data); $item['rate'] = empty($total_product_num)? 0 : floor($total_sales_num/$total_product_num*100); $item['total_sales_num'] = $total_sales_num; $item['total_sales_money'] = OrderProductModel::getSalesMoneyByGrade($item["grade_id"],$post_data); } } $array = $list->toArray(); usort($array, function($a, $b) { return $b['rate'] - $a['rate']; }); return $array; } }