lrf hace 2 años
padre
commit
bd82c50420
Se han modificado 3 ficheros con 65 adiciones y 84 borrados
  1. 0 26
      app/model/ground.js
  2. 0 30
      app/model/race.js
  3. 65 28
      app/service/matchTeamGroup.js

+ 0 - 26
app/model/ground.js

@@ -1,26 +0,0 @@
-'use strict';
-const Schema = require('mongoose').Schema;
-const metaPlugin = require('naf-framework-mongoose-free/lib/model/meta-plugin');
-const { ObjectId } = require('mongoose').Types;
-const source = 'race';
-// 表
-const ground = {
-  name: { type: String, zh: '场地名' },
-  remark: { type: String },
-};
-const schema = new Schema(ground, { toJSON: { virtuals: true } });
-schema.index({ id: 1 });
-schema.index({ 'meta.createdAt': 1 });
-schema.plugin(metaPlugin);
-module.exports = app => {
-  const is_multiple = app.mongooseDB.clients;
-  let model;
-  if (is_multiple) {
-    const conn = app.mongooseDB.get(source);
-    model = conn.model('Ground', schema, 'ground');
-  } else {
-    const { mongoose } = app;
-    model = mongoose.model('Ground', schema, 'ground');
-  }
-  return model;
-};

+ 0 - 30
app/model/race.js

@@ -1,30 +0,0 @@
-'use strict';
-const Schema = require('mongoose').Schema;
-const metaPlugin = require('naf-framework-mongoose-free/lib/model/meta-plugin');
-const { ObjectId } = require('mongoose').Types;
-const source = 'race';
-// 表
-const race = {
-  title: { type: String, zh: '比赛名称' },
-  user: { type: String, zh: '用户', ref: 'base.User', getProp: [ 'name' ] },
-  ground: { type: String, zh: '场地', ref: 'Ground', getProp: [ 'name' ] },
-  player: { type: String, zh: '选手', refPath: 'player_from', getProp: [ 'name' ] },
-  player_from: { type: String, zh: '选手数据来源' },
-  remark: { type: String },
-};
-const schema = new Schema(race, { toJSON: { virtuals: true } });
-schema.index({ id: 1 });
-schema.index({ 'meta.createdAt': 1 });
-schema.plugin(metaPlugin);
-module.exports = app => {
-  const is_multiple = app.mongooseDB.clients;
-  let model;
-  if (is_multiple) {
-    const conn = app.mongooseDB.get(source);
-    model = conn.model('Race', schema, 'race');
-  } else {
-    const { mongoose } = app;
-    model = mongoose.model('Race', schema, 'race');
-  }
-  return model;
-};

+ 65 - 28
app/service/matchTeamGroup.js

@@ -26,42 +26,39 @@ class MatchTeamGroupService extends CrudService {
   async findGroupPersonSelects({ project_id, person_type, team_id }) {
     // 1.查找所有报名的选手/小队
     let allPerson;
+    // 2.查找该项目下已经分完组的成员, 如果是针对某组的修改,则不查该组
+    const teamGroups = await this.model.find({ project_id, _id: { $ne: team_id } }, { person: 1 });
+    // 已经有分组的成员id集合
+    const personList = _.flattenDeep(teamGroups.map(i => i.person));
     if (person_type === 'TeamApply') {
-      allPerson = await this.teamApplyModel.find({ project_id, status: '1' });
+      allPerson = await this.teamApplyModel.find({ project_id, status: '1', _id: { $nin: personList } });
     } else {
-      allPerson = await this.matchSignModel.find({ project_id, pay_status: '1' });
-    }
-    // 2.查找该项目下已经分完组的成员
-    const teamGroups = await this.model.find({ project_id });
-    // 已经有分组的成员id集合
-    let personList = _.flattenDeep(teamGroups.map(i => i.person));
-    // 3.如果有team_id,则将team_id内的组员从上面有分组的成员id集合中剔除
-    if (team_id) {
-      const teamGroup = await this.model.findById(team_id);
-      const thisTeamPerson = _.flattenDeep(teamGroup.map(i => i.person));
-      personList = personList.filter(f => !thisTeamPerson.includes(f));
+      const conn = this.app.mongooseDB.get('base');
+      const schema = _.get(this.ctx.model, 'Base.User.schema');
+      const m = conn.model('User', schema);
+      allPerson = await this.matchSignModel.find({ project_id, pay_status: '1', user_id: { $nin: personList } }).populate({
+        path: 'user_id',
+        select: 'user_id',
+        populate: {
+          path: 'user_id',
+          select: 'name',
+          model: m,
+        },
+      });
+      allPerson = JSON.parse(JSON.stringify(allPerson));
+      allPerson = allPerson.map(i => {
+        i.user_name = _.get(i, 'user_id.user_id.name');
+        i.user_id = _.get(i, 'user_id._id');
+        return i;
+      });
     }
+
     // 过滤掉 allPerson 中 在personList中的数据
     if (person_type === 'TeamApply') {
       allPerson = allPerson.filter(f => !personList.includes(f._id));
       return allPerson;
     }
-    // 找人的信息
-    allPerson = allPerson.filter(f => !personList.includes(f.user_id));
-    const users = [];
-    const conn = this.app.mongooseDB.get('base');
-    const schema = _.get(this.ctx.model, 'Base.User.schema');
-    const m = conn.model('User', schema);
-    for (const i of allPerson) {
-      const { user_id } = i;
-      const user = await this.userModel.findById(user_id).populate({
-        path: 'user_id',
-        select: 'name',
-        model: m,
-      });
-      users.push({ ...JSON.parse(JSON.stringify(i)), user_name: _.get(user, 'user_id.name') });
-    }
-    return users;
+    return allPerson;
   }
 
   async saveAll(data) {
@@ -145,6 +142,46 @@ class MatchTeamGroupService extends CrudService {
 
     return returnData;
   }
+
+  // TODO: 检查该组的成员是否在该比赛项目中的其他组中
+  async beforeCreate(body) {
+    const r = await this.checkPersonInOtherGroup(body);
+    if (!r) throw new BusinessError(ErrorCode.DATA_INVALID, '组员已在其他组,请先将其退出原组后,再添置该组');
+
+    return body;
+  }
+
+  async beforeUpdate(filter, update) {
+    const r = await this.checkPersonInOtherGroup({ ...filter, ...update });
+    if (!r) throw new BusinessError(ErrorCode.DATA_INVALID, '组员已在其他组,请先将其退出原组后,再添置该组');
+    return { filter, update };
+  }
+
+  // 检查该组的成员是否在该比赛项目中的其他组中
+  // true:没有问题,可以操作; false:有人在其他组,不能允许操作
+  async checkPersonInOtherGroup(data) {
+    const { person, id, project_id } = data;
+    // 没有person,就不需要检查
+    if (!person) return true;
+    const query = { _id: { $ne: id } };
+    if (project_id) query.project_id = project_id;
+    else {
+      // 没有比赛项目id,也没有数据id: 是新增,但是没有比赛项目id,说明数据有错误
+      if (!id) throw new BusinessError(ErrorCode.DATA_INVALID, '新增的数据中缺少比赛项目信息');
+      const data = await this.model.findById(id);
+      if (!data) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到要修改的数据');
+      query.project_id = _.get(data, 'project_id');
+    }
+    let res = true;
+    for (const p of person) {
+      const num = await this.model.count({ ...query, person: p });
+      if (num > 0) {
+        res = false;
+        break;
+      }
+    }
+    return res;
+  }
 }
 
 module.exports = MatchTeamGroupService;