BrandService.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace addons\QiShangTong\common\service;
  3. use addons\QiShangTong\common\expand\sdk\qishangtong\src\model\BrandModel;
  4. use addons\QiShangTong\common\expand\sdk\qishangtong\src\request\BrandRequest;
  5. use addons\QiShangTong\common\helper\RequestHelper;
  6. use addons\QiShangTong\common\models\QiShangTongBrand;
  7. use common\enums\RabbitMqEnum;
  8. use common\enums\StatusEnum;
  9. use common\helpers\FormatHelper;
  10. class BrandService
  11. {
  12. public function find(int $mall_id, int $page = 1)
  13. {
  14. try {
  15. \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_ORDER,
  16. RabbitMqEnum::ORDER_QUEUE, ['mall_id' => $mall_id], self::class, 'async');
  17. } catch (\Exception $e) {
  18. return $e->getMessage();
  19. }
  20. return true;
  21. }
  22. /**
  23. * 同步
  24. * @param array $params
  25. * @return bool
  26. */
  27. public function async(array $params)
  28. {
  29. try {
  30. $mall_id = $params['mall_id'];
  31. $resp = RequestHelper::send(new BrandRequest(), new BrandModel(), $mall_id);
  32. if (!empty($resp) && is_array($resp)) {
  33. $brand_ids = array_column($resp, 'id');
  34. $local_ids = QiShangTongBrand::find()
  35. ->where(['mall_id' => $mall_id])
  36. ->andWhere(['brand_id' => $brand_ids])
  37. ->andWhere(['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])
  38. ->select('brand_id')
  39. ->column();
  40. foreach ($resp as $key => &$item) {
  41. if (in_array($item['id'], $local_ids)) {
  42. unset($resp[$key]);
  43. continue;
  44. }
  45. $item['created_at'] = time();
  46. $item['updated_at'] = time();
  47. $item['mall_id'] = $mall_id;
  48. $item['brand_id'] = $item['id'];
  49. unset($item['id']);
  50. }
  51. if (!empty($resp)) {
  52. QiShangTongBrand::find()->createCommand()->batchInsert(QiShangTongBrand::tableName(), array_keys($resp[0]), $resp)->execute();
  53. }
  54. }
  55. } catch (\Exception $e) {
  56. \Yii::error(FormatHelper::exception($e, __METHOD__));
  57. return false;
  58. }
  59. return true;
  60. }
  61. }