136 lines
4.4 KiB
TypeScript
136 lines
4.4 KiB
TypeScript
/**
|
|
* 更新音频素材模板字段
|
|
*
|
|
* 将音频模板的字段从通用的 SPU/店铺/场景 更新为更适合音频的字段:
|
|
* - productModel -> 音频名称
|
|
* - categoryName -> 音频分类
|
|
* - sceneName -> 使用场景
|
|
* - 新增 renderTime (时长)
|
|
* - 新增 authorName (作者)
|
|
*
|
|
* 运行方式: npx ts-node scripts/update-audio-template-fields.ts
|
|
*/
|
|
|
|
import { PrismaClient } from '@prisma/client'
|
|
|
|
const prisma = new PrismaClient()
|
|
|
|
const AUDIO_TEMPLATE_ID = 'builtin-audio-material'
|
|
|
|
const AUDIO_FIELDS = [
|
|
{ key: 'productModel', label: '音频名称', type: 'text', required: true, layoutWidth: 'full', visibleOnHomeFilter: false, isSearchable: true, addToCategory: false },
|
|
{ key: 'audioCategory', label: '音频分类', type: 'select', required: true, layoutWidth: 'full', visibleOnHomeFilter: true, isSearchable: true, addToCategory: true },
|
|
{ key: 'audioScene', label: '音频使用场景', type: 'select', required: true, layoutWidth: 'full', visibleOnHomeFilter: true, isSearchable: true, addToCategory: true },
|
|
{ key: 'authorName', label: '作者', type: 'text', required: false, layoutWidth: 'half', visibleOnHomeFilter: false, isSearchable: true, addToCategory: false },
|
|
]
|
|
|
|
async function updateAudioTemplateFields() {
|
|
console.log('开始更新音频素材模板字段...\n')
|
|
|
|
try {
|
|
// 检查模板是否存在
|
|
const template = await prisma.uploadTemplate.findUnique({
|
|
where: { id: AUDIO_TEMPLATE_ID },
|
|
include: { fields: true },
|
|
})
|
|
|
|
if (!template) {
|
|
console.log(`模板 ${AUDIO_TEMPLATE_ID} 不存在,将创建新模板`)
|
|
|
|
// 创建模板
|
|
await prisma.uploadTemplate.create({
|
|
data: {
|
|
id: AUDIO_TEMPLATE_ID,
|
|
name: '音频素材模板',
|
|
description: '适用于音频文件素材,支持 MP3、WAV、FLAC、AAC 等格式',
|
|
status: 'active',
|
|
materialType: '音频',
|
|
materialKind: 'AUDIO',
|
|
version: 1,
|
|
},
|
|
})
|
|
|
|
// 创建字段
|
|
for (let i = 0; i < AUDIO_FIELDS.length; i++) {
|
|
const field = AUDIO_FIELDS[i]
|
|
await prisma.uploadField.create({
|
|
data: {
|
|
templateId: AUDIO_TEMPLATE_ID,
|
|
key: field.key,
|
|
label: field.label,
|
|
type: field.type,
|
|
required: field.required,
|
|
orderIndex: i,
|
|
layoutWidth: field.layoutWidth,
|
|
visibleOnHomeFilter: field.visibleOnHomeFilter,
|
|
isSearchable: field.isSearchable,
|
|
addToCategory: field.addToCategory,
|
|
},
|
|
})
|
|
console.log(` ✓ 创建字段: ${field.label} (${field.key})`)
|
|
}
|
|
|
|
console.log('\n✅ 音频模板创建完成')
|
|
return
|
|
}
|
|
|
|
console.log(`找到模板: ${template.name}`)
|
|
console.log(`当前字段数: ${template.fields.length}`)
|
|
|
|
// 删除现有字段
|
|
await prisma.uploadField.deleteMany({
|
|
where: { templateId: AUDIO_TEMPLATE_ID },
|
|
})
|
|
console.log('已删除旧字段')
|
|
|
|
// 创建新字段
|
|
for (let i = 0; i < AUDIO_FIELDS.length; i++) {
|
|
const field = AUDIO_FIELDS[i]
|
|
await prisma.uploadField.create({
|
|
data: {
|
|
templateId: AUDIO_TEMPLATE_ID,
|
|
key: field.key,
|
|
label: field.label,
|
|
type: field.type,
|
|
required: field.required,
|
|
orderIndex: i,
|
|
layoutWidth: field.layoutWidth,
|
|
visibleOnHomeFilter: field.visibleOnHomeFilter,
|
|
isSearchable: field.isSearchable,
|
|
addToCategory: field.addToCategory,
|
|
},
|
|
})
|
|
console.log(` ✓ 创建字段: ${field.label} (${field.key})`)
|
|
}
|
|
|
|
console.log('\n✅ 音频模板字段更新完成')
|
|
|
|
// 验证更新结果
|
|
const updatedTemplate = await prisma.uploadTemplate.findUnique({
|
|
where: { id: AUDIO_TEMPLATE_ID },
|
|
include: { fields: { orderBy: { orderIndex: 'asc' } } },
|
|
})
|
|
|
|
console.log('\n更新后的字段:')
|
|
updatedTemplate?.fields.forEach((field, index) => {
|
|
console.log(` ${index + 1}. ${field.label} (${field.key}) - ${field.required ? '必填' : '可选'}`)
|
|
})
|
|
|
|
} catch (error) {
|
|
console.error('更新失败:', error)
|
|
throw error
|
|
} finally {
|
|
await prisma.$disconnect()
|
|
}
|
|
}
|
|
|
|
updateAudioTemplateFields()
|
|
.then(() => {
|
|
console.log('\n脚本执行完成')
|
|
process.exit(0)
|
|
})
|
|
.catch((error) => {
|
|
console.error('脚本执行失败:', error)
|
|
process.exit(1)
|
|
})
|