SpuService.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. namespace addons\StarChain\common\services;
  3. use addons\StarChain\common\listeners\GoodsImportListener;
  4. use addons\StarChain\common\models\Goods;
  5. use addons\StarChain\common\models\Storage;
  6. use common\enums\RabbitMqEnum;
  7. use Exception;
  8. use Yii;
  9. use yii\helpers\Json;
  10. /**
  11. * 商品库服务
  12. */
  13. class SpuService extends BaseService
  14. {
  15. /**
  16. * 获取SkuList
  17. *
  18. * @param array $params
  19. * @return array
  20. */
  21. public function getSpuList($params = [])
  22. {
  23. list($params['createStartTime'], $params['createEndTime']) = $params['data'];
  24. unset($params['data']);
  25. try {
  26. // 获取
  27. $data = $this->api->getSpuIdList($params);
  28. // ids
  29. $spu_ids = $data['spuIdList'];
  30. // 获取详情
  31. $list_detail = $spu_ids ? $this->api->getSpuBySpuIds($spu_ids) : [];
  32. // sku详情
  33. $sku_list_detail = $spu_ids ? $this->api->listSkuBySpuIds($spu_ids) : [];
  34. } catch (Exception $e) {
  35. return $this->error($e->getMessage());
  36. }
  37. $list = $this->formatData($list_detail, $sku_list_detail);
  38. $page_count = $data['pages'];
  39. $current_page = $data['pageIndex'];
  40. return compact('list', 'page_count', 'current_page');
  41. }
  42. /**
  43. * 格式化数据
  44. *
  45. * @param array $list_detail
  46. * @param array $sku_list_detail
  47. * @return array
  48. */
  49. protected function formatData($list_detail, $sku_list_detail)
  50. {
  51. $spu_ids = Storage::find()->where(['mall_id' => $this->mall_id, 'is_delete' => 0])->select('spu_id')->asArray()->column();
  52. foreach ($list_detail as $k => $v) {
  53. foreach ($sku_list_detail as $item) {
  54. if ($v['spuId'] == $item['spuId']) {
  55. $list_detail[$k]['skuAttr'] = $item['data'];
  56. $list_detail[$k]['is_select'] = $list_detail[$k]['storage'] = in_array($v['spuId'], $spu_ids) ? true : false;
  57. }
  58. }
  59. }
  60. return $list_detail;
  61. }
  62. /**
  63. * 商品导入
  64. *
  65. * @param array $ids
  66. * @return boolean
  67. */
  68. public function import($ids = [])
  69. {
  70. if (empty($ids)) {
  71. return $this->error('参数不能为空');
  72. }
  73. // 商品列表
  74. try {
  75. $spu_list = $this->api->getSpuBySpuIds($ids);
  76. } catch (Exception $e) {
  77. return $this->error($e->getMessage());
  78. }
  79. $t = Yii::$app->db->beginTransaction();
  80. try {
  81. // 添加到商品
  82. Goods::saveList($spu_list);
  83. // 添加到导入
  84. Storage::saveList($this->mall_id, $spu_list);
  85. $t->commit();
  86. } catch (Exception $e) {
  87. $t->rollback();
  88. return $this->error($e->getMessage());
  89. }
  90. // 队列执行添加
  91. Yii::$app->services->rabbitMq->push(
  92. RabbitMqEnum::EXCHANGE_ORDER,
  93. RabbitMqEnum::TASK_QUEUE,
  94. [
  95. 'mall_id' => $this->mall_id,
  96. ],
  97. GoodsImportListener::class,
  98. 'toImport'
  99. );
  100. return true;
  101. }
  102. /**
  103. * 任务导入商品
  104. *
  105. * @return boolean
  106. */
  107. public function taskToImport()
  108. {
  109. // 未导入
  110. $list = Storage::find()->where(['mall_id' => $this->mall_id, 'import' => 0])->select('id')->all();
  111. $this->addTaskDoImport(array_column($list, 'id'));
  112. // 修改为导入中
  113. Storage::updateAll(['import' => 1], ['mall_id' => $this->mall_id, 'id' => array_column($list, 'id')]);
  114. }
  115. /**
  116. * 添加任务执行
  117. *
  118. * @param array $ids
  119. * @return void|integer
  120. */
  121. public function addTaskDoImport($ids, $task = true)
  122. {
  123. if ($task === true) {
  124. foreach ($ids as $id) {
  125. // 队列执行添加
  126. Yii::$app->services->rabbitMq->push(
  127. RabbitMqEnum::EXCHANGE_ORDER,
  128. RabbitMqEnum::TASK_QUEUE,
  129. [
  130. 'mall_id' => $this->mall_id,
  131. 'id' => $id,
  132. ],
  133. GoodsImportListener::class,
  134. 'doImport'
  135. );
  136. }
  137. }
  138. if ($task === false) {
  139. $count = 0;
  140. foreach ($ids as $id) {
  141. $this->taskDoImport($id) && $count++;
  142. }
  143. return $count;
  144. }
  145. }
  146. /**
  147. * 执行商品添加
  148. *
  149. * @param string $id
  150. * @return boolean
  151. */
  152. public function taskDoImport($id)
  153. {
  154. $storage = Storage::find()->where(['mall_id' => $this->mall_id, 'id' => $id])->one();
  155. if (empty($storage)) return $this->error('选品不存在');
  156. try {
  157. $spu_list = $this->api->getSpuBySpuIds([$storage->spu_id]);
  158. } catch (Exception $e) {
  159. return $this->error($e->getMessage());
  160. }
  161. // 添加到商品
  162. Goods::saveList($spu_list);
  163. $t = Yii::$app->db->beginTransaction();
  164. try {
  165. $service = new GoodsService(['mall_id' => $this->mall_id]);
  166. $goods = $service->addGoods($storage->spu_id);
  167. $storage->mall_goods_id = $goods->id;
  168. $storage->import = 2;
  169. $storage->save();
  170. $t->commit();
  171. } catch (Exception $e) {
  172. $t->rollback();
  173. $storage->import = -1;
  174. $storage->error = $e->getMessage();
  175. $storage->save();
  176. return $this->error($e->getMessage());
  177. }
  178. return true;
  179. }
  180. /**
  181. * 移除选品
  182. *
  183. * @param array $ids
  184. * @return boolean
  185. */
  186. public function removeImport($ids)
  187. {
  188. $storages = Storage::find()->where(['mall_id' => $this->mall_id, 'id' => $ids])->all();
  189. $t = Yii::$app->db->beginTransaction();
  190. try {
  191. $count = 0;
  192. foreach ($storages as $storage) {
  193. // 移除该选品
  194. $storage->remove() && $count++;
  195. }
  196. $t->commit();
  197. } catch (Exception $e) {
  198. $t->rollback();
  199. return $this->error($e->getMessage());
  200. }
  201. return $count;
  202. }
  203. /**
  204. * 通过spu_id移除商品
  205. *
  206. * @param array $spu_ids
  207. * @return void
  208. */
  209. public function removeImportSpuIds($spu_ids)
  210. {
  211. $storages = Storage::find()->where(['mall_id' => $this->mall_id, 'spu_id' => $spu_ids])->all();
  212. try {
  213. foreach ($storages as $storage) {
  214. // 移除该选品
  215. $storage->remove();
  216. }
  217. } catch (Exception $e) {
  218. // 不处理异常
  219. }
  220. }
  221. }