| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- <?php
- namespace addons\QiDingDong\common\service;
- use addons\QiDingDong\common\enums\QiDingDongEnums;
- use addons\QiDingDong\common\expand\sdk\qidingdong\src\model\BrandModel;
- use addons\QiDingDong\common\expand\sdk\qidingdong\src\request\BrandRequest;
- use addons\QiDingDong\common\helper\RequestHelper;
- use addons\QiDingDong\common\models\QiDingDongBrand;
- use common\enums\RabbitMqEnum;
- use common\enums\StatusEnum;
- use common\helpers\FormatHelper;
- class BrandService
- {
- 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'); } catch (\Exception $e) {
- return $e->getMessage();
- }
- return true;
- }
- /**
- * @param array $params
- * @return bool
- */
- public function async(array $params)
- {
- try {
- $mall_id = $params['mall_id'];
- $page = 1;
- while (true) {
- $resp = $this->req($mall_id, $page);
- if (!empty($resp['list']) && is_array($resp)) {
- $brand_ids = array_column($resp['list'], 'id');
- $local_ids = QiDingDongBrand::find()
- ->where(['mall_id' => $mall_id])
- ->andWhere(['brand_id' => $brand_ids])
- ->andWhere(['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])
- ->select('brand_id')
- ->column();
- $total = count($resp['list']);
- foreach ($resp['list'] as $key => &$item) {
- if (in_array($item['id'], $local_ids)) {
- unset($resp['list'][$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['list'])) {
- QiDingDongBrand::find()->createCommand()->batchInsert(QiDingDongBrand::tableName(), array_keys($resp['list'][0]), $resp['list'])->execute();
- }
- if ($total == 100) {
- $page++;
- } else {
- break;
- }
- } else {
- break;
- }
- }
- } catch (\Exception $e) {
- \Yii::error(FormatHelper::exception($e, __METHOD__));
- return false;
- }
- return true;
- }
- /**
- * @param int $mall_id
- * @param int $page
- * @return array|int|string
- * @throws \Exception
- */
- public function req(int $mall_id, int $page)
- {
- $contentModel = RequestHelper::contentModelInstance($mall_id);
- $contentModel->content->data = new BrandModel();
- $contentModel->content->data->device = QiDingDongEnums::REQUEST_PARAMS_DEVICE; // 固定值
- $contentModel->content->data->page = $page; // 固定值
- $contentModel->content->data->page_size = 100; // 固定值
- return RequestHelper::send(new BrandRequest(), RequestHelper::enCrypto($contentModel, $mall_id), $mall_id); }
- }
|