'desc']; if (!empty($param['order'])) { $order = [$param['order_field'] => $param['order']]; } return $this->with(['product', 'groupbuySku']) ->alias('gp') ->join('product p', 'p.product_id = gp.product_id') ->where($filter) ->order($order) ->paginate([ 'list_rows' => $param['list_rows'] ?? 15, 'query' => $param ]); } /** * 添加团购商品 */ public function add($data) { $this->startTrans(); try { // 检查商品是否已存在该活动中 $exist = $this->where([ 'product_id' => $data['product_id'], 'groupbuy_active_id' => $data['groupbuy_active_id'] ])->find(); if ($exist) { $this->error = '该商品已在当前活动中'; return false; } // 保存团购商品基本信息 $result = $this->save($data); if ($result !== false) { // 处理团购SKU if (!empty($data['sku_list'])) { $this->handleGroupbuySku($data['sku_list']); } } $this->commit(); return $result !== false; } catch (\Exception $e) { $this->rollback(); $this->error = $e->getMessage(); return false; } } /** * 编辑团购商品 */ public function edit($data) { $this->startTrans(); try { $result = $this->save($data); if ($result !== false && !empty($data['sku_list'])) { // 更新团购SKU $this->handleGroupbuySku($data['sku_list']); } $this->commit(); return $result !== false; } catch (\Exception $e) { $this->rollback(); $this->error = $e->getMessage(); return false; } } /** * 处理团购SKU数据 */ private function handleGroupbuySku($skuList) { $skuModel = new \app\admin\model\plus\groupbuy\ProductSku(); // 先删除原有SKU $skuModel->where('groupbuy_product_id', $this->groupbuy_product_id)->delete(); // 添加新的团购SKU foreach ($skuList as $sku) { $sku['groupbuy_product_id'] = $this->groupbuy_product_id; $skuModel->create($sku); } } /** * 删除团购商品 */ public function remove() { $this->startTrans(); try { // 删除团购SKU $skuModel = new \app\admin\model\plus\groupbuy\ProductSku(); $skuModel->where('groupbuy_product_id', $this->groupbuy_product_id)->delete(); // 删除团购订单用户记录 $billUserModel = new \app\admin\model\plus\groupbuy\BillUser(); $billUserModel->where('groupbuy_product_id', $this->groupbuy_product_id)->delete(); // 删除团购订单 $billModel = new \app\admin\model\plus\groupbuy\Bill(); $billModel->where('groupbuy_product_id', $this->groupbuy_product_id)->delete(); // 最后删除团购商品 $result = $this->delete(); $this->commit(); return $result !== false; } catch (\Exception $e) { $this->rollback(); $this->error = $e->getMessage(); return false; } } }