RegionService.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace addons\StarChain\common\services;
  3. use addons\StarChain\common\listeners\RegionImportListener;
  4. use addons\StarChain\common\models\Region;
  5. use common\enums\RabbitMqEnum;
  6. use Yii;
  7. /**
  8. * 地址服务
  9. */
  10. class RegionService extends BaseService
  11. {
  12. /**
  13. * 导入地址
  14. *
  15. * @param string $region_code
  16. * @return boolean
  17. */
  18. public function import($region_code = '')
  19. {
  20. Yii::$app->services->rabbitMq->push(
  21. RabbitMqEnum::EXCHANGE_ORDER,
  22. RabbitMqEnum::TASK_QUEUE,
  23. [
  24. 'mall_id' => $this->mall_id,
  25. 'region_code' => $region_code,
  26. ],
  27. RegionImportListener::class,
  28. 'async_handle'
  29. );
  30. return true;
  31. }
  32. /**
  33. * 任务执行导入
  34. *
  35. * @param string $region_code
  36. * @return boolean
  37. */
  38. public function taskImport($region_code = '')
  39. {
  40. $list = $this->api->getRegionByCodeOpen($region_code);
  41. if (empty($list)) {
  42. return true;
  43. }
  44. $list = array_map(function ($v) {
  45. $arr['region_id'] = $v['id'];
  46. $arr['name'] = $v['name'];
  47. $arr['region_code'] = $v['regionCode'];
  48. $arr['parent_code'] = $v['parentCode'];
  49. $arr['parent_name'] = $v['parentName'];
  50. $arr['region_full_name'] = $v['regionFullName'];
  51. $arr['region_full_codes'] = $v['regionFullCodes'];
  52. $arr['longitude'] = $v['longitude'];
  53. $arr['latitude'] = $v['latitude'];
  54. $arr['initial'] = $v['initial'];
  55. $arr['region_level'] = $v['regionLevel'];
  56. return $arr;
  57. }, $list);
  58. foreach ($list as $v) {
  59. $this->import($v['region_code']);
  60. }
  61. Region::batchInsert($list, false);
  62. }
  63. }