fix-chapter-status.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. const { PrismaClient } = require('@prisma/client');
  2. const prisma = new PrismaClient();
  3. async function fixStatus() {
  4. console.log('\n📊 检查status字段:\n');
  5. const bookId = 5;
  6. // 检查status字段
  7. const statusCount = await prisma.bookChapter.groupBy({
  8. by: ['status'],
  9. where: {
  10. bookId,
  11. level: 3
  12. },
  13. _count: true
  14. });
  15. console.log('小节status统计:');
  16. statusCount.forEach(s => {
  17. console.log(` ${s.status || 'null'}: ${s._count}个`);
  18. });
  19. // 检查contentStatus
  20. const contentStatusCount = await prisma.bookChapter.groupBy({
  21. by: ['contentStatus'],
  22. where: {
  23. bookId,
  24. level: 3
  25. },
  26. _count: true
  27. });
  28. console.log('\n小节contentStatus统计:');
  29. contentStatusCount.forEach(s => {
  30. console.log(` ${s.contentStatus || 'null'}: ${s._count}个`);
  31. });
  32. // 如果status不是completed,修复它
  33. const notCompleted = await prisma.bookChapter.count({
  34. where: {
  35. bookId,
  36. level: 3,
  37. status: { not: 'completed' }
  38. }
  39. });
  40. if (notCompleted > 0) {
  41. console.log(`\n🔧 发现${notCompleted}个小节status不是completed,正在修复...\n`);
  42. const updated = await prisma.bookChapter.updateMany({
  43. where: {
  44. bookId,
  45. level: 3,
  46. status: { not: 'completed' }
  47. },
  48. data: {
  49. status: 'completed'
  50. }
  51. });
  52. console.log(`✅ 已更新${updated.count}个小节的status为completed\n`);
  53. } else {
  54. console.log('\n✅ 所有小节status都是completed\n');
  55. }
  56. await prisma.$disconnect();
  57. }
  58. fixStatus().catch(console.error);