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 ]); } } }