| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- <?php
- namespace addons\StarChain\common\services;
- use addons\StarChain\common\listeners\GoodsImportListener;
- use addons\StarChain\common\models\Goods;
- use addons\StarChain\common\models\Storage;
- use common\enums\RabbitMqEnum;
- use Exception;
- use Yii;
- use yii\helpers\Json;
- /**
- * 商品库服务
- */
- class SpuService extends BaseService
- {
- /**
- * 获取SkuList
- *
- * @param array $params
- * @return array
- */
- public function getSpuList($params = [])
- {
- list($params['createStartTime'], $params['createEndTime']) = $params['data'];
- unset($params['data']);
- try {
- // 获取
- $data = $this->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) {
- // 不处理异常
- }
- }
- }
|