1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- 'use strict';
- const { CrudService } = require('naf-framework-mongoose-free/lib/service');
- const template = require('../public/table-template');
- const TSTemplate = require('../public/ts-template');
- const _ = require('lodash');
- const { ObjectId } = require('mongoose').Types;
- class TableService extends CrudService {
- constructor(ctx) {
- super(ctx, 'table');
- this.model = this.ctx.model.Table;
- this.dirModel = this.ctx.model.Dir;
- }
- async query(query) {
- console.log('line 11 in function:');
- const project = _.get(query, 'project');
- let dirs = [];
- if (project) {
- dirs = await this.dirModel.find({ project }, { name: 1, super: 1, project: 1, remark: 1, type: 'dir' }).lean();
- }
- const datas = await this.model.find(query, { name: 1, name_zh: 1, project: 1, dir: 1, type: 'table' }).lean();
- const newData = this.compareList(dirs, datas);
- return newData;
- }
- /**
- * 组合显示列表
- * @param {Array} dirs 文件夹
- * @param {Array} tables 表数据
- */
- compareList(dirs, tables) {
- const result = [];
- const noDirTables = tables.filter((f) => !f.dir);
- const inDirTables = tables.filter((f) => f.dir);
- const firstLevelDir = dirs.filter((f) => !f.super);
- const inDirDirs = dirs.filter((f) => f.super);
- let newDir = this.makeDir(firstLevelDir, inDirDirs);
- this.compareTablesForDir(newDir, tables);
- result.push(...newDir, ...noDirTables);
- return result;
- }
- /**
- * 整理文件夹数据
- * @param {Array} thisLevel 当前层级文件夹
- * @param {Array} others 其他所属文件夹
- */
- makeDir(thisLevel, others) {
- for (const tl of thisLevel) {
- const _id = tl._id;
- let children = others.filter((f) => ObjectId(_id).equals(f.super));
- if (children.length <= 0) continue;
- const newOthers = others.filter((f) => children.find((cf) => !ObjectId(cf._id).equals(f._id)));
- children = this.makeDir(children, newOthers);
- tl.children = children;
- }
- return thisLevel;
- }
- /**
- * 组合文件夹和表
- * @param {Array} dirs 文件夹
- * @param {Array} tables 表数据
- */
- compareTablesForDir(dirs, tables) {
- for (const dir of dirs) {
- const { _id, children = [] } = dir;
- const dts = tables.filter((f) => ObjectId(f.dir).equals(_id));
- dir.tables = dts;
- if (children.length <= 0) continue;
- const othersTables = tables.filter((f) => !ObjectId(f.dir).equals(_id));
- dir.children = this.compareTablesForDir(children, othersTables);
- }
- return dirs;
- }
- async toExport({ ids }) {
- const res = await this.model.find({ _id: { $in: ids } });
- const data = {};
- for (const i of res) {
- const d = template(i);
- data[i.name_zh ? i.name_zh : i.name] = d;
- }
- return data;
- }
- async toExportTS({ ids }) {
- const res = await this.model.find({ _id: { $in: ids } }).lean();
- const data = {};
- for (const i of res) {
- const d = TSTemplate(i);
- data[i.name_zh ? i.name_zh : i.name] = d;
- }
- return data;
- }
- }
- module.exports = TableService;
|