services->rabbitMq->push(RabbitMqEnum::EXCHANGE_ORDER, RabbitMqEnum::ORDER_QUEUE, ['mall_id' => $mall_id], self::class, 'async'); // $this->async(['mall_id' => $mall_id]); } catch (\Exception $e) { return $e->getMessage(); } return true; } /** * 写入到本地表 * @return bool */ public function writeCateTable() { $t = \Yii::$app->db->beginTransaction(); try { $cats = QiShangTongCate::lists([ 'where' => [ ['parent_id' => StatusEnum::DISABLED], ] ], false); /** * @var $cat QiShangTongCate */ foreach ($cats as $cat) { if (!empty($cat->cate_id)) { $lCat = GoodsCate::findOne(['id' => $cat->cate_id]); } else { $lCat = GoodsCate::setData([ 'mall_id' => $cat->mall_id, 'mch_id' => 0, 'parent_id' => 0, 'name' => $cat->cate_name, 'pic' => $cat->icon ]); if (!$lCat) throw new \Exception(GoodsCate::getStaticError()); $cat->cate_id = $lCat->id; if (!$cat->save()) throw new \Exception($cat->getErrorMessage()); } $cCats = QiShangTongCate::lists([ 'where' => [ ['parent_id' => $cat->third_cate_id], ['cate_id' => StatusEnum::DISABLED], ] ], false); if (!empty($cCats)) { /** * @var $item QiShangTongCate */ foreach ($cCats as $item) { $lCat = GoodsCate::setData([ 'mall_id' => $item->mall_id, 'mch_id' => 0, 'parent_id' => $lCat->id, 'name' => $item->cate_name, 'pic' => $item->icon ]); if (!$lCat) throw new \Exception(GoodsCate::getStaticError()); $item->cate_id = $lCat->id; if (!$item->save()) throw new \Exception($item->getErrorMessage()); } } } $t->commit(); } catch (\Exception $e) { $t->rollBack(); \Yii::error(FormatHelper::exception($e, __METHOD__)); return false; } return true; } /** * @param array $params * @return bool */ public function async(array $params) { try { $mall_id = $params['mall_id']; $resp = RequestHelper::send(new GoodsCategoryRequest(), new GoodsCategoryModel(), $mall_id); if (!empty($resp) && is_array($resp)) { $cats = []; foreach ($resp as $k => $v) { $cate = QiShangTongCate::findOne(['third_cate_id' => $v['cate_id'], 'mall_id' => $mall_id]); if (empty($cate)) { $cats[] = [ 'mall_id' => $mall_id, 'created_at' => time(), 'updated_at' => time(), 'cate_name' => $v['cate_name'], 'level' => $v['level'], 'third_cate_id' => $v['cate_id'], 'parent_id' => $v['parent_id'], ]; } foreach ($v['next'] as $item) { $cate = QiShangTongCate::findOne(['third_cate_id' => $item['cate_id'], 'mall_id' => $mall_id]); if (empty($cate)) { $cats[] = [ 'mall_id' => $mall_id, 'created_at' => time(), 'updated_at' => time(), 'cate_name' => $item['cate_name'], 'level' => $item['level'], 'third_cate_id' => $item['cate_id'], 'parent_id' => $item['parent_id'], ]; } } } if (!empty($cats)) QiShangTongCate::find()->createCommand()->batchInsert(QiShangTongCate::tableName(), array_keys($cats[0]), $cats)->execute(); // 写入到本地类目 $this->writeCateTable(); } } catch (\Exception $e) { \Yii::error(FormatHelper::exception($e, __METHOD__)); return false; } return true; } public static function custom(int $mall_id) { return QiShangTongCate::lists([ 'where' => [ ['mall_id' => $mall_id], ['level' => 1], ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]], ], 'select'=> 'third_cate_id, cate_id, cate_name' ]); } /** * 写入类目 * @param int $mall_id * @param int $c1 * @param int $c2 * @return array|string */ public static function firstCate(int $mall_id, int $c1, int $c2) { try { $ret = []; $resp = RequestHelper::send(new GoodsCategoryRequest(), new GoodsCategoryModel(), $mall_id); $t = \Yii::$app->db->beginTransaction(); if (!empty($resp)) { if (!is_array($resp)) throw new \Exception($resp); foreach ($resp as $item) { if ($c1 == $item['cate_id']) { // 一级 $ret[] = self::cWrite($mall_id, $c1, 0, $item); if (!empty($item['next'])) { foreach ($item['next'] as $value) { // 二级 if ($c2 == $value['cate_id']) { $ret[] = self::cWrite($mall_id, $c2, $ret[0], $value); } } } } } } $t->commit(); return $ret; } catch (\Exception $e) { if (!empty($t)) $t->rollBack(); return $e->getMessage(); } } /** * @param int $mall_id * @param int $cid * @param int $pid * @param array $item * @return int * @throws \Exception */ protected static function cWrite(int $mall_id, int $cid, int $pid, array $item) { /** * @var $cat QiShangTongCate */ $cat = QiShangTongCate::getOne([ ['mall_id' => $mall_id], ['third_cate_id' => $cid], ]); if (empty($cat)) { // 需要写入 $cat = new QiShangTongCate(); $cat->mall_id = $mall_id; $cat->cate_name = $item['cate_name']; $cat->level = $item['level']; $cat->third_cate_id = $item['cate_id']; $cat->parent_id = $item['parent_id']; if (!$cat->save()) throw new \Exception($cat->getErrorMessage()); } if (empty($cat->cate_id)) { // 需要写入 $lCat = GoodsCate::setData([ 'mall_id' => $mall_id, 'mch_id' => 0, 'parent_id' => $pid, 'name' => $cat->cate_name, 'pic' => $cat->icon ?? '' ]); if (!$lCat) throw new \Exception(GoodsCate::getStaticError()); $cat->cate_id = $lCat->id; if (!$cat->save()) throw new \Exception($cat->getErrorMessage()); } return $cat->cate_id; } }