JsonImporter.template.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. istesting = ___istesting___;
  17. function teststore() {
  18. // 输入JSON临时存入DFS
  19. dir = "/opt___classname___";
  20. dtm = new Date().toJSON().replace(/[\-\:\.TZ ]/mg, "");
  21. fn = dir + "/" + dtm;
  22. if (input.id) {
  23. fn += "." + input.id;
  24. } else {
  25. fn += "_" + _.random(0, 1000000);
  26. }
  27. fn += ".json";
  28. dfs.write(fn, JSON.stringify(input, " ", 4));
  29. output.filename = fn;
  30. // 刪除旧交件
  31. files = dfs.readdir(dir);
  32. dt = Date.now() - 1000 * 3600 * 24 * 10;
  33. for (i = 0; i < files.length; i++) {
  34. if (files[i].mtime < dt) {
  35. dfs.remove(files[i].fullname);
  36. }
  37. }
  38. // 留最后10个
  39. files.sort(function(a,b){ return a.mtime-b.mtime; });
  40. for (i = 0; i < files.length-10; i++) {
  41. dfs.remove(files[i].fullname);
  42. }
  43. }
  44. // 主执行阶段
  45. try {
  46. if (istesting) {
  47. teststore();
  48. }
  49. // 数据合法性检查
  50. if (___datacheck___) {}
  51. // mql定义
  52. mql = `___mql___`;
  53. // 执行mql
  54. ret = odb.mql(mql, ___values___);
  55. // 打印完成信息
  56. output.info=___datainfo___;
  57. log.info(output.info);
  58. } catch(e) {
  59. if (typeof(e) == "object") {
  60. output.error = e;
  61. } else if (typeof(e) == "string") {
  62. output.error = "插库错误:" + e;
  63. } else {
  64. output.error = JSON.stringify(e);
  65. }
  66. log.error(output.error);
  67. }
  68. // 返回输出信息
  69. OUTPUT = output;