quanwei
2 days ago 04102f7237efefa744090ed7c25f7b5d0807b679
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
 
namespace app\operations\service\statistics;
 
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\Category as CategoryModel;
use app\operations\model\supplier\Supplier as SupplierRegionModel;
use app\operations\model\user\Grade as GradeModel;
 
/**
 * 数据统计-用户排行
 */
class ProductRankingService
{
    /**
     * 订单商品占比
     */
    public function getProductRanking($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)));
        }
 
        $shop_supplier_ids = SupplierRegionModel::getSupplierIdsByUser(session('jjjshop_operations')['user']);
        if (empty($shop_supplier_ids)){
             $model = $model->where('order.shop_supplier_id', -1);
            $tmodel = $tmodel->where('order.shop_supplier_id', -1);
        }else{
            $model = $model->where( 'order.shop_supplier_id', 'in', $shop_supplier_ids);
            $tmodel = $tmodel->where( 'order.shop_supplier_id', 'in', $shop_supplier_ids);
        }
        //获取所有的订单商品数量
        $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)));
        }
        $shop_supplier_ids = SupplierRegionModel::getSupplierIdsByUser(session('jjjshop_operations')['user']);
        if (empty($shop_supplier_ids)){
            $model = $model->where('order.shop_supplier_id', -1);
            $tmodel = $tmodel->where('order.shop_supplier_id', -1);
        }else{
            $model = $model->where( 'order.shop_supplier_id', 'in', $shop_supplier_ids);
            $tmodel = $tmodel->where( 'order.shop_supplier_id', 'in', $shop_supplier_ids);
        }
        //获取所有的订单商品数量
        $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)));
        }
        $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);
        }
        //获取所有的订单商品数量
        $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)));
        }
        $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);
        }
        //获取所有的订单商品数量
        $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;
    }
 
 
 
 
}