| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- <?php
- namespace app\queue\redis\slow;
- use app\extra\life\data\Store;
- use app\model\hx\HxAgent;
- use app\model\hx\HxGoods;
- use app\model\system\SystemConfig;
- use Webman\RedisQueue\Consumer;
- class SyncGoods implements Consumer
- {
- /**
- * 消费队列名
- * 同步线上商品
- * @var string
- */
- public string $queue = "goods-sync";
- /**
- * 连接配置
- * @var string
- */
- public string $connection = "default";
- public function consume($data): bool
- {
- try {
- if (empty($data['account'])) return true;
- $config = (new HxAgent)->where("agent_id",$data['account'])->findOrEmpty();
- $config['version'] = "res";
- if ($config->isEmpty()) return true;
- $cursor = "";
- $hasMore = true;
- $mode = (new HxGoods);
- while ($hasMore) {
- $result = (new Store)->config($config->toArray())->token()->getStoreGoods($config['account'], $cursor);
- if (empty($result)) {
- $hasMore = false;
- break;
- }
- echo $result['has_more']."====\n";
- if (!$result['has_more']) {
- $hasMore = false;
- echo getDateFull()."==数据获取结束-has_more\n";
- }
- if (empty($result['products'])) {
- $hasMore = false;
- echo getDateFull()."==数据获取结束\n";
- break;
- }
- foreach ($result['products'] as $key=>$val) {
- if ($val['product']['category_id']) {
- echo getDateFull()."==商品=={$val['product']['product_id']}===\n";
- $goods = $mode->where("product_id",$val['product']['product_id'])->findOrEmpty();
- if ($goods->isEmpty()) {
- echo getDateFull()."==新增商品=={$val['product']['product_name']}===\n";
- $goods->insertGetId([
- "biz_line" => $val['product']['biz_line'],
- "agent_id" => $data['account'],
- "pois" => json_encode($val['product']['pois']??[]),
- "product_id" => $val['product']['product_id'],
- "product_name" => $val['product']['product_name'],
- "sold_end_time" => $val['product']['sold_end_time'],
- "sold_start_time" => $val['product']['sold_start_time'],
- "product_type" => $val['product']['product_type'],
- "actual_amount" => $val['sku']['actual_amount']??0,
- "create_at" => date("Y-m-d H:i:s", $val['product']['create_time']),
- ]);
- }
- } else {
- echo getDateFull()."==跳过商品=={$val['product']['product_name']}===\n";
- }
- }
- $cursor = $result["next_cursor"];
- $hasMore = true;
- }
- } catch (\Throwable $throwable) {
- echo getDateFull()."==获取商品失败\n";
- echo $throwable->getMessage()."\n";
- echo $throwable->getFile()."【".$throwable->getLine()."】\n";
- }
- return true;
- }
- }
|