request->input('stock'); $skuId = $this->request->input('sku_id'); $skuInfo = $this->skuModel->find($skuId); if (empty($skuInfo)) throw new ErrException('该商品不存在'); $spuInfo = $this->spuModel->find($skuInfo->spu_id); if (empty($spuInfo)) throw new ErrException('该商品spu不存在'); if ($stock > $skuInfo->surplus_stock) throw new ErrException('该商品库存不支持参与如此的秒杀库存,请减少至小于剩余库存,剩余库存为'.$skuInfo->surplus_stock); if ($skuInfo->favorable == GoodCode::IS_FAVORABLE) throw new ErrException('该商品已参与秒杀,不支持重复参与秒杀'); if ($skuInfo->is_add_staple_food == GoodCode::IS_ADD_STAPLE_FOOD) throw new ErrException('该商品为 staple food 商品,不支持参与秒杀'); if ($spuInfo->type == GoodCode::SPU_TYPE_MEAL) throw new ErrException('该商品为套餐商品,不支持参与秒杀'); $favorable = $this->spuModel->where('type',GoodCode::SPU_TYPE_OPTIONAL)->where('kitchen_id',$spuInfo->kitchen_id)->where('cycle_id',$spuInfo->cycle_id)->where('favorable',GoodCode::IS_FAVORABLE)->first(); Db::transaction(function () use ($skuId, $stock, $skuInfo, $favorable, $spuInfo) { if (empty($favorable)) { $favorable = new Spu(); $favorable->city_id = $spuInfo->city_id; $favorable->cycle_id = $spuInfo->cycle_id; $favorable->kitchen_id = $spuInfo->kitchen_id; // $insertModel->chef_id = $this->request->input('chef_id'); $favorable->title = '特惠商品'; $favorable->sub_title = $spuInfo->sub_title; $favorable->caterer_id = $spuInfo->caterer_id; $favorable->category_id = $spuInfo->category_id; $favorable->saleable = $spuInfo->saleable; $favorable->type = $spuInfo->type; $favorable->sort = $spuInfo->sort; $favorable->favorable = GoodCode::IS_FAVORABLE; if (!$favorable->save()) throw new ErrException('添加特惠 spu 失败'); } $insertModel = new Sku(); $insertModel->spu_id = $favorable->id; $insertModel->title = $skuInfo->title; $insertModel->sub_title = $skuInfo->sub_title; $insertModel->price = $this->request->input('price'); $insertModel->chef_id = $skuInfo->chef_id; $insertModel->image_ids = $skuInfo->image_ids; $insertModel->param = $skuInfo->param; $insertModel->extra = $skuInfo->extra; $insertModel->total_stock = $stock; $insertModel->surplus_stock = $stock; $insertModel->sales_num = 0; $insertModel->order_num = 0; $insertModel->cancel_num = 0; $insertModel->refund_num = 0; $insertModel->occupied = $skuInfo->occupied; // $insertModel->behind_refund_num = 0; $insertModel->saleable = $skuInfo->saleable; $insertModel->is_add_staple_food = $skuInfo->is_add_staple_food; $insertModel->code_number = $skuInfo->code_number; $insertModel->sort = $skuInfo->sort; $insertModel->origin_sku_id = $skuInfo->id; $insertModel->favorable = GoodCode::IS_FAVORABLE; if (!$insertModel->save()) throw new ErrException('添加sku失败'); $skuInfo->total_stock = $skuInfo->total_stock - $stock; $skuInfo->surplus_stock = $skuInfo->surplus_stock - $stock; if ($skuInfo->total_stock <= 0 || $skuInfo->surplus_stock <= 0) throw new ErrException('库存不足'); if (!$skuInfo->save()) throw new ErrException('修改原 sku 库存失败'); }); //删除缓存 $this->goodCache->delCache($spuInfo->kitchen_id,$spuInfo->cycle_id); return $this->return->success(); } /** * @return array */ public function list(): array { $cycleId = (int)$this->request->input('cycle_id'); $kitchenId = (int)$this->request->input('search_kitchen_id'); $cityId = (int)$this->request->input('search_city_id'); $spuIds = $this->spuModel ->where('cycle_id',$cycleId) ->where('city_id',$cityId) ->where('kitchen_id',$kitchenId) ->where('is_del',GoodCode::SPU_IS_NO_DEL) ->where('type',GoodCode::SPU_TYPE_OPTIONAL) ->where('favorable',GoodCode::NOT_FAVORABLE) // ->orderByDesc('id') ->pluck('id') ->toArray(); $skuList = $this->skuModel ->whereIn('spu_id',$spuIds) ->where('is_del',GoodCode::SKU_IS_NO_DEL) ->orderBy('sort') ->pluck('title','id') ->toArray(); $favorableSpuId = $this->spuModel ->where('cycle_id',$cycleId) ->where('city_id',$cityId) ->where('kitchen_id',$kitchenId) ->where('is_del',GoodCode::SPU_IS_NO_DEL) ->where('type',GoodCode::SPU_TYPE_OPTIONAL) ->where('favorable',GoodCode::NOT_FAVORABLE) ->value('id'); $favorableOriginSkuIds = $this->skuModel ->where('spu_id',$favorableSpuId) ->where('is_del',GoodCode::SKU_IS_NO_DEL) ->pluck('origin_sku_id') ->toArray(); if (empty($skuList)) return $this->return->success('success', ['list' => []]); $res = []; foreach ($skuList as $sku => $title) { $res[] = [ 'id' => $sku, 'title' => $title, 'is_favorable' => (empty($favorableSpuId) || empty($favorableSpuId[$sku]) || !in_array($sku,$favorableOriginSkuIds)) ? GoodCode::NOT_FAVORABLE : GoodCode::IS_FAVORABLE, ]; } return $this->return->success('success', ['list' => $res]); } }