lrf 2 jaren geleden
bovenliggende
commit
4b0862589f
2 gewijzigde bestanden met toevoegingen van 58 en 38 verwijderingen
  1. 6 5
      app/controller/config/.matchSmallGroupSchedule.js
  2. 52 33
      app/service/matchSmallGroupSchedule.js

+ 6 - 5
app/controller/config/.matchSmallGroupSchedule.js

@@ -19,11 +19,11 @@ module.exports = {
     ],
   },
   destroy: {
-    params: [ '!id' ],
+    params: ['!id'],
     service: 'delete',
   },
   update: {
-    params: [ '!id' ],
+    params: ['!id'],
     requestBody: [
       'match_id',
       'group_id',
@@ -44,7 +44,7 @@ module.exports = {
   },
   show: {
     parameters: {
-      params: [ '!id' ],
+      params: ['!id'],
     },
     service: 'fetch',
   },
@@ -65,6 +65,7 @@ module.exports = {
         player_two: 'player_two',
         is_change: 'is_change',
         status: 'status',
+        user_id: 'user_id',
       },
       // options: {
       //   "meta.state": 0 // 默认条件
@@ -72,8 +73,8 @@ module.exports = {
     },
     service: 'query',
     options: {
-      query: [ 'skip', 'limit' ],
-      sort: [ 'meta.createdAt' ],
+      query: ['skip', 'limit'],
+      sort: ['meta.createdAt'],
       desc: true,
       count: true,
     },

+ 52 - 33
app/service/matchSmallGroupSchedule.js

@@ -12,6 +12,7 @@ class MatchSmallGroupScheduleService extends CrudService {
     this.model = this.ctx.model.Race.MatchSmallGroupSchedule;
     this.baseUserModel = this.ctx.model.Base.User;
     this.userModel = this.ctx.model.Race.User;
+    this.teamApplyModel = this.ctx.model.Race.TeamApply;
   }
 
   async saveAll(data) {
@@ -44,6 +45,55 @@ class MatchSmallGroupScheduleService extends CrudService {
     return body;
   }
 
+  async beforeQuery(filter) {
+    // 可查询自己的赛程-user_id
+    const { user_id } = filter;
+    // 没有user_id就不需要处理查询条件
+    if (!user_id) return filter;
+    // 需要查:该用户是 单打 和 双打 的情况
+    // 单打,直接user_id 为player_one/two 就可以,双打需要查teamApply
+    // 所以先把user_id添加进查询范围里
+    let probablyList = [ user_id ];
+    // 尽可能的缩小查询范围
+    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 } }];
+    return filter;
+  }
+
+  turnFilter(filter) {
+    const str = /^%\S*%$/;
+    // $是mongodb固定条件,不用处理;大多为手写特殊处理过的条件
+    let keys = Object.keys(filter).filter(f => !f.includes('$'));
+    for (const key of keys) {
+      const res = key.match(str);
+      if (res) {
+        const newKey = key.slice(1, key.length - 1);
+        if (!ObjectId.isValid(filter[key])) filter[newKey] = new RegExp(filter[key]);
+        delete filter[key];
+      }
+    }
+    // 再次过滤数据,将数组的数据都变成{$in:value},因为查询变成了聚合查询
+    // $是mongodb固定条件,不用处理;大多为手写特殊处理过的条件
+    keys = Object.keys(filter).filter(f => !f.includes('$'));
+    for (const key of keys) {
+      if (_.isArray(filter[key])) {
+        filter[key] = { $in: filter[key] };
+      } else if (filter[key] === 'true' || filter[key] === 'false') {
+        // 布尔类型的值检查,如果是布尔类型,则将字符串转为布尔
+        filter[key] = filter[key] === 'true';
+      }
+    }
+    return filter;
+  }
+
   async afterQuery(filter, data) {
     data = JSON.parse(JSON.stringify(data));
     for (const d of data) {
@@ -52,11 +102,11 @@ class MatchSmallGroupScheduleService extends CrudService {
         const pouid = _.get(player_one, 'user_id');
         const user1 = await this.baseUserModel.findById(pouid);
         d.player_one_name = _.get(user1, 'name');
-        d.player_one = _.get(data, 'player_one._id');
+        d.player_one = _.get(d, 'player_one._id');
         const ptuid = _.get(player_two, 'user_id');
         const user2 = await this.baseUserModel.findById(ptuid);
         d.player_two_name = _.get(user2, 'name');
-        d.player_two = _.get(data, 'player_two._id');
+        d.player_two = _.get(d, 'player_two._id');
       } else if (player_type === 'TeamApply') {
         d.player_one_name = `${_.get(d, 'player_one.one_member_name')}-${_.get(d, 'player_one.two_member_name')}`;
         d.player_one = _.get(d, 'player_one._id');
@@ -137,37 +187,6 @@ class MatchSmallGroupScheduleService extends CrudService {
 
     return d;
   }
-
-  // async getRefMods() {
-  //   const mod = await this.getModel();
-  //   const refMods = [];
-  //   const populate = [];
-  //   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 };
-  //     if (mod[key].ref) {
-  //       const ref = mod[key].ref;
-  //       if (ref.includes('.')) {
-  //         // 说明是跨数据源
-  //         const arr = ref.split('.');
-  //         const conn = this.app.mongooseDB.get(getHead(arr));
-  //         const refModel = last(arr);
-  //         const schema = get(this.ctx.model, `${refModel}.schema`);
-  //         const model = conn.model(refModel, schema);
-  //         const p = { path: key, model };
-  //         populate.push(p);
-  //       } else {
-  //         const p = { path: key };
-  //         populate.push(p);
-  //       }
-  //     } else if (mod[key].refPath) {
-  //       const p = { path: key };
-  //       populate.push(p);
-  //     }
-  //     refMods.push(obj);
-  //   }
-  //   return { refMods, populate };
-  // }
 }
 
 module.exports = MatchSmallGroupScheduleService;