| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- <?php
- namespace addons\QiDingDong\common\service;
- use addons\QiDingDong\common\enums\QiDingDongEnums;
- use addons\QiDingDong\common\expand\sdk\qidingdong\src\model\GoodsCategoryModel;
- use addons\QiDingDong\common\expand\sdk\qidingdong\src\request\GoodsCategoryRequest;
- use addons\QiDingDong\common\helper\RequestHelper;
- use addons\QiDingDong\common\models\QiDingDongCate;
- use common\enums\RabbitMqEnum;
- use common\enums\StatusEnum;
- use common\helpers\FormatHelper;
- use common\models\goods\GoodsCate;
- class CategoryService
- {
- /**
- * @param int $mall_id
- * @param int $parent_id
- * @return string|true
- */
- public function find(int $mall_id, int $parent_id = 0)
- {
- try {
- \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_ORDER, RabbitMqEnum::ORDER_QUEUE,
- [
- 'mall_id' => $mall_id,
- 'parent_id' => $parent_id
- ],
- self::class, 'async');
- // $this->async(['mall_id' => $mall_id, 'parent_id' => $parent_id]);
- } catch (\Exception $e) {
- return $e->getMessage();
- }
- return true;
- }
- /**
- * @param array $params
- * @return bool
- */
- public function async(array $params)
- {
- try {
- $mall_id = $params['mall_id'];
- $parent_id = $params['parent_id'];
- $resp = $this->req($mall_id, $parent_id);
- if (is_array($resp) && !empty($resp)) {
- $cat_ids = array_column($resp, 'cate_id');
- $local_list = QiDingDongCate::lists([
- 'where' => [
- ['third_cate_id' => $cat_ids],
- ['mall_id' => $mall_id],
- ['status' => [StatusEnum::DISABLED, StatusEnum::ENABLED]]
- ],
- 'select'=> 'third_cate_id',
- 'index' => 'third_cate_id'
- ]);
- $inserts = [];
- foreach ($resp as $v) {
- if (!empty($local_list[$v['cate_id']])) {
- continue;
- }
- $v['mall_id'] = $mall_id;
- $v['created_at'] = time();
- $v['updated_at'] = time();
- $v['third_cate_id'] = $v['cate_id'];
- $v['cate_id'] = 0;
- $inserts[] = $v;
- }
- if (!empty($inserts)) QiDingDongCate::find()->createCommand()->batchInsert(QiDingDongCate::tableName(), array_keys($inserts[0]), $inserts)->execute();
- $this->writeCateTable($mall_id, $cat_ids);
- foreach ($resp as $val) {
- \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_ORDER, RabbitMqEnum::ORDER_QUEUE,
- [
- 'mall_id' => $mall_id,
- 'parent_id' => $val['cate_id']
- ],
- self::class, 'async');
- // $this->async([
- // 'mall_id' => $mall_id,
- // 'parent_id' => $val['cate_id']
- // ]);
- }
- }
- } catch (\Exception $e) {
- \Yii::error(FormatHelper::exception($e, __METHOD__));
- return false;
- }
- return true;
- }
- /**
- * @param int $mall_id
- * @param array $third_cat_ids
- * @return void
- * @throws \Exception
- */
- protected function writeCateTable(int $mall_id, array $third_cat_ids)
- {
- $l_cats = QiDingDongCate::lists([
- 'where' => [
- ['mall_id' => $mall_id],
- ['third_cate_id' => $third_cat_ids],
- ['cate_id' => StatusEnum::DISABLED]
- ]
- ], false);
- if (!empty($l_cats)) {
- /**
- * @var $cat QiDingDongCate
- */
- foreach ($l_cats as $cat) {
- if (!empty($cat->parent_id)) {
- $parentCat = QiDingDongCate::findOne(['mall_id' => $mall_id, 'third_cate_id' => $cat->parent_id]);
- }
- $lCat = GoodsCate::setData([
- 'mall_id' => $mall_id,
- 'mch_id' => 0,
- 'parent_id' => !empty($parentCat) ? $parentCat->cate_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());
- }
- }
- }
- /**
- * @param $mall_id
- * @param $parent_id
- * @return array|int|string
- * @throws \Exception
- */
- public function req($mall_id, $parent_id)
- {
- $contentModel = RequestHelper::contentModelInstance($mall_id);
- $contentModel->content->data = new GoodsCategoryModel();
- $contentModel->content->data->device = QiDingDongEnums::REQUEST_PARAMS_DEVICE; // 固定值
- $contentModel->content->data->parent_id = $parent_id; // 固定值
- return RequestHelper::send(new GoodsCategoryRequest(), RequestHelper::enCrypto($contentModel, $mall_id), $mall_id);
- }
- public static function custom(int $mall_id)
- {
- return QiDingDongCate::lists([
- 'where' => [
- ['mall_id' => $mall_id],
- ['level' => 1],
- ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]],
- ],
- 'select'=> 'cate_id, cate_name, third_cate_id'
- ]);
- }
- public static function firstCate(int $mall_id, int $pid, int $cid)
- {
- if (empty($cid)) return 0;
- $request = new CategoryService();
- try {
- /**
- * @var $cat QiDingDongCate
- */
- $cat = QiDingDongCate::getOne([
- ['mall_id' => $mall_id],
- ['third_cate_id' => $cid]
- ]);
- if (!empty($cat) && !empty($cat->cate_id)) return $cat->cate_id;
- $t = \Yii::$app->db->beginTransaction();
- // 没数据 就需要写入数据
- if (empty($cat)) {
- $resp = $request->req($mall_id, $pid);
- if (!empty($resp)) {
- $r_cat = null;
- foreach ($resp as $item) {
- if ($item['cate_id'] == $cid) {
- $r_cat = $item;
- break;
- }
- }
- if (!empty($r_cat)) {
- $cat = new QiDingDongCate();
- $cat->mall_id = $mall_id;
- $cat->cate_name = $r_cat['cate_name'];
- $cat->level = $r_cat['level'];
- $cat->icon = $r_cat['icon'];
- $cat->select_icon = $r_cat['select_icon'];
- $cat->parent_id = $pid;
- $cat->third_cate_id = $cid;
- if (!$cat->save()) throw new \Exception($cat->getErrorMessage());
- } else {
- $cat = null;
- }
- // if (empty($r_cat)) throw new \Exception("未找到 $pid -> $cid 类目");
- }
- }
- if (!empty($cat) && empty($cat->cate_id)) {
- $lCat = GoodsCate::setData([
- 'mall_id' => $mall_id,
- 'mch_id' => 0,
- 'parent_id' => !empty($cat->parent_id) ? intval(QiDingDongCate::find()->where(['mall_id' => $mall_id])
- ->andWhere(['third_cate_id' => $cat->parent_id])->select('cate_id')->scalar()) : 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());
- }
- $t->commit();
- return $cat->cate_id ?? 0;
- } catch (\Exception $e) {
- if (!empty($t)) $t->rollBack();
- return $e->getMessage();
- }
- }
- }
|