| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- <?php
- namespace addons\QiShangTong\common\service;
- use addons\QiShangTong\common\expand\sdk\qishangtong\src\model\GoodsCategoryModel;
- use addons\QiShangTong\common\expand\sdk\qishangtong\src\request\GoodsCategoryRequest;
- use addons\QiShangTong\common\helper\RequestHelper;
- use addons\QiShangTong\common\models\QiShangTongCate;
- use common\enums\RabbitMqEnum;
- use common\enums\StatusEnum;
- use common\helpers\FormatHelper;
- use common\models\goods\GoodsCate;
- class CategoryService
- {
- /**
- * @param int $mall_id
- * @return string|true
- */
- public function find(int $mall_id)
- {
- try {
- \Yii::$app->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;
- }
- }
|