isEmpty()){ throw new \Exception('商品不存在'); } $goodSpecInventoryModel = ShopGoodSpecsInventory::where(['id'=>$params['goods_specs_inventory_id'],'shop_goods_id'=>$params['shop_goods_id']])->findOrEmpty(); if($goodSpecInventoryModel->isEmpty()){ throw new \Exception('规格不存在'); } $model = ShopCart::where(['shop_goods_id'=>$params['shop_goods_id'],'goods_specs_inventory_id'=>$params['goods_specs_inventory_id'],'worker_id'=>$params['worker_id']])->findOrEmpty(); if(!$model->isEmpty()){ if($goodSpecInventoryModel->remaining_inventory < $params['number'] + $model->number){ throw new \Exception('库存不足'); } $model->number = $model->number + $params['number']; $model->save(); Db::commit(); return true; } ShopCart::create([ 'shop_goods_id' => $params['shop_goods_id'], 'goods_specs_inventory_id' => $params['goods_specs_inventory_id'], 'number' => $params['number'], 'worker_id' => $params['worker_id'], ]); Db::commit(); return true; }catch(\Exception $e){ Db::rollback(); self::setError($e->getMessage()); return false; } } public static function delete($params) { Db::startTrans(); try{ ShopCart::whereIn('id',$params['ids'])->where('worker_id',$params['worker_id'])->delete(); Db::commit(); return true; }catch (\Exception $e){ Db::rollback(); self::setError($e->getMessage()); return false; } } public static function edit($params) { Db::startTrans(); try{ $cartModel = ShopCart::findOrEmpty($params['id']); if($cartModel->isEmpty()){ throw new \Exception('数据不存在'); } if($cartModel->worker_id != $params['worker_id']){ throw new \Exception('非法操作'); } $goodSpecInventoryModel = ShopGoodSpecsInventory::findOrEmpty($cartModel->goods_specs_inventory_id); if($goodSpecInventoryModel->isEmpty()){ throw new \Exception('规格不存在'); } if($params['number'] > $goodSpecInventoryModel->remaining_inventory){ throw new \Exception('库存不足'); } $cartModel->number = $params['number']; $cartModel->save(); Db::commit(); return true; }catch (\Exception $e){ Db::rollback(); self::setError($e->getMessage()); return false; } } }