lrf 2 tahun lalu
induk
melakukan
cee850ff78
1 mengubah file dengan 51 tambahan dan 35 penghapusan
  1. 51 35
      app/service/matchSmallGroupSchedule.js

+ 51 - 35
app/service/matchSmallGroupSchedule.js

@@ -13,6 +13,8 @@ class MatchSmallGroupScheduleService extends CrudService {
     this.baseUserModel = this.ctx.model.Base.User;
     this.userModel = this.ctx.model.Race.User;
     this.teamApplyModel = this.ctx.model.Race.TeamApply;
+    // 加入框架
+    this.defaultModule = this.app.config.defaultModel || 'Race';
   }
 
   async saveAll(data) {
@@ -81,8 +83,6 @@ class MatchSmallGroupScheduleService extends CrudService {
       filter.$or = [{ player_one: { $in: probablyList } }, { player_two: { $in: probablyList } }];
     }
     return filter;
-
-
   }
 
   turnFilter(filter) {
@@ -154,30 +154,30 @@ class MatchSmallGroupScheduleService extends CrudService {
     }
     const { refMods, populate } = await this.getRefMods();
 
-    // let res = await this.model.findOne(filter, projection).populate(populate).exec();
-    // res = JSON.parse(JSON.stringify(res));
-    // for (const obj of refMods) {
-    //   const { col, prop, type } = obj;
-    //   if (!prop) continue;
-    //   if (_.isArray(prop)) {
-    //     for (const p of prop) {
-    //       if (type === 'String') res[`${col}_${p}`] = _.get(res, `${col}.${p}`);
-    //       if (type === 'Array') {
-    //         const list = [];
-    //         const oList = _.get(res, `${col}`);
-    //         for (const d of oList) {
-    //           const obj = { _id: d._id };
-    //           obj[p] = _.get(d, p);
-    //           list.push(obj);
-    //         }
-    //         res[`${col}_${p}`] = list;
-    //       }
-    //     }
-    //     res[col] = _.get(res, `${col}._id`);
-    //   }
-    // }
-    // res = await this.afterFetch(filter, res);
-    // return res;
+    let res = await this.model.findOne(filter, projection).populate(populate).exec();
+    res = JSON.parse(JSON.stringify(res));
+    for (const obj of refMods) {
+      const { col, prop, type } = obj;
+      if (!prop) continue;
+      if (_.isArray(prop)) {
+        for (const p of prop) {
+          if (type === 'String') res[`${col}_${p}`] = _.get(res, `${col}.${p}`);
+          if (type === 'Array') {
+            const list = [];
+            const oList = _.get(res, `${col}`);
+            for (const d of oList) {
+              const obj = { _id: d._id };
+              obj[p] = _.get(d, p);
+              list.push(obj);
+            }
+            res[`${col}_${p}`] = list;
+          }
+        }
+        res[col] = _.get(res, `${col}._id`);
+      }
+    }
+    res = await this.afterFetch(filter, res);
+    return res;
   }
   async afterFetch(filter, d) {
     const { player_type, player_one, player_two, referee_id } = d;
@@ -216,38 +216,54 @@ class MatchSmallGroupScheduleService extends CrudService {
   //     refMods.push(obj);
   //     // 检查ref/refPath是否还有嵌套
   //     if (ref) {
-  //       const res = await this.checkSchema(ref);
+  //       // 递归查找ref的关系,构造ref关系树图
+  //       const res = this.checkSchema(ref);
+  //       // console.log(res);
   //     }
   //   }
   //   return { refMods, populate };
   // }
   // 组成子 populate => {path,model}
-  async checkSchema(ref) {
-    const schema = this.findModel(ref);
+  checkSchema(thisRef, path) {
+    const { schema } = this.findModel(thisRef, path);
     for (const key in schema) {
       const { ref, refPath } = schema[key];
       if (!ref && !refPath) continue;
-      const obj = { };
+      const obj = {};
       if (ref) {
-        const arr = ref.split('.');
+        const arr = ref.split('.').map(i => _.upperFirst(i));
         obj.ref = _.upperFirst(_.last(arr));
+        let mpath = path;
+        // 如果有path,就应该不会有arr.length>0的情况
         if (arr.length > 0) {
           // 说明是有路径的,需要将路径也填充
           arr.pop();
-
+          mpath = arr.join('.');
         }
+        // 递归寻找ref
+        const { schema: sch } = this.checkSchema(obj.ref, mpath);
+        console.log(key, ref);
+        console.log(sch);
+        // for (const key in sch) {
+        //   const obj = sch[key];
+        //   const populate = this.dealSchema(obj);
+        // }
       }
-
     }
+    return schema;
   }
-  findModel(modelName, path) {
+
+  // dealSchema(schema) {
+  // }
+
+  findModel(modelName, path = this.defaultModule) {
     // 有没有默认路径,将路径整合进去
     const modelPath = [];
     if (path) modelPath.push(...path.split('.'));
     modelPath.push(_.upperFirst(modelName));
     const modelPathStr = modelPath.join('.');
     const m = _.get(this.ctx.model, modelPathStr);
-    if (m) return _.get(m, 'prototype.schema.obj');
+    if (m) return { schema: _.get(m, 'prototype.schema.obj'), path: modelPathStr };
     let returnData;
     // 去掉要找到的目标model
     modelPath.pop();