update shopee product entity parse
This commit is contained in:
@@ -8,6 +8,7 @@ use App\Constants\ProductStatus;
|
||||
use App\Model\Company;
|
||||
use App\Model\Store;
|
||||
use App\Platform\AbstractProductParse;
|
||||
use App\Platform\Shopee\Constants\ItemStatus;
|
||||
use Carbon\Carbon;
|
||||
use Hyperf\Collection\LazyCollection;
|
||||
use InvalidArgumentException;
|
||||
@@ -103,15 +104,13 @@ class Product extends AbstractProductParse
|
||||
*/
|
||||
public function entityMap(array $raw_data): LazyCollection
|
||||
{
|
||||
// TODO: 实现 Shopee 产品数据映射
|
||||
// 参考 Tmall\EntityParse\Product::entityMap() 实现
|
||||
return LazyCollection::make(function () use ($raw_data) {
|
||||
$records = isset($raw_data[0]) ? $raw_data : [$raw_data];
|
||||
|
||||
foreach ($records as $record) {
|
||||
// TODO: 解析 model_list(规格列表)
|
||||
// 无规格时返回单条记录,有规格时每个 model 返回一条记录
|
||||
yield $this->mapSingleProduct($record, null);
|
||||
yield $this->mapSingleProduct($record);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -119,17 +118,22 @@ class Product extends AbstractProductParse
|
||||
/**
|
||||
* 映射单个产品记录
|
||||
*
|
||||
* @TODO 待实现:根据 Shopee 实际数据结构完善字段映射
|
||||
*
|
||||
* @param array $record 商品主记录
|
||||
* @param array|null $model 规格记录
|
||||
* @return array
|
||||
*/
|
||||
private function mapSingleProduct(array $record, ?array $model): array
|
||||
private function mapSingleProduct(array $record): array
|
||||
{
|
||||
$raw = \json_encode($record);
|
||||
|
||||
$platform_item_id = (string) ($record['item_id'] ?? '');
|
||||
$platform_model_id = $model ? (string) ($model['model_id'] ?? '') : null;
|
||||
|
||||
$platform_model_id = $record['has_model'] ? (string) ($record['model_info']['model_id'] ?? '') : null;
|
||||
|
||||
$price = $this->determinePriceAndCurrency($record);
|
||||
$gtin_code = $record['has_model'] ? strval($record['model_info']['gtin_code']) : $record['gtin_code'];
|
||||
|
||||
$stock = $this->determineStoreNum($record);
|
||||
|
||||
return [
|
||||
'company_id' => $this->getCompany()->id,
|
||||
@@ -143,12 +147,12 @@ class Product extends AbstractProductParse
|
||||
'platform_model_id' => $platform_model_id,
|
||||
'origin_sku_id' => $model['model_sku'] ?? $record['item_sku'] ?? null,
|
||||
'mapped_sku_id' => null,
|
||||
'barcode' => null,
|
||||
'barcode' => $gtin_code == '00' ? null : $gtin_code,
|
||||
'hscode' => null,
|
||||
'bundled' => null,
|
||||
'price' => $model['current_price'] ?? $record['price_info']['current_price'] ?? '0.00',
|
||||
'currency' => $record['price_info']['currency'] ?? 'USD',
|
||||
'num' => $model['stock_info']['current_stock'] ?? $record['stock_info']['current_stock'] ?? 0,
|
||||
'price' => $price['0'] ?? '0.00',
|
||||
'currency' => $price['1'] ?? 'USD',
|
||||
'num' => $stock,
|
||||
'url' => null,
|
||||
'picture' => $record['image']['image_url_list'][0] ?? null,
|
||||
'name' => $record['item_name'] ?? null,
|
||||
@@ -166,6 +170,48 @@ class Product extends AbstractProductParse
|
||||
];
|
||||
}
|
||||
|
||||
private function determinePriceAndCurrency(array $record): array | null
|
||||
{
|
||||
if(!empty($record['current_price']) && !empty($record['currency'])){
|
||||
return [strval($record['current_price']), $record['currency']];
|
||||
}
|
||||
|
||||
if($record['has_model']){
|
||||
|
||||
$model = $record['model_info'];
|
||||
|
||||
$price_info = !empty($model['price_info']) ? $model['price_info']['0'] : null;
|
||||
|
||||
if($price_info){
|
||||
return [$price_info['current_price'], $price_info['currency']];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
if(!empty($record['price_info'])){
|
||||
|
||||
$price_info = !empty($record['price_info']) ? $record['price_info']['0'] : null;
|
||||
|
||||
if($price_info){
|
||||
return [$price_info['current_price'], $price_info['currency']];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
private function determineStoreNum(array $record): int
|
||||
{
|
||||
$stock = $record['has_model'] ? $record['model_info']['stock_info_v2'] : $record['stock_info_v2'];
|
||||
|
||||
return $stock['summary_info']['total_available_stock'] ?? 0;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取唯一键字段
|
||||
*
|
||||
@@ -218,14 +264,14 @@ class Product extends AbstractProductParse
|
||||
public function getProductStatusId(string $platform_status): int
|
||||
{
|
||||
$status_map = [
|
||||
'NORMAL' => ProductStatus::ACTIVE->value,
|
||||
'BANNED' => ProductStatus::BANNED->value,
|
||||
'UNLIST' => ProductStatus::INACTIVE->value,
|
||||
'REVIEWING' => ProductStatus::REVIEWING->value,
|
||||
'SELLER_DELETE' => ProductStatus::SELLER_DELETE->value,
|
||||
'SHOPEE_DELETE' => ProductStatus::PLATFORM_DELETE->value,
|
||||
ItemStatus::NORMAL->name => ProductStatus::ACTIVE->value,
|
||||
ItemStatus::BANNED->name => ProductStatus::BANNED->value,
|
||||
ItemStatus::UNLIST->name => ProductStatus::INACTIVE->value,
|
||||
ItemStatus::REVIEWING->name => ProductStatus::REVIEWING->value,
|
||||
ItemStatus::SELLER_DELETE->name => ProductStatus::SELLER_DELETE->value,
|
||||
ItemStatus::SHOPEE_DELETE->name => ProductStatus::PLATFORM_DELETE->value,
|
||||
];
|
||||
|
||||
return $status_map[$platform_status] ?? ProductStatus::ACTIVE->value;
|
||||
return $status_map[$platform_status] ?? ProductStatus::INACTIVE->value;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user