update producer
This commit is contained in:
@@ -0,0 +1,238 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Platform\Shopee\EntityParse;
|
||||
|
||||
use App\Model\Company;
|
||||
use App\Model\Model as Entity;
|
||||
use App\Model\Store;
|
||||
use App\Platform\AbstractProductParse;
|
||||
use App\Platform\Shopee\Constants\OrderStatus;
|
||||
use App\Constants\PaymentMethod;
|
||||
use Carbon\Carbon;
|
||||
use Hyperf\Collection\LazyCollection;
|
||||
use Hyperf\Context\ApplicationContext;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use InvalidArgumentException;
|
||||
|
||||
/**
|
||||
* Shopee 订单解析器
|
||||
*
|
||||
* 继承 AbstractOrderParse, 实现 Shopee 平台特定的 Order 解析逻辑
|
||||
* 实现 OrderContract 契约中定义的 parseOrderItems 方法
|
||||
*
|
||||
* @attention 此类文件名为 Product.php,但实际处理订单数据,建议重命名为 Order.php
|
||||
*/
|
||||
class Product extends AbstractProductParse
|
||||
{
|
||||
/**
|
||||
* 公司作用域匹配
|
||||
*
|
||||
* 从 metadata 中提取 company_id,然后查询数据库获取公司对象
|
||||
*
|
||||
* @param array $metadata
|
||||
* @return Company
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function companyScopeMatch(array $metadata): Company
|
||||
{
|
||||
// 验证必需字段
|
||||
if (!isset($metadata['company_id'])) {
|
||||
throw new InvalidArgumentException('company_id is required in metadata');
|
||||
}
|
||||
|
||||
$company_id = $metadata['company_id'];
|
||||
|
||||
$company = Company::find($company_id);
|
||||
|
||||
if (!$company) {
|
||||
throw new InvalidArgumentException("Company with ID {$company_id} not found");
|
||||
}
|
||||
|
||||
return $company;
|
||||
}
|
||||
|
||||
/**
|
||||
* 店铺作用域匹配
|
||||
*
|
||||
* 从 metadata 中提取 store_id,然后查询数据库获取店铺对象
|
||||
*
|
||||
* @param array $metadata
|
||||
* @return Store
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function storeScopeMatch(array $metadata): Store
|
||||
{
|
||||
// 验证必需字段
|
||||
if (!isset($metadata['platform_store_id'])) {
|
||||
throw new InvalidArgumentException('platform_store_id is required in metadata');
|
||||
}
|
||||
|
||||
$platform_store_id = $metadata['platform_store_id'];
|
||||
|
||||
$shopee_platform_id = 25;
|
||||
|
||||
$store = Store::where('platform_id','=', $shopee_platform_id)
|
||||
->where('platform_store_id', '=', $platform_store_id)
|
||||
->first();
|
||||
|
||||
if (!$store) {
|
||||
throw new InvalidArgumentException("Platform shopee store id {$shopee_platform_id} not found");
|
||||
}
|
||||
|
||||
return $store;
|
||||
}
|
||||
|
||||
/**
|
||||
* 实体数据映射
|
||||
*
|
||||
* 将原始数据映射为可供 Model 使用的数组集合
|
||||
*
|
||||
* @attention 当前返回格式为 ['order' => [...], 'items' => [...]]
|
||||
* 但 ProductConsumer 第 153-157 行期望直接返回订单数据数组(参考 Tmall 实现)
|
||||
* 需要调整返回格式以兼容 ProductConsumer 的 upsert 操作
|
||||
*
|
||||
* @TODO 调整返回格式:
|
||||
* - 方案1:改为直接返回订单数据数组(与 Tmall 一致)
|
||||
* - 方案2:修改 ProductConsumer 以支持当前的嵌套结构
|
||||
*
|
||||
* @param array $raw_data 原始数据数组,通常来自 $data['raw_data']
|
||||
* @return LazyCollection 返回 LazyCollection,每个元素为可供 Model::fill() 使用的数组
|
||||
*/
|
||||
public function entityMap(array $raw_data): LazyCollection
|
||||
{
|
||||
// 使用 LazyCollection 进行延迟处理,提高性能
|
||||
return LazyCollection::make(function () use ($raw_data) {
|
||||
// 如果 raw_data 是单个记录,转换为数组
|
||||
$records = isset($raw_data[0]) ? $raw_data : [$raw_data];
|
||||
|
||||
foreach ($records as $record) {
|
||||
dump($record);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取唯一键字段(对应数据库唯一索引)
|
||||
*
|
||||
* 对应数据库约束:
|
||||
* UNIQUE INDEX orders_store_platform_order_unique (store_id, platform_order_id)
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getUniqueBy(): array
|
||||
{
|
||||
return ['store_id', 'platform_product_id'];
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取可更新字段列表
|
||||
*
|
||||
* 排除:主键、唯一键、创建时间、关联 ID
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getUpdateFields(): array
|
||||
{
|
||||
// 手动指定(推荐:明确且高效,无数据库查询开销)
|
||||
return [
|
||||
'order_status_id',
|
||||
'payment_method_id',
|
||||
'buyer_user_id',
|
||||
'presale',
|
||||
'total_amount',
|
||||
'total_paid',
|
||||
'total_discount',
|
||||
'total_received',
|
||||
'freight_fee',
|
||||
'tax_fee',
|
||||
'discount_fee',
|
||||
'commission_fee',
|
||||
'coupon_amount',
|
||||
'voucher_amount',
|
||||
'order_type_id',
|
||||
'updated_date',
|
||||
'paid_date',
|
||||
'shipping_date',
|
||||
'zipcode',
|
||||
'city',
|
||||
'province',
|
||||
'country',
|
||||
'raw',
|
||||
'ext',
|
||||
'updated_at',
|
||||
];
|
||||
|
||||
// 动态计算方案(如果表字段经常变化,可以使用):
|
||||
// $entity = $this->entityMatch([
|
||||
// 'company_id' => $this->getCompany()->id,
|
||||
// 'platform_id' => $this->getPlatform()->id,
|
||||
// 'store_id' => $this->getStore()->id,
|
||||
// ]);
|
||||
// $excludeFields = array_merge(
|
||||
// ['id', 'created_at', 'created_date', 'company_id', 'platform_id'],
|
||||
// $this->getUniqueBy()
|
||||
// );
|
||||
// return $this->getTableColumnsExcept($entity, $excludeFields);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据 Shopee 产品状态确定对应的本地订单状态
|
||||
*
|
||||
* @required ProductConsumer 要求实现此方法(第 130-132 行)
|
||||
* @see \App\Constants\OrderStatus
|
||||
*
|
||||
* @param string $platform_order_status Shopee 平台订单状态
|
||||
* @return int 本地订单状态 ID
|
||||
*/
|
||||
public function getProductStatusId(string $platform_order_status): int
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Shopee 产品状态到本地产品状态的映射表
|
||||
*
|
||||
* @see \App\Platform\Shopee\Constants\ProductStatus Shopee 产品订单状态枚举
|
||||
* @see \App\Constants\OrderStatus 本地产品状态枚举
|
||||
*
|
||||
* @return array<string, int>
|
||||
*/
|
||||
private function ProducttatusMap(): array
|
||||
{
|
||||
return [
|
||||
// 未付款 -> 等待付款
|
||||
'UNPAID' => \App\Constants\OrderStatus::PAYMENT_PENDING->value,
|
||||
|
||||
// 可发货(已付款,等待卖家安排发货)-> 付款完成
|
||||
'READY_TO_SHIP' => \App\Constants\OrderStatus::PAYMENT_COMPLETE->value,
|
||||
|
||||
// 已处理(卖家已安排发货并获取物流单号)-> 等待发货
|
||||
'PROCESSED' => \App\Constants\OrderStatus::AWAITING_SHIPMENT->value,
|
||||
|
||||
// 重新发货(3PL 取件失败,需要重新安排发货)-> 等待发货
|
||||
'RETRY_SHIP' => \App\Constants\OrderStatus::AWAITING_SHIPMENT->value,
|
||||
|
||||
// 已发货(包裹已交给 3PL 或已被 3PL 取走)-> 已发货
|
||||
'SHIPPED' => \App\Constants\OrderStatus::SHIPPED->value,
|
||||
|
||||
// 待确认收货(买家已收到订单)-> 已发货
|
||||
'TO_CONFIRM_RECEIVE' => \App\Constants\OrderStatus::SHIPPED->value,
|
||||
|
||||
// 取消中(订单取消正在处理)-> 取消请求中
|
||||
'IN_CANCEL' => \App\Constants\OrderStatus::CANCEL_REQUESTED->value,
|
||||
|
||||
// 已取消 -> 取消确认
|
||||
'CANCELLED' => \App\Constants\OrderStatus::CANCEL_CONFIRMED->value,
|
||||
|
||||
// 退货中(买家请求退货,退货正在处理)-> 取消请求中
|
||||
'TO_RETURN' => \App\Constants\OrderStatus::CANCEL_REQUESTED->value,
|
||||
|
||||
// 已完成 -> 完成
|
||||
'COMPLETED' => \App\Constants\OrderStatus::FINISHED->value,
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Platform\Tmall\Producer;
|
||||
|
||||
use Hyperf\Amqp\Annotation\Producer;
|
||||
use App\Platform\OrderProducer;
|
||||
|
||||
#[Producer('tmall.exchange', 'product.tmall')]
|
||||
class TmallProductProducer extends OrderProducer
|
||||
{
|
||||
protected string $exchange = 'tmall.exchange';
|
||||
protected string|array $routingKey = 'product.tmall';
|
||||
|
||||
protected function buildMessage(array $data): array
|
||||
{
|
||||
$parent_meta = parent::buildMessage($data);
|
||||
$parent_meta['platform'] = 'tmall';
|
||||
$parent_meta['data_type'] = 'product';
|
||||
$parent_meta['meta']['source_system'] = 'tmall_api';
|
||||
|
||||
return $parent_meta;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Platform\Tmall\Producer;
|
||||
|
||||
use Hyperf\Amqp\Annotation\Producer;
|
||||
use App\Platform\OrderProducer;
|
||||
|
||||
#[Producer('tmall.exchange', 'refund.tmall')]
|
||||
class TmallRefundProducer extends OrderProducer
|
||||
{
|
||||
protected string $exchange = 'tmall.exchange';
|
||||
protected string|array $routingKey = 'refund.tmall';
|
||||
|
||||
protected function buildMessage(array $data): array
|
||||
{
|
||||
$parent_meta = parent::buildMessage($data);
|
||||
$parent_meta['platform'] = 'tmall';
|
||||
$parent_meta['data_type'] = 'refund';
|
||||
$parent_meta['meta']['source_system'] = 'tmall_api';
|
||||
|
||||
return $parent_meta;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user