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
<?php
 
namespace app\operations\controller\statistics;
 
use app\operations\controller\Controller;
use app\operations\service\commoNstatistics\OrderService;
use app\operations\service\commoNstatistics\ProductRankingService;
use app\operations\model\supplier\Supplier as SupplierModel;
 
/**
 * 销售数据控制器
 */
class Sales extends Controller
{
    /**
     * 销售数据统计
     */
    public function index($shop_supplier_id=0)
    {
        return $this->renderSuccess('', [
            // 成交订单统计
            'order' => (new OrderService($shop_supplier_id))->getData(),
            // 成交商品统计
            'product' => (new OrderService($shop_supplier_id))->getProductData(),
            // 销量top10
            'productSaleRanking' => (new ProductRankingService())->getSaleRanking($shop_supplier_id),
            // 浏览top10
            'productViewRanking' => (new ProductRankingService())->getViewRanking($shop_supplier_id),
            // 退款top10
            'productRefundRanking' => (new ProductRankingService())->getRefundRanking($shop_supplier_id),
            'supplier_list' => SupplierModel::getAll(null),
        ]);
    }
 
    /**
     * 订单数据统计
     */
    public function orderData($shop_supplier_id=0,$search_time='')
    {
        return $this->renderSuccess('', [
            // 成交订单统计
            'order' => (new OrderService($shop_supplier_id))->getData($search_time),
        ]);
    }
 
    /**
     * 通过时间段查询本期上期金额
     * $type类型:order refund
     */
    public function order($search_time, $type = 'order',$shop_supplier_id=0)
    {
        $days = $this->getDays($search_time);
        $data = [];
        if($type == 'order'){
            $data = (new OrderService($shop_supplier_id))->getDataByDate($days);
        }else if($type == 'refund'){
            $data = (new OrderService($shop_supplier_id))->getRefundByDate($days);
        }
        return $this->renderSuccess('', [
            // 日期
            'days' => $days,
            // 数据
            'data' => $data,
        ]);
    }
 
    /**
     * 通过时间段查询本期上期金额
     */
    public function product($search_time,$shop_supplier_id=0)
    {
        $days = $this->getDays($search_time);
        return $this->renderSuccess('', [
            // 日期
            'days' => $days,
            // 数据
            'data' => (new OrderService($shop_supplier_id))->getProductDataByDate($days),
        ]);
    }
 
    /**
     * 获取具体日期数组
     */
    private function getDays($search_time)
    {
        //搜索时间段
        if(!isset($search_time) || empty($search_time)){
            //没有传,则默认为最近7天
            $end_time = date('Y-m-d', time());
            $start_time = date('Y-m-d', strtotime('-7 day',time()));
        }else{
            $start_time = array_shift($search_time);
            $end_time = array_pop($search_time);
        }
 
        $dt_start = strtotime($start_time);
        $dt_end = strtotime($end_time);
        $date = [];
        $date[] = date('Y-m-d', strtotime($start_time));
        while($dt_start < $dt_end) {
            $date[] = date('Y-m-d', strtotime('+1 day',$dt_start));
            $dt_start = strtotime('+1 day',$dt_start);
        }
        return $date;
    }
}