StorageService.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. <?php
  2. namespace addons\QiJuhe\common\service;
  3. use addons\QiJuhe\common\api\Api;
  4. use addons\QiJuhe\common\models\GoodsModel;
  5. use addons\QiJuhe\common\models\StorageModel;
  6. use common\enums\RabbitMqEnum;
  7. use common\models\goods\Goods;
  8. use Yii;
  9. use yii\data\Pagination;
  10. use yii\helpers\Json;
  11. class StorageService extends BaseService
  12. {
  13. public $mall_id;
  14. public $supply_id;
  15. public function setMallId(int $mall_id)
  16. {
  17. $this->mall_id = $mall_id;
  18. }
  19. /**
  20. * 添加
  21. *
  22. * @param array $goods_ids
  23. * @return array
  24. */
  25. public function add(array $goods_ids)
  26. {
  27. StorageModel::updateAll(['is_delete' => 1], ['and', ['mall_id' => $this->mall_id], ['supply_id' => $this->supply_id], ['is_delete' => 0], ['middle_goods_id' => $goods_ids]]);
  28. // $db_goods = StorageModel::find()
  29. // ->select('middle_goods_id')
  30. // ->where(['mall_id' => $this->mall_id, 'supply_id' => $this->supply_id, 'is_delete' => 0])
  31. // ->andWhere(['in', 'middle_goods_id', $goods_ids])
  32. // ->asArray()
  33. // ->all();
  34. // // 新增的看是否在数据库中存在,存在的过滤掉
  35. // if (!empty($db_goods)) {
  36. // $db_goods_id = array_column($db_goods, 'middle_goods_id');
  37. // $goods_ids = array_diff($goods_ids, $db_goods_id);
  38. // }
  39. //
  40. // if (empty($goods_ids)) {
  41. // return $this->error('goods_id列表为空');
  42. // }
  43. $batch_log = [];
  44. $batch_no = uniqid();
  45. foreach ($goods_ids as $item) {
  46. $data = [
  47. 'middle_goods_id' => $item,
  48. 'mall_id' => $this->mall_id,
  49. 'supply_id' => $this->supply_id,
  50. 'updated_at' => time(),
  51. 'created_at' => time(),
  52. 'batch_no' => $batch_no,
  53. ];
  54. $batch_log[] = $data;
  55. }
  56. if (empty($batch_log)) {
  57. return $this->error('没有可添加的商品');
  58. }
  59. // 中台加入选品库
  60. $api = ApiService::getConnect($this->mall_id, $this->supply_id);
  61. $res = $api->addStorage($goods_ids);
  62. if ($res === false) {
  63. return $this->error($api->error);
  64. }
  65. // 存库
  66. $result = StorageModel::batchAdd($batch_log);
  67. if (!$result) {
  68. return $this->error('批量添加失败');
  69. }
  70. $storage_id = StorageModel::find()->select('id')
  71. ->where(['mall_id' => $this->mall_id, 'batch_no' => $batch_log])
  72. ->column();
  73. // 导入插件商品库
  74. $goods = new GoodsService(['mall_id' => $this->mall_id, 'supply_id' => $this->supply_id]);
  75. $goods->add($storage_id);
  76. // 导入系统商品库
  77. QiGoodsService::queueAdd($storage_id, $this->mall_id);
  78. return $storage_id;
  79. }
  80. /**
  81. * 列表
  82. *
  83. * @param array $param
  84. * @return array
  85. */
  86. public function lists($param)
  87. {
  88. $page = $param['page'] ?? 1;
  89. $limit = $param['pageSize'] ?? 20;
  90. $model = StorageModel::find()
  91. ->select("g.*,s.is_import,s.*,s.id as storage_id")
  92. ->alias('s')
  93. ->leftJoin(GoodsModel::tableName() . ' g', 's.goods_id = g.id and s.supply_id ');
  94. $model->andFilterWhere(['s.mall_id' => $param['mall_id']]);
  95. $model->andFilterWhere(['s.supply_id' => $param['supply_id'] ?? null]);
  96. isset($param['is_import']) && $param['is_import'] === '0' && $model->andFilterWhere(['s.is_import' => [0, 1]]);
  97. isset($param['is_import']) && $param['is_import'] == 1 && $model->andFilterWhere(['s.is_import' => 2]);
  98. !empty($param['category_1_id']) && $model->andWhere(['like', 'g.category_1', '"id":' . $param['category_1_id'] . ',']);
  99. !empty($param['category_2_id']) && $model->andWhere(['like', 'g.category_2', '"id":' . $param['category_2_id'] . ',']);
  100. !empty($param['category_3_id']) && $model->andWhere(['like', 'g.category_3', '"id":' . $param['category_3_id'] . ',']);
  101. $model->andFilterWhere(['like', 'g.title', $param['search_words'] ?? null]);
  102. if (isset($param['is_display']) && $param['is_display'] > -1) {
  103. $model->andFilterWhere(['=', 'g.is_display', $param['is_display']]);
  104. }
  105. if (isset($param['source_id']) && $param['source_id'] > -1) {
  106. $model->andFilterWhere(['=', 'g.source', $param['source_id']]);
  107. }
  108. $pages = new Pagination(['totalCount' => $model->count(), 'pageSize' => $limit]);
  109. $list = $model->limit($pages->limit)
  110. ->offset($pages->offset)
  111. ->orderBy('s.id desc')
  112. ->asArray()
  113. ->all();
  114. foreach ($list as &$item) {
  115. $item['category_1'] = Json::decode($item["category_1"]);
  116. $item['category_2'] = Json::decode($item["category_2"]);
  117. $item['category_3'] = Json::decode($item["category_3"]);
  118. $item['category_text'] = $item['category_1']['name'] . '/' . $item['category_2']['name'] . '/' . $item['category_3']['name'];
  119. $item['category_text'] = trim($item['category_text'], '/');
  120. // 导入超时
  121. if ($item['is_import'] == 1 && (time() - $item['import_at']) > 120 && !empty($item['import_msg'])) {
  122. $item['is_import'] = 0;
  123. $item['import_msg'] = '导入超时,请重新导入';
  124. }
  125. if ((time() - $item['updated_at']) > 60 && empty($item['goods_id'])) {
  126. // 60秒了,还灭有导入成功重新导入
  127. $goods = new GoodsService(['mall_id' => $item['mall_id'], 'supply_id' => $item['supply_id']]);
  128. $goods->add([$item['id']]);
  129. StorageModel::updateAll(['updated_at' => time()], ['id' => $item['id']]);
  130. }
  131. }
  132. $data['list'] = $list;
  133. $data['page_count'] = $pages->pageCount;
  134. $data['current_page'] = $page + 1;
  135. return $data;
  136. }
  137. /**
  138. * 导入商城
  139. *
  140. * @param array $ids
  141. * @param boolean $again
  142. * @return boolean
  143. */
  144. public function imports(array $ids, $again = false)
  145. {
  146. StorageModel::updateImportStatus($ids);
  147. // 导入系统商品库
  148. QiGoodsService::queueAdd($ids, $this->mall_id);
  149. return true;
  150. }
  151. /**
  152. * 删除
  153. *
  154. * @param array $ids
  155. * @return void
  156. */
  157. public function removes(array $ids, bool $del_goods = true)
  158. {
  159. $data = StorageModel::find()->select('supply_id,middle_goods_id')->where(['id' => $ids])->all();
  160. $supply = [];
  161. foreach ($data as $item) {
  162. $supply[$item['supply_id']][] = intval($item['middle_goods_id']);
  163. }
  164. // 数据库移除
  165. StorageModel::remove($ids, $del_goods);
  166. foreach ($supply as $supply_id => $goods_ids) {
  167. // api接口移除
  168. $api = ApiService::getConnect($this->mall_id, $supply_id);
  169. $api->deleteStorage($goods_ids);
  170. }
  171. return true;
  172. }
  173. public function removesByGoodsIds(array $ids)
  174. {
  175. $ids = StorageModel::find()->where(['mall_goods_id' => $ids])->select('id')->column();
  176. if (!empty($ids)) return $this->removes($ids, false);
  177. return true;
  178. }
  179. /**
  180. * 获取供应链端商品详情
  181. *
  182. * @param integer $id
  183. * @return array|boolean
  184. */
  185. public function getGoodsDetail(int $id)
  186. {
  187. $supply_id = StorageModel::find()->where([
  188. 'mall_id' => $this->mall_id,
  189. 'middle_goods_id' => $id,
  190. ])->select('supply_id')->scalar();
  191. $api = ApiService::getConnect($this->mall_id, $supply_id);
  192. $goods_detail = $api->getGoodsInfo($id);
  193. if (empty($goods_detail['list'])) {
  194. return $this->error($api->error ?? '接口请求商品不存在');
  195. }
  196. return current($goods_detail['list']);
  197. }
  198. /**
  199. * 下架检测
  200. *
  201. * @return void
  202. */
  203. public function checkPull(int $supply_id)
  204. {
  205. $limit = 100;
  206. $goods_ids = StorageModel::getMiddleGoodsIdsBySupplyId($this->mall_id, $supply_id);
  207. $count = ceil(bcdiv(count($goods_ids), $limit, 2));
  208. // 分批执行
  209. for ($i=0; $i < $count; $i++) {
  210. // 商品ID
  211. $ids = array_slice($goods_ids, bcmul($i, $limit), $limit);
  212. Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE, [
  213. 'ids' => $ids,
  214. 'supply_id' => $supply_id,
  215. 'mall_id' => $this->mall_id,
  216. ], StorageService::class, 'handleCheckPull');
  217. }
  218. }
  219. /**
  220. * 执行下架检测
  221. *
  222. * @param array $params
  223. * [
  224. * ids => array,
  225. * supply_id => int,
  226. * mall_id => int,
  227. * ]
  228. * @return void
  229. */
  230. public static function handleCheckPull($params)
  231. {
  232. $ids = $params['ids'];
  233. $mall_id = $params['mall_id'];
  234. $supply_id = $params['supply_id'];
  235. $api = ApiService::getConnect($mall_id, $supply_id);
  236. // id转Int
  237. $ids = array_map(function ($v) {
  238. return (int) $v;
  239. }, $ids);
  240. // 商品详情
  241. $result = $api->getGoodsInfo($ids);
  242. $list = current($result);
  243. $pull_ids = [];
  244. foreach ($list as $detail) {
  245. // 下架
  246. if ($detail['is_display'] == 0) {
  247. $pull_ids[] = $detail['id'];
  248. }
  249. }
  250. // 商城商品下架
  251. static::mallGoodsPull($pull_ids);
  252. // 执行商品下架
  253. GoodsModel::GoodsPullByMiddleIds($pull_ids);
  254. }
  255. /**
  256. * 下架主商城商品
  257. *
  258. * @param array $ids
  259. * @return void
  260. */
  261. public static function mallGoodsPull(array $ids)
  262. {
  263. $mall_goods_ids = StorageModel::getMallGoodIdsByMiddleGoodsIds($ids);
  264. Goods::updateAll(['is_on_sale' => 0], ['id' => $mall_goods_ids]);
  265. }
  266. }