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
<?php
 
namespace app\api\model\plus\groupbuy;
 
use app\common\model\plus\groupbuy\Bill as BillModel;
 
/**
 * 团购订单模型(API端)
 */
class Bill extends BillModel
{
    /**
     * 获取正在进行中的团购订单
     */
    public function getBill($groupbuy_product_id, $groupbuy_active_id, $groupbuy_num)
    {
        $filter = [
            'groupbuy_product_id' => $groupbuy_product_id,
            'groupbuy_active_id' => $groupbuy_active_id,
            'status' => 10
        ];
        $res = $this->with(['user','billUser'])
            ->where($filter)
            ->order(['create_time' => 'desc'])
            ->select();
        foreach ($res as $key => $val) {
            $res[$key]['dif_people'] = $groupbuy_num - $val['actual_people'];
        }
        return $res;
    }
 
    /**
     * 获取用户参与的团购订单
     */
    public function getUserBills($user_id, $groupbuy_product_id = null)
    {
        $model = $this->with(['active', 'user', 'billUser.user'])
            ->where('creator_id', '=', $user_id)
            ->whereOrExists(function ($query) use ($user_id) {
                $query->table('groupbuy_bill_user gb_user')
                    ->where('gb_user.groupbuy_bill_id', '=', 'groupbuy_bill.groupbuy_bill_id')
                    ->where('gb_user.user_id', '=', $user_id);
            });
 
        if ($groupbuy_product_id) {
            $model = $model->where('groupbuy_product_id', '=', $groupbuy_product_id);
        }
 
        return $model->order(['create_time' => 'desc'])->select();
    }
}