import-data.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*const { CloudQuery, CloudObject } = require("../lib/ncloud");
  2. const { QuestionnaireResultList, QuestionnaireList, QuestionList, OptionList, UserInterestProfileList } = require("./data");
  3. importData();
  4. let DataMap = {
  5. QuestionnaireResult: {},
  6. Questionnaire: {},
  7. Question: {},
  8. Option: {},
  9. UserInterestProfile: {}
  10. }
  11. async function importData() {
  12. // 导入问卷结果数据
  13. let questionnaireResultList = QuestionnaireResultList;
  14. for (let index = 0; index < questionnaireResultList.length; index++) {
  15. let result = questionnaireResultList[index];
  16. result = await importObject("QuestionnaireResult", result);
  17. }
  18. // 导入问卷数据
  19. let questionnaireList = QuestionnaireList;
  20. for (let index = 0; index < questionnaireList.length; index++) {
  21. let questionnaire = questionnaireList[index];
  22. questionnaire = await importObject("Questionnaire", questionnaire);
  23. }
  24. // 导入问题数据
  25. let questionList = QuestionList;
  26. for (let index = 0; index < questionList.length; index++) {
  27. let question = questionList[index];
  28. question = await importObject("Question", question);
  29. }
  30. // 导入选项数据
  31. let optionList = OptionList;
  32. for (let index = 0; index < optionList.length; index++) {
  33. let option = optionList[index];
  34. option = await importObject("Option", option);
  35. }
  36. // 导入用户兴趣画像数据
  37. let userInterestProfileList = UserInterestProfileList;
  38. for (let index = 0; index < userInterestProfileList.length; index++) {
  39. let profile = userInterestProfileList[index];
  40. profile = await importObject("UserInterestProfile", profile);
  41. }
  42. // console.log(DataMap)
  43. }
  44. async function importObject(className, data) {
  45. // 获取类名对应的 ID 字段名称(如 OptionId, QuestionnaireId 等)
  46. let idField = `${className}Id`;
  47. let srcId = data[idField]; // 使用 classnameId 作为 srcId 的值
  48. // 查重:查找是否已经存在相同 srcId 的数据
  49. let query = new CloudQuery(className);
  50. query.equalTo("srcId", srcId);
  51. let importObj = await query.first();
  52. console.log(importObj);
  53. // 导入前批量处理 Pointer 类型数据,进行重定向
  54. Object.keys(data)?.forEach(key => {
  55. let field = data[key];
  56. let fieldSrcId = field?.objectId || field?.OptionId || field?.QuestionId || field?.UserInterestProfileId;
  57. if (fieldSrcId) { // 是 Pointer 类型
  58. if (key === "userId" || key === "questionnaireId" || key === "questionId") {
  59. // 确保插入的是一个 Pointer 对象,而不是直接的 id
  60. data[key] = {
  61. "__type": "Pointer",
  62. "className": key.replace("Id", ""),
  63. "objectId": DataMap?.[key.replace("Id", "")]?.[fieldSrcId]?.id || fieldSrcId
  64. };
  65. } else if (Array.isArray(field)) {
  66. data[key] = field.map(item => {
  67. return {
  68. "__type": "Pointer",
  69. "className": item.className,
  70. "objectId": DataMap?.[item.className]?.[item.objectId]?.id || item.objectId
  71. };
  72. });
  73. }
  74. }
  75. });
  76. // 若未添加,则创建新对象并保存
  77. if (!importObj?.id) {
  78. importObj = new CloudObject(className);
  79. }
  80. // 保存或更新数据
  81. data.srcId = srcId; // 将 srcId 设置为与 classnameId 相同的值
  82. importObj.set(data);
  83. importObj = await importObj.save();
  84. // 更新 DataMap,确保 srcId 和 classnameId 保持一致
  85. DataMap[className][srcId] = importObj;
  86. }
  87. // 展示当前问卷内的问题及其选项
  88. async function displayQuestionnaireDetails(questionnaireId) {
  89. // 查询问卷
  90. let questionnaireQuery = new CloudQuery("Questionnaire");
  91. questionnaireQuery.equalTo("objectId", questionnaireId);
  92. let questionnaire = await questionnaireQuery.first();
  93. // 检查问卷是否存在
  94. if (!questionnaire) {
  95. console.log("问卷未找到,无法展示问题和选项。");
  96. return;
  97. }
  98. console.log(`问卷标题: ${questionnaire.title}`);
  99. console.log(`问卷状态: ${questionnaire.status}`);
  100. console.log("问题列表:");
  101. // 查询问题
  102. let questionQuery = new CloudQuery("Question");
  103. questionQuery.equalTo("questionnaireId", {
  104. "__type": "Pointer",
  105. "className": "Questionnaire",
  106. "objectId": questionnaireId
  107. });
  108. let questions = await questionQuery.find();
  109. // 检查是否有问题
  110. if (questions.length === 0) {
  111. console.log("当前问卷没有问题。");
  112. return;
  113. }
  114. // 遍历问题并查询选项
  115. for (let question of questions) {
  116. // 检查问题是否存在
  117. if (!question) {
  118. console.log("问题未找到,无法展示选项。");
  119. continue; // 跳过当前循环,继续下一个问题
  120. }
  121. console.log(` 问题: ${question.questionText}`);
  122. // 查询选项
  123. let optionQuery = new CloudQuery("Option");
  124. optionQuery.equalTo("questionId", {
  125. "__type": "Pointer",
  126. "className": "Question",
  127. "objectId": question.objectId
  128. });
  129. let options = await optionQuery.find();
  130. // 输出选项
  131. console.log(" 选项:");
  132. if (options.length === 0) {
  133. console.log(" - 此问题没有选项。");
  134. } else {
  135. for (let option of options) {
  136. console.log(` - ${option.optionText} (已选: ${option.isSelected})`);
  137. }
  138. }
  139. console.log(""); // 添加空行以便于阅读
  140. }
  141. }
  142. // 示例调用
  143. displayQuestionnaireDetails("jbg6EYXk3G");
  144. */
  145. const { CloudQuery, CloudObject } = require("../lib/ncloud");
  146. const { QuestionnaireResultList, QuestionnaireList, QuestionList, OptionList, UserInterestProfileList } = require("./data");
  147. //importData();
  148. let DataMap = {
  149. QuestionnaireResult: {},
  150. Questionnaire: {},
  151. Question: {},
  152. Option: {},
  153. UserInterestProfile: {}
  154. };
  155. async function importData() {
  156. // 确保 UserInterestProfileList 已经加载
  157. if (!UserInterestProfileList || UserInterestProfileList.length === 0) {
  158. console.log("UserInterestProfileList 数据为空或未定义!");
  159. return;
  160. }
  161. // 导入问卷结果数据
  162. let questionnaireResultList = QuestionnaireResultList;
  163. for (let index = 0; index < questionnaireResultList.length; index++) {
  164. let result = questionnaireResultList[index];
  165. result = await importObject("QuestionnaireResult", result);
  166. }
  167. // 导入问卷数据
  168. let questionnaireList = QuestionnaireList;
  169. for (let index = 0; index < questionnaireList.length; index++) {
  170. let questionnaire = questionnaireList[index];
  171. questionnaire = await importObject("Questionnaire", questionnaire);
  172. }
  173. // 导入问题数据
  174. let questionList = QuestionList;
  175. for (let index = 0; index < questionList.length; index++) {
  176. let question = questionList[index];
  177. question = await importObject("Question", question);
  178. }
  179. // 导入选项数据
  180. let optionList = OptionList;
  181. for (let index = 0; index < optionList.length; index++) {
  182. let option = optionList[index];
  183. option = await importObject("Option", option);
  184. }
  185. // 导入用户兴趣画像数据
  186. let userInterestProfileList = UserInterestProfileList;
  187. for (let index = 0; index < userInterestProfileList.length; index++) {
  188. let profile = userInterestProfileList[index];
  189. profile = await importObject("UserInterestProfile", profile);
  190. }
  191. // console.log(DataMap);
  192. }
  193. async function importObject(className, data) {
  194. // 获取类名对应的 ID 字段名称(如 OptionId, QuestionnaireId 等)
  195. let idField = `${className}Id`;
  196. let srcId = data[idField]; // 使用 classnameId 作为 srcId 的值
  197. // 查重:查找是否已经存在相同 srcId 的数据
  198. let query = new CloudQuery(className);
  199. query.equalTo("srcId", srcId);
  200. let importObj = await query.first();
  201. console.log(importObj);
  202. // 导入前批量处理 Array 类型数据,进行重定向
  203. Object.keys(data)?.forEach(key => {
  204. let field = data[key];
  205. let fieldSrcId = field?.objectId || field?.OptionId || field?.QuestionId || field?.UserInterestProfileId;
  206. if (fieldSrcId) { // 是 Pointer 类型的字段
  207. if (key === "userIds" || key === "questionnaireIds" || key === "questionIds") {
  208. // 确保插入的是一个数组而不是 Pointer 对象
  209. data[key] = Array.isArray(field) ? field.map(item => {
  210. return {
  211. className: item.className,
  212. srcId: DataMap?.[item.className]?.[item.srcId]?.id || item.srcId
  213. };
  214. }) : [{
  215. className: key.replace("Ids", ""),
  216. srcId: DataMap?.[key.replace("Ids", "")]?.[fieldSrcId]?.id || fieldSrcId
  217. }];
  218. } else if (Array.isArray(field)) {
  219. data[key] = field.map(item => {
  220. return {
  221. className: item.className,
  222. srcId: DataMap?.[item.className]?.[item.srcId]?.id || item.srcId
  223. };
  224. });
  225. }
  226. }
  227. });
  228. // 若未添加,则创建新对象并保存
  229. if (!importObj?.id) {
  230. importObj = new CloudObject(className);
  231. }
  232. // 保存或更新数据
  233. data.srcId = srcId; // 将 srcId 设置为与 classnameId 相同的值
  234. importObj.set(data);
  235. importObj = await importObj.save();
  236. // 更新 DataMap,确保 srcId 和 classnameId 保持一致
  237. DataMap[className][srcId] = importObj;
  238. }
  239. // 展示当前问卷内的问题及其选项
  240. async function displayQuestionnaireDetails(questionnaireId) {
  241. // 查询问卷
  242. let questionnaireQuery = new CloudQuery("Questionnaire");
  243. questionnaireQuery.equalTo("QuestionnaireId", questionnaireId); // 使用 QuestionnaireId 查询
  244. let questionnaire = await questionnaireQuery.first();
  245. // 检查问卷是否存在
  246. if (!questionnaire) {
  247. console.log("问卷未找到,无法展示问题和选项。");
  248. return;
  249. }
  250. console.log(`问卷标题: ${questionnaire.get('title')}`);
  251. console.log(`问卷状态: ${questionnaire.get('status')}`);
  252. console.log("问题列表:");
  253. // 获取问题 IDs(数组字段)
  254. let questionIds = questionnaire.get('questions');
  255. // 查询所有相关问题
  256. let questionQuery = new CloudQuery("Question");
  257. questionQuery.containedIn("QuestionId", questionIds); // 查询 QuestionId 在问题 ID 列表中的问题
  258. let questions = await questionQuery.find();
  259. // 检查是否有问题
  260. if (questions.length === 0) {
  261. console.log("当前问卷没有问题。");
  262. return;
  263. }
  264. // 遍历问题并查询选项
  265. for (let question of questions) {
  266. // 检查问题是否存在
  267. if (!question) {
  268. console.log("问题未找到,无法展示选项。");
  269. continue; // 跳过当前循环,继续下一个问题
  270. }
  271. console.log(` 问题: ${question.questionText}`);
  272. // 获取问题中的选项 IDs(数组字段)
  273. let optionIds = question.options;
  274. // 查询所有相关选项
  275. let optionQuery = new CloudQuery("Option");
  276. optionQuery.containedIn("OptionId", optionIds); // 查询 OptionId 在选项 ID 列表中的选项
  277. let options = await optionQuery.find();
  278. // 输出选项
  279. console.log(" 选项:");
  280. if (options.length === 0) {
  281. console.log(" - 此问题没有选项。");
  282. } else {
  283. for (let option of options) {
  284. console.log(` - ${option.optionText} (已选: ${option.isSelected ? "是" : "否"})`);
  285. }
  286. }
  287. console.log(""); // 添加空行以便于阅读
  288. }
  289. }
  290. // 示例调用
  291. displayQuestionnaireDetails("test_q1");