table.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. console.log('line 11 in function:');
  15. const project = _.get(query, 'project');
  16. let dirs = [];
  17. if (project) {
  18. dirs = await this.dirModel.find({ project }, { name: 1, super: 1, project: 1, remark: 1, type: 'dir' }).lean();
  19. }
  20. const datas = await this.model.find(query, { name: 1, name_zh: 1, project: 1, dir: 1, type: 'table' }).lean();
  21. const newData = this.compareList(dirs, datas);
  22. return newData;
  23. }
  24. /**
  25. * 组合显示列表
  26. * @param {Array} dirs 文件夹
  27. * @param {Array} tables 表数据
  28. */
  29. compareList(dirs, tables) {
  30. const result = [];
  31. const noDirTables = tables.filter((f) => !f.dir);
  32. const inDirTables = tables.filter((f) => f.dir);
  33. const firstLevelDir = dirs.filter((f) => !f.super);
  34. const inDirDirs = dirs.filter((f) => f.super);
  35. let newDir = this.makeDir(firstLevelDir, inDirDirs);
  36. this.compareTablesForDir(newDir, tables);
  37. result.push(...newDir, ...noDirTables);
  38. return result;
  39. }
  40. /**
  41. * 整理文件夹数据
  42. * @param {Array} thisLevel 当前层级文件夹
  43. * @param {Array} others 其他所属文件夹
  44. */
  45. makeDir(thisLevel, others) {
  46. for (const tl of thisLevel) {
  47. const _id = tl._id;
  48. let children = others.filter((f) => ObjectId(_id).equals(f.super));
  49. if (children.length <= 0) continue;
  50. const newOthers = others.filter((f) => children.find((cf) => !ObjectId(cf._id).equals(f._id)));
  51. children = this.makeDir(children, newOthers);
  52. tl.children = children;
  53. }
  54. return thisLevel;
  55. }
  56. /**
  57. * 组合文件夹和表
  58. * @param {Array} dirs 文件夹
  59. * @param {Array} tables 表数据
  60. */
  61. compareTablesForDir(dirs, tables) {
  62. for (const dir of dirs) {
  63. const { _id, children = [] } = dir;
  64. const dts = tables.filter((f) => ObjectId(f.dir).equals(_id));
  65. if (children.length <= 0) {
  66. children.push(...dts);
  67. dir.children = children;
  68. continue;
  69. } else {
  70. dir.children.push(...dts);
  71. }
  72. const othersTables = tables.filter((f) => !ObjectId(f.dir).equals(_id));
  73. dir.children = this.compareTablesForDir(children, othersTables);
  74. }
  75. return dirs;
  76. }
  77. async toExport({ ids }) {
  78. const res = await this.model.find({ _id: { $in: ids } });
  79. const data = {};
  80. for (const i of res) {
  81. const d = template(i);
  82. data[i.name_zh ? i.name_zh : i.name] = d;
  83. }
  84. return data;
  85. }
  86. async toExportTS({ ids }) {
  87. const res = await this.model.find({ _id: { $in: ids } }).lean();
  88. const data = {};
  89. for (const i of res) {
  90. const d = TSTemplate(i);
  91. data[i.name_zh ? i.name_zh : i.name] = d;
  92. }
  93. return data;
  94. }
  95. }
  96. module.exports = TableService;