| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309 |
- <?php
- namespace addons\QiJuhe\common\service;
- use addons\QiJuhe\common\api\Api;
- use addons\QiJuhe\common\models\GoodsModel;
- use addons\QiJuhe\common\models\StorageModel;
- use common\enums\RabbitMqEnum;
- use common\models\goods\Goods;
- use Yii;
- use yii\data\Pagination;
- use yii\helpers\Json;
- class StorageService extends BaseService
- {
- public $mall_id;
- public $supply_id;
- public function setMallId(int $mall_id)
- {
- $this->mall_id = $mall_id;
- }
- /**
- * 添加
- *
- * @param array $goods_ids
- * @return array
- */
- public function add(array $goods_ids)
- {
- StorageModel::updateAll(['is_delete' => 1], ['and', ['mall_id' => $this->mall_id], ['supply_id' => $this->supply_id], ['is_delete' => 0], ['middle_goods_id' => $goods_ids]]);
- // $db_goods = StorageModel::find()
- // ->select('middle_goods_id')
- // ->where(['mall_id' => $this->mall_id, 'supply_id' => $this->supply_id, 'is_delete' => 0])
- // ->andWhere(['in', 'middle_goods_id', $goods_ids])
- // ->asArray()
- // ->all();
- // // 新增的看是否在数据库中存在,存在的过滤掉
- // if (!empty($db_goods)) {
- // $db_goods_id = array_column($db_goods, 'middle_goods_id');
- // $goods_ids = array_diff($goods_ids, $db_goods_id);
- // }
- //
- // if (empty($goods_ids)) {
- // return $this->error('goods_id列表为空');
- // }
- $batch_log = [];
- $batch_no = uniqid();
- foreach ($goods_ids as $item) {
- $data = [
- 'middle_goods_id' => $item,
- 'mall_id' => $this->mall_id,
- 'supply_id' => $this->supply_id,
- 'updated_at' => time(),
- 'created_at' => time(),
- 'batch_no' => $batch_no,
- ];
- $batch_log[] = $data;
- }
- if (empty($batch_log)) {
- return $this->error('没有可添加的商品');
- }
- // 中台加入选品库
- $api = ApiService::getConnect($this->mall_id, $this->supply_id);
- $res = $api->addStorage($goods_ids);
- if ($res === false) {
- return $this->error($api->error);
- }
- // 存库
- $result = StorageModel::batchAdd($batch_log);
- if (!$result) {
- return $this->error('批量添加失败');
- }
- $storage_id = StorageModel::find()->select('id')
- ->where(['mall_id' => $this->mall_id, 'batch_no' => $batch_log])
- ->column();
- // 导入插件商品库
- $goods = new GoodsService(['mall_id' => $this->mall_id, 'supply_id' => $this->supply_id]);
- $goods->add($storage_id);
- // 导入系统商品库
- QiGoodsService::queueAdd($storage_id, $this->mall_id);
- return $storage_id;
- }
-
- /**
- * 列表
- *
- * @param array $param
- * @return array
- */
- public function lists($param)
- {
- $page = $param['page'] ?? 1;
- $limit = $param['pageSize'] ?? 20;
- $model = StorageModel::find()
- ->select("g.*,s.is_import,s.*,s.id as storage_id")
- ->alias('s')
- ->leftJoin(GoodsModel::tableName() . ' g', 's.goods_id = g.id and s.supply_id ');
- $model->andFilterWhere(['s.mall_id' => $param['mall_id']]);
- $model->andFilterWhere(['s.supply_id' => $param['supply_id'] ?? null]);
- isset($param['is_import']) && $param['is_import'] === '0' && $model->andFilterWhere(['s.is_import' => [0, 1]]);
- isset($param['is_import']) && $param['is_import'] == 1 && $model->andFilterWhere(['s.is_import' => 2]);
- !empty($param['category_1_id']) && $model->andWhere(['like', 'g.category_1', '"id":' . $param['category_1_id'] . ',']);
- !empty($param['category_2_id']) && $model->andWhere(['like', 'g.category_2', '"id":' . $param['category_2_id'] . ',']);
- !empty($param['category_3_id']) && $model->andWhere(['like', 'g.category_3', '"id":' . $param['category_3_id'] . ',']);
- $model->andFilterWhere(['like', 'g.title', $param['search_words'] ?? null]);
- if (isset($param['is_display']) && $param['is_display'] > -1) {
- $model->andFilterWhere(['=', 'g.is_display', $param['is_display']]);
- }
- if (isset($param['source_id']) && $param['source_id'] > -1) {
- $model->andFilterWhere(['=', 'g.source', $param['source_id']]);
- }
- $pages = new Pagination(['totalCount' => $model->count(), 'pageSize' => $limit]);
- $list = $model->limit($pages->limit)
- ->offset($pages->offset)
- ->orderBy('s.id desc')
- ->asArray()
- ->all();
- foreach ($list as &$item) {
- $item['category_1'] = Json::decode($item["category_1"]);
- $item['category_2'] = Json::decode($item["category_2"]);
- $item['category_3'] = Json::decode($item["category_3"]);
- $item['category_text'] = $item['category_1']['name'] . '/' . $item['category_2']['name'] . '/' . $item['category_3']['name'];
- $item['category_text'] = trim($item['category_text'], '/');
- // 导入超时
- if ($item['is_import'] == 1 && (time() - $item['import_at']) > 120 && !empty($item['import_msg'])) {
- $item['is_import'] = 0;
- $item['import_msg'] = '导入超时,请重新导入';
- }
- if ((time() - $item['updated_at']) > 60 && empty($item['goods_id'])) {
- // 60秒了,还灭有导入成功重新导入
- $goods = new GoodsService(['mall_id' => $item['mall_id'], 'supply_id' => $item['supply_id']]);
- $goods->add([$item['id']]);
- StorageModel::updateAll(['updated_at' => time()], ['id' => $item['id']]);
- }
- }
- $data['list'] = $list;
- $data['page_count'] = $pages->pageCount;
- $data['current_page'] = $page + 1;
- return $data;
- }
- /**
- * 导入商城
- *
- * @param array $ids
- * @param boolean $again
- * @return boolean
- */
- public function imports(array $ids, $again = false)
- {
- StorageModel::updateImportStatus($ids);
- // 导入系统商品库
- QiGoodsService::queueAdd($ids, $this->mall_id);
- return true;
- }
- /**
- * 删除
- *
- * @param array $ids
- * @return void
- */
- public function removes(array $ids, bool $del_goods = true)
- {
- $data = StorageModel::find()->select('supply_id,middle_goods_id')->where(['id' => $ids])->all();
- $supply = [];
- foreach ($data as $item) {
- $supply[$item['supply_id']][] = intval($item['middle_goods_id']);
- }
- // 数据库移除
- StorageModel::remove($ids, $del_goods);
- foreach ($supply as $supply_id => $goods_ids) {
- // api接口移除
- $api = ApiService::getConnect($this->mall_id, $supply_id);
- $api->deleteStorage($goods_ids);
- }
- return true;
- }
- public function removesByGoodsIds(array $ids)
- {
- $ids = StorageModel::find()->where(['mall_goods_id' => $ids])->select('id')->column();
- if (!empty($ids)) return $this->removes($ids, false);
- return true;
- }
- /**
- * 获取供应链端商品详情
- *
- * @param integer $id
- * @return array|boolean
- */
- public function getGoodsDetail(int $id)
- {
- $supply_id = StorageModel::find()->where([
- 'mall_id' => $this->mall_id,
- 'middle_goods_id' => $id,
- ])->select('supply_id')->scalar();
- $api = ApiService::getConnect($this->mall_id, $supply_id);
- $goods_detail = $api->getGoodsInfo($id);
- if (empty($goods_detail['list'])) {
- return $this->error($api->error ?? '接口请求商品不存在');
- }
- return current($goods_detail['list']);
- }
- /**
- * 下架检测
- *
- * @return void
- */
- public function checkPull(int $supply_id)
- {
- $limit = 100;
- $goods_ids = StorageModel::getMiddleGoodsIdsBySupplyId($this->mall_id, $supply_id);
- $count = ceil(bcdiv(count($goods_ids), $limit, 2));
- // 分批执行
- for ($i=0; $i < $count; $i++) {
- // 商品ID
- $ids = array_slice($goods_ids, bcmul($i, $limit), $limit);
- Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE, [
- 'ids' => $ids,
- 'supply_id' => $supply_id,
- 'mall_id' => $this->mall_id,
- ], StorageService::class, 'handleCheckPull');
- }
- }
- /**
- * 执行下架检测
- *
- * @param array $params
- * [
- * ids => array,
- * supply_id => int,
- * mall_id => int,
- * ]
- * @return void
- */
- public static function handleCheckPull($params)
- {
- $ids = $params['ids'];
- $mall_id = $params['mall_id'];
- $supply_id = $params['supply_id'];
- $api = ApiService::getConnect($mall_id, $supply_id);
- // id转Int
- $ids = array_map(function ($v) {
- return (int) $v;
- }, $ids);
- // 商品详情
- $result = $api->getGoodsInfo($ids);
- $list = current($result);
- $pull_ids = [];
- foreach ($list as $detail) {
- // 下架
- if ($detail['is_display'] == 0) {
- $pull_ids[] = $detail['id'];
- }
- }
- // 商城商品下架
- static::mallGoodsPull($pull_ids);
-
- // 执行商品下架
- GoodsModel::GoodsPullByMiddleIds($pull_ids);
- }
- /**
- * 下架主商城商品
- *
- * @param array $ids
- * @return void
- */
- public static function mallGoodsPull(array $ids)
- {
- $mall_goods_ids = StorageModel::getMallGoodIdsByMiddleGoodsIds($ids);
- Goods::updateAll(['is_on_sale' => 0], ['id' => $mall_goods_ids]);
- }
- }
|