221 lines
6.0 KiB
TypeScript
221 lines
6.0 KiB
TypeScript
/**
|
||
* 创建系统笔记脚本
|
||
* 用于在"高效沟通的艺术"课程的前两节插入系统笔记数据
|
||
*/
|
||
|
||
import prisma from '../src/utils/prisma';
|
||
import { SYSTEM_USER_ID } from '../src/constants';
|
||
import { logger } from '../src/utils/logger';
|
||
|
||
/**
|
||
* 创建系统用户(如果不存在)
|
||
*/
|
||
async function ensureSystemUser() {
|
||
const existingUser = await prisma.user.findUnique({
|
||
where: { id: SYSTEM_USER_ID },
|
||
});
|
||
|
||
if (existingUser) {
|
||
logger.info('系统用户已存在');
|
||
return existingUser;
|
||
}
|
||
|
||
const systemUser = await prisma.user.create({
|
||
data: {
|
||
id: SYSTEM_USER_ID,
|
||
nickname: '系统',
|
||
agreementAccepted: true,
|
||
},
|
||
});
|
||
|
||
logger.info('系统用户创建成功');
|
||
return systemUser;
|
||
}
|
||
|
||
/**
|
||
* 查找"高效沟通的艺术"课程
|
||
*/
|
||
async function findCourse() {
|
||
// 直接使用已知的课程ID和节点ID
|
||
const courseId = 'course_vertical_001';
|
||
const node1Id = 'node_vertical_001_01';
|
||
const node2Id = 'node_vertical_001_02';
|
||
|
||
const course = await prisma.course.findUnique({
|
||
where: { id: courseId },
|
||
include: {
|
||
nodes: {
|
||
where: {
|
||
id: { in: [node1Id, node2Id] },
|
||
},
|
||
orderBy: { orderIndex: 'asc' },
|
||
},
|
||
},
|
||
});
|
||
|
||
if (!course) {
|
||
throw new Error(`未找到课程: ${courseId}`);
|
||
}
|
||
|
||
if (course.nodes.length < 2) {
|
||
throw new Error(`课程只有 ${course.nodes.length} 个节点,需要至少2个节点`);
|
||
}
|
||
|
||
const node1 = course.nodes.find(n => n.id === 'node_vertical_001_01');
|
||
const node2 = course.nodes.find(n => n.id === 'node_vertical_001_02');
|
||
|
||
if (!node1 || !node2) {
|
||
throw new Error('未找到前两节节点');
|
||
}
|
||
|
||
logger.info(`找到课程: ${course.title},前两节: ${node1.title}, ${node2.title}`);
|
||
return { course, node1, node2 };
|
||
}
|
||
|
||
/**
|
||
* 创建系统笔记
|
||
*/
|
||
async function createSystemNote(data: {
|
||
nodeId: string;
|
||
courseId: string;
|
||
startIndex: number;
|
||
length: number;
|
||
type: 'highlight' | 'thought';
|
||
content?: string;
|
||
quotedText: string;
|
||
}) {
|
||
// 检查是否已存在相同的系统笔记(避免重复创建)
|
||
const existing = await prisma.note.findFirst({
|
||
where: {
|
||
userId: SYSTEM_USER_ID,
|
||
nodeId: data.nodeId,
|
||
startIndex: data.startIndex,
|
||
length: data.length,
|
||
type: data.type,
|
||
},
|
||
});
|
||
|
||
if (existing) {
|
||
logger.info(`系统笔记已存在: ${data.quotedText.substring(0, 20)}...`);
|
||
return existing;
|
||
}
|
||
|
||
const note = await prisma.note.create({
|
||
data: {
|
||
userId: SYSTEM_USER_ID,
|
||
courseId: data.courseId,
|
||
nodeId: data.nodeId,
|
||
startIndex: data.startIndex,
|
||
length: data.length,
|
||
type: data.type,
|
||
content: data.content || '', // content 字段是必填的,不能为 null
|
||
quotedText: data.quotedText,
|
||
},
|
||
});
|
||
|
||
logger.info(`系统笔记创建成功: ${data.quotedText.substring(0, 20)}...`);
|
||
return note;
|
||
}
|
||
|
||
/**
|
||
* 主函数
|
||
*/
|
||
async function main() {
|
||
try {
|
||
logger.info('开始创建系统笔记...');
|
||
|
||
// 1. 确保系统用户存在
|
||
await ensureSystemUser();
|
||
|
||
// 2. 查找课程
|
||
const { course, node1, node2 } = await findCourse();
|
||
|
||
// 3. 为第一节创建系统笔记
|
||
logger.info('为第一节创建系统笔记...');
|
||
|
||
// 系统笔记1:关于倾听的重要性(对应HTML中的"倾听才是沟通的核心")
|
||
await createSystemNote({
|
||
nodeId: node1.id,
|
||
courseId: course.id,
|
||
startIndex: 0, // 实际位置需要根据解析后的纯文本计算,这里先用0
|
||
length: 10,
|
||
type: 'thought',
|
||
content: '倾听是沟通的基础,只有真正听懂对方,才能做出有效回应。',
|
||
quotedText: '倾听才是沟通的核心',
|
||
});
|
||
|
||
// 系统笔记2:关于倾听的三个层次(对应"第一层:听到")
|
||
await createSystemNote({
|
||
nodeId: node1.id,
|
||
courseId: course.id,
|
||
startIndex: 0,
|
||
length: 15,
|
||
type: 'highlight',
|
||
quotedText: '第一层:听到 - 你听到了对方的声音,但可能没有理解。',
|
||
});
|
||
|
||
// 系统笔记3:关于如何提升倾听能力(对应"保持专注,避免分心")
|
||
await createSystemNote({
|
||
nodeId: node1.id,
|
||
courseId: course.id,
|
||
startIndex: 0,
|
||
length: 8,
|
||
type: 'thought',
|
||
content: '保持专注,避免分心,是提升倾听能力的第一步。专注能让你捕捉到对方话语中的细微情绪和真实意图。',
|
||
quotedText: '保持专注,避免分心',
|
||
});
|
||
|
||
// 4. 为第二节创建系统笔记
|
||
logger.info('为第二节创建系统笔记...');
|
||
|
||
// 系统笔记4:关于金字塔原理(对应"金字塔原理")
|
||
await createSystemNote({
|
||
nodeId: node2.id,
|
||
courseId: course.id,
|
||
startIndex: 0,
|
||
length: 5,
|
||
type: 'thought',
|
||
content: '金字塔原理是结构化表达的核心方法:先结论,后原因,再案例。这样能让你的表达更有逻辑性和说服力。',
|
||
quotedText: '金字塔原理',
|
||
});
|
||
|
||
// 系统笔记5:关于结论先行(对应"结论先行")
|
||
await createSystemNote({
|
||
nodeId: node2.id,
|
||
courseId: course.id,
|
||
startIndex: 0,
|
||
length: 4,
|
||
type: 'highlight',
|
||
quotedText: '结论先行 - 先说你的核心观点',
|
||
});
|
||
|
||
// 系统笔记6:关于语言的力量(对应"用肯定的语言替代模糊的表达")
|
||
await createSystemNote({
|
||
nodeId: node2.id,
|
||
courseId: course.id,
|
||
startIndex: 0,
|
||
length: 12,
|
||
type: 'thought',
|
||
content: '用肯定的语言替代模糊的表达,会让你的观点更可信。避免使用"可能"、"也许"等不确定的词汇。',
|
||
quotedText: '用肯定的语言替代模糊的表达,会让你的观点更可信',
|
||
});
|
||
|
||
logger.info('系统笔记创建完成!');
|
||
} catch (error) {
|
||
logger.error('创建系统笔记失败:', error);
|
||
throw error;
|
||
} finally {
|
||
await prisma.$disconnect();
|
||
}
|
||
}
|
||
|
||
// 运行脚本
|
||
main()
|
||
.then(() => {
|
||
process.exit(0);
|
||
})
|
||
.catch((error) => {
|
||
console.error(error);
|
||
process.exit(1);
|
||
});
|