176 lines
5.1 KiB
TypeScript
176 lines
5.1 KiB
TypeScript
/**
|
||
* 更新测试数据脚本
|
||
* 修复数据库中的测试产品数据,使其可以正常显示
|
||
*/
|
||
const CONFIRM_TOKEN = 'update-local-test-data'
|
||
|
||
function assertLocalScriptAllowed() {
|
||
if (!process.argv.includes(`--confirm=${CONFIRM_TOKEN}`)) {
|
||
throw new Error(`update-test-data 只允许显式本地补数:请追加 --confirm=${CONFIRM_TOKEN}。`)
|
||
}
|
||
if (process.env.NODE_ENV === 'production') {
|
||
throw new Error('update-test-data 禁止在 NODE_ENV=production 下执行。')
|
||
}
|
||
const databaseUrl = process.env.DATABASE_URL || ''
|
||
if (!databaseUrl) {
|
||
throw new Error('update-test-data 必须先配置 DATABASE_URL。')
|
||
}
|
||
const host = new URL(databaseUrl).hostname
|
||
if (!['localhost', '127.0.0.1', '::1'].includes(host)) {
|
||
throw new Error('update-test-data 只允许连接本机数据库。')
|
||
}
|
||
}
|
||
|
||
function buildDefaultAssetProfile() {
|
||
return {
|
||
size: { label: '标准尺寸' },
|
||
color: '原木色',
|
||
material: '实木',
|
||
syncedAt: new Date(),
|
||
lockedReasons: {},
|
||
sourceVariants: [],
|
||
}
|
||
}
|
||
|
||
async function main() {
|
||
assertLocalScriptAllowed()
|
||
const { prisma } = await import('../src/lib/prisma')
|
||
console.log('开始更新测试数据...')
|
||
|
||
try {
|
||
// 获取所有产品
|
||
const products = await prisma.product.findMany({
|
||
include: {
|
||
skus: true,
|
||
assemblySteps: true,
|
||
shareLinks: true,
|
||
}
|
||
})
|
||
|
||
console.log(`找到 ${products.length} 个产品`)
|
||
|
||
// 更新每个产品
|
||
for (const product of products) {
|
||
console.log(`\n更新产品: ${product.modelNumber} - ${product.name}`)
|
||
|
||
// 确保产品有基本信息
|
||
await prisma.product.update({
|
||
where: { id: product.id },
|
||
data: {
|
||
description: product.description || `这是一款${product.name},采用现代设计理念,注重实用性和美观性的完美结合。`,
|
||
category: product.category || '家具',
|
||
thumbnailUrl: product.thumbnailUrl || '/placeholder-furniture.jpg',
|
||
}
|
||
})
|
||
|
||
// 获取产品创建者作为 SKU 负责人
|
||
const skuOwner = await prisma.user.findFirst({
|
||
where: { status: 'ACTIVE' },
|
||
})
|
||
|
||
if (!skuOwner) {
|
||
console.error('❌ 没有找到活跃用户,无法创建 SKU')
|
||
continue
|
||
}
|
||
|
||
// 如果产品没有 SKU,创建一个默认 SKU
|
||
if (product.skus.length === 0) {
|
||
console.log(' 创建默认 SKU...')
|
||
await prisma.productSKU.create({
|
||
data: {
|
||
productId: product.id,
|
||
skuCode: `${product.modelNumber}-DEFAULT`,
|
||
attributes: {},
|
||
price: 2999.00,
|
||
costPrice: 1500.00,
|
||
stock: 100,
|
||
images: ['/placeholder-furniture.jpg'],
|
||
isActive: true,
|
||
ownerId: skuOwner.id,
|
||
assetProfile: {
|
||
create: buildDefaultAssetProfile(),
|
||
},
|
||
}
|
||
})
|
||
} else {
|
||
// 更新现有 SKU
|
||
for (const sku of product.skus) {
|
||
console.log(` 更新 SKU: ${sku.skuCode}`)
|
||
await prisma.productSKU.update({
|
||
where: { id: sku.id },
|
||
data: {
|
||
images: sku.images.length > 0 ? sku.images : ['/placeholder-furniture.jpg'],
|
||
stock: sku.stock > 0 ? sku.stock : 50,
|
||
}
|
||
})
|
||
}
|
||
}
|
||
|
||
// 如果产品没有分享链接,创建一个
|
||
if (product.shareLinks.length === 0) {
|
||
console.log(' 创建分享链接...')
|
||
await prisma.shareLink.create({
|
||
data: {
|
||
productId: product.id,
|
||
token: `share-${product.modelNumber.toLowerCase()}-${Date.now()}`,
|
||
viewCount: Math.floor(Math.random() * 100),
|
||
}
|
||
})
|
||
}
|
||
|
||
// 如果产品没有装配步骤,创建一些示例步骤
|
||
if (product.assemblySteps.length === 0) {
|
||
console.log(' 创建示例装配步骤...')
|
||
const steps = [
|
||
{
|
||
order: 1,
|
||
title: '准备工作',
|
||
description: '检查所有配件是否齐全,准备好所需工具',
|
||
partIds: ['base'],
|
||
transform: { position: [0, 0, 0], rotation: [0, 0, 0], scale: [1, 1, 1] },
|
||
duration: 1000,
|
||
easing: 'ease-out',
|
||
},
|
||
{
|
||
order: 2,
|
||
title: '组装底座',
|
||
description: '将底座部件按照标记进行组装',
|
||
partIds: ['base', 'legs'],
|
||
transform: { position: [0, 0.5, 0], rotation: [0, 0, 0], scale: [1, 1, 1] },
|
||
duration: 1500,
|
||
easing: 'ease-in-out',
|
||
},
|
||
{
|
||
order: 3,
|
||
title: '安装主体',
|
||
description: '将主体部分固定到底座上',
|
||
partIds: ['main'],
|
||
transform: { position: [0, 1, 0], rotation: [0, 0, 0], scale: [1, 1, 1] },
|
||
duration: 2000,
|
||
easing: 'ease-out',
|
||
},
|
||
]
|
||
|
||
for (const step of steps) {
|
||
await prisma.assemblyStep.create({
|
||
data: {
|
||
productId: product.id,
|
||
...step,
|
||
}
|
||
})
|
||
}
|
||
}
|
||
}
|
||
|
||
console.log('\n✅ 测试数据更新完成!')
|
||
} finally {
|
||
await prisma.$disconnect()
|
||
}
|
||
}
|
||
|
||
main()
|
||
.catch((e) => {
|
||
console.error('❌ 更新失败:', e)
|
||
process.exit(1)
|
||
})
|