table.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. dir.tables = dts;
  66. if (children.length <= 0) continue;
  67. const othersTables = tables.filter((f) => !ObjectId(f.dir).equals(_id));
  68. dir.children = this.compareTablesForDir(children, othersTables);
  69. }
  70. return dirs;
  71. }
  72. async toExport({ ids }) {
  73. const res = await this.model.find({ _id: { $in: ids } });
  74. const data = {};
  75. for (const i of res) {
  76. const d = template(i);
  77. data[i.name_zh ? i.name_zh : i.name] = d;
  78. }
  79. return data;
  80. }
  81. async toExportTS({ ids }) {
  82. const res = await this.model.find({ _id: { $in: ids } }).lean();
  83. const data = {};
  84. for (const i of res) {
  85. const d = TSTemplate(i);
  86. data[i.name_zh ? i.name_zh : i.name] = d;
  87. }
  88. return data;
  89. }
  90. }
  91. module.exports = TableService;