lrf 2 år sedan
förälder
incheckning
249efe108a
2 ändrade filer med 118 tillägg och 41 borttagningar
  1. 1 0
      app/controller/config/.matchSmallGroupSchedule.js
  2. 117 41
      app/service/matchSmallGroupSchedule.js

+ 1 - 0
app/controller/config/.matchSmallGroupSchedule.js

@@ -66,6 +66,7 @@ module.exports = {
         is_change: 'is_change',
         status: 'status',
         user_id: 'user_id',
+        user_name: 'user_name',
       },
       // options: {
       //   "meta.state": 0 // 默认条件

+ 117 - 41
app/service/matchSmallGroupSchedule.js

@@ -46,26 +46,43 @@ class MatchSmallGroupScheduleService extends CrudService {
   }
 
   async beforeQuery(filter) {
-    // 可查询自己的赛程-user_id
-    const { user_id } = filter;
+    // 可查询自己的赛程-user_id / 某人的赛程
+    // const { user_id, user_name } = filter;
+    const user_name = _.get(filter, 'user_name');
+    let user_id = _.get(filter, 'user_id');
     // 没有user_id就不需要处理查询条件
-    if (!user_id) return filter;
-    // 需要查:该用户是 单打 和 双打 的情况
-    // 单打,直接user_id 为player_one/two 就可以,双打需要查teamApply
-    // 所以先把user_id添加进查询范围里
-    let probablyList = [ user_id ];
-    // 尽可能的缩小查询范围
+    if (!user_id && !user_name) return filter;
     const usualCondition = _.pick(filter, [ 'match_id', 'group_id', 'project_id' ]);
-    // 接着找组队申请中和该用户有关的人
-    const teamApplyList = await this.teamApplyModel.find({ ...usualCondition, status: '1', $or: [{ one_member_id: user_id }, { two_member_id: user_id }] }, { _id: 1 });
-    const teamApplyIds = teamApplyList.map(i => i._id);
-    probablyList.push(...teamApplyIds);
-    // 删除user_id.会造成错误
-    delete filter.user_id;
-    // 添加该用户正确的范围条件
-    probablyList = probablyList.map(i => ObjectId(i).toString());
-    filter.$or = [{ player_one: { $in: probablyList } }, { player_two: { $in: probablyList } }];
+
+    if (user_name) {
+      // 要先找比赛用户模块的数据id
+      const baseUser = await this.baseUserModel.findOne({ name: new RegExp(user_name) }, { _id: 1 });
+      if (!baseUser) delete filter.user_name;
+      delete filter.user_name;
+      const baseUserId = baseUser._id;
+      const raceUser = await this.userModel.findOne({ user_id: baseUserId, type: '0' });
+      if (raceUser) user_id = raceUser._id;
+    }
+
+    if (user_id) {
+      // 需要查:该用户是 单打 和 双打 的情况
+      // 单打,直接user_id 为player_one/two 就可以,双打需要查teamApply
+      // 所以先把user_id添加进查询范围里
+      let probablyList = [ user_id ];
+      // 尽可能的缩小查询范围
+      // 接着找组队申请中和该用户有关的人
+      const teamApplyList = await this.teamApplyModel.find({ ...usualCondition, status: '1', $or: [{ one_member_id: user_id }, { two_member_id: user_id }] }, { _id: 1 });
+      const teamApplyIds = teamApplyList.map(i => i._id);
+      probablyList.push(...teamApplyIds);
+      // 删除user_id.会造成错误
+      delete filter.user_id;
+      // 添加该用户正确的范围条件
+      probablyList = probablyList.map(i => ObjectId(i).toString());
+      filter.$or = [{ player_one: { $in: probablyList } }, { player_two: { $in: probablyList } }];
+    }
     return filter;
+
+
   }
 
   turnFilter(filter) {
@@ -137,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;
@@ -187,6 +204,65 @@ class MatchSmallGroupScheduleService extends CrudService {
 
     return d;
   }
+
+  // async getRefMods() {
+  //   const mod = await this.getModel();
+  //   const refMods = [];
+  //   const populate = [];
+  //   for (const key in mod) {
+  //     const { ref, refPath } = mod[key];
+  //     if (!ref && !refPath) continue;
+  //     const obj = { col: key, prop: mod[key].getProp, type: mod[key].type.name };
+  //     refMods.push(obj);
+  //     // 检查ref/refPath是否还有嵌套
+  //     if (ref) {
+  //       const res = await this.checkSchema(ref);
+  //     }
+  //   }
+  //   return { refMods, populate };
+  // }
+  // 组成子 populate => {path,model}
+  async checkSchema(ref) {
+    const schema = this.findModel(ref);
+    for (const key in schema) {
+      const { ref, refPath } = schema[key];
+      if (!ref && !refPath) continue;
+      const obj = { };
+      if (ref) {
+        const arr = ref.split('.');
+        obj.ref = _.upperFirst(_.last(arr));
+        if (arr.length > 0) {
+          // 说明是有路径的,需要将路径也填充
+          arr.pop();
+
+        }
+      }
+
+    }
+  }
+  findModel(modelName, path) {
+    // 有没有默认路径,将路径整合进去
+    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');
+    let returnData;
+    // 去掉要找到的目标model
+    modelPath.pop();
+    const dir = modelPath.join('.');
+    const rootModel = _.get(this.ctx.model, dir, this.ctx.model);
+    for (const mn in rootModel) {
+      const type = typeof rootModel[mn];
+      if (type === 'object') {
+        // rootModel[mn] 是目录,要进入目录里面找内容
+        returnData = this.findModel(modelName, mn);
+      }
+      // 没有 else 的原因是,对于model来说;路径是object类型,model是function类型,而model在 上面 有关m变量 部分已经处理完,不会有其他形式
+    }
+    return returnData;
+  }
 }
 
 module.exports = MatchSmallGroupScheduleService;