123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346 |
- /*const { CloudQuery, CloudObject } = require("../lib/ncloud");
- const { QuestionnaireResultList, QuestionnaireList, QuestionList, OptionList, UserInterestProfileList } = require("./data");
- importData();
- let DataMap = {
- QuestionnaireResult: {},
- Questionnaire: {},
- Question: {},
- Option: {},
- UserInterestProfile: {}
- }
- async function importData() {
- // 导入问卷结果数据
- let questionnaireResultList = QuestionnaireResultList;
- for (let index = 0; index < questionnaireResultList.length; index++) {
- let result = questionnaireResultList[index];
- result = await importObject("QuestionnaireResult", result);
- }
- // 导入问卷数据
- let questionnaireList = QuestionnaireList;
- for (let index = 0; index < questionnaireList.length; index++) {
- let questionnaire = questionnaireList[index];
- questionnaire = await importObject("Questionnaire", questionnaire);
- }
- // 导入问题数据
- let questionList = QuestionList;
- for (let index = 0; index < questionList.length; index++) {
- let question = questionList[index];
- question = await importObject("Question", question);
- }
- // 导入选项数据
- let optionList = OptionList;
- for (let index = 0; index < optionList.length; index++) {
- let option = optionList[index];
- option = await importObject("Option", option);
- }
- // 导入用户兴趣画像数据
- let userInterestProfileList = UserInterestProfileList;
- for (let index = 0; index < userInterestProfileList.length; index++) {
- let profile = userInterestProfileList[index];
- profile = await importObject("UserInterestProfile", profile);
- }
- // console.log(DataMap)
- }
- async function importObject(className, data) {
- // 获取类名对应的 ID 字段名称(如 OptionId, QuestionnaireId 等)
- let idField = `${className}Id`;
- let srcId = data[idField]; // 使用 classnameId 作为 srcId 的值
- // 查重:查找是否已经存在相同 srcId 的数据
- let query = new CloudQuery(className);
- query.equalTo("srcId", srcId);
- let importObj = await query.first();
- console.log(importObj);
- // 导入前批量处理 Pointer 类型数据,进行重定向
- Object.keys(data)?.forEach(key => {
- let field = data[key];
- let fieldSrcId = field?.objectId || field?.OptionId || field?.QuestionId || field?.UserInterestProfileId;
- if (fieldSrcId) { // 是 Pointer 类型
- if (key === "userId" || key === "questionnaireId" || key === "questionId") {
- // 确保插入的是一个 Pointer 对象,而不是直接的 id
- data[key] = {
- "__type": "Pointer",
- "className": key.replace("Id", ""),
- "objectId": DataMap?.[key.replace("Id", "")]?.[fieldSrcId]?.id || fieldSrcId
- };
- } else if (Array.isArray(field)) {
- data[key] = field.map(item => {
- return {
- "__type": "Pointer",
- "className": item.className,
- "objectId": DataMap?.[item.className]?.[item.objectId]?.id || item.objectId
- };
- });
- }
- }
- });
- // 若未添加,则创建新对象并保存
- if (!importObj?.id) {
- importObj = new CloudObject(className);
- }
- // 保存或更新数据
- data.srcId = srcId; // 将 srcId 设置为与 classnameId 相同的值
- importObj.set(data);
- importObj = await importObj.save();
- // 更新 DataMap,确保 srcId 和 classnameId 保持一致
- DataMap[className][srcId] = importObj;
- }
- // 展示当前问卷内的问题及其选项
- async function displayQuestionnaireDetails(questionnaireId) {
- // 查询问卷
- let questionnaireQuery = new CloudQuery("Questionnaire");
- questionnaireQuery.equalTo("objectId", questionnaireId);
- let questionnaire = await questionnaireQuery.first();
- // 检查问卷是否存在
- if (!questionnaire) {
- console.log("问卷未找到,无法展示问题和选项。");
- return;
- }
- console.log(`问卷标题: ${questionnaire.title}`);
- console.log(`问卷状态: ${questionnaire.status}`);
- console.log("问题列表:");
- // 查询问题
- let questionQuery = new CloudQuery("Question");
- questionQuery.equalTo("questionnaireId", {
- "__type": "Pointer",
- "className": "Questionnaire",
- "objectId": questionnaireId
- });
- let questions = await questionQuery.find();
- // 检查是否有问题
- if (questions.length === 0) {
- console.log("当前问卷没有问题。");
- return;
- }
- // 遍历问题并查询选项
- for (let question of questions) {
- // 检查问题是否存在
- if (!question) {
- console.log("问题未找到,无法展示选项。");
- continue; // 跳过当前循环,继续下一个问题
- }
- console.log(` 问题: ${question.questionText}`);
- // 查询选项
- let optionQuery = new CloudQuery("Option");
- optionQuery.equalTo("questionId", {
- "__type": "Pointer",
- "className": "Question",
- "objectId": question.objectId
- });
- let options = await optionQuery.find();
- // 输出选项
- console.log(" 选项:");
- if (options.length === 0) {
- console.log(" - 此问题没有选项。");
- } else {
- for (let option of options) {
- console.log(` - ${option.optionText} (已选: ${option.isSelected})`);
- }
- }
- console.log(""); // 添加空行以便于阅读
- }
- }
- // 示例调用
- displayQuestionnaireDetails("jbg6EYXk3G");
- */
- const { CloudQuery, CloudObject } = require("../lib/ncloud");
- const { QuestionnaireResultList, QuestionnaireList, QuestionList, OptionList, UserInterestProfileList } = require("./data");
- //importData();
- let DataMap = {
- QuestionnaireResult: {},
- Questionnaire: {},
- Question: {},
- Option: {},
- UserInterestProfile: {}
- };
- async function importData() {
- // 确保 UserInterestProfileList 已经加载
- if (!UserInterestProfileList || UserInterestProfileList.length === 0) {
- console.log("UserInterestProfileList 数据为空或未定义!");
- return;
- }
- // 导入问卷结果数据
- let questionnaireResultList = QuestionnaireResultList;
- for (let index = 0; index < questionnaireResultList.length; index++) {
- let result = questionnaireResultList[index];
- result = await importObject("QuestionnaireResult", result);
- }
- // 导入问卷数据
- let questionnaireList = QuestionnaireList;
- for (let index = 0; index < questionnaireList.length; index++) {
- let questionnaire = questionnaireList[index];
- questionnaire = await importObject("Questionnaire", questionnaire);
- }
- // 导入问题数据
- let questionList = QuestionList;
- for (let index = 0; index < questionList.length; index++) {
- let question = questionList[index];
- question = await importObject("Question", question);
- }
- // 导入选项数据
- let optionList = OptionList;
- for (let index = 0; index < optionList.length; index++) {
- let option = optionList[index];
- option = await importObject("Option", option);
- }
- // 导入用户兴趣画像数据
- let userInterestProfileList = UserInterestProfileList;
- for (let index = 0; index < userInterestProfileList.length; index++) {
- let profile = userInterestProfileList[index];
- profile = await importObject("UserInterestProfile", profile);
- }
- // console.log(DataMap);
- }
- async function importObject(className, data) {
- // 获取类名对应的 ID 字段名称(如 OptionId, QuestionnaireId 等)
- let idField = `${className}Id`;
- let srcId = data[idField]; // 使用 classnameId 作为 srcId 的值
- // 查重:查找是否已经存在相同 srcId 的数据
- let query = new CloudQuery(className);
- query.equalTo("srcId", srcId);
- let importObj = await query.first();
- console.log(importObj);
- // 导入前批量处理 Array 类型数据,进行重定向
- Object.keys(data)?.forEach(key => {
- let field = data[key];
- let fieldSrcId = field?.objectId || field?.OptionId || field?.QuestionId || field?.UserInterestProfileId;
- if (fieldSrcId) { // 是 Pointer 类型的字段
- if (key === "userIds" || key === "questionnaireIds" || key === "questionIds") {
- // 确保插入的是一个数组而不是 Pointer 对象
- data[key] = Array.isArray(field) ? field.map(item => {
- return {
- className: item.className,
- srcId: DataMap?.[item.className]?.[item.srcId]?.id || item.srcId
- };
- }) : [{
- className: key.replace("Ids", ""),
- srcId: DataMap?.[key.replace("Ids", "")]?.[fieldSrcId]?.id || fieldSrcId
- }];
- } else if (Array.isArray(field)) {
- data[key] = field.map(item => {
- return {
- className: item.className,
- srcId: DataMap?.[item.className]?.[item.srcId]?.id || item.srcId
- };
- });
- }
- }
- });
- // 若未添加,则创建新对象并保存
- if (!importObj?.id) {
- importObj = new CloudObject(className);
- }
- // 保存或更新数据
- data.srcId = srcId; // 将 srcId 设置为与 classnameId 相同的值
- importObj.set(data);
- importObj = await importObj.save();
- // 更新 DataMap,确保 srcId 和 classnameId 保持一致
- DataMap[className][srcId] = importObj;
- }
- // 展示当前问卷内的问题及其选项
- async function displayQuestionnaireDetails(questionnaireId) {
- // 查询问卷
- let questionnaireQuery = new CloudQuery("Questionnaire");
- questionnaireQuery.equalTo("QuestionnaireId", questionnaireId); // 使用 QuestionnaireId 查询
- let questionnaire = await questionnaireQuery.first();
- // 检查问卷是否存在
- if (!questionnaire) {
- console.log("问卷未找到,无法展示问题和选项。");
- return;
- }
- console.log(`问卷标题: ${questionnaire.get('title')}`);
- console.log(`问卷状态: ${questionnaire.get('status')}`);
- console.log("问题列表:");
- // 获取问题 IDs(数组字段)
- let questionIds = questionnaire.get('questions');
- // 查询所有相关问题
- let questionQuery = new CloudQuery("Question");
- questionQuery.containedIn("QuestionId", questionIds); // 查询 QuestionId 在问题 ID 列表中的问题
- let questions = await questionQuery.find();
- // 检查是否有问题
- if (questions.length === 0) {
- console.log("当前问卷没有问题。");
- return;
- }
- // 遍历问题并查询选项
- for (let question of questions) {
- // 检查问题是否存在
- if (!question) {
- console.log("问题未找到,无法展示选项。");
- continue; // 跳过当前循环,继续下一个问题
- }
- console.log(` 问题: ${question.questionText}`);
- // 获取问题中的选项 IDs(数组字段)
- let optionIds = question.options;
- // 查询所有相关选项
- let optionQuery = new CloudQuery("Option");
- optionQuery.containedIn("OptionId", optionIds); // 查询 OptionId 在选项 ID 列表中的选项
- let options = await optionQuery.find();
- // 输出选项
- console.log(" 选项:");
- if (options.length === 0) {
- console.log(" - 此问题没有选项。");
- } else {
- for (let option of options) {
- console.log(` - ${option.optionText} (已选: ${option.isSelected ? "是" : "否"})`);
- }
- }
- console.log(""); // 添加空行以便于阅读
- }
- }
- // 示例调用
- displayQuestionnaireDetails("test_q1");
|