furniture-3d-system-Wu/scripts/update-test-data.ts
WuYongJie 5a9482bc93 feat: 实现包装方案功能并修复所有类型错误
- 新增 PackagingPlan 数据库模型和迁移
- 实现包装方案 API (GET/POST/PUT/DELETE)
- 创建 PackagingEditor 组件用于管理包装方案
- 在产品详情页集成包装方案标签页
- 修复 API 路由 params Promise 类型适配 (Next.js 16)
- 修复脚本文件中 SKU ownerId 字段缺失问题
- 修复 jwt.ts、performance.ts、VirtualList.tsx 类型错误
- 修复测试文件导入路径和类型注解
- 所有 TypeScript 类型检查通过
2025-12-22 17:43:28 +08:00

149 lines
4.1 KiB
TypeScript
Raw Permalink 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.

/**
* 更新测试数据脚本
* 修复数据库中的测试产品数据,使其可以正常显示
*/
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()
async function main() {
console.log('开始更新测试数据...')
// 获取所有产品
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: {
color: '原木色',
size: '标准尺寸',
material: '实木'
},
price: 2999.00,
costPrice: 1500.00,
stock: 100,
images: ['/placeholder-furniture.jpg'],
isActive: true,
ownerId: skuOwner.id,
}
})
} 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✅ 测试数据更新完成!')
}
main()
.catch((e) => {
console.error('❌ 更新失败:', e)
process.exit(1)
})
.finally(async () => {
await prisma.$disconnect()
})