quanwei
18 hours ago c441dea81bd86bdfb12dff35821fed51f4cc91c2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
 
namespace app\operations\service\commoNstatistics;
 
use app\operations\model\order\OrderProduct as OrderProductModel;
use app\common\enum\order\OrderStatusEnum;
use app\common\enum\order\OrderPayStatusEnum;
use app\operations\model\product\Product as ProductModel;
use app\operations\model\order\OrderRefund as OrderRefundModel;
use app\operations\model\supplier\Supplier as SupplierRegionModel;
 
/**
 * 数据统计-商品销售榜
 */
class ProductRankingService
{
    /**
     * 商品销售榜
     */
    public function getSaleRanking($shop_supplier_id = 0)
    {
        $model = new OrderProductModel();
        if($shop_supplier_id > 0){
            $model = $model->where('order.shop_supplier_id', '=', $shop_supplier_id);
        }
        $shop_supplier_ids = SupplierRegionModel::getSupplierIdsByUser(session('jjjshop_operations')['user']);
        if (empty($shop_supplier_ids)){
            $model = $model->where('order.shop_supplier_id', -1);
        }else{
            $model = $model->where( 'order.shop_supplier_id', 'in', $shop_supplier_ids);
        }
        $list = $model->alias('o_product')
            ->field([
                'o_product.product_id',
                'SUM(total_pay_price) AS sales_volume',
                '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(10)
            ->select();
        foreach ($list as &$item){
            $detail = $model->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']:'';
        }
        return $list;
    }
 
    /**
     * 商品浏览榜
     */
    public function getViewRanking($shop_supplier_id = 0)
    {
        $model = new ProductModel();
        if($shop_supplier_id > 0){
            $model = $model->where('shop_supplier_id', '=', $shop_supplier_id);
        }
        return $model->with(['image.file'])
            ->hidden(['content'])
            ->where('view_times', '>', 0)
            ->order(['view_times' => 'DESC'])
            ->limit(10)
            ->select();
    }
 
    /**
     * 商品退款榜
     */
    public function getRefundRanking($shop_supplier_id = 0)
    {
        $model = new OrderRefundModel();
        if($shop_supplier_id > 0){
            $model = $model->where('shop_supplier_id', '=', $shop_supplier_id);
        }
        $list = $model->alias('order_refund')
            ->field([
                'order_product.product_id',
                'count(product_id) AS refund_count',
            ])->hidden(['content'])
            ->join('order_product', 'order_product.order_product_id = order_refund.order_product_id')
            ->group('order_product.product_id')
            ->having('refund_count>0')
            ->order(['refund_count' => 'DESC'])
            ->limit(10)
            ->select();
        $product_model = new OrderProductModel();
        foreach ($list as &$item){
            $detail = $product_model->with(['image'])->where('product_id', '=', $item['product_id'])->find();
            $item['image_path'] = $detail['image']['file_path'];
            $item['product_name'] = $detail['product_name'];
        }
        return $list;
    }
}