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
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
<?php
 
namespace app\operations\model\order;
 
use app\common\model\order\OrderProduct as OrderProductModel;
use app\operations\model\product\Product as ProductModel;
use app\operations\model\supplier\Supplier as SupplierRegionModel;
use app\operations\model\user\User as UserModel;
use app\common\enum\order\OrderStatusEnum;
use app\common\enum\order\OrderPayStatusEnum;
 
/**
 * 订单商品模型
 */
class OrderProduct extends OrderProductModel
{
 
    //获取某个分类的购买商品数量
    public static function getSalesNumByCategory($category_id,$postData)
    {
        $product_ids = (new ProductModel())->where('category_id', '=', $category_id)->column("product_id");
        if(!empty($product_ids)){
            $model = new static();
            //搜索时间段
            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);
            }
            return $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)
                ->where('o_product.product_id', 'in', $product_ids)
                ->group('o_product.product_id')
                ->sum("total_num");
        }
        return 0;
    }
    //获取某个分类的购买商品金额
    public static function getSalesMoneyByCategory($category_id,$postData)
    {
        $product_ids = (new ProductModel())->where('category_id', '=', $category_id)->column("product_id");
        if(!empty($product_ids)){
            $model = new static();
            //搜索时间段
            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);
            }
            return $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)
                ->where('o_product.product_id', 'in', $product_ids)
                ->group('o_product.product_id')
                ->sum("total_pay_price");
        }
        return 0;
    }
 
    //获取会员等级的购买商品数量
    public static function getSalesNumByGrade($grade_id,$postData)
    {
        $user_ids = (new UserModel())->where('grade_id', '=', $grade_id)->column("user_id");
        if(!empty($user_ids)){
            $model = new static();
            //搜索时间段
            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);
            }
            return $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)
                ->where('o_product.user_id', 'in', $user_ids)
                ->group('o_product.product_id')
                ->sum("total_num");
        }
        return 0;
    }
    //获取会员等级的购买商品数量
    public static function getSalesMoneyByGrade($grade_id,$postData)
    {
        $user_ids = (new UserModel())->where('grade_id', '=', $grade_id)->column("user_id");
        if(!empty($user_ids)){
            $model = new static();
            //搜索时间段
            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);
            }
            return $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)
                ->where('o_product.user_id', 'in', $user_ids)
                ->group('o_product.product_id')
                ->sum("total_pay_price");
        }
        return 0;
    }
 
    public static function getPurchaseCount($userId, $postData)
    {
        $model = new static();
        //搜索时间段
        if (!empty($postData['start_time'])) {
            $sta_time = $postData['start_time'];
            $end_time = $postData['end_time'];
            $model = $model->whereBetweenTime('order.create_time', strtotime($sta_time), strtotime($end_time));
        }
        return $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)
            ->where('o_product.user_id', '=', $userId)
            ->where('o_product.is_vip',1)
            ->group('o_product.product_id')
            ->sum("total_num");
    }
 
}