lrf 2 rokov pred
rodič
commit
5ee8cf020a

+ 40 - 0
app/controller/.dir.js

@@ -0,0 +1,40 @@
+module.exports = {
+  create: {
+    requestBody: ['name', 'project', 'super', 'sort', 'remark'],
+  },
+  destroy: {
+    params: ['!id'],
+    service: 'delete',
+  },
+  update: {
+    params: ['!id'],
+    requestBody: ['name', 'project', 'super', 'sort', 'remark'],
+  },
+  show: {
+    parameters: {
+      params: ['!id'],
+    },
+    service: 'fetch',
+  },
+  index: {
+    parameters: {
+      query: {
+        name: 'name',
+        project: 'project',
+        super: 'super',
+        'meta.createdAt@start': 'meta.createdAt@start',
+        'meta.createdAt@end': 'meta.createdAt@end',
+      },
+      // options: {
+      //   "meta.state": 0 // 默认条件
+      // },
+    },
+    service: 'query',
+    options: {
+      query: ['skip', 'limit'],
+      sort: ['sort', 'meta.createdAt'],
+      desc: true,
+      count: true,
+    },
+  },
+};

+ 3 - 4
app/controller/.table.js

@@ -1,6 +1,6 @@
 module.exports = {
   create: {
-    requestBody: ['!name_zh', '!name', '!project', 'columns', 'remark'],
+    requestBody: ['!name_zh', '!name', '!project', 'columns', 'sort', 'remark'],
   },
   destroy: {
     params: ['!id'],
@@ -8,7 +8,7 @@ module.exports = {
   },
   update: {
     params: ['!id'],
-    requestBody: ['!name_zh', '!name', '!project', 'columns', 'remark'],
+    requestBody: ['!name_zh', '!name', '!project', 'columns', 'sort', 'remark'],
   },
   show: {
     parameters: {
@@ -32,9 +32,8 @@ module.exports = {
     service: 'query',
     options: {
       query: ['skip', 'limit'],
-      sort: ['meta.createdAt'],
+      sort: ['sort', 'meta.createdAt'],
       desc: true,
-      count: true,
     },
   },
   toExport: {

+ 13 - 0
app/controller/dir.js

@@ -0,0 +1,13 @@
+'use strict';
+const meta = require('./.dir.js');
+const Controller = require('egg').Controller;
+const { CrudController } = require('naf-framework-mongoose-free/lib/controller');
+
+// 
+class DirController extends Controller {
+  constructor(ctx) {
+    super(ctx);
+    this.service = this.ctx.service.dir;
+  }
+}
+module.exports = CrudController(DirController, meta);

+ 22 - 0
app/model/dir.js

@@ -0,0 +1,22 @@
+'use strict';
+const Schema = require('mongoose').Schema;
+const metaPlugin = require('naf-framework-mongoose-free/lib/model/meta-plugin');
+const { ObjectId } = require('mongoose').Types;
+// 文件夹表
+const dir = {
+  name: { type: String },
+  project: { type: String },
+  super: { type: String },
+  sort: { type: Number, default: 0 },
+  remark: { type: String },
+};
+const schema = new Schema(dir, { toJSON: { virtuals: true } });
+schema.index({ id: 1 });
+schema.index({ super: 1 });
+schema.index({ sort: 1 });
+schema.index({ 'meta.createdAt': 1 });
+schema.plugin(metaPlugin);
+module.exports = (app) => {
+  const { mongoose } = app;
+  return mongoose.model('Dir', schema, 'dir');
+};

+ 3 - 0
app/model/table.js

@@ -24,14 +24,17 @@ const column = new Schema(
 const table = {
   name: { type: String, required: true }, // 表名
   name_zh: { type: String, required: true }, // 表中文名
+  dir: { type: String }, // 文件夹
   project: { type: ObjectId, required: true }, // 项目
   columns: [column], // 字段列表
+  sort: { type: Number, default: 0 },
   remark: { type: String },
 };
 const schema = new Schema(table, { toJSON: { virtuals: true } });
 schema.index({ id: 1 });
 schema.index({ name: 1 });
 schema.index({ project: 1 });
+schema.index({ sort: 1 });
 schema.index({ 'meta.createdAt': 1 });
 schema.plugin(metaPlugin);
 module.exports = (app) => {

+ 1 - 0
app/router.js

@@ -10,4 +10,5 @@ module.exports = app => {
   router.post(`${prefix}/options`, controller.options.index);
   require('./z_router/project')(app); // 项目
   require('./z_router/table')(app); // 表
+  require('./z_router/dir')(app); // 文件夹
 };

+ 15 - 0
app/service/dir.js

@@ -0,0 +1,15 @@
+'use strict';
+const { CrudService } = require('naf-framework-mongoose-free/lib/service');
+const { BusinessError, ErrorCode } = require('naf-core').Error;
+const _ = require('lodash');
+const assert = require('assert');
+
+// 
+class DirService extends CrudService {
+  constructor(ctx) {
+    super(ctx, 'dir');
+    this.model = this.ctx.model.Dir;
+  }
+}
+
+module.exports = DirService;

+ 63 - 2
app/service/table.js

@@ -2,14 +2,75 @@
 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 test(data) {
-    console.log(data);
+  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 = {};

+ 9 - 0
app/z_router/dir.js

@@ -0,0 +1,9 @@
+'use strict';
+
+module.exports = app => {
+  const { router, controller, config } = app;
+  const target = 'dir';
+  const prefix = config.routePrefix;
+  router.resources(target, `${prefix}/${target}`, controller[target]); // index、create、show、destroy
+  router.post(target, `${prefix}/${target}/:id`, controller[target].update);
+};