SyncGoods.php 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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\HxGoods;
  6. use app\model\system\SystemConfig;
  7. use Webman\RedisQueue\Consumer;
  8. class SyncGoods implements Consumer
  9. {
  10. /**
  11. * 消费队列名
  12. * 同步线上商品
  13. * @var string
  14. */
  15. public string $queue = "goods-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. $config = (new HxAgent)->where("agent_id",$data['account'])->findOrEmpty();
  26. $config['version'] = "res";
  27. if ($config->isEmpty()) return true;
  28. $cursor = "";
  29. $hasMore = true;
  30. $mode = (new HxGoods);
  31. while ($hasMore) {
  32. $result = (new Store)->config($config->toArray())->token()->getStoreGoods($config['account'], $cursor);
  33. if (empty($result)) {
  34. $hasMore = false;
  35. break;
  36. }
  37. echo $result['has_more']."====\n";
  38. if (!$result['has_more']) {
  39. $hasMore = false;
  40. echo getDateFull()."==数据获取结束-has_more\n";
  41. }
  42. if (empty($result['products'])) {
  43. $hasMore = false;
  44. echo getDateFull()."==数据获取结束\n";
  45. break;
  46. }
  47. foreach ($result['products'] as $key=>$val) {
  48. if ($val['product']['category_id']) {
  49. echo getDateFull()."==商品=={$val['product']['product_id']}===\n";
  50. $goods = $mode->where("product_id",$val['product']['product_id'])->findOrEmpty();
  51. if ($goods->isEmpty()) {
  52. echo getDateFull()."==新增商品=={$val['product']['product_name']}===\n";
  53. $goods->insertGetId([
  54. "biz_line" => $val['product']['biz_line'],
  55. "agent_id" => $data['account'],
  56. "pois" => json_encode($val['product']['pois']??[]),
  57. "product_id" => $val['product']['product_id'],
  58. "product_name" => $val['product']['product_name'],
  59. "sold_end_time" => $val['product']['sold_end_time'],
  60. "sold_start_time" => $val['product']['sold_start_time'],
  61. "product_type" => $val['product']['product_type'],
  62. "actual_amount" => $val['sku']['actual_amount']??0,
  63. "create_at" => date("Y-m-d H:i:s", $val['product']['create_time']),
  64. ]);
  65. }
  66. } else {
  67. echo getDateFull()."==跳过商品=={$val['product']['product_name']}===\n";
  68. }
  69. }
  70. $cursor = $result["next_cursor"];
  71. $hasMore = true;
  72. }
  73. } catch (\Throwable $throwable) {
  74. echo getDateFull()."==获取商品失败\n";
  75. echo $throwable->getMessage()."\n";
  76. echo $throwable->getFile()."【".$throwable->getLine()."】\n";
  77. }
  78. return true;
  79. }
  80. }