get(Redis::class); $inventoryKey = 'goods_inventory_sold_1_'.$goods->id; // 拼接activity_type和goods_id if (empty($goods)) { return ErrorCode::GOODS_NOT_EXISTS; } // 商户歇业 if($goods->store->is_rest == 1){ return ErrorCode::STORE_REST; } // 商品下架或已删除 if($goods->on_sale == 0 || !is_null($goods->deleted_at)){ return ErrorCode::GOODS_ON_SALE_NO; } // 商品库存不足 // 获取冻结的库存 //$inventoryFrozen = (int)$redis->get($inventoryKey); $inventoryFrozen = 0; if($goods->is_infinite != 1 && $goods->inventory < ($num+$inventoryFrozen)){ return ErrorCode::GOODS_INVENTORY_ERROR; } // 是否超过限购数量 if ($goods->restrict_num != 0 && $goods->restrict_num < $num) { return ErrorCode::GOODS_RESTRICT_LIMIT; } return true; } public function undo() { // TODO: Implement undo() method. } public function getBanner($goodsId) { $banner = GoodsBanner::query()->where('goods_id',$goodsId)->orderByDesc('type')->get(); return $banner; } public function detail($goodsId) { $res = Goods::query()->with('store')->where('id',$goodsId)->first(); return $res; } public function getByType($storeId,$typeId) { return Goods::query()->withoutGlobalScope('normal')->where(['store_id' => $storeId,'category_id' => $typeId])->orderByDesc('on_sale')->orderByDesc('created_at')->get()->toArray(); } public function update($params) { $goods = Goods::query()->withoutGlobalScope('normal')->find($params['id']); if (empty($goods)) { throw new ErrorCodeException( ErrorCode::GOODS_NOT_EXISTS); } foreach ($params as $k => $v){ if(isset($goods->$k)){ $goods->$k = $v; } } return $goods->save(); } public function info($goodsId) { $res = Goods::query()->withoutGlobalScope('normal')->where('id',$goodsId)->select('*','name as goods_name')->first(); return $res; } }