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
<?php
 
namespace app\common\model\plus\groupbuy;
 
use app\common\model\BaseModel;
use app\common\model\order\Order as OrderModel;
use app\common\model\plus\groupbuy\BillUser as BillUserModel;
 
/**
 * 团购订单模型
 */
class Bill extends BaseModel
{
    protected $name = 'groupbuy_bill';
    protected $pk = 'groupbuy_bill_id';
 
    /**
     * 获取团购订单详情
     */
    public static function detail($groupbuy_bill_id, $with = [])
    {
        return (new static())->with($with)->where('groupbuy_bill_id', '=', $groupbuy_bill_id)->find();
    }
 
    /**
     * 关联团购活动表
     */
    public function active()
    {
        return $this->belongsTo('app\\common\\model\\plus\\groupbuy\\Active', 'groupbuy_active_id', 'groupbuy_active_id');
    }
 
    /**
     * 关联创建者
     */
    public function user()
    {
        return $this->belongsTo('app\\common\\model\\user\\User', 'creator_id', 'user_id')
            ->field(['user_id', 'nickName', 'avatarUrl']);
    }
 
    /**
     * 关联团购成员表
     */
    public function billUser()
    {
        return $this->hasMany('app\\common\\model\\plus\\groupbuy\\BillUser', 'groupbuy_bill_id', 'groupbuy_bill_id')
            ->field(['user_id','groupbuy_bill_id'])
            ->order(['create_time' => 'asc']);
    }
 
    /**
     * 创建团购订单
     */
    public function createBill($data)
    {
        $this->allowField(true)->save($data);
        return $this['groupbuy_bill_id'];
    }
 
    /**
     * 更新团购订单状态
     */
    public function updateBillStatus($status)
    {
        return $this->save(['status' => $status]);
    }
 
    /**
     * 更新团购人数
     */
    public function updatePeopleCount($increment = 1)
    {
        return $this->where('groupbuy_bill_id', '=', $this['groupbuy_bill_id'])
            ->inc('actual_people', $increment)
            ->update();
    }
 
    /**
     * 处理团购订单
     */
    public function processOrder($product, $sku)
    {
        // 更新团购人数
        $this->where('groupbuy_bill_id', '=', $this['groupbuy_bill_id'])
            ->inc('actual_people', 1)
            ->update();
 
        // 插入团购记录表
        $bill_user_model = new BillUser();
        $bill_user_model->save([
            'groupbuy_bill_id' => $this['groupbuy_bill_id'],
            'order_id' => $product['order_id'],
            'user_id' => $product['user_id'],
            'is_creator' => 0,
            'app_id' => $product['app_id'],
        ]);
 
        // 获取团购商品信息
        $groupbuy_product = Product::detail($sku['groupbuy_product_id']);
 
        // 判断团购是否成功
        if($this['actual_people'] + 1 >= $groupbuy_product['groupbuy_num']){
            $this->save([
                'status' => 20
            ]);
 
            $order_list = (new BillUserModel)
                ->field(['order_id'])
                ->where('groupbuy_bill_id', '=', $this['groupbuy_bill_id'])
                ->select();
            $orderIds = \app\common\library\helper::getArrayColumn($order_list, 'order_id');
 
            // 更新主订单表团购状态
            (new OrderModel)->where('order_id', 'in', $orderIds)
                ->save([
                    'groupbuy_status' => 20
                ]);
        }
    }
}