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
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
<?php
 
namespace app\operations\service\commoNstatistics;
 
use app\common\library\helper;
use app\operations\model\order\Order as OrderModel;
use app\operations\model\order\OrderRefund as OrderRefundModel;
use app\operations\model\product\Product as ProductModel;
use app\operations\model\order\OrderProduct as OrderProductModel;
 
/**
 * 订单数据概况
 */
class OrderService
{
    // 商户id
    private $shop_supplier_id;
 
    public function __construct($shop_supplier_id)
    {
        $this->shop_supplier_id = $shop_supplier_id;
    }
    /**
     * 获取数据概况
     */
    public function getData($search_time='')
    {
        $today = date('Y-m-d');
        $yesterday = date('Y-m-d', strtotime('-1 day'));
        $start_day = '2020-1-1';//默认查询全部
        $end_day = null;
        if(!empty($search_time)){
            $start_day = array_shift($search_time);
            $end_day = array_pop($search_time);
        }
 
        $data = [
            // 成交额(元)
            'order_total_price' => [
                'total' => helper::number2($this->getOrderData($start_day, $end_day, 'order_total_price')),
                'today' => helper::number2($this->getOrderData($today, null, 'order_total_price')),
                'yesterday' => helper::number2($this->getOrderData($yesterday, null, 'order_total_price'))
            ],
            // 支付订单数
             'order_total' => [
                'total' => number_format($this->getOrderData($start_day, $end_day, 'order_total')),
                'today' => number_format($this->getOrderData($today, null, 'order_total')),
                'yesterday' => number_format($this->getOrderData($yesterday, null, 'order_total'))
            ],
            // 下单用户数
            'order_user_total' => [
                'total' => $this->getOrderData($start_day, $end_day, 'order_user_total'),
                'today' => $this->getOrderData($today, null, 'order_user_total'),
                'yesterday' => $this->getOrderData($yesterday, null, 'order_user_total')
            ],
            // 退款成功总金额
           'order_refund_money' => [
                'total' => $this->getOrderRefundData($start_day, $end_day, 'order_refund_money'),
                'today' => $this->getOrderRefundData($today, null, 'order_refund_money'),
                'yesterday' => $this->getOrderRefundData($yesterday, null, 'order_refund_money')
            ],
            // 退款成功订单数
            'order_refund_total' => [
                'total' => $this->getOrderRefundData($start_day, $end_day, 'order_refund_total'),
                'today' => $this->getOrderRefundData($today, null, 'order_refund_total'),
                'yesterday' => $this->getOrderRefundData($yesterday, null, 'order_refund_total')
            ],
        ];
 
        // 客单价
        $data['order_per_price'] = [
            'total' => $data['order_user_total']['total'] == 0?0:helper::number2($data['order_total_price']['total']/$data['order_user_total']['total']),
            'today' => $data['order_user_total']['today'] == 0?0:helper::number2($data['order_total_price']['today']/$data['order_user_total']['today']),
            'yesterday' => $data['order_user_total']['yesterday'] == 0?0:helper::number2($data['order_total_price']['yesterday']/$data['order_user_total']['yesterday'])
        ];
        return $data;
    }
 
    /**
     * 通过时间段查询订单数据
     */
    public function getDataByDate($days)
    {
        $data = [];
        foreach ($days as $day) {
            $data[] = [
                'day' => $day,
                'total_money' => $this->getOrderData($day, null, 'order_total_price'),
                'total_num' => $this->getOrderData($day, null, 'order_total')
            ];
        }
        return $data;
    }
 
    /**
     * 通过时间段查询退款订单数据
     */
    public function getRefundByDate($days)
    {
        $data = [];
        foreach ($days as $day) {
            $data[] = [
                'day' => $day,
                'total_money' => $this->getOrderRefundData($day, null, 'order_refund_money'),
                'total_num' => $this->getOrderRefundData($day, null, 'order_refund_total')
            ];
        }
        return $data;
    }
 
    /**
     * 获取订单数据
     */
    private function getOrderData($startDate = null, $endDate = null, $type)
    {
        return (new OrderModel)->getOrderData($startDate, $endDate, $type, $this->shop_supplier_id);
    }
 
    /**
     * 获取订单退款数据
     */
    private function getOrderRefundData($startDate = null, $endDate = null, $type)
    {
        return (new OrderRefundModel)->getOrderRefundData($startDate, $endDate, $type, $this->shop_supplier_id);
    }
 
    /**
     * 获取数据概况
     */
    public function getProductData()
    {
        $today = date('Y-m-d');
        $yesterday = date('Y-m-d', strtotime('-1 day'));
        $where["product_status"] = 10;
        if($this->shop_supplier_id > 0){
            $where["shop_supplier_id"] = $this->shop_supplier_id;
        }
        $data = [
            // 在售商品
            'sale' => [
                'today' => number_format((new ProductModel)->getProductTotal($where)),
                'yesterday' => '--'
            ],
            // 未付款商品(件)
            'no_pay' => [
                'today' => number_format($this->getOrderProductData($today, null, 'no_pay')),
                'yesterday' => number_format($this->getOrderProductData($yesterday, null, 'no_pay'))
            ],
            // 已付款商品(件)
            'pay' => [
                'today' => number_format($this->getOrderProductData($today, null, 'pay')),
                'yesterday' => number_format($this->getOrderProductData($yesterday, null, 'pay'))
            ],
        ];
        return $data;
    }
 
    /**
     * 获取订单商品数据
     */
    private function getOrderProductData($startDate = null, $endDate = null, $type)
    {
        return (new OrderProductModel)->getProductData($startDate, $endDate, $type, $this->shop_supplier_id);
    }
 
 
    /**
     * 通过时间段查询商品订单数据
     */
    public function getProductDataByDate($days)
    {
        $data = [];
        foreach ($days as $day) {
            $data[] = [
                'day' => $day,
                'total_num' => $this->getOrderProductData($day, null, 'pay')
            ];
        }
        return $data;
    }
}