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
<?php
 
namespace app\operations\model\plus\groupbuy;
 
use app\common\model\plus\groupbuy\Bill as BillModel;
 
/**
 * 商家端团购订单模型
 */
class Bill extends BillModel
{
    // 定义全局的查询范围
    protected $globalScope = ['status'];
    public function scopeStatus($query)
    {
        $shop_supplier_ids = \app\operations\model\supplier\Supplier::getSupplierIdsByUser(session('jjjshop_operations')['user']);
        if (empty($shop_supplier_ids)){
            $query->where($query->getTable() . '.shop_supplier_id', -1);
        }else{
            $query->where($query->getTable() . '.shop_supplier_id', 'in', $shop_supplier_ids);
        }
    }
    /**
     * 获取团购订单列表
     */
    public function getList($param)
    {
        $filter = [];
        if (!empty($param['search'])) {
            $filter[] = ['order_no', 'like', '%' . trim($param['search']) . '%'];
        }
        
        if (!empty($param['groupbuy_active_id'])) {
            $filter[] = ['groupbuy_active_id', '=', (int)$param['groupbuy_active_id']];
        }
        
        if (!empty($param['groupbuy_product_id'])) {
            $filter[] = ['groupbuy_product_id', '=', (int)$param['groupbuy_product_id']];
        }
        
        if (!empty($param['user_id'])) {
            $filter[] = ['user_id', '=', (int)$param['user_id']];
        }
        
        if (isset($param['status']) && $param['status'] !== '') {
            $filter[] = ['status', '=', (int)$param['status']];
        }
        
        // 店铺ID筛选
        $filter[] = ['shop_supplier_id', '=', $param['shop_supplier_id'] ?? 0];
        
        $order = ['create_time' => 'desc'];
        if (!empty($param['order'])) {
            $order = [$param['order_field'] => $param['order']];
        }
        
        return $this->with(['user', 'product', 'active'])
            ->where($filter)
            ->order($order)
            ->paginate([
                'list_rows' => $param['list_rows'] ?? 15,
                'query' => $param
            ]);
    }
 
    /**
     * 获取团购订单统计
     */
    public function getStatistics($param)
    {
        $filter = [];
        if (!empty($param['groupbuy_active_id'])) {
            $filter[] = ['groupbuy_active_id', '=', (int)$param['groupbuy_active_id']];
        }
        
        if (!empty($param['groupbuy_product_id'])) {
            $filter[] = ['groupbuy_product_id', '=', (int)$param['groupbuy_product_id']];
        }
        
        // 店铺ID筛选
        $filter[] = ['shop_supplier_id', '=', $param['shop_supplier_id'] ?? 0];
        
        // 总订单数
        $totalOrders = $this->where($filter)->count();
        
        // 总金额
        $totalAmount = $this->where($filter)->sum('total_price');
        
        // 已支付订单数
        $paidOrders = $this->where($filter)->where('status', '>', 0)->count();
        
        // 已支付金额
        $paidAmount = $this->where($filter)->where('status', '>', 0)->sum('total_price');
        
        return [
            'total_orders' => $totalOrders,
            'total_amount' => $totalAmount,
            'paid_orders' => $paidOrders,
            'paid_amount' => $paidAmount
        ];
    }
 
    /**
     * 导出团购订单
     */
    public function exportList($param)
    {
        $list = $this->getList($param)->items();
        
        // 构建导出数据
        $exportData = [];
        $exportData[] = ['订单号', '团购活动', '团购商品', '用户', '总金额', '实付金额', '状态', '创建时间'];
        
        foreach ($list as $item) {
            $exportData[] = [
                $item['order_no'],
                $item['active']['active_name'] ?? '',
                $item['product']['product']['product_name'] ?? '',
                $item['user']['nickName'] ?? '',
                $item['total_price'],
                $item['actual_price'],
                $this->getStatusText($item['status']),
                date('Y-m-d H:i:s', $item['create_time'])
            ];
        }
        
        // 导出到Excel
        $fileName = 'groupbuy_orders_' . date('YmdHis') . '.xlsx';
        $filePath = 'uploads/export/' . $fileName;
        
        // 这里应该使用PHPExcel或类似的库来导出Excel
        // 由于当前系统可能已有相关服务,这里简化处理
        
        return $filePath;
    }
 
    /**
     * 获取状态文本
     */
    private function getStatusText($status)
    {
        $statusMap = [
            -1 => '已取消',
            0 => '待支付',
            1 => '已支付',
            2 => '已完成'
        ];
        
        return $statusMap[$status] ?? '未知状态';
    }
 
    /**
     * 更新订单状态
     */
    public function updateStatus($status)
    {
        return $this->save(['status' => $status]) !== false;
    }
}