From 55904722b384cad79115ff95dc955b04c4fc4051 Mon Sep 17 00:00:00 2001 From: Nick Zeng Date: Fri, 6 Feb 2026 14:28:09 +0800 Subject: [PATCH] update tmall product entity --- .../Platform/Tmall/EntityParse/Product.php | 211 ++++++++++++++++++ 1 file changed, 211 insertions(+) create mode 100644 backend/app/Platform/Tmall/EntityParse/Product.php diff --git a/backend/app/Platform/Tmall/EntityParse/Product.php b/backend/app/Platform/Tmall/EntityParse/Product.php new file mode 100644 index 0000000..811cdc8 --- /dev/null +++ b/backend/app/Platform/Tmall/EntityParse/Product.php @@ -0,0 +1,211 @@ +mapSingleProduct($record, null); + } else { + // 有 SKU 规格商品,每个 SKU 一条记录 + foreach ($skus as $sku) { + yield $this->mapSingleProduct($record, $sku); + } + } + } + }); + } + + /** + * 映射单个产品记录 + * + * @param array $record 商品主记录 + * @param array|null $sku SKU 规格记录 + * @return array + */ + private function mapSingleProduct(array $record, ?array $sku): array + { + $raw = \json_encode($record); + $platform_item_id = (string) $record['num_iid']; + $platform_model_id = $sku ? (string) $sku['sku_id'] : null; + + return [ + 'company_id' => $this->getCompany()->id, + 'platform_id' => $this->getPlatform()->id, + 'store_id' => $this->getStore()->id, + 'status_id' => $this->getProductStatusId($record['approve_status'] ?? ItemStatus::ONSALE->value), + 'type_id' => 1, // 默认类型 + 'warehouse_id' => null, + 'sub_warehouse_id' => null, + 'platform_item_id' => $platform_item_id, + 'platform_model_id' => $platform_model_id, + 'origin_sku_id' => $sku['outer_id'] ?? $record['outer_id'] ?? null, + 'mapped_sku_id' => null, + 'barcode' => $sku['barcode'] ?? $sku['outer_id'] ?? $record['outer_id'] ?? null, + 'hscode' => null, + 'bundled' => null, + 'price' => $sku['price'] ?? $record['price'] ?? '0.00', + 'currency' => 'CNY', + 'num' => $sku['quantity'] ?? $record['num'] ?? 0, + 'url' => $record['detail_url'] ?? null, + 'picture' => $record['pic_url'] ?? null, + 'name' => $record['title'] ?? null, + 'raw' => $raw, + 'ext' => null, + 'hash' => \md5($raw), + 'created_date' => isset($record['created']) ? Carbon::parse($record['created'], $this->getStore()->getTimezoneString())->format('Y-m-d H:i:sP') : null, + 'updated_date' => isset($record['modified']) ? Carbon::parse($record['modified'], $this->getStore()->getTimezoneString())->format('Y-m-d H:i:sP') : null, + 'created_at' => Carbon::now()->format('Y-m-d H:i:sP'), + 'updated_at' => Carbon::now()->format('Y-m-d H:i:sP'), + ]; + } + + /** + * 获取唯一键字段 + * + * 对应数据库约束:products_store_item_model_unique + * + * @return array + */ + public function getUniqueBy(): array + { + return ['store_id', 'platform_item_id', 'platform_model_id']; + } + + /** + * 获取可更新字段列表 + * + * @return array + */ + public function getUpdateFields(): array + { + return [ + 'status_id', + 'type_id', + 'warehouse_id', + 'sub_warehouse_id', + 'origin_sku_id', + 'mapped_sku_id', + 'barcode', + 'hscode', + 'bundled', + 'price', + 'currency', + 'num', + 'url', + 'picture', + 'name', + 'raw', + 'ext', + 'hash', + 'updated_date', + 'updated_at', + ]; + } + + /** + * 获取产品状态 ID + * + * 将 Tmall 产品状态映射到本地 ProductStatus 枚举 + * + * @see ItemStatus Tmall 平台商品状态枚举 + * @see ProductStatus 本地产品状态枚举 + * + * @param string $platform_status Tmall 平台状态 + * @return int + */ + public function getProductStatusId(string $platform_status): int + { + $status_map = [ + ItemStatus::ONSALE->value => ProductStatus::ACTIVE->value, + ItemStatus::INSTOCK->value => ProductStatus::INACTIVE->value, + ]; + + return $status_map[$platform_status] ?? ProductStatus::INACTIVE->value; + } +}