table.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. 'use strict';
  2. const { CrudService } = require('naf-framework-mongoose-free/lib/service');
  3. const template = require('../public/table-template');
  4. const TSTemplate = require('../public/ts-template');
  5. const _ = require('lodash');
  6. const { ObjectId } = require('mongoose').Types;
  7. class TableService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'table');
  10. this.model = this.ctx.model.Table;
  11. this.dirModel = this.ctx.model.Dir;
  12. }
  13. async query(query) {
  14. const project = _.get(query, 'project');
  15. let dirs = [];
  16. if (project) {
  17. dirs = await this.dirModel.find({ project }, { name: 1, super: 1, project: 1, remark: 1, type: 'dir' }).lean();
  18. }
  19. const datas = await this.model.find(query, { name: 1, name_zh: 1, project: 1, dir: 1, type: 'table' }).lean();
  20. const newData = this.compareList(dirs, datas);
  21. return newData;
  22. }
  23. /**
  24. * 组合显示列表
  25. * @param {Array} dirs 文件夹
  26. * @param {Array} tables 表数据
  27. */
  28. compareList(dirs, tables) {
  29. const result = [];
  30. const noDirTables = tables.filter((f) => !f.dir);
  31. const firstLevelDir = dirs.filter((f) => !f.super);
  32. const inDirDirs = dirs.filter((f) => f.super);
  33. let newDir = this.makeDir(firstLevelDir, inDirDirs);
  34. this.compareTablesForDir(newDir, tables);
  35. result.push(...newDir, ...noDirTables);
  36. return result;
  37. }
  38. /**
  39. * 整理文件夹数据
  40. * @param {Array} thisLevel 当前层级文件夹
  41. * @param {Array} others 其他所属文件夹
  42. */
  43. makeDir(thisLevel, others) {
  44. for (const tl of thisLevel) {
  45. const _id = tl._id;
  46. let children = others.filter((f) => ObjectId(_id).equals(f.super));
  47. if (children.length <= 0) continue;
  48. const newOthers = others.filter((f) => children.find((cf) => !ObjectId(cf._id).equals(f._id)));
  49. children = this.makeDir(children, newOthers);
  50. tl.children = children;
  51. }
  52. return thisLevel;
  53. }
  54. /**
  55. * 组合文件夹和表
  56. * @param {Array} dirs 文件夹
  57. * @param {Array} tables 表数据
  58. */
  59. compareTablesForDir(dirs, tables) {
  60. for (const dir of dirs) {
  61. const { _id, children = [] } = dir;
  62. const dts = tables.filter((f) => ObjectId(f.dir).equals(_id));
  63. if (children.length <= 0) {
  64. children.push(...dts);
  65. dir.children = children;
  66. continue;
  67. } else {
  68. dir.children.push(...dts);
  69. }
  70. const othersTables = tables.filter((f) => !ObjectId(f.dir).equals(_id));
  71. dir.children = this.compareTablesForDir(children, othersTables);
  72. }
  73. return dirs;
  74. }
  75. async toExport({ ids }) {
  76. const res = await this.model.find({ _id: { $in: ids } });
  77. const data = {};
  78. for (const i of res) {
  79. const d = template(i);
  80. data[i.name_zh ? i.name_zh : i.name] = d;
  81. }
  82. return data;
  83. }
  84. /**
  85. * 整理文件夹路径数组(一维)
  86. * @param {Array} dirs 递归文件夹数据
  87. * @param {String} prefix 前缀
  88. */
  89. getDirPath(dirs, prefix = '', idPath = []) {
  90. const list = [];
  91. for (const dir of dirs) {
  92. const { children = [], name, _id } = dir;
  93. let path = `${prefix}/${name}`;
  94. let idp = [...idPath, ObjectId(_id).toString()];
  95. let obj = { path, idPath: idp };
  96. list.push(obj);
  97. if (children.length > 0) {
  98. let ml = this.getDirPath(children, path, idp);
  99. list.push(...ml);
  100. }
  101. }
  102. return list;
  103. }
  104. async toExportTS({ ids }) {
  105. const res = await this.model.find({ _id: { $in: ids } }).lean();
  106. const project = res.find((f) => f.project && f.dir);
  107. let dirs = [];
  108. let dirPaths = [];
  109. if (project) {
  110. dirs = await this.dirModel.find({ project: project.project }).lean();
  111. const firstLevelDir = dirs.filter((f) => !f.super);
  112. const inDirDirs = dirs.filter((f) => f.super);
  113. let newDir = this.makeDir(firstLevelDir, inDirDirs);
  114. dirPaths = this.getDirPath(newDir);
  115. }
  116. const data = {};
  117. for (const i of res) {
  118. const { dir, name } = i;
  119. if (dir) {
  120. const dirObj = dirPaths.find((f) => _.last(f.idPath) === dir);
  121. if (dirObj) {
  122. let str = '../';
  123. const levelNumber = dirObj.idPath.length || 0;
  124. for (let i = 0; i < levelNumber; i++) {
  125. str = `${str}../`;
  126. }
  127. i.getPath = (type) => `${str}${type}${dirObj.path}/${_.lowerFirst(name)}.${type}`;
  128. }
  129. } else i.getPath = (type) => `../${type}/${_.lowerFirst(name)}.${type}`;
  130. const d = TSTemplate(i);
  131. data[i.name_zh ? i.name_zh : i.name] = d;
  132. }
  133. return data;
  134. }
  135. }
  136. module.exports = TableService;