lrf 2 years ago
parent
commit
daafc97198
2 changed files with 56 additions and 0 deletions
  1. 1 0
      app/controller/config/.teamApply.js
  2. 55 0
      app/service/teamApply.js

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

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

+ 55 - 0
app/service/teamApply.js

@@ -38,6 +38,61 @@ class TeamApplyService extends CrudService {
 
     return list;
   }
+
+  async query(filter, { skip = 0, limit, sort, desc, projection } = {}) {
+    // 处理排序
+    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 }), {});
+    }
+    let condition = _.cloneDeep(filter);
+    condition = await this.beforeQuery(condition);
+    condition = this.dealFilter(condition);
+    condition = this.selfQueryDeal(condition);
+    console.log(condition);
+    // 过滤出ref字段
+    const { refMods, populate } = await this.getRefMods();
+    // 带ref查询
+    let rs = await this.model.find(condition, projection, { skip, limit, sort }).populate(populate).exec();
+    rs = JSON.parse(JSON.stringify(rs));
+    // 整理ref数据
+    rs = rs.map(i => {
+      for (const obj of refMods) {
+        const { col, prop, type } = obj;
+        if (!prop) continue;
+        if (_.isArray(prop)) {
+          for (const p of prop) {
+            if (type === 'String') i[`${col}_${p}`] = _.get(i, `${col}.${p}`);
+            if (type === 'Array') {
+              const list = [];
+              const oList = _.get(i, `${col}`);
+              for (const d of oList) {
+                const obj = { _id: d._id };
+                obj[p] = _.get(d, p);
+                list.push(obj);
+              }
+              i[`${col}_${p}`] = list;
+            }
+          }
+          i[col] = _.get(i, `${col}._id`);
+        }
+      }
+      return i;
+    });
+    rs = await this.afterQuery(filter, rs);
+    return rs;
+  }
+
+  selfQueryDeal(condition) {
+    if (_.get(condition, 'user_id')) {
+      const user_id = _.get(condition, 'user_id');
+      condition.$or = [{ one_member_id: user_id }, { two_member_id: user_id }];
+      delete condition.user_id;
+    }
+
+    return condition;
+  }
 }
 
 module.exports = TeamApplyService;