lrf 2 years ago
parent
commit
5f59648be7
3 changed files with 71 additions and 3 deletions
  1. 36 1
      app/service/matchGroup.js
  2. 35 1
      app/service/matchProject.js
  3. 0 1
      app/service/matchTeamGroup.js

+ 36 - 1
app/service/matchGroup.js

@@ -4,11 +4,46 @@ const { BusinessError, ErrorCode } = require('naf-core').Error;
 const _ = require('lodash');
 const assert = require('assert');
 
-// 
+//
 class MatchGroupService extends CrudService {
   constructor(ctx) {
     super(ctx, 'matchgroup');
     this.model = this.ctx.model.Race.MatchGroup;
+    this.matchModel = this.ctx.model.Race.Match;
+  }
+
+  async beforeCreate(data) {
+    const r = await this.canOpera(data);
+    if (!r) throw new BusinessError(ErrorCode.SERVICE_FAULT, '当前赛事不处于可更改赛事相关信息状态');
+    return data;
+  }
+  async beforeUpdate(filter, update) {
+    const r = await this.canOpera({ ...filter, ...update });
+    if (!r) throw new BusinessError(ErrorCode.SERVICE_FAULT, '当前赛事不处于可更改赛事相关信息状态');
+    return { filter, update };
+  }
+  async beforeDelete(filter) {
+    const r = await this.canOpera(filter);
+    if (!r) throw new BusinessError(ErrorCode.SERVICE_FAULT, '当前赛事不处于可更改赛事相关信息状态');
+    return filter;
+  }
+
+  /**
+   * 根据该赛事状态,判断是否可以操作
+   * 赛事状态为 0 可以操作, 其余状态不能操作
+   * @param {Object} data 数据
+   * @return {Boolean} 是否可以操作
+   */
+  async canOpera(data) {
+    let match_id = _.get(data, 'match_id');
+    if (!match_id) {
+      const id = data.id || data._id;
+      if (!id) throw new BusinessError(ErrorCode.DATA_INVALID, '缺少检查可操作条件');
+      const d = await this.model.findById(id);
+      match_id = _.get(d, 'match_id');
+    }
+    const num = await this.matchModel.count({ _id: match_id, status: '0' });
+    return num > 0;
   }
 }
 

+ 35 - 1
app/service/matchProject.js

@@ -4,11 +4,45 @@ const { BusinessError, ErrorCode } = require('naf-core').Error;
 const _ = require('lodash');
 const assert = require('assert');
 
-// 
+//
 class MatchProjectService extends CrudService {
   constructor(ctx) {
     super(ctx, 'matchproject');
     this.model = this.ctx.model.Race.MatchProject;
+    this.matchModel = this.ctx.model.Race.Match;
+  }
+  async beforeCreate(data) {
+    const r = await this.canOpera(data);
+    if (!r) throw new BusinessError(ErrorCode.SERVICE_FAULT, '当前赛事不处于可更改赛事相关信息状态');
+    return data;
+  }
+  async beforeUpdate(filter, update) {
+    const r = await this.canOpera({ ...filter, ...update });
+    if (!r) throw new BusinessError(ErrorCode.SERVICE_FAULT, '当前赛事不处于可更改赛事相关信息状态');
+    return { filter, update };
+  }
+  async beforeDelete(filter) {
+    const r = await this.canOpera(filter);
+    if (!r) throw new BusinessError(ErrorCode.SERVICE_FAULT, '当前赛事不处于可更改赛事相关信息状态');
+    return filter;
+  }
+
+  /**
+   * 根据该赛事状态,判断是否可以操作
+   * 赛事状态为 0 可以操作, 其余状态不能操作
+   * @param {Object} data 数据
+   * @return {Boolean} 是否可以操作
+   */
+  async canOpera(data) {
+    let match_id = _.get(data, 'match_id');
+    if (!match_id) {
+      const id = data.id || data._id;
+      if (!id) throw new BusinessError(ErrorCode.DATA_INVALID, '缺少检查可操作条件');
+      const d = await this.model.findById(id);
+      match_id = _.get(d, 'match_id');
+    }
+    const num = await this.matchModel.count({ _id: match_id, status: '0' });
+    return num > 0;
   }
 }
 

+ 0 - 1
app/service/matchTeamGroup.js

@@ -194,7 +194,6 @@ class MatchTeamGroupService extends CrudService {
   async canOpera(match_id) {
     const num = await this.matchModel.count({ _id: match_id, status: [ '1', '2' ] });
     return num > 0;
-
   }
 }