initTodayCycleId(); if (empty($cycleId)) return $this->return->success('success', ['list' => []]); $this->goodCache->cycleId = (int)$cycleId; $siteInfo = $this->siteCache->getSiteInfo((int)$this->request->input('site_id')); if (empty($siteInfo) || empty($siteInfo['kitchen_id'])) return $this->return->success('success', ['list' => []]); $this->goodCache->kitchenId = (int)$siteInfo['kitchen_id']; $data = $this->goodCache->getOptionalGoodList(); if (empty($data)) return $this->return->success('success', ['list' => []]); $res = $this->buildData($data); return $this->return->success('success', ['list' => $res]); } use OssTrait; /** * @param $data * @return array * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface */ private function buildData($data): array { $categoryIds = $this->categoryModel->whereIn('id',array_column($data,'category_id'))->get(); if (empty($categoryIds)) throw new ErrException('数据错误'); $categoryList = array_column($categoryIds->toArray(),null, 'id'); $categoryImages = $this->getOssObjects(array_keys($categoryList)); $res = []; $stockKey = ApiRedisKey::goodStockKey($this->goodCache->cycleId, $this->goodCache->kitchenId); foreach ($data as &$item) { foreach ($item['sku_list'] as &$v) { $v['surplus_stock'] = $this->redisCache->zScore($stockKey,$v['id']) ?? 0; } if (empty($res[$item['category_id']])) { $res[$item['category_id']] = [ 'category_name' => $categoryList[$item['category_id']]['name'] ?? '', 'category_image' => $categoryImages[$item['category_id']]['url'] ?? '', 'spu_list' => [] ]; } $res[$item['category_id']]['spu_list'][] = $item; } return $res; } }