SyncPoi.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace app\queue\redis\slow;
  3. use app\extra\life\data\Store;
  4. use app\model\hx\HxAgent;
  5. use app\model\hx\HxStorePoi;
  6. use app\model\system\SystemConfig;
  7. use Webman\RedisQueue\Consumer;
  8. class SyncPoi implements Consumer
  9. {
  10. /**
  11. * 消费队列名
  12. * 同步POI门店
  13. * @var string
  14. */
  15. public string $queue = "poi-sync";
  16. /**
  17. * 连接配置
  18. * @var string
  19. */
  20. public string $connection = "default";
  21. public function consume($data): bool
  22. {
  23. try {
  24. if (empty($data['account'])) return true;
  25. $hasMore = true;
  26. $config = (new HxAgent)->where("agent_id",$data['account'])->findOrEmpty();
  27. $config['version'] = "res";
  28. if ($config->isEmpty()) return true;
  29. $page = 1;
  30. $mode = (new HxStorePoi);
  31. while ($hasMore) {
  32. $result = (new Store)->config($config->toArray())->token()->getStoreData($config['account'],$page);
  33. if (empty($result)) {
  34. $hasMore = false;
  35. break;
  36. }
  37. if (empty($result['total'])) {
  38. $hasMore = false;
  39. echo getDateFull()."==第{$page}页==数据获取结束\n";
  40. break;
  41. }
  42. $totalPage = ceil($result['total'] / 2);
  43. if ($page >= $totalPage) {
  44. $hasMore = false;
  45. }
  46. foreach ($result['pois'] as $key=>$val) {
  47. $poi = $mode->where("poi_id",$val['poi']['poi_id'])->findOrEmpty();
  48. $inData = $val['poi'];
  49. $inData['agent_id'] = $data['account'];
  50. if ($poi->isEmpty()) {
  51. $inData['poi_address'] = $val['poi']['address'];
  52. $inData['order_start'] = "08:00";
  53. $inData['order_end'] = "22:00";
  54. $inData['poi_account_id'] = $val['account']['poi_account']['account_id'];
  55. echo "写入新数据===".getDateFull()."===".$inData['poi_id']."\n";
  56. $poi->strict(false)->insertGetId($inData);
  57. } else {
  58. echo "更新数据===".getDateFull()."===".$inData['poi_id']."\n";
  59. $poi->update_at = getDateFull();
  60. $poi->poi_address = $val['poi']['address'];
  61. $poi->latitude = $inData['latitude'];
  62. $poi->longitude = $inData['longitude'];
  63. $poi->poi_name = $inData['poi_name'];
  64. $poi->save();
  65. }
  66. }
  67. echo getDateFull()."-页码:{$page}\n";
  68. $page ++;
  69. }
  70. } catch (\Throwable $throwable) {
  71. echo getDateFull()."==获取POI失败\n";
  72. echo $throwable->getMessage()."\n";
  73. echo $throwable->getFile()."【".$throwable->getLine()."】\n";
  74. }
  75. return true;
  76. }
  77. }