BrandService.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace addons\QiDingDong\common\service;
  3. use addons\QiDingDong\common\enums\QiDingDongEnums;
  4. use addons\QiDingDong\common\expand\sdk\qidingdong\src\model\BrandModel;
  5. use addons\QiDingDong\common\expand\sdk\qidingdong\src\request\BrandRequest;
  6. use addons\QiDingDong\common\helper\RequestHelper;
  7. use addons\QiDingDong\common\models\QiDingDongBrand;
  8. use common\enums\RabbitMqEnum;
  9. use common\enums\StatusEnum;
  10. use common\helpers\FormatHelper;
  11. class BrandService
  12. {
  13. public function find(int $mall_id)
  14. {
  15. try {
  16. \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_ORDER, RabbitMqEnum::ORDER_QUEUE,
  17. ['mall_id' => $mall_id], self::class, 'async'); } catch (\Exception $e) {
  18. return $e->getMessage();
  19. }
  20. return true;
  21. }
  22. /**
  23. * @param array $params
  24. * @return bool
  25. */
  26. public function async(array $params)
  27. {
  28. try {
  29. $mall_id = $params['mall_id'];
  30. $page = 1;
  31. while (true) {
  32. $resp = $this->req($mall_id, $page);
  33. if (!empty($resp['list']) && is_array($resp)) {
  34. $brand_ids = array_column($resp['list'], 'id');
  35. $local_ids = QiDingDongBrand::find()
  36. ->where(['mall_id' => $mall_id])
  37. ->andWhere(['brand_id' => $brand_ids])
  38. ->andWhere(['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])
  39. ->select('brand_id')
  40. ->column();
  41. $total = count($resp['list']);
  42. foreach ($resp['list'] as $key => &$item) {
  43. if (in_array($item['id'], $local_ids)) {
  44. unset($resp['list'][$key]);
  45. continue;
  46. }
  47. $item['created_at'] = time();
  48. $item['updated_at'] = time();
  49. $item['mall_id'] = $mall_id;
  50. $item['brand_id'] = $item['id'];
  51. unset($item['id']);
  52. }
  53. if (!empty($resp['list'])) {
  54. QiDingDongBrand::find()->createCommand()->batchInsert(QiDingDongBrand::tableName(), array_keys($resp['list'][0]), $resp['list'])->execute();
  55. }
  56. if ($total == 100) {
  57. $page++;
  58. } else {
  59. break;
  60. }
  61. } else {
  62. break;
  63. }
  64. }
  65. } catch (\Exception $e) {
  66. \Yii::error(FormatHelper::exception($e, __METHOD__));
  67. return false;
  68. }
  69. return true;
  70. }
  71. /**
  72. * @param int $mall_id
  73. * @param int $page
  74. * @return array|int|string
  75. * @throws \Exception
  76. */
  77. public function req(int $mall_id, int $page)
  78. {
  79. $contentModel = RequestHelper::contentModelInstance($mall_id);
  80. $contentModel->content->data = new BrandModel();
  81. $contentModel->content->data->device = QiDingDongEnums::REQUEST_PARAMS_DEVICE; // 固定值
  82. $contentModel->content->data->page = $page; // 固定值
  83. $contentModel->content->data->page_size = 100; // 固定值
  84. return RequestHelper::send(new BrandRequest(), RequestHelper::enCrypto($contentModel, $mall_id), $mall_id); }
  85. }