lrf 2 năm trước cách đây
mục cha
commit
cf4e0d139c
1 tập tin đã thay đổi với 118 bổ sung0 xóa
  1. 118 0
      app/service/matchSmallGroupSchedule.js

+ 118 - 0
app/service/matchSmallGroupSchedule.js

@@ -3,12 +3,15 @@ const { CrudService } = require('naf-framework-mongoose-free/lib/service');
 const { BusinessError, ErrorCode } = require('naf-core').Error;
 const _ = require('lodash');
 const assert = require('assert');
+const { ObjectId } = require('mongoose').Types;
 
 //
 class MatchSmallGroupScheduleService extends CrudService {
   constructor(ctx) {
     super(ctx, 'matchsmallgroupschedule');
     this.model = this.ctx.model.Race.MatchSmallGroupSchedule;
+    this.baseUserModel = this.ctx.model.Base.User;
+    this.userModel = this.ctx.model.Race.User;
   }
 
   async saveAll(data) {
@@ -40,6 +43,121 @@ class MatchSmallGroupScheduleService extends CrudService {
     if (d) await this.model.deleteOne(query);
     return body;
   }
+
+  async afterQuery(filter, data) {
+    data = JSON.parse(JSON.stringify(data));
+    for (const d of data) {
+      const { player_type, player_one, player_two, referee_id } = d;
+      if (player_type === 'User') {
+        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');
+        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');
+      }
+
+      const referee = await this.userModel.findById(referee_id).populate({
+        path: 'user_id',
+        model: this.baseUserModel,
+      });
+      d.referee_id_name = _.get(referee, 'user_id.name');
+    }
+    return data;
+  }
+
+  async fetch(filter, { sort, desc, projection } = {}) {
+    assert(filter);
+    filter = await this.beforeFetch(filter);
+    const { _id, id } = filter;
+    if (_id || id) filter = { _id: ObjectId(_id || id) };
+
+    // 处理排序
+    if (sort && _.isString(sort)) {
+      sort = { [sort]: desc ? -1 : 1 };
+    } else if (sort && _.isArray(sort)) {
+      sort = sort.map(f => ({ [f]: desc ? -1 : 1 })).reduce((p, c) => ({ ...p, ...c }), {});
+    }
+    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;
+  }
+  async afterFetch(filter, d) {
+    const { player_type, player_one, player_two, referee_id } = d;
+    if (player_type === 'User') {
+      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(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(d, 'player_two._id');
+    }
+    const referee = await this.userModel.findById(referee_id).populate({
+      path: 'user_id',
+      model: this.baseUserModel,
+    });
+    d.referee_id_name = _.get(referee, 'user_id.name');
+
+    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;