api->getSpuIdList($params); // ids $spu_ids = $data['spuIdList']; // 获取详情 $list_detail = $spu_ids ? $this->api->getSpuBySpuIds($spu_ids) : []; // sku详情 $sku_list_detail = $spu_ids ? $this->api->listSkuBySpuIds($spu_ids) : []; } catch (Exception $e) { return $this->error($e->getMessage()); } $list = $this->formatData($list_detail, $sku_list_detail); $page_count = $data['pages']; $current_page = $data['pageIndex']; return compact('list', 'page_count', 'current_page'); } /** * 格式化数据 * * @param array $list_detail * @param array $sku_list_detail * @return array */ protected function formatData($list_detail, $sku_list_detail) { $spu_ids = Storage::find()->where(['mall_id' => $this->mall_id, 'is_delete' => 0])->select('spu_id')->asArray()->column(); foreach ($list_detail as $k => $v) { foreach ($sku_list_detail as $item) { if ($v['spuId'] == $item['spuId']) { $list_detail[$k]['skuAttr'] = $item['data']; $list_detail[$k]['is_select'] = $list_detail[$k]['storage'] = in_array($v['spuId'], $spu_ids) ? true : false; } } } return $list_detail; } /** * 商品导入 * * @param array $ids * @return boolean */ public function import($ids = []) { if (empty($ids)) { return $this->error('参数不能为空'); } // 商品列表 try { $spu_list = $this->api->getSpuBySpuIds($ids); } catch (Exception $e) { return $this->error($e->getMessage()); } $t = Yii::$app->db->beginTransaction(); try { // 添加到商品 Goods::saveList($spu_list); // 添加到导入 Storage::saveList($this->mall_id, $spu_list); $t->commit(); } catch (Exception $e) { $t->rollback(); return $this->error($e->getMessage()); } // 队列执行添加 Yii::$app->services->rabbitMq->push( RabbitMqEnum::EXCHANGE_ORDER, RabbitMqEnum::TASK_QUEUE, [ 'mall_id' => $this->mall_id, ], GoodsImportListener::class, 'toImport' ); return true; } /** * 任务导入商品 * * @return boolean */ public function taskToImport() { // 未导入 $list = Storage::find()->where(['mall_id' => $this->mall_id, 'import' => 0])->select('id')->all(); $this->addTaskDoImport(array_column($list, 'id')); // 修改为导入中 Storage::updateAll(['import' => 1], ['mall_id' => $this->mall_id, 'id' => array_column($list, 'id')]); } /** * 添加任务执行 * * @param array $ids * @return void|integer */ public function addTaskDoImport($ids, $task = true) { if ($task === true) { foreach ($ids as $id) { // 队列执行添加 Yii::$app->services->rabbitMq->push( RabbitMqEnum::EXCHANGE_ORDER, RabbitMqEnum::TASK_QUEUE, [ 'mall_id' => $this->mall_id, 'id' => $id, ], GoodsImportListener::class, 'doImport' ); } } if ($task === false) { $count = 0; foreach ($ids as $id) { $this->taskDoImport($id) && $count++; } return $count; } } /** * 执行商品添加 * * @param string $id * @return boolean */ public function taskDoImport($id) { $storage = Storage::find()->where(['mall_id' => $this->mall_id, 'id' => $id])->one(); if (empty($storage)) return $this->error('选品不存在'); try { $spu_list = $this->api->getSpuBySpuIds([$storage->spu_id]); } catch (Exception $e) { return $this->error($e->getMessage()); } // 添加到商品 Goods::saveList($spu_list); $t = Yii::$app->db->beginTransaction(); try { $service = new GoodsService(['mall_id' => $this->mall_id]); $goods = $service->addGoods($storage->spu_id); $storage->mall_goods_id = $goods->id; $storage->import = 2; $storage->save(); $t->commit(); } catch (Exception $e) { $t->rollback(); $storage->import = -1; $storage->error = $e->getMessage(); $storage->save(); return $this->error($e->getMessage()); } return true; } /** * 移除选品 * * @param array $ids * @return boolean */ public function removeImport($ids) { $storages = Storage::find()->where(['mall_id' => $this->mall_id, 'id' => $ids])->all(); $t = Yii::$app->db->beginTransaction(); try { $count = 0; foreach ($storages as $storage) { // 移除该选品 $storage->remove() && $count++; } $t->commit(); } catch (Exception $e) { $t->rollback(); return $this->error($e->getMessage()); } return $count; } /** * 通过spu_id移除商品 * * @param array $spu_ids * @return void */ public function removeImportSpuIds($spu_ids) { $storages = Storage::find()->where(['mall_id' => $this->mall_id, 'spu_id' => $spu_ids])->all(); try { foreach ($storages as $storage) { // 移除该选品 $storage->remove(); } } catch (Exception $e) { // 不处理异常 } } }