| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- <?php
- namespace addons\QiShangTong\common\service;
- use addons\QiShangTong\common\expand\sdk\qishangtong\src\model\BrandModel;
- use addons\QiShangTong\common\expand\sdk\qishangtong\src\request\BrandRequest;
- use addons\QiShangTong\common\helper\RequestHelper;
- use addons\QiShangTong\common\models\QiShangTongBrand;
- use common\enums\RabbitMqEnum;
- use common\enums\StatusEnum;
- use common\helpers\FormatHelper;
- class BrandService
- {
- public function find(int $mall_id, int $page = 1)
- {
- try {
- \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_ORDER,
- RabbitMqEnum::ORDER_QUEUE, ['mall_id' => $mall_id], self::class, 'async');
- } catch (\Exception $e) {
- return $e->getMessage();
- }
- return true;
- }
- /**
- * 同步
- * @param array $params
- * @return bool
- */
- public function async(array $params)
- {
- try {
- $mall_id = $params['mall_id'];
- $resp = RequestHelper::send(new BrandRequest(), new BrandModel(), $mall_id);
- if (!empty($resp) && is_array($resp)) {
- $brand_ids = array_column($resp, 'id');
- $local_ids = QiShangTongBrand::find()
- ->where(['mall_id' => $mall_id])
- ->andWhere(['brand_id' => $brand_ids])
- ->andWhere(['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])
- ->select('brand_id')
- ->column();
- foreach ($resp as $key => &$item) {
- if (in_array($item['id'], $local_ids)) {
- unset($resp[$key]);
- continue;
- }
- $item['created_at'] = time();
- $item['updated_at'] = time();
- $item['mall_id'] = $mall_id;
- $item['brand_id'] = $item['id'];
- unset($item['id']);
- }
- if (!empty($resp)) {
- QiShangTongBrand::find()->createCommand()->batchInsert(QiShangTongBrand::tableName(), array_keys($resp[0]), $resp)->execute();
- }
- }
- } catch (\Exception $e) {
- \Yii::error(FormatHelper::exception($e, __METHOD__));
- return false;
- }
- return true;
- }
- }
|