lrf 2 år sedan
förälder
incheckning
3e65adc6e1
3 ändrade filer med 9 tillägg och 77 borttagningar
  1. 0 57
      app/service/matchSmallGroupSchedule.js
  2. 5 16
      app/service/matchTeamGroup.js
  3. 4 4
      app/service/teamApply.js

+ 0 - 57
app/service/matchSmallGroupSchedule.js

@@ -203,63 +203,6 @@ class MatchSmallGroupScheduleService extends CrudService {
 
     return d;
   }
-
-  getRefMods() {
-    // 找到该表的schema(表结构)
-    const mod = this.getSchema();
-    const populate = this.resetPopulate(mod);
-    const refMods = [];
-    for (const key in mod) {
-      if (!mod[key].ref && !mod[key].refPath) continue;
-      const obj = { col: key, prop: mod[key].getProp, type: mod[key].type.name };
-      refMods.push(obj);
-    }
-    return { refMods, populate };
-  }
-
-  // 针对每个表进行检查
-  resetPopulate(schema) {
-    const arr = [];
-    for (const key in schema) {
-      const e = schema[key];
-      const { ref } = e;
-      if (!ref) continue;
-      const obj = { path: key };
-      const modelPath = this.formatModelPath(ref);
-      const model = this.getModel(modelPath);
-      obj.model = model;
-      const msch = this.getSchema(modelPath);
-      const popu = this.resetPopulate(msch);
-      if (popu.length > 0) obj.populate = popu;
-      arr.push(obj);
-    }
-    return arr;
-  }
-
-  // 格式化model路径
-  formatModelPath(str) {
-    let arr = str.split('.');
-    arr = arr.map(i => _.upperFirst(i));
-    const modelPath = arr.join('.');
-    return modelPath;
-  }
-
-  // 获取model的模式
-  getSchema(path) {
-    const model = this.getModel(path);
-    return _.get(model, 'prototype.schema.obj');
-  }
-
-  // 获取model实例
-  getModel(path) {
-    if (!path) return this.model;
-    let model = _.get(this.ctx.model, path);
-    const clients = this.app.mongooseDB.clients;
-    if (clients && !model) {
-      model = _.get(this.ctx.model, `${this.defaultModule}.${path}`);
-    }
-    return model;
-  }
 }
 
 module.exports = MatchSmallGroupScheduleService;

+ 5 - 16
app/service/matchTeamGroup.js

@@ -36,16 +36,14 @@ class MatchTeamGroupService extends CrudService {
     if (person_type === 'TeamApply') {
       allPerson = await this.teamApplyModel.find({ project_id, status: '1', _id: { $nin: personList } });
     } else {
-      const conn = this.app.mongooseDB.get('base');
-      const schema = _.get(this.ctx.model, 'Base.User.schema');
-      const m = conn.model('User', schema);
       allPerson = await this.matchSignModel.find({ project_id, pay_status: '1', user_id: { $nin: personList } }).populate({
         path: 'user_id',
         select: 'user_id',
+        model: this.userModel,
         populate: {
           path: 'user_id',
           select: 'name',
-          model: m,
+          model: this.baseUserModel,
         },
       });
       allPerson = JSON.parse(JSON.stringify(allPerson));
@@ -128,9 +126,6 @@ class MatchTeamGroupService extends CrudService {
     } else {
       // 单打
       const users = [];
-      const conn = this.app.mongooseDB.get('base');
-      const schema = _.get(this.ctx.model, 'Base.User.schema');
-      const m = conn.model('User', schema);
       for (const i of returnData) {
         const { person } = i;
         for (const p of person) {
@@ -138,7 +133,7 @@ class MatchTeamGroupService extends CrudService {
           const user = await this.userModel.findById(user_id).populate({
             path: 'user_id',
             select: 'name',
-            model: m,
+            model: this.baseUserModel,
           });
           p.user_name = _.get(user, 'user_id.name');
         }
@@ -205,10 +200,7 @@ class MatchTeamGroupService extends CrudService {
       const { person, person_type } = d;
       if (person_type === 'User') {
         // 单人
-        const conn = this.app.mongooseDB.get('base');
-        const schema = _.get(this.ctx.model, 'Base.User.schema');
-        const m = conn.model('User', schema);
-        const users = await this.userModel.find({ _id: person }).populate({ path: 'user_id', model: m, select: 'name' });
+        const users = await this.userModel.find({ _id: person }).populate({ path: 'user_id', model: this.baseUserModel, select: 'name' });
         const parr = person.map(i => {
           const r = users.find(f => ObjectId(f._id).equals(i));
           if (r) return { id: r._id, name: _.get(r, 'user_id.name') };
@@ -236,10 +228,7 @@ class MatchTeamGroupService extends CrudService {
     const { person = [], person_type } = d;
     if (person_type === 'User') {
       // 单人
-      const conn = this.app.mongooseDB.get('base');
-      const schema = _.get(this.ctx.model, 'Base.User.schema');
-      const m = conn.model('User', schema);
-      const users = await this.userModel.find({ _id: person }).populate({ path: 'user_id', model: m, select: 'name' });
+      const users = await this.userModel.find({ _id: person }).populate({ path: 'user_id', model: this.baseUserModel, select: 'name' });
       const parr = person.map(i => {
         const r = users.find(f => ObjectId(f._id).equals(i));
         if (r) return { id: r._id, name: _.get(r, 'user_id.name') };

+ 4 - 4
app/service/teamApply.js

@@ -11,6 +11,8 @@ class TeamApplyService extends CrudService {
     this.model = this.ctx.model.Race.TeamApply;
     this.matchSignModel = this.ctx.model.Race.MatchSign;
     this.matchProjectModel = this.ctx.model.Race.MatchProject;
+    this.userModel = this.ctx.model.Race.User;
+    this.baseUserModel = this.ctx.model.Base.User;
   }
 
   async beforeCreate(body) {
@@ -46,13 +48,11 @@ class TeamApplyService extends CrudService {
    */
   async findPartner({ project_id, user_id }) {
     // 多层嵌套populate
-    const conn = this.app.mongooseDB.get('base');
-    const schema = _.get(this.ctx.model, 'Base.User.schema');
-    const m = conn.model('User', schema);
     let list = await this.matchSignModel.find({ user_id: { $ne: user_id }, pay_status: '1', project_id }).populate({
       path: 'user_id',
       select: 'user_id',
-      populate: { path: 'user_id', model: m, select: 'name' },
+      model: this.userModel,
+      populate: { path: 'user_id', model: this.baseUserModel, select: 'name' },
     });
     if (list.length > 0) {
       list = JSON.parse(JSON.stringify(list));