request->input('id'); // 获取数据源 $this->logInfo = $this->orderMealCateringLogModel->find($id); $this->siteInfo = $this->siteModel->find($this->logInfo->site_id); // 检测数据 $this->check(); // 生成 key $this->__initMealRedisKey(); try { Db::beginTransaction(); // 配餐 $this->catering(); // 查询该点所有套餐是否已经配餐 如果是就缓存关闭该点 $this->isCloseMealSite(); Db::commit(); }catch (Exception|ErrException $e) { Db::rollBack(); throw new ErrException($e->getMessage()); } // 加缓存 $this->joinMealCateringCache(); // 关闭线路缓存 if ($this->closeSiteFlag == 1) $this->closeMealWholeLine(); return $this->return->success('success',['num' => $this->resCount]); } /** * @return void * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface */ private function isCloseMealSite(): void { $res = $this->orderMealCateringLogModel ->where('site_id', $this->siteInfo->id) ->where('cycle_id', $this->logInfo->cycle_id) ->where('quantity','>',0) ->count() ?? 0; if ($res > 0) return; // 证明还没完结 配餐 跳出 $this->closeMealSite(); $orderIds = $this->orderModel ->where('site_id', $this->siteInfo->id) ->where('cycle_id', $this->logInfo->cycle_id) ->where('type',OrderCode::ORDER_TYPE_MEAL) ->pluck('order_id') ->toArray(); if (empty($orderIds)) throw new ErrException('配餐失败,更新逻辑失败'); foreach (array_chunk($orderIds, 100) as $chunk) { if (!$this->orderModel->isCateringByOrderIds($chunk) || !$this->orderGoodModel->isCateringByOrderIds($chunk)) throw new ErrException('修改订单数据状态失败'); } $this->closeSiteFlag = 1; } /** * @return void */ private function catering(): void { $this->logInfo->status = CateringCode::CATERING_STATUS_FINISH; if (!$this->logInfo->save()) throw new ErrException('配餐失败1'); } /** * @return void */ private function check(): void { if (empty($this->logInfo)) throw new ErrException('配餐数据不存在'); if ($this->logInfo->quantity <= 0) throw new ErrException('该配餐数量为0,不可配餐'); $this->resCount = $this->logInfo->quantity; $allOrderIds = $this->orderModel ->where('site_id', $this->logInfo->site_id) ->where('cycle_id', $this->cycleId) ->where('type',OrderCode::ORDER_TYPE_MEAL) ->where('status',OrderCode::PAYED) ->pluck('id') ->toArray(); $totalCopies = $this->orderGoodModel ->whereIn('order_id', $allOrderIds) ->where('cycle_id', $this->cycleId) ->where('sku_id', $this->logInfo->sku_id) ->sum('copies') ?? 0; if ($totalCopies != $this->logInfo->quantity) $this->resCount = (int)$totalCopies; } }