erp-backend/app/Http/Controllers/PlatformProductController.php
2026-04-01 17:07:04 +08:00

237 lines
8.3 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Http\Controllers;
use App\Models\Platform;
use App\Models\PlatformSkuBinding;
use App\Models\ErpSku;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class PlatformProductController extends Controller
{
/**
* 平台商品列表(包含绑定状态)
*/
public function index(Request $request)
{
$query = Platform::with(['activeBinding.erpSku']);
if ($request->filled('shop_auth_id')) {
$query->where('shop_auth_id', $request->shop_auth_id);
}
if ($request->filled('sku_code')) {
$query->where('sku_code', 'like', "%{$request->sku_code}%");
}
if ($request->filled('title')) {
$query->where('title', 'like', "%{$request->title}%");
}
if ($request->filled('binding_status')) {
if ($request->binding_status === 'bound') {
$query->whereHas('activeBinding');
} elseif ($request->binding_status === 'unbound') {
$query->whereDoesntHave('activeBinding');
}
}
$perPage = $request->input('limit', 15);
$platforms = $query->orderBy('updated_at', 'desc')->paginate($perPage);
// 格式化数据
$platforms->getCollection()->transform(function ($platform) {
return [
'id' => $platform->id,
'shop_auth_id' => $platform->shop_auth_id,
'platform_product_id' => $platform->platform_product_id,
'platform_sku_id' => $platform->platform_sku_id,
'sku_code' => $platform->sku_code,
'title' => $platform->title,
'price' => $platform->price,
'specs' => $platform->specs,
'status' => $platform->status,
'bound_erp_sku' => $platform->activeBinding ? [
'id' => $platform->activeBinding->erpSku->id,
'sku_code' => $platform->activeBinding->erpSku->sku_code,
'name' => $platform->activeBinding->erpSku->name,
] : null,
'last_sync_at' => $platform->last_sync_at,
];
});
return response()->json([
'code' => 200,
'data' => [
'list' => $platforms->items(),
'total' => $platforms->total(),
'current_page' => $platforms->currentPage(),
'last_page' => $platforms->lastPage(),
],
'message' => 'success'
]);
}
/**
* 绑定ERP商品
*/
public function bind(Request $request, $id)
{
$request->validate([
'erp_sku_id' => 'required|exists:erp_skus,id',
]);
$platform = Platform::findOrFail($id);
// 将之前生效的绑定置为无效
DB::transaction(function () use ($platform, $request) {
$platform->activeBinding()?->update([
'is_active' => false,
'unbound_at' => now(),
'reason' => '手动重新绑定'
]);
// 创建新绑定
PlatformSkuBinding::create([
'platform_sku_id' => $platform->id,
'erp_sku_id' => $request->erp_sku_id,
'is_active' => true,
'bound_at' => now(),
]);
});
// 可选异步重新匹配所有包含此平台SKU的订单
// dispatch(new RematchOrdersForPlatformSku($platform->sku_code));
return response()->json([
'code' => 200,
'message' => '绑定成功'
]);
}
/**
* 同步平台商品(模拟)
* 实际开发中应调用平台API获取最新商品信息并检测变化
*/
public function sync(Request $request)
{
$request->validate([
'shop_auth_id' => 'required|exists:shop_auths,id',
]);
// 模拟从平台拉取最新商品数据
$mockPlatformSkus = [
[
'platform_product_id' => '123456',
'platform_sku_id' => '789012',
'sku_code' => 'SKU001',
'title' => '测试商品1',
'price' => 99.50,
'specs' => ['颜色' => '红色', '尺寸' => 'M'],
// ...
],
// 更多商品...
];
$updatedCount = 0;
$newCount = 0;
foreach ($mockPlatformSkus as $data) {
$platform = Platform::where('shop_auth_id', $request->shop_auth_id)
->where('platform_product_id', $data['platform_product_id'])
->where('platform_sku_id', $data['platform_sku_id'])
->first();
if ($platform) {
// 检查商品信息是否有变化(比较关键字段)
$changed = false;
if ($platform->title !== $data['title'] || $platform->price != $data['price'] || json_encode($platform->specs) !== json_encode($data['specs'])) {
$changed = true;
}
// 更新平台商品信息
$platform->update([
'title' => $data['title'],
'price' => $data['price'],
'specs' => $data['specs'],
'last_sync_at' => now(),
]);
if ($changed) {
// 将当前生效的绑定作废
$platform->activeBinding()?->update([
'is_active' => false,
'unbound_at' => now(),
'reason' => '平台商品信息更新'
]);
$updatedCount++;
}
} else {
// 新增平台商品
Platform::create([
'shop_auth_id' => $request->shop_auth_id,
'platform_product_id' => $data['platform_product_id'],
'platform_sku_id' => $data['platform_sku_id'],
'sku_code' => $data['sku_code'],
'title' => $data['title'],
'price' => $data['price'],
'specs' => $data['specs'],
'last_sync_at' => now(),
]);
$newCount++;
}
}
return response()->json([
'code' => 200,
'data' => [
'new' => $newCount,
'updated' => $updatedCount,
],
'message' => '同步完成'
]);
}
/**
* 获取单个平台商品详情(包含绑定历史)
*/
public function show($id)
{
$platform = Platform::with(['skuBindings.erpSku'])->findOrFail($id);
$data = [
'id' => $platform->id,
'shop_auth_id' => $platform->shop_auth_id,
'platform_product_id' => $platform->platform_product_id,
'platform_sku_id' => $platform->platform_sku_id,
'sku_code' => $platform->sku_code,
'title' => $platform->title,
'price' => $platform->price,
'specs' => $platform->specs,
'status' => $platform->status,
'current_binding' => $platform->activeBinding ? [
'erp_sku_id' => $platform->activeBinding->erpSku->id,
'sku_code' => $platform->activeBinding->erpSku->sku_code,
'name' => $platform->activeBinding->erpSku->name,
'bound_at' => $platform->activeBinding->bound_at,
] : null,
'binding_history' => $platform->skuBindings->map(function ($binding) {
return [
'erp_sku' => $binding->erpSku ? [
'id' => $binding->erpSku->id,
'sku_code' => $binding->erpSku->sku_code,
'name' => $binding->erpSku->name,
] : null,
'is_active' => $binding->is_active,
'bound_at' => $binding->bound_at,
'unbound_at' => $binding->unbound_at,
'reason' => $binding->reason,
];
}),
'last_sync_at' => $platform->last_sync_at,
];
return response()->json([
'code' => 200,
'data' => $data,
'message' => 'success'
]);
}
}