const { PrismaClient } = require('@prisma/client');
const prisma = new PrismaClient();

async function fixNullParentId() {
  console.log('查找parentId为NULL的记录...');
  
  const nullRecords = await prisma.bookChapter.count({
    where: { parentId: null }
  });
  
  console.log(`找到 ${nullRecords} 条parentId为NULL的记录`);
  
  if (nullRecords > 0) {
    console.log('将所有parentId为NULL的记录更新为0...');
    const result = await prisma.bookChapter.updateMany({
      where: { parentId: null },
      data: { parentId: 0 }
    });
    console.log(`✅ 已更新 ${result.count} 条记录`);
  }
  
  console.log('完成！');
  await prisma.$disconnect();
}

fixNullParentId().catch(console.error);
