lrf 2 years ago
parent
commit
46b91912a4
2 changed files with 31 additions and 14 deletions
  1. 7 4
      app/service/matchSign.js
  2. 24 10
      app/service/matchTeamGroup.js

+ 7 - 4
app/service/matchSign.js

@@ -21,6 +21,9 @@ class MatchSignService extends CrudService {
    * @param {Object} body 参数体
    */
   async beforeCreate(body) {
+    const { match_id } = body;
+    const num = await this.matchModel.count({ _id: match_id, status: '1' });
+    if (num <= 0) throw new BusinessError(ErrorCode.SERVICE_FAULT, '当前赛事无法报名');
     await this.checkHas(body);
     return body;
   }
@@ -31,7 +34,7 @@ class MatchSignService extends CrudService {
     const { id } = filter;
     const data = await this.model.findById(id);
     const { match_id } = data;
-    const num = await this.matchModel.count({ _id: match_id, status: { $nin: [ '0', '1', '2' ] } });
+    const num = await this.matchModel.count({ _id: match_id, status: { $nin: ['0', '1', '2'] } });
     if (num > 0) throw new BusinessError(ErrorCode.DATA_INVALID, '赛事已经处于准备开赛或开赛中,不能退赛');
     return { filter, update };
   }
@@ -43,7 +46,7 @@ class MatchSignService extends CrudService {
       if (!raceUser) continue;
       const { user_id } = raceUser;
       const user = await this.baseUserModel.findById(user_id);
-      if (user)i.user_name = user.name;
+      if (user) i.user_name = user.name;
     }
     return data;
   }
@@ -53,7 +56,7 @@ class MatchSignService extends CrudService {
     const { pay_status } = data;
     if (pay_status !== '-3') return data;
     // 线下管理人员允许退款,生成账单
-    const obj = _.pick(data, [ 'match_id', 'group_id', 'project_id' ]);
+    const obj = _.pick(data, ['match_id', 'group_id', 'project_id']);
     obj.payer_id = data.user_id;
     obj.time = moment().format('YYYY-MM-DD HH:mm:ss');
     obj.type = '-1';
@@ -68,7 +71,7 @@ class MatchSignService extends CrudService {
    */
   async checkHas(body) {
     const { match_id, group_id, project_id, user_id } = body;
-    const query = { match_id, group_id, project_id, user_id, pay_status: [ '0', '1' ] };
+    const query = { match_id, group_id, project_id, user_id, pay_status: ['0', '1'] };
     const num = await this.model.count(query);
     if (num > 0) throw new BusinessError(ErrorCode.DATA_EXISTED, '您已报名');
     return true;

+ 24 - 10
app/service/matchTeamGroup.js

@@ -15,6 +15,7 @@ class MatchTeamGroupService extends CrudService {
     this.matchSignModel = this.ctx.model.Race.MatchSign;
     this.baseUserModel = this.ctx.model.Base.User;
     this.userModel = this.ctx.model.Race.User;
+    this.matchModel = this.ctx.model.Race.Match;
   }
   /**
    * 根据项目和选手类型查询可选择的分组人员
@@ -61,16 +62,18 @@ class MatchTeamGroupService extends CrudService {
     return allPerson;
   }
 
+  // 自动建组
   async saveAll(data) {
-    for (const i of data) {
-      const { _id } = i;
-      if (!_id) {
-        // TODO: 创建前:需要检查这些人员是否出现在这个项目中的别的组中
-        await this.model.create(i);
-      } else {
-        await this.model.updateOne({ _id }, i);
-      }
-    }
+    // 确保大家的project_id都是一个
+    const match_id = _.get(_.head(data), 'match_id');
+    const canOpera = await this.canOpera(match_id);
+    if (!canOpera) throw new BusinessError(ErrorCode.SERVICE_FAULT, '当前赛事不处于可更改赛事相关信息状态');
+    const project_id = _.get(_.head(data), 'project_id');
+    const belongOneProject = data.every(e => e.project_id === project_id);
+    if (!belongOneProject) throw new BusinessError(ErrorCode.DATA_INVALID, '自动创建的小组并不全都属于同一个比赛项目,无法创建小组');
+    // 先删除该比赛项目的所有小组
+    await this.model.deleteMany({ project_id });
+    await this.model.insertMany(data);
   }
 
   async auto({ match_id, group_id, project_id, team_number = 0 }) {
@@ -143,7 +146,6 @@ class MatchTeamGroupService extends CrudService {
     return returnData;
   }
 
-  // TODO: 检查该组的成员是否在该比赛项目中的其他组中
   async beforeCreate(body) {
     const r = await this.checkPersonInOtherGroup(body);
     if (!r) throw new BusinessError(ErrorCode.DATA_INVALID, '组员已在其他组,请先将其退出原组后,再添置该组');
@@ -182,6 +184,18 @@ class MatchTeamGroupService extends CrudService {
     }
     return res;
   }
+
+  /**
+   * 根据该赛事状态,判断是否可以操作
+   * 赛事状态为 1,2可以组队, 其余状态不能组队,不能修改
+   * @param {String} match_id 赛事id
+   * @return {Boolean} 是否可以操作
+   */
+  async canOpera(match_id) {
+    const num = await this.matchModel.count({ _id: match_id, status: [ '1', '2' ] });
+    return num <= 0;
+
+  }
 }
 
 module.exports = MatchTeamGroupService;