JsonImporter.template.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // ___classname___ JSON数据导入程序
  2. // ___datetime_now___ 由 JsonImportGen.js 自动生成,请勿手动修改
  3. // 输入输出参数格式化
  4. input = INPUT;
  5. try {
  6. input = decodeURIComponent(input);
  7. } catch (e) { }
  8. try {
  9. input = base64.decode(input);
  10. } catch (e) { }
  11. try {
  12. input = JSON.parse(input);
  13. input = JSON.parse(input);
  14. } catch (e) { }
  15. output = {};
  16. testcfg = {};
  17. try {
  18. testcfg = JSON.parse(etcd.get("/api/test.json"));
  19. } catch (e) { }
  20. function teststoredfs() {
  21. // 输入JSON临时存入DFS
  22. dir = "/opt___classname___";
  23. dtm = new Date().toJSON().replace(/[\-\:\.TZ ]/mg, "");
  24. fn = dir + "/" + dtm;
  25. if (input.id) {
  26. fn += "." + input.id;
  27. } else {
  28. fn += "_" + _.random(0, 1000000);
  29. }
  30. fn += ".json";
  31. dfs.write(fn, JSON.stringify(input, " ", 4));
  32. output.filename = fn;
  33. // 刪除旧交件
  34. files = dfs.readdir(dir);
  35. dt = Date.now() - 1000 * 3600 * 24 * 10;
  36. for (i = 0; i < files.length; i++) {
  37. if (files[i].mtime < dt) {
  38. dfs.remove(files[i].fullname);
  39. }
  40. }
  41. // 留最后10个
  42. files.sort(function (a, b) { return a.mtime - b.mtime; });
  43. for (i = 0; i < files.length - 10; i++) {
  44. dfs.remove(files[i].fullname);
  45. }
  46. }
  47. // 主执行阶段
  48. try {
  49. if (!input["--testing--"]) {
  50. if (testcfg.stoedfs) {
  51. teststoredfs();
  52. }
  53. if (testcfg.checkfields) {
  54. eval(dfs.read("/script/matrix/utils/ajs/checkfields.js"));
  55. if (!checkfields(input, ___field_map___)) {
  56. eval(dfs.read("/script/matrix/utils/ajs/JsonImporterFuncs.js"));
  57. output = runServerJS("/matrix/utils/ajs/InitJsonImporter.js", {classname: "___classname___"});
  58. throw("ok");
  59. }
  60. }
  61. }
  62. // 数据合法性检查
  63. if (___datacheck___) { }
  64. // mql定义
  65. mql = `___mql___`;
  66. // 执行mql
  67. ret = odb.mql(mql, ___values___);
  68. // 打印完成信息
  69. output.info = ___datainfo___;
  70. log.info(output.info);
  71. } catch (e) {
  72. if (e != "ok") {
  73. if (typeof (e) == "object") {
  74. output.error = e;
  75. } else if (typeof (e) == "string") {
  76. output.error = "插库错误:" + e;
  77. } else {
  78. output.error = JSON.stringify(e);
  79. }
  80. log.error(output.error);
  81. }
  82. }
  83. // 返回输出信息
  84. OUTPUT = output;